power_ipmi.pm.in 8.65 KB
Newer Older
1 2 3
#!/usr/bin/perl -w

#
4
# Copyright (c) 2000-2016 University of Utah and the Flux Group.
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
# 
# {{{EMULAB-LICENSE
# 
# This file is part of the Emulab network testbed software.
# 
# This file is free software: you can redistribute it and/or modify it
# under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or (at
# your option) any later version.
# 
# This file is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Affero General Public
# License for more details.
# 
# You should have received a copy of the GNU Affero General Public License
# along with this file.  If not, see <http://www.gnu.org/licenses/>.
# 
# }}}
#

#
# module for controlling SuperMicro IPMI cards
# needs a working "/usr/local/bin/ipmitool" binary installed
# (try pkg_add -r ipmitool)
#
# supports new(ip), power(on|off|cyc[le]), status
#

package power_ipmi;

$| = 1; # Turn off line buffering on output

use strict;
use lib "@prefix@/lib";
use libdb;
use emutil;

$ENV{'PATH'} = '/bin:/usr/bin:/usr/local/bin'; # Required when using system() or backticks `` in combination with the perl -T taint checks

my $IPMITOOL = "/usr/local/bin/ipmitool";
my $KTYPE    = "ipmi";

# Constants for moonshot chassis support.
my $MS_MAXSLOT = 45;
my $MS_NODEBASEADDR = 0x72;
my $MS_NODEBRIDGEID = 0x7;
my $MS_CARTBASEADDR = 0x82;
my $MS_CARTBRIDGEID = 0x0;

55
sub new($$$;$$) {
56 57 58 59 60 61 62 63

    # The next two lines are some voodoo taken from perltoot(1)
    my $proto = shift;
    my $class = ref($proto) || $proto;

    my $devicetype = shift;
    my $devicename = shift;
    my $debug = shift;
64
    my $withsdr = shift;
65 66 67 68

    if (!defined($debug)) {
        $debug = 0;
    }
69 70 71
    if (!defined($withsdr)) {
	$withsdr = 0;
    }
72 73 74 75 76 77 78 79

    if ($debug) {
        print "power_ipmi module initializing... debug level $debug\n";
    }

    my $self = {};

    $self->{DEBUG} = $debug;
80
    $self->{WITHSDR} = $withsdr;
81 82 83 84 85 86 87 88

    $self->{DEVICETYPE} = $devicetype;
    $self->{DEVICENAME} = $devicename;
    $self->{USERNAME}   = "ADMIN";              # default username
    $self->{PASSWORD}   = "ADMIN";              # default password
    $self->{KGKEY}      = "";

    # Fetch authentication credentials from the DB.
89
    my $res = DBQueryFatal("select key_role,key_uid,mykey,key_privlvl" . 
90 91 92 93 94 95 96 97 98 99 100
			   " from outlets_remoteauth" . 
			   " where node_id='$devicename'".
			   " and key_type='$KTYPE'");
    if (!defined($res) || !$res || $res->num_rows() == 0) {
	warn "No remote auth info for $devicename. Using defaults!\n";
    } else {
	while (my $row = $res->fetchrow_hashref()) {
	    my $role = $row->{'key_role'};
	    if ($role eq "ipmi-passwd") {
		$self->{USERNAME} = $row->{'key_uid'};
		$self->{PASSWORD} = $row->{'mykey'};
101 102 103
	        if ($row->{'key_privlvl'}) {
	            $self->{PRIVLVL} = $row->{'key_privlvl'};
	        }
104 105
	    } 
	    elsif ($role eq "ipmi-kgkey") {
106
		($self->{KGKEY} = $row->{'mykey'}) =~ s/^0x//;
107 108 109 110
	    }
	}
    }

111
    $self->{IPMICMD} = "$IPMITOOL -H $self->{DEVICENAME} -U $self->{USERNAME} -P '$self->{PASSWORD}'";
112
    if ($self->{KGKEY}) {
113
	$self->{IPMICMD} .= " -I lanplus -y '$self->{KGKEY}'";
114 115 116
    } elsif ($self->{DEVICETYPE} eq "ipmi-ms") {
	$self->{IPMICMD} .= " -I lanplus";
    }
117 118 119
    if ($self->{PRIVLVL}) {
        $self->{IPMICMD} .= " -L $self->{PRIVLVL}";
    }
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137

    # Do a quick query, to see if it works
    system("$self->{IPMICMD} power status >/dev/null 2>\&1");
    if ( $? != 0 ) {                    # system() sets $? to a non-zero value in case of failure
        warn "ERROR: Unable to connect to $devicename via IPMI\n";
        return undef;
    }

    bless($self,$class);
    $self->_mkmsmap();

    return $self;
}

