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 if (!nagiosSubmitHost($host, 0, 'Received passive service check'))
81 if (!nagiosSubmitService($host, $service, $status, $text))
87 function getHttpStatusText($status)
90 case 200: return "OK";
91 case 400: return "Bad Request";
92 case 401: return "Unauthorized";
93 case 500: return "Internal Server Error";
94 default: return "Unknown";
98 openlog("NagCollect", LOG_ODELAY, LOG_DAEMON);
100 $httpStatus = processRequest($_POST);
101 $httpStatusText = getHttpStatusText($httpStatus);
103 $statusText = $httpStatus . ' - ' . $httpStatusText;
105 if ($httpStatus != 200) {
106 syslog(LOG_WARNING, "Warning: $httpStatusText ($httpStatus) from "
107 . "\"{$_SERVER['REMOTE_ADDR']}\" ({$_SERVER['HTTP_USER_AGENT']})");
112 header("HTTP/1.0 $httpStatus $httpStatusText");
114 ?><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
115 "http://www.w3.org/TR/html4/strict.dtd">
118 <meta http-equiv="Content-type" content="text/html; charset=utf-8">
119 <title>NagCollect: <?php echo $statusText ?></title>
121 <body id="nagcollect">
122 <h1><?php echo $statusText ?></h1>