#!/usr/bin/perl -T use strict; use warnings; use VServer; use Nagios::Plugin; our $VERSION = '1.6'; sub init_nagios_plugin() { my $plugin = Nagios::Plugin->new( 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." ); $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]]", ); $plugin->add_arg( spec => 'd|domain=s', help => 'Domainname to append in bulk mode or to stip ' . 'in single host mode.', label => "DOMAIN", ); 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 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)/) { $ENV{'NAGIOS_PLUGIN'} = $1; } 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 (defined $crit{$load} && defined $warn{$load} && $crit{$load} < $warn{$load}) { die("Parameter inconsistency: warning load is greater " . "than critical load for '$load'"); } } 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"); } handle_vserver($context, \%warn, \%crit, $vserver); $n->nagios_exit(UNKNOWN, 'unable to parse loadavg');