From dafb8e9f1b0c0d7cfe092ddb467a7650109efb51 Mon Sep 17 00:00:00 2001 From: James Date: Mon, 17 Mar 2014 11:20:42 +0000 Subject: fish --- inf.pl | 309 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 309 insertions(+) create mode 100755 inf.pl (limited to 'inf.pl') diff --git a/inf.pl b/inf.pl new file mode 100755 index 0000000..f6677f9 --- /dev/null +++ b/inf.pl @@ -0,0 +1,309 @@ +#!/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); + + exit(0); +} + +sub thing($$$) { + my ( $inf, $port, $thing ) = @_; + + 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'; + + 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"; + 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/ ) ); + print "Outlet is $s\n"; + $i++; + } until ( ( $i > $wait ) + and ( not( $s =~ /Pending/ ) ) + and ( $s =~ /$looke/i ) ); + +} + +sub thing_search($$$) { + my ( $infs, $re, $thing ) = @_; + + 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 ); + 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( "%.1f 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}\n"; + print " cycle -c, turn on -o or turn off -f or view -v,\n"; + print " port on switch\n"; + print "inf [-s hostname] {-c|-o|-f|-v} 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\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( "v: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}; +} + +if ( exists $options->{c} ) { + if ( defined $port ) { + thing( INF->new( $infs->[0] ), $port, 'cycle' ); + exit(0); + } + else { + thing_search( $infs, $options->{c}, 'cycle' ); + } +} + +if ( exists $options->{o} ) { + if ( defined $port ) { + thing( INF->new( $infs->[0] ), $port, 'on' ); + exit(0); + } + else { + thing_search( $infs, $options->{o}, 'on' ); + } +} + +if ( exists $options->{f} ) { + if ( defined $port ) { + thing( INF->new( $infs->[0] ), $port, 'off' ); + exit(0); + } + else { + thing_search( $infs, $options->{f}, 'off' ); + } +} + +if ( exists $options->{v} ) { + if ( defined $port ) { + thing( INF->new( $infs->[0] ), $port, 'view' ); + exit(0); + } + else { + thing_search( $infs, $options->{v}, 'view' ); + } +} + +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); +} + -- cgit v1.2.3