diff -r 000000000000 -r 6f49d13a20aa check_mem.pl --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/check_mem.pl Thu Jun 30 14:44:08 2011 +0200 @@ -0,0 +1,177 @@ +#! /usr/bin/perl -w + +# Copyright (C) 2011 Christian Arnold +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . +# +# Christian Arnold + +use 5.010; +use strict; +use File::Basename; +use Getopt::Long; +use Pod::Usage; + +delete @ENV{ grep /^LC_/ => keys %ENV }; +$ENV{LANG} = "C"; +$ENV{LC_ALL} = "C"; + +sub version($$); +sub get_meminfo(); +sub report($$$$); + +my %ERRORS = ( + OK => 0, + WARNING => 1, + CRITICAL => 2, + UNKNOWN => 3, + DEPENDENT => 4 +); + +my $ME = basename $0; +my $NAME = "MEM"; +my $VERSION = "0.1"; + +my %opt = ( + warning => 70, + critical => 90 +); + +MAIN: { + Getopt::Long::Configure('bundling'); + GetOptions( + "w|warning=i" => \$opt{warning}, + "c|critical=i" => \$opt{critical}, + "h|help" => sub { pod2usage( -verbose => 1, -exitval => $ERRORS{OK} ) }, + "m|man" => sub { pod2usage( -verbose => 2, -exitval => $ERRORS{OK} ) }, + "V|version" => sub { version( $ME, $VERSION ); exit $ERRORS{OK}; } + ) or pod2usage( -verbose => 1, -exitval => $ERRORS{CRITICAL} ); + + my ( $percent, $total, $used, $free ) = get_meminfo(); + report( $percent, $total, $used, $free ); +} + +sub report($$$$) { + my ( $percent, $total, $used, $free ) = @_; + + if ( $percent >= $opt{critical} ) { + say +"$NAME CRITICAL: $percent\% used memory - total: $total MB, used: $used MB, free: $free MB|MemUsed=$percent\%;$opt{warning};$opt{critical}"; + exit $ERRORS{CRITICAL}; + } + if ( $percent >= $opt{warning} ) { + say +"$NAME WARNING: $percent\% used memory - total: $total MB, used: $used MB, free: $free MB|MemUsed=$percent\%;$opt{warning};$opt{critical}"; + exit $ERRORS{WARNING}; + } + say +"$NAME OK: $percent\% used memory - total: $total MB, used: $used MB, free: $free MB|MemUsed=$percent\%;$opt{warning};$opt{critical}"; + exit $ERRORS{OK}; +} + +sub get_meminfo() { + my ( $total, $free, $used, $percent ); + my $file = '/proc/meminfo'; + + open( FH, $file ) + or say "$NAME CRITICAL: $file - $!." and exit $ERRORS{CRITICAL}; + + while () { + /^MemTotal:\s+(?[\d.]+)/ + and $total = ( sprintf( "%.0f", $+{total} / 1024 ) ) + and next; + /^MemFree:\s+(?[\d.]+)/ + and $free = ( sprintf( "%.0f", $+{free} / 1024 ) ) + and last; + } + close(FH); + + $used = ( $total - $free ); + $percent = ( sprintf( "%.0f", ( $used / $total ) * 100 ) ); + + return ( $percent, $total, $used, $free ); +} + +sub version($$) { + my ( $progname, $version ) = @_; + + say <<_VERSION; +$progname version $version +Copyright (C) 2011 by Christian Arnold and Schlittermann internet & unix support. + +$ME comes with ABSOLUTELY NO WARRANTY. This is free software, +and you are welcome to redistribute it under certain conditions. +See the GNU General Public Licence for details. +_VERSION +} + +__END__ + +=head1 NAME + +check_mem - nagios plugin to check memory usage + +=head1 SYNOPSIS + +check_mem [B<-w>|B<--warning> I] [B<-c>|B<--critical> I] + +check_mem [B<-h>|B<--help>] + +check_mem [B<-m>|B<--man>] + +check_mem [B<-v>|B<--version>] + +=head1 OPTIONS + +=over + +=item B<-w>|B<--warning> I + +Exit with WARNING status if memory usage in percent exceeds this value (default: 70) + +=item B<-c>|B<--critical> I + +Exit with CRITICAL status if memory usage in percent exceeds this value (default: 90) + +=item B<-h>|B<--help> + +Print detailed help screen. + +=item B<-m>|B<--man> + +Print manual page. + +=item B<-V>|B<--version> + +Print version information. + +=back + +=head1 DESCRIPTION + +This nagios plugin check the memory usage. + +=head1 VERSION + +This man page is current for version 0.1 of B. + +=head1 AUTHOR + +Written by Christian Arnold L + +=head1 COPYRIGHT + +Copyright (C) 2011 by Christian Arnold and Schlittermann internet & unix support. +This is free software, and you are welcome to redistribute it under certain conditions. +See the GNU General Public Licence for details.