#!/usr/bin/perl # # By Jorge Schrauwen 2011 # http://blackdot.be # # imports use strict; use File::Basename; use Nagios::Plugin; use Nagios::Status::Host; use Nagios::Status::HostStatus; use Nagios::Status::Service; use Nagios::Status::ServiceStatus; # variables my $np; my $ok = 0; my $warning = 0; my $warning_text = ""; my $critical = 0; my $critical_text = ""; my $unknown = 0; my $unknow_text =""; # plugin setup $np = Nagios::Plugin->new( plugin => 'check_host_services', shortname => 'SERVICES', version => '1.0', usage => 'Usage: %s -H [-s ]', blurb => 'This plugin checks the services for a host and returns with the worst status.', license => 'This nagios plugin is free software, and comes with ABSOLUTELY no WARRANTY!' ); $np->add_arg( spec => 'hostname|H=s', help => 'hostname of which the services should be checked', required => 1 ); $np->add_arg( spec => 'statusfile|s=s', help => 'location of nagios status file', default => '/usr/local/nagios/var/status.dat', required => 0 ); $np->add_arg( spec => 'mode|m=s', help => 'mode=any or all - any will return OK if there is alteast 1 OK, all will return OK if all services are OK', default => 'any', required => 0 ); ## main $np->getopts; # check status file if ( ! -e $np->opts->statusfile ) { $np->nagios_die(sprintf("You're status file is not located at %s!", $np->opts->statusfile)); } # check host exists my $hosts = Nagios::Status::HostStatus->new($np->opts->statusfile); my $host_exist = 0; if (defined($hosts->check_up)) { for my $h (@{$hosts->check_up}) { if (lc($h->get_hostname) eq lc($np->opts->hostname)) { $host_exist = 1; } } } if (defined($hosts->check_down)) { for my $h (@{$hosts->check_down}) { if (lc($h->get_hostname) eq lc($np->opts->hostname)) { $host_exist = 1; } } } if (defined($hosts->check_unreachable)) { for my $h (@{$hosts->check_unreachable}) { if (lc($h->get_hostname) eq lc($np->opts->hostname)) { $host_exist = 1; } } } if ($host_exist <= 0) { $np->nagios_exit(UNKNOWN, sprintf("I can't find \"%s\"!", $np->opts->hostname)); } # count service states for host my $services = Nagios::Status::ServiceStatus->new($np->opts->statusfile); if (defined($services->check_ok)) { for my $srv (@{$services->check_ok}) { if ($srv->get_hostname eq $np->opts->hostname) { $ok++; } } } if (defined($services->check_warning)) { for my $srv (@{$services->check_warning}) { if ($srv->get_hostname eq $np->opts->hostname) { $warning++; } } } if (defined($services->check_critical)) { for my $srv (@{$services->check_critical}) { if ($srv->get_hostname eq $np->opts->hostname) { $critical++; } } } if (defined($services->check_unknown)) { for my $srv (@{$services->check_unknown}) { if ($srv->get_hostname eq $np->opts->hostname) { $unknown++; } } } # performance data $np->add_perfdata( label => "ok", value => $ok, ); $np->add_perfdata( label => "warning", value => $warning, ); $np->add_perfdata( label => "critical", value => $critical, ); $np->add_perfdata( label => "unknown", value => $unknown, ); # calculate result if ( (lc($np->opts->mode) eq "any") && ($ok > 0) ) { $np->nagios_exit(OK, sprintf("There is atleast service in OK state for \"%s\"!", $np->opts->hostname)); } if ($critical > 0) { $np->nagios_exit(CRITICAL, sprintf("There is atleast one service in CRITICAL state for \"%s\"!", $np->opts->hostname)); } if ($warning > 0) { $np->nagios_exit(WARNING, sprintf("There is atleast one service in WARNING state for \"%s\"!", $np->opts->hostname)); } if ($unknown > 0) { $np->nagios_exit(UNKNOWN, sprintf("There is atleast one service in UNKNOWN state for \"%s\"!", $np->opts->hostname)); } if (lc($np->opts->mode) ne "any") { $np->nagios_exit(OK, sprintf("All services are OK for \"%s\"!", $np->opts->hostname)); } else { $np->nagios_exit(UNKNOWN, sprintf("Oops? Not sure how \"%s\" is doing!", $np->opts->hostname)); }