Create separate notifications for different objects in service_check().
[gnucomo.git] / src / gcm_daemon / gcm_daemon.php
index 958895a..bd65a72 100755 (executable)
    * DATES *
    First        : November 8th 2002
    Gnucomo-0.0.3: December 6th 2002
+   Gnucomo-0.0.8: September 4th 2003
 
  $Log: gcm_daemon.php,v $
- Revision 1.14  2003-09-01 06:51:07  arjen
+ Revision 1.17  2003-10-29 09:58:29  arjen
+ Create separate notifications for different objects in service_check().
+
+ Revision 1.16  2003/09/03 12:48:48  arjen
+ Check the log table against the servies running on an object and
+ create notifications if a service is not supposed to be available
+ or is not known at all.
+
+ Revision 1.15  2003/09/02 12:48:09  arjen
+ BUGFIX: Secondary indices on log_notification were unique.
+ Additional information in the 'usr' table: 'display_name' and 'email'.
+ Added new issues and services.
+
+ Revision 1.14  2003/09/01 06:51:07  arjen
  Accept command argument '-c config' to use an alternate
  gnucomo configuration.
 
@@ -45,7 +59,7 @@
 
 */
 
-// $Id: gcm_daemon.php,v 1.14 2003-09-01 06:51:07 arjen Exp $
+// $Id: gcm_daemon.php,v 1.17 2003-10-29 09:58:29 arjen Exp $
 
 ini_set('include_path', '.:./classes:../phpclasses');
 ini_set('html_errors', 'false');
@@ -62,7 +76,7 @@ require_once "gnucomo.process_log.php";
 $project_name   = "gnucomo";    // name of the entire project
 $app_name       = "gcm_daemon"; // name of the application running
 $developrelease = "FALSE";      // Indicates if special debug settings are needed
-$db_version     = 42;           // The db_version indicates what the level of
+$db_version     = 43;           // The db_version indicates what the level of
                                 // the database should be. If the database is
                                 // old an update will be generated.
 $gcmd_version   = 5;            // This value indicates the active version of
@@ -183,6 +197,12 @@ if ($dbms->fetch_row() == "TRUE")
 do
 {
 
+   //At this place we start processing new log-lines
+
+   process_log ();
+   service_check();
+   find_notifications();
+
    //  Gather the statistics for each object
 
    $obj_result = $dbms->query("SELECT objectid FROM object");
@@ -193,12 +213,7 @@ do
       GatherStatistics($object->objectid);
    }
 
-  //At this place we start processing new log-lines
-
-  process_log ();
-  find_notifications();
-
-  $keep_running = false;
+   $keep_running = false;
 
 } while ($keep_running == true);
 
@@ -221,6 +236,8 @@ function process_log ()
   global $dbms_working;
   global $class_settings;
 
+  $last_log = 0;
+
   // Find records in log that still have to be processed.
 
   $local_sql = "SELECT setting_value FROM db_value WHERE setting = 'log_processing'";
@@ -430,8 +447,103 @@ function GatherStatistics($objectid)
    UpdateStatistic($objectid, 'logs', $cnt->count);
 }
 
+/*
+ *   Service_check - Check the log entries if there are any unknown
+ *   services.
+ */
+
+function service_check()
+{
+   global  $dbms;
+
+   $unknown_notification = array();
+   $unused_notification  = array();
+   $last_log             = 0;
+
+   //  How far did we get last time ?
+
+   $lastlogres = $dbms->query("SELECT setting_value FROM db_value
+                               WHERE setting = 'log_servicecheck'");
+
+   if ($dbms->num_rows($lastlogres) == 1)
+   {
+     $last_log = $dbms->Field($lastlogres, 0, 'setting_value');
+   }
+   else
+   {
+      $dbms->query("INSERT INTO db_value (setting, setting_value)
+                            VALUES ('log_servicecheck', '0')");
+   }
+
+   // Query the log-table
+
+   $qry = "SELECT logid, objectid, servicecode FROM log
+           WHERE logid > CAST(".$last_log." AS BIGINT) ORDER BY logid";
+   $log_res = $dbms->query($qry);
+   //$log_res = $dbms->query("SELECT logid, objectid, servicecode,rawdata FROM log");
+
+   for ($log_row = 0; $log_row < $dbms->num_rows($log_res); $log_row++)
+   {
+      $log_entry = $dbms->fetch_object($log_res, $log_row);
+      $last_log  = $log_entry->logid;
 
-function find_notifications () {
+      //   Check if the service is used on the object.
+
+      $qry = "SELECT * FROM object_service WHERE objectid='";
+      $qry .= $log_entry->objectid . "' AND servicecode='";
+      $qry .= $log_entry->servicecode . "'";
+
+      $os_res = $dbms->query($qry);
+      if ($dbms->num_rows($os_res) == 0)
+      {
+         //   Service is not found for the object, check if the service
+         //   exists at all.
+
+         $qry = "SELECT * FROM service WHERE servicecode='";
+         $qry .= $log_entry->servicecode . "'";
+
+         if ($dbms->num_rows($dbms->query($qry)) == 0)
+         {
+            if (!isset($unknown_notification[$log_entry->objectid]))
+            {
+               $remark = "One or more log entries from a service that is not in the database";
+               $unknown_notification[$log_entry->objectid] =
+                         $dbms->new_notification($log_entry->objectid, 'service unknown', $remark);
+            }
+            if (isset($unknown_notification[$log_entry->objectid]))
+            {
+               $insertion = "INSERT INTO log_notification (notificationid, logid) VALUES ('";
+               $insertion .= $unknown_notification[$log_entry->objectid] . "', '";
+               $insertion .= $log_entry->logid . "')";
+               $dbms->query($insertion);
+            }
+         }
+         else
+         {
+            if (!isset($unused_notification[$log_entry->objectid]))
+            {
+               $remark = "One or more log entries from a service not running on this object";
+               $unused_notification[$log_entry->objectid] =
+                         $dbms->new_notification($log_entry->objectid, 'service not used', $remark);
+            }
+            if (isset($unused_notification[$log_entry->objectid]))
+            {
+               $insertion = "INSERT INTO log_notification (notificationid, logid) VALUES ('";
+               $insertion .= $unused_notification[$log_entry->objectid] . "', '";
+               $insertion .= $log_entry->logid . "')";
+               $dbms->query($insertion);
+            }
+         }
+      }
+   }
+
+   $qry = "UPDATE db_value SET setting_value = '"
+                   . $last_log . "' WHERE setting = 'log_servicecheck'";
+   $dbms->query($qry);
+}
+
+function find_notifications ()
+{
 
 /*
  *  Do something with notification checks.