#!/usr/bin/env perl BEGIN { push @INC, "/usr/local/share/inf"; } use INF; use Data::Dumper; use Getopt::Std; # sub view($$) { my ( $inf, $port ) = @_; $inf->view($port); $inf->logout; exit(0); } sub media($$) { my ( $inf, $port ) = @_; $inf->media($port); $inf->logout; exit(0); } sub thing($$$$) { my ( $inf, $port, $thing, $force ) = @_; unless ( defined $inf ) { print "No device\n"; exit(1); } my $host = $inf->{host}; my $name = $inf->name_get(); my $port_name = $inf->port_name_get($port); view( $inf, $port ) if $thing eq 'view'; media( $inf, $port ) if $thing eq 'media'; unless ( $force == 1 ) { print "Are you sure you want to $thing\n"; print "$port_name, port $port on PDU $host($name)\n"; print "Type YES to continue\n"; my $yes = ; chomp $yes; if ( not( $yes =~ /^yes$/i ) ) { print "did nothing, $yes isn't yes.\n"; exit(0); } } my $ret = undef; my $looks = undef; my $looke = undef; if ( $thing eq 'cycle' ) { $ret = $inf->port_cycle($port); $looks = 'Off'; $looke = 'On'; } elsif ( $thing eq 'on' ) { $ret = $inf->port_on($port); $looks = 'On'; $looke = 'On'; } elsif ( $thing eq 'off' ) { $ret = $inf->port_off($port); $looks = 'Off'; $looke = 'Off'; } else { printf "Unknown operation $thing\n"; } if ($ret) { print "Command successful\n"; } else { print "Command failed\n"; $inf->logout; exit(1); } my $i = 0; my $s = 0; my $wait = 20; do { sleep(1); $s = $inf->port_state_get_no_cache($port); $i = $wait if ( ( defined $looks ) and ( not( $s =~ /Pending/ ) ) and ( $s =~ /$looks/i ) ); print "Outlet is $s\n"; $i++; } until ( ( $i > $wait ) and ( not( $s =~ /Pending/ ) ) and ( $s =~ /$looke/i ) ); $inf->logout; } sub thing_search($$$$) { my ( $infs, $re, $thing, $force ) = @_; for my $h (@$infs) { my $name = $h->{host}; print "Searching $name:\n"; my $inf = INF->new($h); my $n = $inf->port_count(); for ( my $i = 1 ; $i <= $n ; ++$i ) { my $port = $inf->port_id_get_by_number($i); my $o = $inf->port_name_get($port); next if ( $thing eq 'view' ) and not( $port =~ /^K/ ); if ( $o =~ /$re/ ) { print "Found $name, outlet $port: $o\n\n"; thing( $inf, $port, $thing, $force ); exit(0); } } } } sub set_outlet_name($$$) { my ( $h, $outlet, $name ) = @_; my $hn = $h->{host}; print "Changing name of $hn outlet $outlet to $name:\n"; my $inf = INF->new($h); my $ret = $inf->port_name_set( $outlet, $name ); if ( not defined $ret ) { print "Command failed\n"; } else { print "Done\n"; } } sub show_status($) { my ($infs) = @_; for my $h (@$infs) { my $hostname = $h->{host}; my $inf = INF->new($h); next unless defined $inf; my $name = $inf->name_get(); print "+-$hostname($name)\n"; my $load = $inf->pdu_load_get(); if ( defined $load ) { $load = sprintf( "%s Amps", $load ); } else { $load = "Unknown"; } print "| +--Load: $load\n"; $psu = $inf->psu_status(); print "| +--PSU: $psu\n" if defined $psu; my $n = $inf->port_count(); for ( my $i = 1 ; $i <= $n ; ++$i ) { my $port_id = $inf->port_id_get_by_number($i); my $port_name = $inf->port_name_get($port_id); my $port_state = $inf->port_state_get($port_id); printf "| +---%-5s %-30s %s\n", $port_id, $port_name, $port_state; } print "|\n"; } } sub set_name($$) { my ( $h, $name ) = @_; my $hn = $h->{host}; print "Changing name of $hn to $name:\n"; my $inf = INF->new($h); my $ret = $inf->name_set($name); if ( not defined $ret ) { print "Command failed\n"; } else { print "Done\n"; } } sub usage() { print "Usage:\n"; print "inf -s hostname [-p port] -n name\n"; print " set name or switch, or port of switch to name\n"; print "inf -s hostname -p port {-c|-o|-f|-v|-m}\n"; print " cycle -c, turn on -o or turn off -f or view -v,\n"; print " port on switch\n"; print "inf [-s hostname] [-F] {-c|-o|-f|-v|-m} regexp\n"; print " cycle -c, turn on -o or turn off -f or view -v,\n"; print " the first thing found which matches regex.\n"; print " -s limits search to only one switch, -F doesn't\n"; print " ask\n"; print "inf [-s hostname] -l\n"; print " show the status of switches. -s limits the output\n"; print " to one switch\n"; print "inf -h\n"; print " show help\n"; exit(1); } my $options = {}; getopts( "Fv:m:c:o:f:s:n:p:lh", $options ); my $infs = []; my $port = undef; if ( ( $options->{h} ) or ( scalar( keys(%$options) ) == 0 ) ) { usage(); } if ( $options->{s} ) { for my $h (@$INF::infs) { if ( ( $h->{nice_name} eq $options->{s} ) or ( $h->{host} eq $options->{s} ) ) { push @$infs, $h; } } if ( scalar(@$infs) == 0 ) { print "Unknown PDU\n"; exit(1); } } else { $infs = $INF::infs; } if ( $options->{p} ) { if ( scalar(@$infs) != 1 ) { print "Use of -p requires use of -s\n"; exit(1); } $port = $options->{p}; } my $force = 0; $force = 1 if exists $options->{F}; if ( exists $options->{c} ) { if ( defined $port ) { thing( INF->new( $infs->[0] ), $port, 'cycle', $force ); exit(0); } else { thing_search( $infs, $options->{c}, 'cycle', $force ); } } if ( exists $options->{o} ) { if ( defined $port ) { thing( INF->new( $infs->[0] ), $port, 'on', $force ); exit(0); } else { thing_search( $infs, $options->{o}, 'on', $force ); } } if ( exists $options->{f} ) { if ( defined $port ) { thing( INF->new( $infs->[0] ), $port, 'off', $force ); exit(0); } else { thing_search( $infs, $options->{f}, 'off', $force ); } } if ( exists $options->{v} ) { if ( defined $port ) { thing( INF->new( $infs->[0] ), $port, 'view', $force ); exit(0); } else { thing_search( $infs, $options->{v}, 'view', $force ); } } if ( exists $options->{m} ) { if ( defined $port ) { thing( INF->new( $infs->[0] ), $port, 'media', $force ); exit(0); } else { thing_search( $infs, $options->{v}, 'media', $force ); } } if ( $options->{n} ) { if ( scalar(@$infs) != 1 ) { print "Use of -n requires use of -s\n"; exit(1); } if ( defined $port ) { set_outlet_name( $infs->[0], $port, $options->{n} ); } else { set_name( $infs->[0], $options->{n} ); } } if ( $options->{l} ) { show_status($infs); }