deepupower123 has asked for the wisdom of the Perl Monks concerning the following question:

Hi everyone, Using the script, I need to get the uptime % of the host. If the uptime drops down to zero, i need to do a fcping and check if i get a positive response. If I get a positive response, it's a Network issue else I need to check the HBA status if it is offline. I'm new to perl scripting, not sure how to proceed. In Other Words, 1. Check the uptime% with SNMP. 2. If SNMP returns a downtime check with fcping if the fabric is up. If fcping returns positive then the SNMP downtime is because of a network issue. If fcping does not work check if the HBA status is offline. Here's the partial code of my script, I'm not sure if it works:
_________________________________________ #! /usr/bin/perl use strict; use Net::SNMP; my $uptimeOID = '1.3.6.1.2.1.1.3.0'; foreach my $host (@ARGV) { my ($session, $error) = Net::SNMP->session( -hostname => $host, -community => 'public', -port => 161 ); warn ("ERROR for $host: $error\n") unless (defined($session)); my $result = $session->get_request( -varbindlist => [$uptimeOID] ); if (!defined($result)) { warn ("ERROR: " . $session->error . "\n"); } else { my $days = $result->{$uptimeOID}; my @TEMP_ARRAY_1 = split(",", $days); $TEMP_ARRAY_1[0] =~ s/days//g; my @TEMP_ARRAY_2 = split(":", $TEMP_ARRAY_1[1]); $days = $TEMP_ARRAY_1[0] + $TEMP_ARRAY_2[0] / 24 + $TEMP_ARRAY +_2[1] / ( 60 * 24 ); print $days; if($days == 0) { my $res = `fcping -d`; my @temp_arr = split(" ", $res); my $port = $temp_arr[2]; chomp($temp = `fcping $port`); @TEMP_1 = split("and", $temp); @TEMP_2 = split(" ", $TEMP_1[1]); if($TEMP_2[0] eq "0") { print "Network Issue"; } else { print "Check HBA Status"; } } } $session->close; } ___________________________________________
fcping output looks like this:
# fcping -d Discovered Port 50:06:0E:80:03:27:AA:11 # fcping 50:06:0E:80:03:27:AA:11 Pinging port 50:06:0E:80:03:27:AA:11, LUN 0 with SCSI Inquiry: Port 50:06:0E:80:03:27:AA:11 replies in 0.013 s as HITACHI OPEN-E + . Port 50:06:0E:80:03:27:AA:11 replies in 0.014 s as HITACHI OPEN-E + . Port 50:06:0E:80:03:27:AA:11 replies in 0.012 s as HITACHI OPEN-E + . 3 successful and 0 unsuccessful pings. Average ping time: 0.013 s. Minimum ping time: 0.012 s. Maximum ping time: 0.014 s.
Can somebody help me with this?

Replies are listed 'Best First'.
Re: getuptime script
by Anonymous Monk on Oct 30, 2008 at 13:49 UTC
    warn ("ERROR for $host: $error\n") unless (defined($session));
    should be
    unless( $session ){ warn ("ERROR for $host: $error\n") ; next; }
    that way if $session is not defined, you warn and then move on to next $host.
      Thank you for your reply. My concern is regarding this part of the script.
      if($days == 0) { my $res = `fcping -d`; my @temp_arr = split(" ", $res); my $port = $temp_arr[2]; chomp($temp = `fcping $port`); @TEMP_1 = split("and", $temp); @TEMP_2 = split(" ", $TEMP_1[1]); if($TEMP_2[0] eq "0") { print "Network Issue"; } else { print "Check HBA Status"; } }
      The Code above this part is fine. I'm successful in getting the uptime. I need uptime% and the conditions I've mentioned. Thanks in Advance
Re: getuptime script
by toolic (Bishop) on Oct 30, 2008 at 17:28 UTC
    I think your problem is in parsing the output of your "fcping 50:06:0E:80:03:27:AA:11" command. You are currently assigning the multi-line output of the command to a scalar variable, $temp. It may make things clearer to assign the output to an array, then loop over the lines of output looking for your special string using a regex instead of split:
    my @fcping_lines = `fcping $port`; for (@fcping_lines) { if (/\s+ and \s+ (.*?) \s+/x) { my $unsucces = $1; if ($unsucces eq "0") { print "Network Issue\n"; } else { print "Check HBA Status\n"; } } }

    Make sure to use strict; use warnings and declare all you variables with my.

    It may also be useful to check the success of your fcping commands, by checking the $? special variable.

      toolic, Thanks for your reply. How do I find the uptime % ? I get the current uptime in days ($days), I need to calculate the %uptime (number of days the host is up / Total no. of days the script is run * 100). I'm not sure how to get these values. Could you please help?