power_ipmi.pm.in 7.9 KB
Newer Older
1 2 3 4 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 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
#!/usr/bin/perl -w

#
# Copyright (c) 2000-2003, 2006-2007 University of Utah and the Flux Group.
# 
# {{{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;

sub new($$$;$) {

    # 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;

    if (!defined($debug)) {
        $debug = 0;
    }

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

    my $self = {};

    $self->{DEBUG} = $debug;

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

    # Fetch authentication credentials from the DB.
84
    my $res = DBQueryFatal("select key_role,key_uid,mykey,key_privlvl" . 
85 86 87 88 89 90 91 92 93 94 95
			   " 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'};
96 97 98
	        if ($row->{'key_privlvl'}) {
	            $self->{PRIVLVL} = $row->{'key_privlvl'};
	        }
99 100
	    } 
	    elsif ($role eq "ipmi-kgkey") {
101
		($self->{KGKEY} = $row->{'mykey'}) =~ s/^0x//;
102 103 104 105 106 107 108 109 110 111
	    }
	}
    }

    $self->{IPMICMD} = "$IPMITOOL -H $self->{DEVICENAME} -U $self->{USERNAME} -P $self->{PASSWORD}";
    if ($self->{KGKEY}) {
	$self->{IPMICMD} .= " -I lanplus -y $self->{KGKEY}";
    } elsif ($self->{DEVICETYPE} eq "ipmi-ms") {
	$self->{IPMICMD} .= " -I lanplus";
    }
112 113 114
    if ($self->{PRIVLVL}) {
        $self->{IPMICMD} .= " -L $self->{PRIVLVL}";
    }
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282

    # 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 = @_;

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

    if    ($op eq "on")  { $op = "power on";    }
    elsif ($op eq "off") { $op = "power off";   }
    elsif ($op =~ /cyc/) {
	if ($self->{DEVICETYPE} eq "ipmi-ms") {
	    $op = "power cycle";
	} else {
	    $op = "power reset";
	}
    }

    ($errors, $output) = $self->_execipmicmd($op, @outlets);
    if ($errors) {
        print STDERR $self->{DEVICENAME}, ": command '$op' failed for some/all outlets: @outlets\n";
    }

    return $errors;
}

# XXX: This isn't actually hooked in, and won't work for the moonshot
#      as it is.
sub status($$$) {
    my $self = shift;
    my $type = shift;
    my $statusp = shift; # pointer to an associative (hashed) array (i.o.w. passed by reference)
    my %status;          # local associative array which we'll pass back through $statusp

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

    # XXX: No support for the moonshot right now.
    if ($type eq "ipmi-ms") {
	return 0;
    }

    # Get power status (i.e. whether system is on/off)
    ($retval,$output) = $self->_execipmicmd("power status");
    if ( $retval != 0 ) {
        $errors++;
        print STDERR $self->{DEVICENAME}, ": could not get power status from device\n";
    }
    else { $status{'outlet1'} = $output; print("Power status is: $output\n") if $self->{DEBUG}; } # there's only 1 "outlet" on an IPMI card


    # Get Sensor Data Repository (sdr) entries and readings
    ($retval,$output) = $self->_execipmicmd("sdr");
    if ( $retval != 0 ) {
        $errors++;
        print STDERR $self->{DEVICENAME}, ": could not get sensor data from device\n";
    }
    else { $status{'sdr'} = $output; print("SDR data is:\n$output\n") if $self->{DEBUG}; }


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

sub _execipmicmd($$@) { # _ indicates that this is a private method (Perl convention)
    my ($self, $op, @outlets) = @_;
    my $exitval = 0;
    my $output = "";
    my @results = ();

    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";
	}

	print STDERR "**** Executing $cmd\n"
	    if ($self->{DEBUG});
	$output = `$cmd`;                  # get output of the $cmd, e.g. many lines of sensor readings or "Chassis power is on"
	chomp ( $output );                 # remove the \n at the end, if any

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

	if ($?) {
	    print STDERR "*** power_ipmi: ipmitool returned non-zero: $?\n";
	    print STDERR "ipmitool output:\n$output\n";
	    return -1;
	}
	return 0;
    };

    if (ParRun(undef, \@results, $coderef, @outlets)) {
	print STDERR "*** power_ipmi: Internal error in ParRun()!\n";
	return (-1,undef);
    }
    #
    # Check the exit codes. 
    #
    foreach my $result (@results) {
	++$exitval
	    if ($result != 0);
    }

    return ($exitval, $output);
}

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;
283 284 285 286

# 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.