3 * NagCollect -- Nagios Data Collector for Passive Checks
4 * Copyright (c)2009-2010 Alexander Barton, alex@barton.de
7 function nagiosSubmitHost($host, $status, $text = null)
9 $params = escapeshellarg($host);
10 $params .= ' ' . escapeshellarg($status);
12 $params .= ' ' . escapeshellarg($text);
13 $cmd = 'sudo -u nagios /usr/local/sbin/nagios-submit-host -c ' . $params;
14 exec($cmd, $output, $result);
16 error_log("NagCollect: nagios-submit-host=$result: " . $output[0]);
17 return ($result == 0);
20 function nagiosSubmitService($host, $service, $status, $text = null)
22 $params = escapeshellarg($host);
23 $params .= ' ' . escapeshellarg($service);
24 $params .= ' ' . escapeshellarg($status);
26 $params .= ' ' . escapeshellarg($text);
27 $cmd = 'sudo -u nagios /usr/local/sbin/nagios-submit-service -c ' . $params;
28 exec($cmd, $output, $result);
30 error_log("NagCollect: nagios-submit-service=$result: " . $output[0]);
31 return ($result == 0);
34 function checkKey($key, $host)
36 $fh = @fopen('/etc/nagios3/nagcollect.keys', 'r');
38 error_log("NagCollect: can't open \"/etc/nagios3/nagcollect.keys\"!");
41 while ($str = fgets($fh, 1024)) {
43 if (!$str || $str[0] == '#' || $str[0] == '/')
45 $p = strpos($str, ':');
47 $str = trim(substr($str, 0, $p));
57 function processRequest($args)
59 // Check that this is a valid (POST-) request
60 if (!isset($args['host']) || !isset($args['status']))
63 $host = $args['host'];
64 $status = $args['status'];
66 // Make sure the authorizsation key is correct
67 if (!isset($args['key']) || !checkKey($args['key'], $host))
70 $service = isset($args['service']) ? $args['service'] : null;
71 $text = isset($args['text']) ? $args['text'] : null;
75 if (!nagiosSubmitHost($host, $status, $text))
79 $hostStatus = 'Received passive service check from '
80 . '"' . $_SERVER['REMOTE_ADDR'] . '"';
81 if (!nagiosSubmitHost($host, 0, $hostStatus))
83 if (!nagiosSubmitService($host, $service, $status, $text))
89 function getHttpStatusText($status)
92 case 200: return "OK";
93 case 400: return "Bad Request";
94 case 401: return "Unauthorized";
95 case 500: return "Internal Server Error";
96 default: return "Unknown";
100 openlog("NagCollect", LOG_ODELAY, LOG_DAEMON);
102 $httpStatus = processRequest($_POST);
103 $httpStatusText = getHttpStatusText($httpStatus);
105 $statusText = $httpStatus . ' - ' . $httpStatusText;
107 if ($httpStatus != 200) {
108 syslog(LOG_WARNING, "Warning: $httpStatusText ($httpStatus) from "
109 . "\"{$_SERVER['REMOTE_ADDR']}\" ({$_SERVER['HTTP_USER_AGENT']})");
114 header("HTTP/1.0 $httpStatus $httpStatusText");
116 ?><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
117 "http://www.w3.org/TR/html4/strict.dtd">
120 <meta http-equiv="Content-type" content="text/html; charset=utf-8">
121 <title>NagCollect: <?php echo $statusText ?></title>
123 <body id="nagcollect">
124 <h1><?php echo $statusText ?></h1>