]> arthur.barton.de Git - nagcollect.git/commitdiff
nagcollect.php: actually use nagcollect.keys file
authorAlexander Barton <alex@barton.de>
Sun, 13 Dec 2009 17:42:29 +0000 (18:42 +0100)
committerAlexander Barton <alex@barton.de>
Sun, 13 Dec 2009 17:42:29 +0000 (18:42 +0100)
server/web/nagcollect.php

index 308b0c9282ba473d9e88e30a4a4d00e2450c52bb..29da2cd0dac179d230cb702d091e4989d4a1ff65 100644 (file)
@@ -4,8 +4,6 @@
  * Copyright (c)2009 Alexander Barton, alex@barton.de
  */
 
-define(KEY, 'qw90nm');
-
 function nagiosSubmitHost($host, $status, $text = null)
 {
        $params  = escapeshellarg($host);
@@ -15,7 +13,7 @@ function nagiosSubmitHost($host, $status, $text = null)
        $cmd = 'sudo -u nagios /usr/local/sbin/nagios-submit-host -c ' . $params;
        exec($cmd, $output, $result);
        if ($result != 0)
-               error_log("submit-host=$result: " . $output[0]);
+               error_log("NagCollect: nagios-submit-host=$result: " . $output[0]);
        return ($result == 0);
 }
 
@@ -29,23 +27,47 @@ function nagiosSubmitService($host, $service, $status, $text = null)
        $cmd = 'sudo -u nagios /usr/local/sbin/nagios-submit-service -c ' . $params;
        exec($cmd, $output, $result);
        if ($result != 0)
-               error_log("submit-service=$result: " . $output[0]);
+               error_log("NagCollect: nagios-submit-service=$result: " . $output[0]);
        return ($result == 0);
 }
 
+function checkKey($key, $host)
+{
+       $fh = @fopen('/etc/nagios3/nagcollect.keys', 'r');
+       if (!$fh) {
+               error_log("NagCollect: can't open \"/etc/nagios3/nagcollect.keys\"!");
+               return false;
+       }
+       while ($str = fgets($fh, 1024)) {
+               $str = trim($str);
+               if (!$str || $str[0] == '#' || $str[0] == '/')
+                       continue;
+               $p = strpos($str, ':');
+               if ($p)
+                       $str = trim(substr($str, 0, $p));
+               if ($str == $key) {
+                       fclose($fh);
+                       return true;
+               }
+       }
+       fclose($fh);
+       error_log("NagCollect: received invalid key \"$key\"!");
+       return false;
+}
+
 function processRequest($args)
 {
        // Check that this is a valid (POST-) request
        if (!isset($args['host']) || !isset($args['status']))
                return 400;
 
-       // Make sure the authorizsation key is correct
-       if (!isset($args['key']) || $args['key'] != KEY)
-               return 401;
-
        $host = $args['host'];
        $status = $args['status'];
 
+       // Make sure the authorizsation key is correct
+       if (!isset($args['key']) || !checkKey($args['key'], $host))
+               return 401;
+
        $service = isset($args['service']) ? $args['service'] : null;
        $text = isset($args['text']) ? $args['text'] : null;