power_racktivity.pm.in 4.08 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 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 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
#!/usr/bin/perl -w

#
# EMULAB-COPYRIGHT
# Copyright (c) 2000-2003, 2006-2007 University of Utah and the Flux Group.
# All rights reserved.
#

#
# module for controlling Ractivity IPMI cards
# needs a working "/usr/local/bin/snmpset" binary installed
#
# supports new(ip), power(on|off|cyc[le]), status
#

package power_racktivity;

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

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

sub new($$;$) {

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

    my $devicename = shift;
    my $debug = shift;

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

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

    my $self = {};

    $self->{DEBUG} = $debug;

    $self->{DEVICENAME} = $devicename;
    $self->{VAL_ON}  = 1;
    $self->{VAL_OFF} = 0;
    $self->{SNMP_VER} = "2c";
    $self->{SNMP_MIB} = "RACKTIVITY";
    $self->{SNMP_OID} = "enterprises.racktivity.port.portTable.portEntry.state";
    $self->{SNMP_COMMUNITY_READ}  = "public";
    $self->{SNMP_COMMUNITY_WRITE} = "private";

    bless($self,$class);
    return $self;
}

sub power {
    my $self = shift;
    my $op = shift;
    my @ports = @_;

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

    my $op_val;

    if    ($op eq "on")  { $op_val = $self->{VAL_ON};  }
    elsif ($op eq "off") { $op_val = $self->{VAL_OFF}; }
    elsif ($op =~ /cyc/) { $op_val = "cycle"; }
    else { die "unsupported op: $op"; }


    foreach my $port (@ports)
    {
	if ( $port < 0 || $port > 7 )
	{
		die "Port number out of range: 0 <= port <= 7";
	}

	my $oid = sprintf("%s.%s", $self->{SNMP_OID}, $port);

	my $ret = -1;

	if ( $op_val eq "cycle" )
	{
		$op_val = $self->{VAL_OFF};
		$ret = $self->_execsnmpset($oid,$op_val);
		if ( $ret != $op_val )
		{
			$errors++;
			print STDERR $self->{DEVICENAME}, ": could not control power status of device on port $port\n";
		}
		sleep 5;
		$op_val = $self->{VAL_ON};
		$ret = $self->_execsnmpset($oid,$self->{VAL_ON});
		if ( $ret != $op_val )
		{
			$errors++;
			print STDERR $self->{DEVICENAME}, ": could not control power status of device on port $port\n";
		}
	}

	$ret = $self->_execsnmpset($oid,$op_val);
	if ( $ret != $op_val )
	{
		$errors++;
		print STDERR $self->{DEVICENAME}, ": could not control power status of device on port $port\n";
	}
    }

    return $errors;
}

sub status {
    my $self = 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);


}

sub _execsnmpset { 
    my ($self,$oid,$value) = @_;

    my $snmpcmd = sprintf("snmpset -Oe -v %s -m %s -c %s %s %s i %i",
    			$self->{SNMP_VER},
 			$self->{SNMP_MIB},
			$self->{SNMP_COMMUNITY_WRITE},
			$self->{DEVICENAME},
			$oid, $value );


    open('SNMPCMD',"$snmpcmd |") or die "Cannot start $snmpcmd for read: $!";

    my $response = -255; 

    while(<SNMPCMD>)
    {
	chomp;
	my ($field,$value) = split(/ = /);
	print "field: $field \n" if ($self->{DEBUG});
	print "value: $value \n" if ($self->{DEBUG});
	
	if ( $field =~ /$oid/ )
	{
		print "found $oid in response\n" if ($self->{DEBUG});
		$response = $value ;
	}
    }

    return ($response);
}

sub _execsnmpget { 
    my ($self,$oid) = @_;

    my $snmpcmd = sprintf("snmpget -Oe -v %s -m %s -c %s %s %s",
    			$self->{SNMP_VER},
 			$self->{SNMP_MIB},
			$self->{SNMP_COMMUNITY_READ},
			$self->{DEVICENAME},
			$oid );


    open('SNMPCMD',"$snmpcmd |") or die "Cannot start $snmpcmd for read: $!";

    my $response = -255; 

    while(<SNMPCMD>)
    {
	chomp;
	my ($field,$value) = split(/ = /);
	print "field: $field \n" if ($self->{DEBUG});
	print "value: $value \n" if ($self->{DEBUG});

	if ( $field eq $oid.".0" )
	{
		print "found $oid in response\n" if ($self->{DEBUG});
		$response = $value ;
	}
    }

    return ($response);
}

# End with true
1;