3 * NagCollect -- Nagios Data Collector for Passive Checks
4 * Copyright (c)2009 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));
54 error_log("NagCollect: received invalid key \"$key\"!");
58 function processRequest($args)
60 // Check that this is a valid (POST-) request
61 if (!isset($args['host']) || !isset($args['status']))
64 $host = $args['host'];
65 $status = $args['status'];
67 // Make sure the authorizsation key is correct
68 if (!isset($args['key']) || !checkKey($args['key'], $host))
71 $service = isset($args['service']) ? $args['service'] : null;
72 $text = isset($args['text']) ? $args['text'] : null;
76 if (!nagiosSubmitHost($host, $status, $text))
80 if (!nagiosSubmitHost($host, 0, 'Received passive service check'))
82 if (!nagiosSubmitService($host, $service, $status, $text))
88 function getHttpStatusText($status)
91 case 200: return "OK";
92 case 400: return "Bad Request";
93 case 401: return "Unauthorized";
94 case 500: return "Internal Server Error";
95 default: return "Unknown";
99 $httpStatus = processRequest($_POST);
100 $httpStatusText = getHttpStatusText($httpStatus);
102 $statusText = $httpStatus . ' - ' . $httpStatusText;
104 header("HTTP/1.0 $httpStatus $ttpStatusText");
106 ?><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
107 "http://www.w3.org/TR/html4/strict.dtd">
110 <meta http-equiv="Content-type" content="text/html; charset=utf-8">
111 <title>NagCollect: <?php echo $statusText ?></title>
113 <body id="nagcollect">
114 <h1><?php echo $statusText ?></h1>