3 * NagCollect -- Nagios Data Collector for Passive Checks
4 * Copyright (c)2009 Alexander Barton, alex@barton.de
9 function nagiosSubmitHost($host, $status, $text = null)
11 $params = escapeshellarg($host);
12 $params .= ' ' . escapeshellarg($status);
14 $params .= ' ' . escapeshellarg($text);
15 $cmd = 'sudo -u nagios /usr/local/sbin/nagios-submit-host -c ' . $params;
16 exec($cmd, $output, $result);
18 error_log("submit-host=$result: " . $output[0]);
19 return ($result == 0);
22 function nagiosSubmitService($host, $service, $status, $text = null)
24 $params = escapeshellarg($host);
25 $params .= ' ' . escapeshellarg($service);
26 $params .= ' ' . escapeshellarg($status);
28 $params .= ' ' . escapeshellarg($text);
29 $cmd = 'sudo -u nagios /usr/local/sbin/nagios-submit-service -c ' . $params;
30 exec($cmd, $output, $result);
32 error_log("submit-service=$result: " . $output[0]);
33 return ($result == 0);
36 function processRequest($args)
38 // Check that this is a valid (POST-) request
39 if (!isset($args['host']) || !isset($args['status']))
42 // Make sure the authorizsation key is correct
43 if (!isset($args['key']) || $args['key'] != KEY)
46 $host = $args['host'];
47 $status = $args['status'];
49 $service = isset($args['service']) ? $args['service'] : null;
50 $text = isset($args['text']) ? $args['text'] : null;
54 if (!nagiosSubmitHost($host, $status, $text))
58 if (!nagiosSubmitHost($host, 0, 'Received passive service check'))
60 if (!nagiosSubmitService($host, $service, $status, $text))
66 function getHttpStatusText($status)
69 case 200: return "OK";
70 case 400: return "Bad Request";
71 case 401: return "Unauthorized";
72 case 500: return "Internal Server Error";
73 default: return "Unknown";
77 $httpStatus = processRequest($_POST);
78 $httpStatusText = getHttpStatusText($httpStatus);
80 $statusText = $httpStatus . ' - ' . $httpStatusText;
82 header("HTTP/1.0 $httpStatus $ttpStatusText");
84 ?><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
85 "http://www.w3.org/TR/html4/strict.dtd">
88 <meta http-equiv="Content-type" content="text/html; charset=utf-8">
89 <title>NagCollect: <?php echo $statusText ?></title>
91 <body id="nagcollect">
92 <h1><?php echo $statusText ?></h1>