sub power($$@) {
    my $self = shift;
    my $op = shift;
    my @outlets = @_;
138
    my %status = ();
139 140

    my $errors = 0;
141
    my $force = 0;
142 143 144 145

    if    ($op eq "on")  { $op = "power on";    }
    elsif ($op eq "off") { $op = "power off";   }
    elsif ($op =~ /cyc/) {
146 147 148
	if ($op eq "forcecycle") {
	    $force = 1;
	}
149 150 151 152 153 154 155
	if ($self->{DEVICETYPE} eq "ipmi-ms") {
	    $op = "power cycle";
	} else {
	    $op = "power reset";
	}
    }

156
    $errors = $self->_execipmicmd($op, \@outlets, \%status, $force);
157 158 159 160 161 162 163
    if ($errors) {
        print STDERR $self->{DEVICENAME}, ": command '$op' failed for some/all outlets: @outlets\n";
    }

    return $errors;
}

164
sub status($$@) {
165
    my $self = shift;
166 167 168
    my $statusp = shift;
    my @outlets = @_;
    my %status;
169 170 171 172 173

    my $errors = 0;
    my ($retval, $output);

    # Get power status (i.e. whether system is on/off)
174
    $retval = $self->_execipmicmd("power status", \@outlets, \%status, 0);
175 176 177 178 179 180
    if ( $retval != 0 ) {
        $errors++;
        print STDERR $self->{DEVICENAME}, ": could not get power status from device\n";
    }

    # Get Sensor Data Repository (sdr) entries and readings
181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201
    # XXX needs work
    if ($self->{WITHSDR}) {
	my %lstatus = ();

	$retval = $self->_execipmicmd("sdr", \@outlets, \%lstatus, 0);
	if ( $retval != 0 ) {
	    $errors++;
	    print STDERR $self->{DEVICENAME}, ": could not get sensor data from device\n";
	}
	else {
	    foreach my $outlet (@outlets) {
		my $oname = "outlet" . $outlet;

		$output = "UNKNOWN";
		if (exists($lstatus{$oname})) {
		    $output = $lstatus{$oname};
		}
		print("SDR data is:\n$output\n") if $self->{DEBUG};
		$status{'sdr'} = $output;
	    }
	}
202 203 204 205 206 207
    }

    if ($statusp) { %$statusp = %status; } # update passed-by-reference array
    return $errors;
}

