#!/usr/bin/perl -wT # # check cpuload op servers # # syntax: # ./check_cpu --host= --community= [--warning=] [--critical=] [--file=] # # jorgbosman@optiver.com - 16 april 2004 use strict; use Getopt::Long; use Net::SNMP; use XML::Simple; #use Data::Dumper; # # settings # my $hostname = ""; my $community = "public"; my $warning = "1"; # 5 min load avg my $critical = "2"; # 5 min load avg my $file; # optional xml configfile my ($min,$hour,$wday) = (localtime)[1,2,6]; my $now = $hour * 100 + $min; GetOptions("hostname:s" => \$hostname, "community:s" => \$community, "warning:i" => \$warning, "critical:i" => \$critical, "file:s" => \$file); my @config; if (defined $file) { $file =~ /([\/\w\.\-]+)/s; # untaint if (-f $file) { my ($config) = XMLin($file, forcearray=>['cpu']); if (defined $config->{'warning'}) { $warning = $config->{'warning'}; } if (defined $config->{'critical'}) { $critical = $config->{'critical'}; } } } # # connect # my $errorcode = 0; my ($session, $error) = Net::SNMP->session(-hostname => $hostname, -community => $community, -timeout => 60, -retries => 1); if (! defined $session) { printf("ERROR Connecting: %s.\n", $error); exit 3; # unknown } # # try to get cpuload the unix-way # my %cpu; my $result; my $result2; my @oids; # laLoad.1 laLoad.2 laLoad.3 ssCpuUser.0 ssCpuSystem.0 ssCpuIdle.0 @oids = qw(.1.3.6.1.4.1.2021.10.1.3.1 .1.3.6.1.4.1.2021.10.1.3.2 .1.3.6.1.4.1.2021.10.1.3.3 .1.3.6.1.4.1.2021.11.9.0 .1.3.6.1.4.1.2021.11.10.0 .1.3.6.1.4.1.2021.11.11.0); $result = $session->get_request(-varbindlist => \@oids); if (! defined($result)) { print "No response.\n"; exit 0; # ok } # # parse data # my $load1 = $result->{$oids[0]}; my $load5 = $result->{$oids[1]}; my $load15 = $result->{$oids[2]}; my $user = $result->{$oids[3]}; my $system = $result->{$oids[4]}; my $idle = $result->{$oids[5]}; if ($load5 > $critical) { $errorcode = 2; } elsif ($load5 > $warning) { $errorcode = 1; } # # print result # printf("Load: %.2f|%.2f:%.2f:%.2f:%.2f:%.2f:%.2f\n", $load5, $load1, $load5, $load15, $user, $system, $idle); # # exit # (0=ok 1=warning 2=critical 3=unknown 4=dependent) # #print "errorcode: $errorcode\n"; $session->close; exit $errorcode;