summaryrefslogtreecommitdiffstats
path: root/check_mem
diff options
context:
space:
mode:
Diffstat (limited to 'check_mem')
-rwxr-xr-xcheck_mem78
1 files changed, 43 insertions, 35 deletions
diff --git a/check_mem b/check_mem
index 725bedf..fb25403 100755
--- a/check_mem
+++ b/check_mem
@@ -8,6 +8,13 @@ use Nagios::Plugin;
our $VERSION = '1.6';
+our %desc = (
+ vm => 'virtual memory',
+ rss => 'resident set size',
+ anon => 'anonymous memory',
+ vml => 'locked memory',
+);
+
sub init_nagios_plugin() {
my $plugin = Nagios::Plugin->new(
usage => "Usage: %s [-h|--help] [-w|--warnings=...] " .
@@ -62,6 +69,41 @@ sub parse_thresholds($) {
return undef;
}
+sub handle_vserver($$$$) {
+ my $id = shift;
+ my $warn = shift;
+ my $crit = shift;
+ my $name = shift;
+
+ my $n = init_nagios_plugin();
+ my $mem = VServer::get_mem($id);
+ if ($mem) {
+ my @status = [ UNKNOWN ];
+ my $output = "";
+
+ for my $key (qw(vm rss anon vml)) {
+ $n->add_perfdata(label => $key,
+ value => $mem->{$key},
+ warning => $warn->{$key},
+ critical => $crit->{$key},
+ min => 0,
+ );
+
+ push(@status,
+ $n->check_threshold(check => $mem->{$key},
+ warning => $warn->{$key},
+ critical => $crit->{$key}
+ )
+ );
+
+ my $mb = sprintf "%.2f", ($mem->{$key} / 1024) / 1024;
+ $output .= " - $desc{$key}: ${mb}MB";
+ }
+
+ $n->nagios_exit($n->max_state(@status), "used memory$output");
+ }
+}
+
# fix taint mode
if ($0 =~ m/(\w[\w\/]*\w)/) {
@@ -91,39 +133,5 @@ unless ($context) {
$n->nagios_exit(UNKNOWN, "vserver '$vserver' not found");
}
-my $mem = VServer::get_mem($id);
-if ($mem) {
- my %desc;
- %desc = (
- vm => 'virtual memory',
- rss => 'resident set size',
- anon => 'anonymous memory',
- vml => 'locked memory',
- );
-
- my @status = [ UNKNOWN ];
- my $output = "";
-
- for my $key (qw( vm rss anon vml )) {
- $n->add_perfdata(label => $key,
- value => $mem->{$key},
- min => 0,
- warning => $warn{$key},
- critical => $crit{$key},
- );
-
- push(@status,
- $n->check_threshold(check => $mem->{$key},
- warning => $warn{$key},
- critical => $crit{$key},
- )
- );
-
- $mb = int(($mem->{$key} / 1024) / 1024);
- $output .= " - $desc{$mem}: ${mb}MB";
- }
-
- $n->nagios_exit($n->max_state(@status), "used memory$output");
-}
-
+handle_vserver($context, \%warn, \%crit, $vserver);
$n->nagios_exit(UNKNOWN, 'unable to parse memory statistics');