From 2816f34bc6b18ee7bf2832966f9534c5c198eec3 Mon Sep 17 00:00:00 2001 From: Alexander Sulfrian Date: Fri, 14 Feb 2014 04:03:38 +0100 Subject: refactor code to have a sub to handle a single vserver --- check_load | 61 +++++++++++++++++++++++++++--------------------- check_mem | 78 ++++++++++++++++++++++++++++++++++---------------------------- 2 files changed, 78 insertions(+), 61 deletions(-) diff --git a/check_load b/check_load index 8a3b2c8..6f068d7 100755 --- a/check_load +++ b/check_load @@ -58,6 +58,40 @@ 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 $load = VServer::get_loadavg($id); + if ($load) { + my @status = [ UNKNOWN ]; + my $output = ""; + + for my $key (qw( load1 load5 load15 )) { + $n->add_perfdata(label => $key, + value => $load->{$key}, + warning => $warn->{$key}, + critical => $crit->{$key}, + min => 0, + ); + + push(@status, + $n->check_threshold(check => $load->{$key}, + warning => $warn->{$key}, + critical => $crit->{$key} + ) + ); + + $output .= " $load->{$key}"; + } + + $n->nagios_exit($n->max_state(@status), "load average:$output"); + } +} + # fix taint mode if ($0 =~ m/(\w[\w\/]*\w)/) { @@ -94,30 +128,5 @@ unless ($context) { $n->nagios_exit(UNKNOWN, "vserver '$vserver' not found"); } -my $load = VServer::get_loadavg($id); -if (@load) { - my @status = [ UNKNOWN ]; - my $output = ""; - - for my $key (qw( load1 load5 load15 )) { - $n->add_perfdata(label => $key, - value => $load->{$key}, - warning => $warn{$key}, - critical => $crit{$key}, - min => 0, - ); - - push(@status, - $n->check_threshold(check => $load->{$key}, - warning => $warn{$key}, - critical => $crit{$key} - ) - ); - - $output .= " $load->{$key}"; - } - - $n->nagios_exit($n->max_state(@status), "load average:$output"); -} - +handle_vserver($context, \%warn, \%crit, $vserver); $n->nagios_exit(UNKNOWN, 'unable to parse loadavg'); 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'); -- cgit v1.2.3-1-g7c22