]> arthur.barton.de Git - nagcollect.git/blob - server/web/nagcollect.php
Initial commit, first functionality implemented :-)
[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 define(KEY, 'qw90nm');
8
9 function nagiosSubmitHost($host, $status, $text = null)
10 {
11         $params  = escapeshellarg($host);
12         $params .= ' ' . escapeshellarg($status);
13         if ($text)
14                 $params .= ' ' . escapeshellarg($text);
15         $cmd = 'sudo -u nagios /usr/local/sbin/nagios-submit-host -c ' . $params;
16         exec($cmd, $output, $result);
17         if ($result != 0)
18                 error_log("submit-host=$result: " . $output[0]);
19         return ($result == 0);
20 }
21
22 function nagiosSubmitService($host, $service, $status, $text = null)
23 {
24         $params  = escapeshellarg($host);
25         $params .= ' ' . escapeshellarg($service);
26         $params .= ' ' . escapeshellarg($status);
27         if ($text)
28                 $params .= ' ' . escapeshellarg($text);
29         $cmd = 'sudo -u nagios /usr/local/sbin/nagios-submit-service -c ' . $params;
30         exec($cmd, $output, $result);
31         if ($result != 0)
32                 error_log("submit-service=$result: " . $output[0]);
33         return ($result == 0);
34 }
35
36 function processRequest($args)
37 {
38         // Check that this is a valid (POST-) request
39         if (!isset($args['host']) || !isset($args['status']))
40                 return 400;
41
42         // Make sure the authorizsation key is correct
43         if (!isset($args['key']) || $args['key'] != KEY)
44                 return 401;
45
46         $host = $args['host'];
47         $status = $args['status'];
48
49         $service = isset($args['service']) ? $args['service'] : null;
50         $text = isset($args['text']) ? $args['text'] : null;
51
52         if (!$service) {
53                 // Host Update
54                 if (!nagiosSubmitHost($host, $status, $text))
55                         return 500;
56         } else {
57                 // Service Update
58                 if (!nagiosSubmitHost($host, 0, 'Received passive service check'))
59                         return 500;
60                 if (!nagiosSubmitService($host, $service, $status, $text))
61                         return 500;
62         }
63         return 200;
64 }
65
66 function getHttpStatusText($status)
67 {
68         switch ($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";
74         }
75 }
76
77 $httpStatus = processRequest($_POST);
78 $httpStatusText = getHttpStatusText($httpStatus);
79
80 $statusText = $httpStatus . ' - ' . $httpStatusText;
81
82 header("HTTP/1.0 $httpStatus $ttpStatusText");
83
84 ?><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
85         "http://www.w3.org/TR/html4/strict.dtd">
86 <html lang="en">
87 <head>
88         <meta http-equiv="Content-type" content="text/html; charset=utf-8">
89         <title>NagCollect: <?php echo $statusText ?></title>
90 </head>
91 <body id="nagcollect">
92 <h1><?php echo $statusText ?></h1>
93 </body>
94 </html>