summaryrefslogtreecommitdiffstats
path: root/check_mem
blob: d3c0722a5412cbec3dfdd71a0d80931d270e3f41 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
#!/usr/bin/perl -T

use strict;
use warnings;

use VServer;
use Nagios::Plugin;
use POSIX;

our $VERSION = '1.5';

sub init_nagios_plugin() {
    my $plugin = Nagios::Plugin->new(
        usage => "Usage: %s [-h|--help] [-w|--warnings=...] " .
                 "[-c|--critical=...] vserver-name",
        version => $VERSION,
        blurb => "This plugin could monitor the load of single vserver guests."
    );

    $plugin->add_arg(
        spec => 'w|warning=s',
        help => "Warning thresholds for the differnet memory types (VM, RSS, ANON, LOCK).",
        label => "RANGE[,RANGE[,RANGE[,RANGE]]]",
    );

    $plugin->add_arg(
        spec => 'c|critical=s',
        help => "Critical thresholds for the differnet memory types (VM, RSS, ANON, LOCK).",
        label => "RANGE[,RANGE[,RANGE[,RANGE]]]",
    );

    return $plugin;
}

sub parse_thresholds($) {
    my $arg = shift;

    if ($arg) {
        if ($arg =~ m/([-:@~0-9]+),([-:@~0-9]+),([-:@~0-9]+),([-:@~0-9]+)/) {
            return ($1, $2, $3, $4);
        }

        if ($arg =~ m/([-:@~0-9]+),([-:@~0-9]+),([-:@~0-9]+)/) {
            return ($1, $2, $3, $3);
        }

        if ($arg =~ m/([-:@~0-9]+),([-:@~0-9]+)/) {
            return ($1, $2, $2, $2);
        }

        if ($arg =~ m/([-:@~0-9]+)/) {
            return ($1, $1, $1, $1);
        }
    }

    return ();
}

sub get_mem($$) {
    my $n = shift;
    my $context = shift;

    my $proc = VServer::get_proc_dir($context);
    $n->nagios_exit(UNKNOWN, "vserver not running") unless($proc);

    my $ps = POSIX::sysconf(&POSIX::_SC_PAGESIZE);
    $n->nagios_exit(UNKNOWN, "unable to determine pagesize") unless $ps;

    open(my $limit, "<", "$proc/limit") ||
        $n->nagios_exit(UNKNOWN, "could not open '$proc/limit'");

    my @mem = undef;
    while (<$limit>) {
        if ($_ =~ m/^VM:\s*([0-9]+)\s/) {
            $mem[0] = $1 * $ps;
        }
        elsif ($_ =~ m/RSS:\s*([0-9]+)\s/) {
            $mem[1] = $1 * $ps;
        }
        elsif ($_ =~ m/ANON:\s*([0-9]+)\s/) {
            $mem[2] = $1 * $ps;
        }
        elsif ($_ =~ m/VML:\s*([0-9]+)\s/) {
            $mem[3] = $1 * $ps;
        }

    }

    close($limit);

    return @mem;
}


# fix taint mode
if ($0 =~ m/(\w[\w\/]*\w)/) {
    $ENV{'NAGIOS_PLUGIN'} = $1;
}

my $n = init_nagios_plugin();
$n->getopts();
alarm $n->opts->timeout;

my (%warn, %crit);
($warn{'VM'}, $warn{'RSS'}, $warn{'ANON'}, $warn{'VML'}) = parse_thresholds($n->opts->w);
($crit{'VM'}, $crit{'RSS'}, $crit{'ANON'}, $crit{'VML'}) = parse_thresholds($n->opts->c);

my $vserver = shift;
unless ($vserver) {
    $n->nagios_exit(UNKNOWN, "vserver name required");
}

my $context = VServer::get_context_id($vserver);
unless ($context) {
    $n->nagios_exit(UNKNOWN, "vserver '$vserver' not found");
}

my @mem = get_mem($n, $context);
if (@mem) {
    my %values;
    my %desc;
    ($values{'VM'}, $values{'RSS'}, $values{'ANON'}, $values{'VML'}) = @mem;
    %desc = (
        VM => 'virtual memory',
        RSS => 'resident set size',
        ANON => 'anonymous memory',
        VML => 'locked memory',
    );

    my @status = [ UNKNOWN ];
    my $output = "";

    for my $mem (qw( VM RSS ANON VML )) {
        $n->add_perfdata(label => $mem,
                         value => $values{$mem},
                         min => 0,
                         warning => $warn{$mem},
                         critical => $crit{$mem},
        );

        push(@status,
             $n->check_threshold(check => $values{$mem},
                                 warning => $warn{$mem},
                                 critical => $crit{$mem},
             )
        );

        $values{$mem} = int(($values{$mem} / 1024) / 1024);
        $output .= " - $desc{$mem}: $values{$mem}MB";
    }

    $n->nagios_exit($n->max_state(@status), "used memory$output");
}

$n->nagios_exit(UNKNOWN, 'unable to parse memory statistics');