| Server IP : 172.64.80.1 / Your IP : 216.73.216.175 Web Server : LiteSpeed System : Linux srv13.swhc.ca 4.18.0-553.126.2.lve.el8.x86_64 #1 SMP Thu May 28 14:12:30 UTC 2026 x86_64 User : hongluck ( 2522) PHP Version : 7.4.33 Disable Function : NONE MySQL : OFF | cURL : ON | WGET : ON | Perl : ON | Python : ON | Sudo : OFF | Pkexec : OFF Directory : /usr/lib64/nagios/plugins/ |
Upload File : |
#!/usr/bin/perl
#
# $Id: check_memory.pl 501 2016-05-24 08:28:14Z phil $
#
# program: check_memory
# author, (c): Philippe Kueck <projects at unixadm dot org>
#
use strict;
use warnings;
use Getopt::Long;
use Pod::Usage;
my $stats = {
'MemTotal' => {},
'MemFree' => {'w' => 20, 'c' => 10, 'of' => 'MemTotal'},
'Buffers' => {},
'Cached' => {},
'SwapTotal' => {},
'SwapFree' => {'w' => 85, 'c' => 50, 'of' => 'SwapTotal'}
};
my $icache = 0;
sub nagexit {
my $exitc = {0 => 'OK', 1 => 'WARNING', 2 => 'CRITICAL', 3 => 'UNKNOWN'};
printf "%s - %s\n", $exitc->{$_[0]}, $_[1];
exit $_[0]
}
Getopt::Long::Configure("no_ignore_case");
GetOptions(
'H=s' => sub {},
'f' => \$icache,
'w=i' => \$stats->{'MemFree'}->{'w'},
'c=i' => \$stats->{'MemFree'}->{'c'},
'W=i' => \$stats->{'SwapFree'}->{'w'},
'C=i' => \$stats->{'SwapFree'}->{'c'},
'h|help' => sub {pod2usage({'-exitval' => 3, '-verbose' => 2})}
) or pod2usage({'-exitval' => 3, '-verbose' => 0});
open FILE, "< /proc/meminfo" or nagexit 3, "no reasonable meminfo";
while (<FILE>) {
next unless /^(\w+?):\s+(\d+?)(?:\s|$)/;
$stats->{$1}->{'val'} = $2
}
close FILE;
my $exit = 0;
my $perfdata;
$stats->{'MemFree'}->{'val'} += $stats->{'Buffers'}->{'val'} +
$stats->{'Cached'}->{'val'} + $stats->{'SReclaimable'}->{'val'}
if $icache;
foreach (sort keys %$stats) {
my ($warn, $crit) = (0,0);
$crit = $stats->{$stats->{$_}->{'of'}}->{'val'} * $stats->{$_}->{'c'}
/ 100 if exists $stats->{$_}->{'c'};
$warn = $stats->{$stats->{$_}->{'of'}}->{'val'} * $stats->{$_}->{'w'}
/ 100 if exists $stats->{$_}->{'w'};
$exit = 2 if $exit < 2 && $stats->{$_}->{'val'} < $crit;
$exit = 1 if $exit < 1 && $stats->{$_}->{'val'} < $warn;
$perfdata .= sprintf "'%s'=%dKB;%d;%d;0;%s ",
$_, ($stats->{$_}->{'val'} or 0), $warn, $crit,
(exists $stats->{$_}->{'of'})?$stats->{$stats->{$_}->{'of'}}->{'val'}:""
}
chop $perfdata;
nagexit $exit, sprintf "memory: %.2f%% free, swap: %.2f%% free|%s",
$stats->{'MemFree'}->{'val'} / $stats->{'MemTotal'}->{'val'} * 100,
($stats->{'SwapTotal'}->{'val'}?$stats->{'SwapFree'}->{'val'} /
$stats->{'SwapTotal'}->{'val'} * 100:0),
$perfdata
__END__
=head1 NAME
check_memory
=head1 VERSION
$Revision: 501 $
=head1 SYNOPSIS
check_memory [-H HOST] [-f] [-w MEMWARN] [-c MEMCRIT] [-W SWAPWARN] [-C SWAPCRIT]
=head1 OPTIONS
=over 8
=item B<H>
Dummy for compatibility with Nagios.
=item B<f>
Count cache, buffers and slab as free memory.
=item B<w>
Free memory warning level in percent. Defaults to 20%.
=item B<c>
Free memory critical level in percent. Defaults to 10%.
=item B<W>
Swap free warning level in percent. Defaults to 85%.
=item B<C>
Swap free critical level in percent. Defaults to 50%.
=back
=head1 DESCRIPTION
This nagios/icinga check script checks the memory and swap usage. It also returns the perfdata.
=head1 DEPENDENCIES
=over 8
=item C<Getopt::Long>
=item C<Pod::Usage>
=back
=head1 AUTHOR
Philippe Kueck <projects at unixadm dot org>
=cut