]> arthur.barton.de Git - nagcollect.git/blob - server/web/nagcollect.php
TeamViewer.tst: New test to check TeamViewer Host on OS X
[nagcollect.git] / server / web / nagcollect.php
1 <?php
2 /*
3  * NagCollect -- Nagios Data Collector for Passive Checks
4  * Copyright (c)2009-2010 Alexander Barton, alex@barton.de
5  */
6
7 function nagiosSubmitHost($host, $status, $text = null)
8 {
9         $params  = escapeshellarg($host);
10         $params .= ' ' . escapeshellarg($status);
11         if ($text)
12                 $params .= ' ' . escapeshellarg($text);
13         $cmd = 'sudo -u nagios /usr/local/sbin/nagios-submit-host -c ' . $params;
14         exec($cmd, $output, $result);
15         if ($result != 0)
16                 error_log("NagCollect: nagios-submit-host=$result: " . $output[0]);
17         return ($result == 0);
18 }
19
20 function nagiosSubmitService($host, $service, $status, $text = null)
21 {
22         $params  = escapeshellarg($host);
23         $params .= ' ' . escapeshellarg($service);
24         $params .= ' ' . escapeshellarg($status);
25         if ($text)
26                 $params .= ' ' . escapeshellarg($text);
27         $cmd = 'sudo -u nagios /usr/local/sbin/nagios-submit-service -c ' . $params;
28         exec($cmd, $output, $result);
29         if ($result != 0)
30                 error_log("NagCollect: nagios-submit-service=$result: " . $output[0]);
31         return ($result == 0);
32 }
33
34 function checkKey($key, $host)
35 {
36         $fh = @fopen('/etc/nagios3/nagcollect.keys', 'r');
37         if (!$fh) {
38                 error_log("NagCollect: can't open \"/etc/nagios3/nagcollect.keys\"!");
39                 return false;
40         }
41         while ($str = fgets($fh, 1024)) {
42                 $str = trim($str);
43                 if (!$str || $str[0] == '#' || $str[0] == '/')
44                         continue;
45                 $p = strpos($str, ':');
46                 if ($p)
47                         $str = trim(substr($str, 0, $p));
48                 if ($str == $key) {
49                         fclose($fh);
50                         return true;
51                 }
52         }
53         fclose($fh);
54         return false;
55 }
56
57 function processRequest($args)
58 {
59         // Check that this is a valid (POST-) request
60         if (!isset($args['host']) || !isset($args['status']))
61                 return 400;
62
63         $host = $args['host'];
64         $status = $args['status'];
65
66         // Make sure the authorizsation key is correct
67         if (!isset($args['key']) || !checkKey($args['key'], $host))
68                 return 401;
69
70         $service = isset($args['service']) ? $args['service'] : null;
71         $text = isset($args['text']) ? $args['text'] : null;
72
73         if (!$service) {
74                 // Host Update
75                 if (!nagiosSubmitHost($host, $status, $text))
76                         return 500;
77         } else {
78                 // Service Update
79                 $hostStatus = 'Received passive service check from '
80                                         . '"' . $_SERVER['REMOTE_ADDR'] . '"';
81                 if (!nagiosSubmitHost($host, 0, $hostStatus))
82                         return 500;
83                 if (!nagiosSubmitService($host, $service, $status, $text))
84                         return 500;
85         }
86         return 200;
87 }
88
89 function getHttpStatusText($status)
90 {
91         switch ($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";
97         }
98 }
99
100 openlog("NagCollect", LOG_ODELAY, LOG_DAEMON);
101
102 $httpStatus = processRequest($_POST);
103 $httpStatusText = getHttpStatusText($httpStatus);
104
105 $statusText = $httpStatus . ' - ' . $httpStatusText;
106
107 if ($httpStatus != 200) {
108         syslog(LOG_WARNING, "Warning: $httpStatusText ($httpStatus) from "
109                    . "\"{$_SERVER['REMOTE_ADDR']}\" ({$_SERVER['HTTP_USER_AGENT']})");
110 }
111
112 closelog();
113
114 header("HTTP/1.0 $httpStatus $httpStatusText");
115
116 ?><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
117         "http://www.w3.org/TR/html4/strict.dtd">
118 <html lang="en">
119 <head>
120         <meta http-equiv="Content-type" content="text/html; charset=utf-8">
121         <title>NagCollect: <?php echo $statusText ?></title>
122 </head>
123 <body id="nagcollect">
124 <h1><?php echo $statusText ?></h1>
125 </body>
126 </html>