#!/usr/bin/perl -w # # check network traffic on servers # # syntax: # ./check_network --host= --community= [--warning=] [--critical=] [--interface=] # # jorgbosman@optiver.com - 15 juni 2004 use strict; use Getopt::Long; use Net::SNMP; #use Data::Dumper; # # settings # my $hostname = ""; my $community = "public"; my $warning = "10"; my $critical = "20"; my $interface; my $tmpdir = '/tmp/nagios'; GetOptions("hostname:s" => \$hostname, "community:s" => \$community, "warning:i" => \$warning, "critical:i" => \$critical, "interface:s" => \$interface); # 2 underscores -> 1 underscore, 1 underscore -> space $interface =~ s/_/ /g; $interface =~ s/ /_/g; my $errorcode = 0; my ($session, $error) = Net::SNMP->session(-hostname => $hostname, -community => $community, -timeout => 60, -retries => 1); if (! defined $session) { printf("ERROR: %s.\n", $error); exit 3; # unknown } # # find number corresponding with interface-name # my $nr; if (defined $interface) { if ($interface =~ /.*\.\d+/) { $nr = $interface; $nr =~ s/.*\.(\d+)$/$1/; } else { my $i = 1; my $result = 1; while (defined $result) { my @oids = (".1.3.6.1.2.1.2.2.1.2.$i"); $result = $session->get_request(-varbindlist => \@oids); if (defined $result) { if ($result->{$oids[0]} eq $interface) { $nr = $i; undef $result; } } $i++; } } } if (! defined $nr) { print("No snmpd running or bad connection\n"); $session->close; exit 0; # ok } # ifDescr.$nr ifInOctets.$nr ifOutOctets.$nr ifInErrors.$nr ifOutErrors.$nr ifInDiscards.$nr my @oids = (".1.3.6.1.2.1.2.2.1.2.$nr", ".1.3.6.1.2.1.2.2.1.10.$nr", ".1.3.6.1.2.1.2.2.1.16.$nr", ".1.3.6.1.2.1.2.2.1.14.$nr", ".1.3.6.1.2.1.2.2.1.20.$nr", ".1.3.6.1.2.1.2.2.1.13.$nr"); # # get snmp-data # my $result = $session->get_request(-varbindlist => \@oids); if (!defined($result)) { print("No snmpd running or bad connection\n"); $session->close; exit 0; # ok } my @current = ($result->{$oids[1]}, $result->{$oids[2]}, $result->{$oids[3]}, $result->{$oids[4]}, $result->{$oids[5]}, 0); # # get data from previous readings # my @previous; if ( -f "$tmpdir/$hostname.$nr.counters" ) { open(FILE, "$tmpdir/$hostname.$nr.counters"); @previous = ; close(FILE); } if ( ! -d "$tmpdir" ) { mkdir("$tmpdir", 0755); } open(FILE, ">$tmpdir/$hostname.$nr.counters"); foreach my $line (@current) { print FILE "$line\n"; } close(FILE); my @diff; my $i = 0; while ($i <= 5) { if ((defined $current[$i]) && (defined $previous[$i])) { $diff[$i] = $current[$i] - $previous[$i]; } else { $diff[$i] = 0; } $i++; } # # netjes weergeven # $diff[0] = $diff[0] / 300; # per second ipv 5 minutes $diff[1] = $diff[1] / 300; # per second ipv 5 minutes if ($diff[0] > 1000000) { $diff[0] = sprintf("%.1fM", $diff[0] / 1000000); } elsif ($diff[0] > 1000) { $diff[0] = sprintf("%.1fK", $diff[0] / 1000); } else { $diff[0] = sprintf("%.1f", $diff[0]); } if ($diff[1] > 1000000) { $diff[1] = sprintf("%.1fM", $diff[1] / 1000000); } elsif ($diff[1] > 1000) { $diff[1] = sprintf("%.1fK", $diff[1] / 1000); } else { $diff[1] = sprintf("%.1f", $diff[1]); } # # parse data # print "Traffic in/out: $diff[0]/$diff[1]"; if (($diff[2] > 0) || ($diff[3] > 0)) { print ", errors: $diff[2]/$diff[3]"; if (($diff[2] > $critical) || ($diff[3] > $critical)) { $errorcode = 2; } elsif (($diff[2] > $warning) || ($diff[3] > $warning)) { $errorcode = 1; } } if ($diff[4] > 0) { print ", discards: $diff[4]"; if ($errorcode < 2) { if ($diff[4] > $critical) { $errorcode = 2; } elsif ($diff[4] > $warning) { $errorcode = 1; } } } print "|traffic,$current[0]:$current[1] errors,$current[2]:$current[3] discards,$current[4]:0\n"; # # exit # (0=ok 1=warning 2=critical 3=unknown 4=dependent) # exit $errorcode;