thorko Repositories scripts

Compare Revisions

Ignore whitespace Rev 10 → Rev 11

/perl/trunk/v1.0/check_memory.pl
13,6 → 13,9
my $critical = "";
my $processname = "";
my $memory;
# fields to be extracted
# check /proc/pid/status for valid fields
our @fields = ("VmData", "VmStk", "VmExe", "VmLib", "VmRSS");
 
my $line;
my @pids;
24,8 → 27,8
check_memory.pl [-d] [-h] -w <warning> -c <crtical> -p <processname>
 
-p, --pname process name to check for memory consumption
-w, --warning warning threshold in KB
-c, --critical critical threshold in KB
-w, --warning warning threshold in KB (VmData + VmStk)
-c, --critical critical threshold in KB (VmData + VmStk)
-d, --debug enable debug mode
-h, --help show help message
HELP
33,13 → 36,20
 
## memusage ##
sub memusage {
my $mem;
my ($process) = @_;
open CMD, "ps -o size --no-header $process |" or die("couldn't run ps -o size $process");
$mem = <CMD>;
chomp $mem;
close CMD;
return $mem;
my ($mem, $line);
my %memory;
my ($process) = @_;
open STAT, "< /proc/$process/status" or die("Couldn't read /proc/$process/status");
while($line = <STAT>) {
foreach my $field (@fields) {
($mem) = $line =~ m/^$field:\s+(.*)\skB/;
if ( $mem ) {
$memory{$field} = $mem;
}
}
}
close STAT;
return \%memory;
}
 
# get options
75,20 → 85,33
if ( scalar @pids ) {
# calculate overall memory
foreach (@pids) {
$memory += memusage($_);
print "$_ memory:".memusage($_)."\n" if ( $debug );
print "$_: " if ($debug);
my $n = memusage($_);
foreach (keys %$n) {
# summarize all fields
$memory->{$_} += $n->{$_};
print "$_: $n->{$_}, " if ($debug);
}
print "\n" if ($debug);
}
}
 
my $perfdata;
my $size = $memory->{"VmData"} + $memory->{"VmStk"};
# create perfdata
foreach (keys %$memory) {
$perfdata .= "$_: $memory->{$_} ";
}
 
# check thresholds
if ( $memory > $critical ) {
print "CRITICAL: $processname, memory: $memory KB | memory: $memory KB\n";
if ( $size > $critical ) {
print "CRITICAL: $processname, memory: $size KB | $perfdata\n";
exit(2);
} elsif ( $memory > $warning ) {
print "WARNING: $processname, memory: $memory KB | memory: $memory KB\n";
} elsif ( $size > $warning ) {
print "WARNING: $processname, memory: $size KB | $perfdata\n";
exit(1);
} else {
print "OK: $processname, memory: $memory KB | memory: $memory KB\n";
print "OK: $processname, memory: $size KB | $perfdata\n";
exit(0);
}