]> arthur.barton.de Git - nagcollect.git/blob - server/web/nagcollect.php
nagcollect.php: actually use nagcollect.keys file
[nagcollect.git] / server / web / nagcollect.php
1 <?php
2 /*
3  * NagCollect -- Nagios Data Collector for Passive Checks
4  * Copyright (c)2009 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         error_log("NagCollect: received invalid key \"$key\"!");
55         return false;
56 }
57
58 function processRequest($args)
59 {
60         // Check that this is a valid (POST-) request
61         if (!isset($args['host']) || !isset($args['status']))
62                 return 400;
63
64         $host = $args['host'];
65         $status = $args['status'];
66
67         // Make sure the authorizsation key is correct
68         if (!isset($args['key']) || !checkKey($args['key'], $host))
69                 return 401;
70
71         $service = isset($args['service']) ? $args['service'] : null;
72         $text = isset($args['text']) ? $args['text'] : null;
73
74         if (!$service) {
75                 // Host Update
76                 if (!nagiosSubmitHost($host, $status, $text))
77                         return 500;
78         } else {
79                 // Service Update
80                 if (!nagiosSubmitHost($host, 0, 'Received passive service check'))
81                         return 500;
82                 if (!nagiosSubmitService($host, $service, $status, $text))
83                         return 500;
84         }
85         return 200;
86 }
87
88 function getHttpStatusText($status)
89 {
90         switch ($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";
96         }
97 }
98
99 $httpStatus = processRequest($_POST);
100 $httpStatusText = getHttpStatusText($httpStatus);
101
102 $statusText = $httpStatus . ' - ' . $httpStatusText;
103
104 header("HTTP/1.0 $httpStatus $ttpStatusText");
105
106 ?><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
107         "http://www.w3.org/TR/html4/strict.dtd">
108 <html lang="en">
109 <head>
110         <meta http-equiv="Content-type" content="text/html; charset=utf-8">
111         <title>NagCollect: <?php echo $statusText ?></title>
112 </head>
113 <body id="nagcollect">
114 <h1><?php echo $statusText ?></h1>
115 </body>
116 </html>