#!/usr/local/bin/perl -w # Program: check_swap.pl # Purpose: Nagios check_swap plugin in perl for Linux # Author: James Briggs, San Jose, California, USA # Date: 2005 08 29 # Env: Perl5 # License: GPL Version 2 use strict; use Getopt::Long; my $DEBUG = 0; my $VERSION = 0.1; my $c = ''; my $w = ''; my $a = ''; my $h = ''; my $v = ''; my $result = GetOptions ('w|warning=s' => \$w, 'c|critical=s' => \$c, 'a|allswaps' => \$a, 'h|help' => \$h, 'V|version' => \$v, ); if ($v) { print_version(); exit; } elsif ($h) { print_help(); exit; } elsif ($a) { print "-a and --allswaps not implemented yet.\n"; exit; } # Swap: 2040244 53292 1986952 die "error: free command not found" if not -e '/usr/bin/free'; my $cmd = `/usr/bin/free -b`; my ($total, $used, $free) = $cmd =~ /Swap:\s+(\d+)\s+(\d+)\s+(\d+)/; print $total, $used, $free, "\n" if $DEBUG; print $c, $w, $a if $DEBUG; die "error: no swap space found" if !$total; my $free_per = 100 * $free / $total; my $used_per = 100 - $free_per; my $free_mb = int($free / 1000000); my $total_mb = int($total / 1000000); my $msg = sprintf("%4.1f%%", $free_per) . " free ($free_mb MB out of $total_mb MB)"; if ($c ne '') { my $err = 0; if ($c =~ /%/) { $c =~ s/%//g; $err = 1 if $used_per > $c; } else { $err = 1 if $free < $c; } if ($err) { print "SWAP CRITICAL: $msg\n"; exit 2; } } if ($w ne '') { my $err = 0; if ($w =~ /%/) { $w =~ s/%//g; $err = 1 if $used_per > $w; } else { $err = 1 if $free < $w; } if ($err) { print "SWAP WARNING: $msg\n"; exit 1; } } print "SWAP OK: $msg\n"; exit 0; sub print_version { print "check_swap.pl $VERSION (Nagios-compatible)\n"; } sub print_help { print <% -c % check_swap [-a] -w -c check_swap (-h | --help) for detailed help check_swap (-V | --version) for version information Options: -w, --warning=INTEGER Exit with WARNING status if less than INTEGER bytes of swap space are free -w, --warning=PERCENT% Exit with WARNING status if less than PERCENT of swap space has been used -c, --critical=INTEGER Exit with CRITICAL status if less than INTEGER bytes of swap space are free -c, --critical=PERCENT% Exit with CRITCAL status if less than PERCENT of swap space has been used -a, --allswaps Conduct comparisons for all swap partitions, one by one -h, --help Print detailed help screen -V, --version Print version information EOD }