WebSVN - scripts - Diff - Rev 10 and 11 - /perl/trunk/v1.0/check_memory.pl

thorko Repositories scripts

Rev

Rev 10 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 10 Rev 11
Line 11... Line 11...
11
my $help = 0;
11
my $help = 0;
12
my $warning = "";
12
my $warning = "";
13
my $critical = "";
13
my $critical = "";
14
my $processname = "";
14
my $processname = "";
15
my $memory;
15
my $memory;
-
 
16
# fields to be extracted
-
 
17
# check /proc/pid/status for valid fields
-
 
18
our @fields = ("VmData", "VmStk", "VmExe", "VmLib", "VmRSS");
16
19
17
my $line;
20
my $line;
18
my @pids;
21
my @pids;
19
my $pid;
22
my $pid;
20
23
Line 22... Line 25...
22
sub help () {
25
sub help () {
23
        print << 'HELP';
26
        print << 'HELP';
24
check_memory.pl [-d] [-h] -w <warning> -c <crtical> -p <processname>
27
check_memory.pl [-d] [-h] -w <warning> -c <crtical> -p <processname>
25

28

26
-p, --pname     process name to check for memory consumption
29
-p, --pname     process name to check for memory consumption
27
-w, --warning   warning threshold in KB
-
 
28
-c, --critical  critical threshold in KB
-
 
-
 
30
-w, --warning   warning threshold in KB (VmData + VmStk)
-
 
31
-c, --critical  critical threshold in KB (VmData + VmStk)
29
-d, --debug     enable debug mode
32
-d, --debug     enable debug mode
30
-h, --help      show help message
33
-h, --help      show help message
31
HELP
34
HELP
32
}
35
}
33
36
34
## memusage ##
37
## memusage ##
35
sub memusage {
38
sub memusage {
36
        my $mem;
-
 
37
        my ($process) = @_;
-
 
38
        open CMD, "ps -o size --no-header $process |" or die("couldn't run ps -o size $process");
-
 
39
        $mem = <CMD>;
-
 
40
        chomp $mem;
-
 
41
        close CMD;
-
 
42
        return $mem;
-
 
-
 
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;
43
}
53
}
44
54
45
# get options
55
# get options
46
Getopt::Long::Configure('bundling');
56
Getopt::Long::Configure('bundling');
47
GetOptions(
57
GetOptions(
Line 73... Line 83...
73
close CMD;
83
close CMD;
74
84
75
if ( scalar @pids ) {
85
if ( scalar @pids ) {
76
        # calculate overall memory
86
        # calculate overall memory
77
        foreach (@pids) {
87
        foreach (@pids) {
78
                $memory += memusage($_);
-
 
79
                print "$_ memory:".memusage($_)."\n" if ( $debug );
-
 
-
 
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);
80
        }
96
        }
-
 
97
}
-
 
98
-
 
99
my $perfdata;
-
 
100
my $size = $memory->{"VmData"} + $memory->{"VmStk"};
-
 
101
# create perfdata
-
 
102
foreach (keys %$memory) {
-
 
103
    $perfdata .= "$_: $memory->{$_} ";
81
}
104
}
82
105
83
# check thresholds
106
# check thresholds
84
if ( $memory > $critical ) {
-
 
85
        print "CRITICAL: $processname, memory: $memory KB | memory: $memory KB\n";
-
 
-
 
107
if ( $size > $critical ) {
-
 
108
        print "CRITICAL: $processname, memory: $size KB | $perfdata\n";
86
        exit(2);
109
        exit(2);
87
} elsif ( $memory > $warning ) {
-
 
88
        print "WARNING: $processname, memory: $memory KB | memory: $memory KB\n";
-
 
-
 
110
} elsif ( $size > $warning ) {
-
 
111
        print "WARNING: $processname, memory: $size KB | $perfdata\n";
89
        exit(1);
112
        exit(1);
90
} else {
113
} else {
91
        print "OK: $processname, memory: $memory KB | memory: $memory KB\n";
-
 
-
 
114
        print "OK: $processname, memory: $size KB | $perfdata\n";
92
        exit(0);
115
        exit(0);
93
}
116
}
94
117
95
__DATA__
118
__DATA__