#!/usr/bin/perl -wT # # check memory op nodes # # syntax: # ./check_memory --host= --community= [--warning=] [--critical=] # # jorg@wirelessleiden.nl - 17 april 2004 use strict; use Getopt::Long; use Net::SNMP; use Data::Dumper; # # settings # my $hostname = ""; my $community = "public"; my $warningstring = "95"; my $criticalstring = "99"; GetOptions("hostname:s" => \$hostname, "community:s" => \$community, "warning:i" => \$warningstring, "critical:i" => \$criticalstring); 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 } # # get snmp-data # # memTotalReal.0 memAvailReal.0 memTotalSwap.0 memAvailSwap.0 my @oids = (".1.3.6.1.4.1.2021.4.5.0", ".1.3.6.1.4.1.2021.4.6.0", ".1.3.6.1.4.1.2021.4.3.0", ".1.3.6.1.4.1.2021.4.4.0"); my $result = $session->get_request(-varbindlist => \@oids); if (! defined $result) { print "No snmpd running or bad connection\n"; exit 0; # ok } # # parse data # my $checknr = 3; # check for free swapspace... my $checktotal = 2; if ($result->{$oids[2]} == 0) { # ...except if there isn't any swapspace. check then for totalfree $checknr = 1; $checktotal = 0; } if ((100 - ($result->{$oids[$checknr]} / $result->{$oids[$checktotal]} * 100)) >= $warningstring) { $errorcode = 1; } if ((100 - ($result->{$oids[$checknr]} / $result->{$oids[$checktotal]} * 100)) >= $criticalstring) { $errorcode = 2; } my $mem = $result->{$oids[1]}; my $swap = $result->{$oids[3]}; if ($mem > 1048576) { $mem = sprintf("%.1fG", $mem / 1048576); } elsif ($mem > 1024) { $mem = sprintf("%.1fM", $mem / 1024); } else { $mem .= "K"; } if ($swap > 1048576) { $swap = sprintf("%.1fG", $swap / 1048576); } elsif ($swap > 1024) { $swap = sprintf("%.1fM", $swap / 1024); } else { $swap .= "K"; } print "Free memory: $mem"; if ($result->{$oids[2]} > 0) { print " Free swap: $swap"; } print "|$result->{$oids[0]}:$result->{$oids[1]}:$result->{$oids[2]}:$result->{$oids[3]}\n"; # # exit # (0=ok 1=warning 2=critical 3=unknown 4=dependent) # exit $errorcode;