#!/usr/bin/perl use strict; use lib '/opt/nagios'; use assets; my $db = new assets; $db->init(); my %service_data = (); my %host_data = (); my $file="/var/cache/nagios3/status.dat"; my $total = 0; my $up = 0; my ($host,$state,$i); my @hosts; #get list of relevant hosts my $filter = $ARGV[0]; my $searchstring="disposed='0' and " . $filter; my $hosts = $db->get_assets($searchstring); #generalised read in of nagios status data as may be useful again #but for this script could equally do something simpler open (FL, $file) or die "Could not open file $file - !"; my $block=""; my $bdata; while () { if ( !$block && /\s*(\w+)\s+{/ ) { $block=$1; $bdata={}; } elsif ( $block && /\s*}/) { if ($block eq "hoststatus" && defined($bdata->{'host_name'})) { $host_data{$bdata->{'host_name'}}=$bdata; } if ($block eq "servicestatus" && defined($bdata->{'host_name'}) && defined($bdata->{'service_description'})) { $service_data{$bdata->{'host_name'}.'_____'.$bdata->{'service_description'}}=$bdata; } $block=""; } elsif ( $block && /\s*(\w+)=(.*)/ ) { $bdata->{$1}=$2; } } close(FL); foreach $i (@$hosts) { $host = $i->{'hostname'}; $state=$host_data{$host}{current_state}; if (defined $state) { $total++; $up++ if $state==0; } } if ( $total > 0 ) { print "OK: $up hosts up out of a total of $total | total=$total up=$up\n"; exit 0; } else { print "CRITICAL: no hosts match search | total=$total up=$up\n"; exit 2; }