From eef23fb03918f05f357a2ef54cf4f949b76c18ce Mon Sep 17 00:00:00 2001 From: Alexander Sulfrian Date: Fri, 14 Feb 2014 04:07:36 +0100 Subject: add multi host mode with NSCA-ng compatible output --- check_load | 60 +++++++++++++++++++++++++++++++++++++++++++---------------- check_mem | 63 ++++++++++++++++++++++++++++++++++++++++++++++---------------- 2 files changed, 91 insertions(+), 32 deletions(-) diff --git a/check_load b/check_load index 6f068d7..59a8554 100755 --- a/check_load +++ b/check_load @@ -5,6 +5,7 @@ use warnings; use VServer; use Nagios::Plugin; +use Nagios::Plugin::Functions; our $VERSION = '1.6'; @@ -13,7 +14,7 @@ sub init_nagios_plugin() { usage => "Usage: %s [-h|--help] [-w|--warnings=...] " . "[-c|--critical=...] [-d|--domain=...] [vserver-name]", version => $VERSION, - blurb => "This plugin could monitor the load of single vserver guests." + blurb => "This plugin could monitor the load of vserver guests." ); $plugin->add_arg( @@ -58,11 +59,28 @@ sub parse_thresholds($) { return undef; } -sub handle_vserver($$$$) { +sub output_results($$$$) { + my $n = shift; + my $name = shift; + my $statuses = shift; + my $output = shift; + + my $service = $ENV{NAGIOS_PLUGIN}; + my $status = $n->max_state(@{$statuses}); + $status = Nagios::Plugin::Functions::UNKNOWN unless + defined $status && exists $Nagios::Plugin::Functions::STATUS_TEXT{$status}; + my $result = $Nagios::Plugin::Functions::STATUS_TEXT{$status}; + my $perf = $n->all_perfoutput(); + + print "$name\t$service\t$status\t$result - load average:$output | $perf\n\x17"; +} + +sub handle_vserver($$$;$$) { my $id = shift; my $warn = shift; my $crit = shift; my $name = shift; + my $domain = shift; my $n = init_nagios_plugin(); my $load = VServer::get_loadavg($id); @@ -88,12 +106,19 @@ sub handle_vserver($$$$) { $output .= " $load->{$key}"; } - $n->nagios_exit($n->max_state(@status), "load average:$output"); + if ($name) { + $n->nagios_exit($n->max_state(@status), "load average:$output"); + } + else { + $name = VServer::get_name($id); + output_results($n, "$name$domain", \@status, $output); + } } } # fix taint mode +$ENV{PATH} = undef; if ($0 =~ m/(\w[\w\/]*\w)/) { $ENV{'NAGIOS_PLUGIN'} = $1; } @@ -113,20 +138,23 @@ for my $load (qw( load1 load5 load15 )) { } } -my $vserver = shift; -unless ($vserver) { - $n->nagios_exit(UNKNOWN, "vserver name required"); -} - my $domain = ''; $domain = ('.' . $n->opts->d) if $n->opts->d; -$domain = quotemeta $domain; -$vserver =~ s/$domain$//; -my $context = VServer::get_context_id($vserver); -unless ($context) { - $n->nagios_exit(UNKNOWN, "vserver '$vserver' not found"); -} +if (my $vserver = shift) { + $domain = quotemeta $domain; + $vserver =~ s/$domain$//; + my $context = VServer::get_context_id_by_name($vserver); + unless ($context) { + $n->nagios_exit(UNKNOWN, "vserver '$vserver' not found"); + } -handle_vserver($context, \%warn, \%crit, $vserver); -$n->nagios_exit(UNKNOWN, 'unable to parse loadavg'); + handle_vserver($context, \%warn, \%crit, $vserver); + $n->nagios_exit(UNKNOWN, 'unable to parse loadavg'); +} +else { + for my $dir (glob("/proc/virtual/*/")) { + my $id = VServer::get_context_id_by_dir($dir); + handle_vserver($id, \%warn, \%crit, undef, $domain); + } +} diff --git a/check_mem b/check_mem index fb25403..009f067 100755 --- a/check_mem +++ b/check_mem @@ -5,6 +5,7 @@ use warnings; use VServer; use Nagios::Plugin; +use Nagios::Plugin::Functions; our $VERSION = '1.6'; @@ -20,7 +21,7 @@ sub init_nagios_plugin() { usage => "Usage: %s [-h|--help] [-w|--warnings=...] " . "[-c|--critical=...] [-d|--domain=...] [vserver-name]", version => $VERSION, - blurb => "This plugin could monitor the load of single vserver guests." + blurb => "This plugin could monitor the memory of vserver guests." ); $plugin->add_arg( @@ -69,11 +70,28 @@ sub parse_thresholds($) { return undef; } -sub handle_vserver($$$$) { +sub output_results($$$$) { + my $n = shift; + my $name = shift; + my $statuses = shift; + my $output = shift; + + my $service = $ENV{NAGIOS_PLUGIN}; + my $status = $n->max_state(@{$statuses}); + $status = Nagios::Plugin::Functions::UNKNOWN unless + defined $status && exists $Nagios::Plugin::Functions::STATUS_TEXT{$status}; + my $result = $Nagios::Plugin::Functions::STATUS_TEXT{$status}; + my $perf = $n->all_perfoutput(); + + print "$name\t$service\t$status\t$result - used memory$output | $perf\n\x17"; +} + +sub handle_vserver($$$;$$) { my $id = shift; my $warn = shift; my $crit = shift; my $name = shift; + my $domain = shift; my $n = init_nagios_plugin(); my $mem = VServer::get_mem($id); @@ -100,12 +118,19 @@ sub handle_vserver($$$$) { $output .= " - $desc{$key}: ${mb}MB"; } - $n->nagios_exit($n->max_state(@status), "used memory$output"); + if ($name) { + $n->nagios_exit($n->max_state(@status), "used memory$output"); + } + else { + $name = VServer::get_name($id); + output_results($n, "$name$domain", \@status, $output); + } } } # fix taint mode +$ENV{PATH} = undef; if ($0 =~ m/(\w[\w\/]*\w)/) { $ENV{'NAGIOS_PLUGIN'} = $1; } @@ -118,20 +143,26 @@ 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 $domain = ''; $domain = ('.' . $n->opts->d) if $n->opts->d; -$domain = quotemeta $domain; -$vserver =~ s/$domain$//; -my $context = VServer::get_context_id($vserver); -unless ($context) { - $n->nagios_exit(UNKNOWN, "vserver '$vserver' not found"); -} +if (my $vserver = shift) { + if (defined $domain) { + $domain = quotemeta $domain; + $vserver =~ s/$domain$//; + } + + my $context = VServer::get_context_id_by_name($vserver); + unless ($context) { + $n->nagios_exit(UNKNOWN, "vserver '$vserver' not found"); + } -handle_vserver($context, \%warn, \%crit, $vserver); -$n->nagios_exit(UNKNOWN, 'unable to parse memory statistics'); + handle_vserver($context, \%warn, \%crit, $vserver); + $n->nagios_exit(UNKNOWN, 'unable to parse memory statistics'); +} +else { + for my $dir (glob("/proc/virtual/*/")) { + my $id = VServer::get_context_id_by_dir($dir); + handle_vserver($id, \%warn, \%crit, undef, $domain); + } +} -- cgit v1.2.3-1-g7c22