208 209 210 211 212 213 214 215 216
#
# The returned status is expected to be:
#
#    $status{'outletN'} = <status string from command> (e.g., "on", "off")
#
# where N is the outlet number.
#
sub _execipmicmd($$$$$) {
    my ($self, $op, $outletsp, $statusp, $force) = @_;
217 218 219
    my $exitval = 0;
    my @results = ();

220 221 222 223 224 225
    if (!defined($outletsp)) {
	return 0;
    }
    
    my $isstatus = ($op eq "power status") ? 1 : 0;
    
226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243
    my $coderef = sub {
	my $outlet = shift;
	my $output = "";
	my $cmd;

	# Construct command based on device type.
	if ($self->{DEVICETYPE} eq "ipmi-ms") {
	    my $ipmiaddrstr = $self->_get_msaddr($outlet);
	    if ($ipmiaddrstr) {
		$cmd = "$self->{IPMICMD} $ipmiaddrstr $op";
	    } else {
		print STDERR "$self->{DEVICENAME}: invalid outlet: $outlet\n";
		return -1;
	    }
	} else {
	    $cmd = "$self->{IPMICMD} $op";
	}

244
      again:
245 246
	print STDERR "**** Executing $cmd\n"
	    if ($self->{DEBUG});
247 248
	$output = `$cmd 2>&1`;
	chomp ( $output );
249 250 251 252 253

	print STDERR "impitool output:\n$output\n"
	    if ($self->{DEBUG});

	if ($?) {
254 255 256 257 258 259 260 261 262 263
	    # XXX special case handling of node powered off
	    if ($output =~ /not supported in present state/) {
		if ($force) {
		    print "*** Cycle failed, trying power on instead.\n"
			if ($self->{DEBUG} > 1);
		    $cmd =~ s/power (cycle|reset)/power on/;
		    $force = 0;
		    goto again;
		}
	    }
264 265 266 267
	    print STDERR "*** power_ipmi: ipmitool returned non-zero: $?\n";
	    print STDERR "ipmitool output:\n$output\n";
	    return -1;
	}
268 269 270 271 272 273 274 275 276 277

	# for a status command we have to interpret the output
	if ($isstatus) {
	    if ($output =~ /power is (off|on)/i) {
		return ($1 eq "off") ? 0 : 1;
	    }
	    print STDERR "*** power_ipmi: unexpected power status:\n$output\n";
	    return -1;
	}

278 279 280
	return 0;
    };

281
    my @outlets = @$outletsp;
282 283
    if (ParRun(undef, \@results, $coderef, @outlets)) {
	print STDERR "*** power_ipmi: Internal error in ParRun()!\n";
284
	return -1;
285 286 287 288
    }
    #
    # Check the exit codes. 
    #
289 290 291 292 293 294 295 296 297 298 299
    for (my $ix = 0; $ix < @outlets; $ix++) {
	my $outlet = "outlet" . $outlets[$ix];
	my $rv = ($results[$ix] >> 8);
	$statusp->{$outlet} = $rv;
	if ($isstatus) {
	    ++$exitval
		if ($rv < 0);
	} else {
	    ++$exitval
		if ($rv != 0);
	}
300 301
    }

302
    return $exitval;
303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339
}

sub _mkmsmap() {
    my $self = shift;
    $self->{MS_OUTLETMAP} = [];
    foreach my $outlet (1 .. $MS_MAXSLOT) {
	my $cartaddr = $MS_CARTBASEADDR + 2*($outlet - 1);
	my $nodeaddr = $MS_NODEBASEADDR;
	my $vals = {
	    'cartaddr' => $cartaddr,
	    'cartbrid' => $MS_CARTBRIDGEID,
	    'nodeaddr' => $nodeaddr,
	    'nodebrid' => $MS_NODEBRIDGEID,
	    };
	$self->{MS_OUTLETMAP}->[$outlet] = $vals;
    }
}

sub _get_msaddr($$) {
    my ($self, $outlet) = @_;
    my $msaddrstr = "";

    return undef
	unless defined($self->{MS_OUTLETMAP}->[$outlet]);

    my $msaddr = $self->{MS_OUTLETMAP}->[$outlet];
    $msaddrstr = sprintf("-T 0x%x -B 0x%x -t 0x%x -b 0x%x", 
			  $msaddr->{'cartaddr'},
			  $msaddr->{'cartbrid'},
			  $msaddr->{'nodeaddr'},
			  $msaddr->{'nodebrid'});

    return $msaddrstr;
}

# End with true
1;
340 341 342 343

# vim: set ft=perl et sw=4 ts=8:
# Not sure what the (no)et sw=? ts=? rules should be in this file - they're kind of mixed.
# Seems like a leading tab in some places and then 4 expanded spaces.  Maybe et sw=4 ts=8.