From 5fc2e04c0621576995dce61228c3ed889cbf0503 Mon Sep 17 00:00:00 2001 From: root Date: Mon, 21 Jan 2013 21:37:04 +0100 Subject: initial commit --- check_load | 134 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 134 insertions(+) create mode 100755 check_load diff --git a/check_load b/check_load new file mode 100755 index 0000000..a414478 --- /dev/null +++ b/check_load @@ -0,0 +1,134 @@ +#!/usr/bin/perl -T + +use strict; +use warnings; + +use VServer; +use Nagios::Plugin; + +our $VERSION = '1.1'; + +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 different load types.", + label => "INTEGER[,INTEGER[,INTEGER]]", + ); + + $plugin->add_arg( + spec => 'c|critical=s', + help => "Critical thresholds for the different load types.", + label => "INTEGER[,INTEGER[,INTEGER]]", + ); + + return $plugin; +} + +sub parse_thresholds($) { + my $arg = shift; + + if ($arg) { + if ($arg =~ m/([0-9]+),([0-9]+),([0-9]+)/) { + return ($1, $2, $3); + } + + if ($arg =~ m/([0-9]+),([0-9]+)/) { + return ($1, $2, $2); + } + + if ($arg =~ m/([0-9]+)/) { + return ($1, $1, $1); + } + } + + return (0, 0, 0); +} + +sub get_load($$) { + my $n = shift; + my $context = shift; + + my $proc = VServer::get_proc_dir($context); + unless ($proc) { + $n->nagios_exit(UNKNOWN, "vserver not running"); + } + + open(my $cvirt, "<", "$proc/cvirt") || + $n->nagios_exit(UNKNOWN, "could not open '$proc/cvirt'"); + + my @load = undef; + while (<$cvirt>) { + if ($_ =~ m/^loadavg:\s*([0-9.]+)\s*([0-9.]+)\s*([0-9.]+)$/) { + @load = ($1, $2, $3); + last; + } + } + + close($cvirt); + + return @load; +} + + +my $n = init_nagios_plugin(); +$n->getopts(); +alarm $n->opts->timeout; + +my (%warn, %crit); +($warn{'load1'}, $warn{'load5'}, $warn{'load15'}) = parse_thresholds($n->opts->w); +($crit{'load1'}, $crit{'load5'}, $crit{'load15'}) = parse_thresholds($n->opts->c); + +for my $load (qw( load1 load5 load15 )) { + if ($crit{$load} < $warn{$load}) { + $n->nagios_exit(UNKNOWN, "Parameter inconsistency: warning load" . + "is greater than critical load for '$load'"); + } +} + +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 @load = get_load($n, $context); +if (@load) { + my %values; + ($values{'load1'}, $values{'load5'}, $values{'load15'}) = @load; + + my @status = [ UNKNOWN ]; + my $output = ""; + + for my $load (qw( load1 load5 load15 )) { + $n->add_perfdata(label => $load, + value => $values{$load}, + warning => $warn{$load}, + critical => $crit{$load}, + min => 0, + ); + + push(@status, + $n->check_threshold(check => $values{$load}, + warning => $warn{$load}, + critical => $crit{$load} + ) + ); + + $output .= " $values{$load}"; + } + + $n->nagios_exit($n->max_state(@status), "load average:$output"); +} + +$n->nagios_exit(UNKNOWN, 'unable to parse loadavg'); -- cgit v1.2.3-1-g7c22