sub check_interfaces { my $crit = 0; my $stat=""; my $count=0; my $allInts_OID = ".1.3.6.1.4.1.3375.2.1.2.4.1.2.1.1"; my $ltm_InBytes_Index = "1.3.6.1.4.1.3375.2.1.2.4.4.3.1.3."; my $ltm_OutBytes_Index = "1.3.6.1.4.1.3375.2.1.2.4.4.3.1.5."; #If $ARGV[2] = 0, then check all interfaces through $allInts_OID. if ($int eq 0) { my $allInts = $session->get_table ( -baseoid => $allInts_OID ); my %int_table = %{$allInts}; #print "\n\n"; OUT: foreach my $key (sort keys %int_table){ #Convert interface name to OID string my @chars = unpack("C" x length($int_table{$key}),$int_table{$key}); my $int_length = length( $int_table{$key} ); unshift(@chars, $int_length); my $int_oid = join('.' , @chars); #Build OID from index & interface my $ltm_intBytesIn = $ltm_InBytes_Index . $int_oid; my $ltm_intBytesOut = $ltm_OutBytes_Index . $int_oid; #Get first instance my $oids_1 = $session->get_request( -varbindlist => [$ltm_intBytesIn, $ltm_intBytesOut] ); # Wait for second poll {Keep it 10 seconds} #Change after testing sleep 5; #Get second instance my $oids_2 = $session->get_request( -varbindlist => [$ltm_intBytesIn, $ltm_intBytesOut] ); #Calculate Rates my $rate_in = ($oids_2->{$ltm_intBytesIn} - $oids_1->{$ltm_intBytesIn})*8 / (10*1000000); my $rate_out = ($oids_2->{$ltm_intBytesOut} - $oids_1->{$ltm_intBytesOut})*8 / (10*1000000); my $rate_total = $rate_in + $rate_out; #Trim to 4 decimal places $rate_in =~s/(^\d{1,}\.\d{4})(.*$)/$1/; $rate_out =~s/(^\d{1,}\.\d{4})(.*$)/$1/; $rate_total =~s/(^\d{1,}\.\d{4})(.*$)/$1/; #Print Results #print "Interface $int_table{$key} rate (Mbits/sec)"; #print "\n\n\t$rate_in (IN)\n"; #print "\t$rate_out (OUT)\n"; #print "\t$rate_total (TOTAL)\n"; my $querystr="select * from device_iface_cache where interface_name='$int_table{$key}' and hostname='$host'"; my ($f5_ifacerow); my $query=$dbh->prepare($querystr); $query->execute; if(!($f5_ifacerow=$query->fetchrow_hashref())) { # no row found... insert into f5_selfip append_log ("IFace Stats: No result. Creating a blank record\n"); my $updatestr="INSERT INTO device_iface_cache (interface_name, interface_status, lastcheck, hostname) VALUES ('$int_table{$key}', '$stat', '$date', '$host');"; append_log ("IFace Stats: SQL\n"); my $update=$dbh->prepare($updatestr); $update->execute; } if($rate_total == $crit){ #Add in flap detection, also, if an interface is down check x4 #before alarming. $stat = "DOWN"; append_log ("CRITICAL: Interface $int_table{$key} is DOWN.\n"); append_status("CRITICAL: Interface $int_table{$key} is DOWN.\n"); my $current_status = $dbh->prepare("SELECT interface_status FROM device_iface_cache WHERE interface_name='$int_table{$key}' and hostname='$host';"); $current_status->execute; my ($f5_iface_status); $f5_iface_status=$current_status->fetchrow_hashref(); if($f5_iface_status->{interface_status} ne $stat) { my $iface_result = $dbh->prepare("UPDATE device_iface_cache SET interface_status='$stat', lastcheck='$date', hostname='$host' WHERE hostname='$host' AND interface_name='$int_table{$key}';"); $iface_result->execute(); append_log ("STATUS CHANGED: Interface $int_table{$key} Was: $f5_iface_status->{interface_status} ------- Now: $stat\n"); } my $result = $dbh->prepare("UPDATE device_iface_cache SET interface_status='$stat', lastcheck='$date', hostname='$host' WHERE hostname='$host' AND interface_name='$int_table{$key}';"); $result->execute(); } #If Status is OK if($rate_total > $crit) { $stat = "UP"; append_log ("OK: Interface $int_table{$key} is UP.\n"); append_status("OK: Interface $int_table{$key} is UP.\n"); my $current_status = $dbh->prepare("SELECT interface_status FROM device_iface_cache WHERE interface_name='$int_table{$key}' and hostname='$host';"); $current_status->execute; my ($f5_iface_status); $f5_iface_status=$current_status->fetchrow_hashref(); if($f5_iface_status->{interface_status} ne $stat) { my $iface_result = $dbh->prepare("UPDATE device_iface_cache SET interface_status='$stat', lastcheck='$date', hostname='$host' WHERE hostname='$host' AND interface_name='$int_table{$key}';"); $iface_result->execute(); append_log ("STATUS CHANGED: Interface $int_table{$key} Was: $f5_iface_status->{interface_status} ------- Now: $stat\n"); } my $result = $dbh->prepare("UPDATE device_iface_cache SET interface_status='$stat', lastcheck='$date', hostname='$host' WHERE hostname='$host' AND interface_name='$int_table{$key}';"); $result->execute(); } }