WebSVN - scripts - Blame - Rev 11 - /perl/trunk/v1.0/check_memory.pl

thorko Repositories scripts

Rev

Rev 10 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
10 guest 1
#!/usr/bin/perl
2
# check memory consumption of process
3
# author: thorko 2012
4
 
5
use strict;
6
use warnings;
7
use Getopt::Long;
8
use Fcntl qw(:flock);
9
 
10
my $debug = 0;
11
my $help = 0;
12
my $warning = "";
13
my $critical = "";
14
my $processname = "";
15
my $memory;
11 guest 16
# fields to be extracted
17
# check /proc/pid/status for valid fields
18
our @fields = ("VmData", "VmStk", "VmExe", "VmLib", "VmRSS");
10 guest 19
 
20
my $line;
21
my @pids;
22
my $pid;
23
 
24
## help function ###
25
sub help () {
26
        print << 'HELP';
27
check_memory.pl [-d] [-h] -w <warning> -c <crtical> -p <processname>
28
 
29
-p, --pname     process name to check for memory consumption
11 guest 30
-w, --warning   warning threshold in KB (VmData + VmStk)
31
-c, --critical  critical threshold in KB (VmData + VmStk)
10 guest 32
-d, --debug     enable debug mode
33
-h, --help      show help message
34
HELP
35
}
36
 
37
## memusage ##
38
sub memusage {
11 guest 39
    my ($mem, $line);
40
    my %memory;
41
    my ($process) = @_;
42
    open STAT, "< /proc/$process/status" or die("Couldn't read /proc/$process/status");
43
    while($line = <STAT>) {
44
        foreach my $field (@fields) {
45
            ($mem) = $line =~ m/^$field:\s+(.*)\skB/;
46
            if ( $mem ) {
47
                $memory{$field} = $mem;
48
            }
49
        }
50
    }
51
    close STAT;
52
    return \%memory;
10 guest 53
}
54
 
55
# get options
56
Getopt::Long::Configure('bundling');
57
GetOptions(
58
        "h|help" => \$help,
59
        "d|debug" => \$debug,
60
        "w|warning=f" => \$warning,
61
        "c|critical=f" => \$critical,
62
        "p|pname=s" => \$processname);
63
 
64
if ( $help ) {
65
        help();
66
        exit(0);
67
}
68
 
69
if ( $processname eq "" || $warning eq "" || $critical eq "" ) {
70
        help();
71
        exit(1);
72
}
73
 
74
flock(DATA, LOCK_EX|LOCK_NB) or die "Program already running.";
75
 
76
# get pid of process
77
open CMD, "pgrep $processname|" or die ("Couldn't run pgrep");
78
while ( $pid = <CMD> ) {
79
        chomp $pid;
80
        push @pids, $pid;
81
        print "pid of $processname: $pid\n" if ($debug);
82
}
83
close CMD;
84
 
85
if ( scalar @pids ) {
86
        # calculate overall memory
87
        foreach (@pids) {
11 guest 88
        print "$_: " if ($debug);
89
                my $n = memusage($_);
90
        foreach (keys %$n) {
91
            # summarize all fields
92
            $memory->{$_} += $n->{$_};
93
            print "$_: $n->{$_}, " if ($debug);
94
        }
95
        print "\n" if ($debug);
10 guest 96
        }
97
}
98
 
11 guest 99
my $perfdata;
100
my $size = $memory->{"VmData"} + $memory->{"VmStk"};
101
# create perfdata
102
foreach (keys %$memory) {
103
    $perfdata .= "$_: $memory->{$_} ";
104
}
105
 
10 guest 106
# check thresholds
11 guest 107
if ( $size > $critical ) {
108
        print "CRITICAL: $processname, memory: $size KB | $perfdata\n";
10 guest 109
        exit(2);
11 guest 110
} elsif ( $size > $warning ) {
111
        print "WARNING: $processname, memory: $size KB | $perfdata\n";
10 guest 112
        exit(1);
113
} else {
11 guest 114
        print "OK: $processname, memory: $size KB | $perfdata\n";
10 guest 115
        exit(0);
116
}
117
 
118
__DATA__