#! /usr/bin/perl
# anders@fupp.net, 2009-11-11
# check external nrpe service, return passive result
# Nagios host_name and host address must match

use Getopt::Std;
getopts('H:c:o:n:s:');

if ($opt_o) {
	$outfile=$opt_o;
} else {
	$outfile = "/var/spool/nagios/rw/nagios.cmd";
}
if ($opt_n) {
	$nrpe=$opt_n;
} else {
	$nrpe="/usr/local/libexec/nagios/check_nrpe2";
}

sub usage {
	print "check_passive_external -H <host> -c <check command> -s <service> [-o cmdfile]\n[-n <nrpe command>]\n";
	exit(1);
}

usage unless ($opt_H && $opt_c && $opt_s);
$txt=`$nrpe -H $opt_H -c $opt_c`;
$ret=$?;
chomp($txt);

# Multiples of 256 it seems
if ($ret >= 256) {
	$ret = $ret/256;
}

#print "txt=$txt\n";
#print "ret=$ret\n";

$time = time();
open OUTFILE, ">> $outfile" or die "Fatal: Cannot create output file $outputfile.\n";
print OUTFILE "[$time] PROCESS_SERVICE_CHECK_RESULT;$opt_H;$opt_s;$ret;$txt\n";
#print "[$time] PROCESS_SERVICE_CHECK_RESULT;$opt_H;$opt_s;$ret;$txt\n";
close OUTFILE;
