1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
|
#!/usr/bin/perl
#
# this software is released under the GPL (http://www.gnu.org/)
# it is written by Eric Lewandowski <fear-the-penguin@home.com>
# and <tmancill@debian.org>
#
# check_swap.pl <host> [warn] [critical] [port]
#
# NetSaint host script to get the %-utilization of swap from netsaint_statd.
#
$SWAPWARN = 85;
$SWAPCRITICAL = 95;
require 5.003;
BEGIN { $ENV{PATH} = '/bin' }
use Socket;
use POSIX;
sub usage;
my $TIMEOUT = 15;
my %ERRORS = ('UNKNOWN', '-1',
'OK', '0',
'WARNING', '1',
'CRITICAL', '2');
my $remote = shift || &usage(%ERRORS);
my $warn = shift || $SWAPWARN;
my $crit = shift || $SWAPCRITICAL;
my $port = shift || 1040;
my $remoteaddr = inet_aton("$remote");
my $paddr = sockaddr_in($port, $remoteaddr) || die "Can't create info for connection: #!\n";;
my $proto = getprotobyname('tcp');
socket(Server, PF_INET, SOCK_STREAM, $proto) || die "Can't create socket: $!";
setsockopt(Server, SOL_SOCKET, SO_REUSEADDR, 1);
# Just in case of problems, let's not hang NetSaint
$SIG{'ALRM'} = sub {
close(Server);
select(STDOUT);
print "No Answer from Client\n";
exit $ERRORS{"UNKNOWN"};
};
alarm($TIMEOUT);
# send ourselves a SIGALRM if we cannot connect to the client
unless (connect(Server, $paddr)) {
kill 'ALRM', $$;
}
my $state = "OK";
my $answer = undef;
select(Server);
$| = 1;
print Server "swap\n";
my ($servanswer) = <Server>;
alarm(0);
close(Server);
select(STDOUT);
if ($servanswer =~ /^(\d+)/) {
$swaputil = $1;
if ($swaputil < $warn) {
$answer = "swap ok - $swaputil\% utilized\n";
} elsif ($swaputil < $crit) {
$state = "WARNING";
$answer = "swap $swaputil\% utilized\n";
} else {
$state = "CRITICAL";
$answer = "swap $swaputil\% utilized\n";
}
} else {
$state = "UNKNOWN";
$answer = "swap utilization unknown!\n";
}
print $answer;
exit $ERRORS{$state};
sub usage {
print "Minimum arguments not supplied!\n";
print "\n";
print "Perl Check Swap plugin for NetSaint\n";
print "Copyright (c) 2001 tony mancill\n";
print "\n";
print "Usage: $0 <host> [<warn [<crit> [<port>]]]\n";
print "\n";
print "<warn> = swap utilization at which a warning message will be generated.\n Defaults to $SWAPWARN.\n";
print "<crit> = swap utilization at which a critical message will be generated.\n Defaults to $SWAPCRITICAL.\n";
print "<port> = Port that the status daemon is running on <host>.\n Defaults to 1040.\n";
exit $ERRORS{"UNKNOWN"};
}
|