+<?php
+/*
+ * NagCollect -- Nagios Data Collector for Passive Checks
+ * Copyright (c)2009 Alexander Barton, alex@barton.de
+ */
+
+define(KEY, 'qw90nm');
+
+function nagiosSubmitHost($host, $status, $text = null)
+{
+ $params = escapeshellarg($host);
+ $params .= ' ' . escapeshellarg($status);
+ if ($text)
+ $params .= ' ' . escapeshellarg($text);
+ $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]);
+ return ($result == 0);
+}
+
+function nagiosSubmitService($host, $service, $status, $text = null)
+{
+ $params = escapeshellarg($host);
+ $params .= ' ' . escapeshellarg($service);
+ $params .= ' ' . escapeshellarg($status);
+ if ($text)
+ $params .= ' ' . escapeshellarg($text);
+ $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]);
+ return ($result == 0);
+}
+
+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'];
+
+ $service = isset($args['service']) ? $args['service'] : null;
+ $text = isset($args['text']) ? $args['text'] : null;
+
+ if (!$service) {
+ // Host Update
+ if (!nagiosSubmitHost($host, $status, $text))
+ return 500;
+ } else {
+ // Service Update
+ if (!nagiosSubmitHost($host, 0, 'Received passive service check'))
+ return 500;
+ if (!nagiosSubmitService($host, $service, $status, $text))
+ return 500;
+ }
+ return 200;
+}
+
+function getHttpStatusText($status)
+{
+ switch ($status) {
+ case 200: return "OK";
+ case 400: return "Bad Request";
+ case 401: return "Unauthorized";
+ case 500: return "Internal Server Error";
+ default: return "Unknown";
+ }
+}
+
+$httpStatus = processRequest($_POST);
+$httpStatusText = getHttpStatusText($httpStatus);
+
+$statusText = $httpStatus . ' - ' . $httpStatusText;
+
+header("HTTP/1.0 $httpStatus $ttpStatusText");
+
+?><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
+ "http://www.w3.org/TR/html4/strict.dtd">
+<html lang="en">
+<head>
+ <meta http-equiv="Content-type" content="text/html; charset=utf-8">
+ <title>NagCollect: <?php echo $statusText ?></title>
+</head>
+<body id="nagcollect">
+<h1><?php echo $statusText ?></h1>
+</body>
+</html>