<?php
/*
* NagCollect -- Nagios Data Collector for Passive Checks
- * Copyright (c)2009 Alexander Barton, alex@barton.de
+ * Copyright (c)2009-2010 Alexander Barton, alex@barton.de
*/
-define(KEY, 'qw90nm');
-
function nagiosSubmitHost($host, $status, $text = null)
{
$params = escapeshellarg($host);
$cmd = 'sudo -u nagios /usr/local/sbin/nagios-submit-host -c ' . $params;
exec($cmd, $output, $result);
if ($result != 0)
- error_log("submit-host=$result: " . $output[0]);
+ error_log("NagCollect: nagios-submit-host=$result: " . $output[0]);
return ($result == 0);
}
$cmd = 'sudo -u nagios /usr/local/sbin/nagios-submit-service -c ' . $params;
exec($cmd, $output, $result);
if ($result != 0)
- error_log("submit-service=$result: " . $output[0]);
+ error_log("NagCollect: nagios-submit-service=$result: " . $output[0]);
return ($result == 0);
}
+function checkKey($key, $host)
+{
+ $fh = @fopen('/etc/nagios3/nagcollect.keys', 'r');
+ if (!$fh) {
+ error_log("NagCollect: can't open \"/etc/nagios3/nagcollect.keys\"!");
+ return false;
+ }
+ while ($str = fgets($fh, 1024)) {
+ $str = trim($str);
+ if (!$str || $str[0] == '#' || $str[0] == '/')
+ continue;
+ $p = strpos($str, ':');
+ if ($p)
+ $str = trim(substr($str, 0, $p));
+ if ($str == $key) {
+ fclose($fh);
+ return true;
+ }
+ }
+ fclose($fh);
+ return false;
+}
+
function processRequest($args)
{
// Check that this is a valid (POST-) request
if (!isset($args['host']) || !isset($args['status']))
return 400;
- // Make sure the authorizsation key is correct
- if (!isset($args['key']) || $args['key'] != KEY)
- return 401;
-
$host = $args['host'];
$status = $args['status'];
+ // Make sure the authorizsation key is correct
+ if (!isset($args['key']) || !checkKey($args['key'], $host))
+ return 401;
+
$service = isset($args['service']) ? $args['service'] : null;
$text = isset($args['text']) ? $args['text'] : null;
return 500;
} else {
// Service Update
- if (!nagiosSubmitHost($host, 0, 'Received passive service check'))
+ $hostStatus = 'Received passive service check from '
+ . '"' . $_SERVER['REMOTE_ADDR'] . '"';
+ if (!nagiosSubmitHost($host, 0, $hostStatus))
return 500;
if (!nagiosSubmitService($host, $service, $status, $text))
return 500;
}
}
+openlog("NagCollect", LOG_ODELAY, LOG_DAEMON);
+
$httpStatus = processRequest($_POST);
$httpStatusText = getHttpStatusText($httpStatus);
$statusText = $httpStatus . ' - ' . $httpStatusText;
-header("HTTP/1.0 $httpStatus $ttpStatusText");
+if ($httpStatus != 200) {
+ syslog(LOG_WARNING, "Warning: $httpStatusText ($httpStatus) from "
+ . "\"{$_SERVER['REMOTE_ADDR']}\" ({$_SERVER['HTTP_USER_AGENT']})");
+}
+
+closelog();
+
+header("HTTP/1.0 $httpStatus $httpStatusText");
?><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">