Added new table to the database: log_adv_daemon_email.
[gnucomo.git] / src / gcm_daemon / gcm_daemon.php
1 #!/usr/bin/php
2 <?PHP
3 /**********************************************************************************
4 **  (c) Copyright 2002, Brenno J.S.A.A.F. de Winter, De Winter Information Solutions
5 ** This is free software; you can redistribute it and/or modify it under the
6 ** terms of the GNU General Public License, see the file COPYING.
7 ***********************************************************************************/
8
9
10 /* 
11    NAME         : gcm_daemon
12    AUTHOR       : Brenno J.S.A.A.F. de Winter
13                   De Winter Information Solutions
14    COPYRIGHT    : 2002 - De Winter Information Solutions, 
15                   Brenno J.S.A.A.F. de Winter
16    
17    * DATES * 
18    First        : November 8th 2002
19    Gnucomo-0.0.3: December 6th 2002
20
21  $Log: gcm_daemon.php,v $
22  Revision 1.9  2003-02-21 08:37:59  arjen
23  Added new table to the database: log_adv_daemon_email.
24
25
26 */
27
28 // $Id: gcm_daemon.php,v 1.9 2003-02-21 08:37:59 arjen Exp $
29
30 ini_set('include_path', '.:./classes:../phpclasses');
31
32 //Tell the log that we're up.
33 define_syslog_variables();
34 openlog("gnucomo", LOG_PID, LOG_DAEMON);
35 syslog(LOG_INFO, "gcm_daemon started");
36
37 require_once "gnucomo_config.php";
38 require_once "db.class.php";
39 require_once "gnucomo.process_log.php";
40
41 // Set the standard variables //
42 $project_name   = "gnucomo";    //name of the entire project
43 $app_name       = "gcm_daemon"; //name of the application running
44 $developrelease = "TRUE";      //Indicates if special debug settings are needed
45 $db_version     = 32;           //The db_version indicates what the level of 
46                                 //the database should be. If the database is 
47                                 //old an update will be generated.
48 $gcmd_version   = 3;            //This value indicates the active version of the gcm_daemon,
49                                 //which is saved in the database. Log records that were not
50                                 //recognized before will now be recognized. The version doesn't
51                                 //mean anything in the overall gnucomo project.
52
53 //Avoid time-limit issues
54 set_time_limit(0);
55
56
57 // Read the database settings //
58 $class_settings = new gnucomo_config();
59 $class_settings->read($project_name);
60 $class_settings->database();
61
62 //Open an connection to the database
63 $dbms_type = $class_settings->find_parameter("database", "type");
64 $dbms_host = $class_settings->find_parameter("database", "host");
65 $dbms_name = $class_settings->find_parameter("database", "name");
66 $dbms_user = $class_settings->find_parameter("gcm_daemon", "user");
67 $dbms_password = $class_settings->find_parameter("gcm_daemon", "password");
68
69 db_select($dbms_type);
70 $dbms = new db();
71 $dbms->db_host = $dbms_host;
72 $dbms->db_name = $dbms_name;
73 $dbms->db_user = $dbms_user;
74 $dbms->db_password = $dbms_password;
75 $dbms->db_connect();
76
77 if ($dbms->have_db_connection() == "FALSE") {
78   exit ("Database connection failed.");
79 } else {
80   //The database connection has been made.
81   $dbms_working = copy_db_class($dbms);
82 }
83
84 //Verify if the database is up-to-date by checking the versionnumber
85 $local_sql = "SELECT setting_value FROM db_value WHERE setting = 'db_version' ";
86 $dbms->query($local_sql);
87
88 if ($dbms->fetch_row() == "TRUE") {
89   $active_version = $dbms->db_result_row[0];
90  
91   //Update the database to the most recent version.
92   if ($active_version < $db_version) { 
93      include ("gnucomo_db_version.php");
94   }
95 } else {
96   syslog (LOG_INFO, "Couldn't initialize database version. Is this a gnucomo database?");
97   die ("Couldn't initialize database version.\n");
98 }
99
100 //If there is a new gcm_daemon_version the logrecords that couldn't be understood can be
101 //reprocessed. For this reason processed is now changed to false again for not recognized
102 //records.
103 $local_sql = "SELECT setting_value FROM db_value WHERE setting = 'gcm_daemon_version'";
104 $dbms->query($local_sql);
105
106 if ($dbms->fetch_row() == "TRUE") {
107    if ($dbms->db_result_row[0] < $gcmd_version) {
108       //Reactive log-records that weren't understood earlier.
109       $local_sql = "UPDATE log SET processed = false WHERE logid NOT IN (SELECT DISTINCT logid FROM log_adv)";
110       $dbms->query($local_sql);
111
112       //Update de gcm_daemon version in the database
113       $local_sql = "UPDATE db_value SET setting_value = '".$gcmd_version;
114       $local_sql .= "' WHERE setting = 'gcm_daemon_version'";
115       $dbms->query($local_sql);
116
117    }
118       
119 }
120
121 //Now we loop the tasks that we have to do.
122
123
124 do {
125
126   //At this place we start processing new log-lines 
127   process_log ();
128   notificationstats();
129
130   $keep_running = 'FALSE';
131
132 } while ($keep_running == 'TRUE');
133
134 //Tell the log that we're ending our efforts in a nice way
135 syslog (LOG_INFO, "gcm_daemon ended nicely");
136
137 function process_log () {
138  
139  /* This function will walk through the log-records that haven't been processed
140   * first a snapshot will be created of a the non-processed records. 
141   * sequentially each record will dealt with. By doing that changes will be made
142   * in several log_adv_xxx tables
143   * INPUT  : NONE
144   * OUTPUT : NONE
145   */
146   global $dbms;
147   global $dbms_working;
148
149   //Find records in log that still have to be processed.
150
151   $local_sql = "SELECT setting_value FROM db_value WHERE setting = 'log_processing'";
152   $dbms->query($local_sql);
153
154   if ($dbms->fetch_row() == "TRUE") {
155      $last_log = $dbms->db_result_row[0];
156   }
157   
158   //Query the log-table
159   $local_sql = "SELECT * FROM log WHERE logid > CAST(".$last_log." AS BIGINT) order by logid";
160   $dbms->query($local_sql);
161
162   //Update the log-statistics in the object-table 
163   $local_statistics_db = copy_db_class($dbms);
164   $local_findobject_db = copy_db_class($dbms);
165
166   //Make totals 
167   $local_upper_row = $dbms->num_rows() + $last_log + 1;
168   $local_sql = "SELECT COUNT(logid), objectid from log WHERE logid > CAST(". $last_log .
169       " AS BIGINT) AND logid < CAST (" . $local_upper_row . " AS BIGINT) GROUP BY objectid";
170   $local_statistics_db->query ($local_sql);
171
172   //Loop the objects
173   for ($i = 1; $i <= $local_statistics_db->num_rows(); $i++) {
174       $local_object_row = $local_statistics_db->fetch_row();
175       $local_sql = "UPDATE object SET log_count = log_count + " . 
176           $local_statistics_db->db_result_row[0] . " WHERE objectid = '" .
177           $local_statistics_db->db_result_row[1] . "'";
178       $local_findobject_db->query($local_sql);  
179   }
180
181   $local_counter = 0;
182
183   if ($dbms->num_rows() > 0) {
184
185     //Create a database connection for changes in the database.
186     $dbms_changes = copy_db_class($dbms);
187     if ($dbms_changes->have_db_connection() == 'TRUE') {
188
189        $local_sql               = 0 ;     
190        $local_sql_statistics    = "";
191        $local_object_os         = "";
192        $local_object_os_version = "";
193
194        while ($local_counter < $dbms->num_rows()) {
195
196          $local_return_row = $dbms->fetch_row();
197          if ($local_return_row == 'TRUE') {
198             //Work on active rows
199             $local_log_id = $dbms->db_result_row[0];
200
201             $local_sql_findobject = "SELECT os, os_version FROM object WHERE objectid = '".$dbms->db_result_row[1]."'";
202             $local_findobject_db->query($local_sql_findobject);
203             $local_findobject_result = $local_findobject_db->fetch_row();
204             if ($local_findobject_result == 'TRUE') {
205                 
206                 //Now work on the OS again
207                 $local_object_os = $local_findobject_db->db_result_row[0];
208                 if  ($local_object_os == "") {
209                     $local_object_os = "Linux";
210                     $local_object_os_version = "Unknown assuming Linux";
211                 } else {
212                   $local_object_os_version = $local_findobject_db->db_result_row[1];
213                 }
214              }
215
216             switch (strtolower($local_object_os)) {
217               case "linux":
218                 $local_process_return = linux_log ();
219                 break;
220               default:
221                 syslog (LOG_INFO, "Couldn't find suitable OS for processing the logline");
222                 break;
223              }
224             
225             if ($local_process_return <> 'TRUE') {
226                $local_process_return = 'FALSE';
227             }
228
229          } else {
230
231            break;
232
233          }
234          $local_counter++;
235        } 
236        
237        //Register that the logrecords have been processed.
238        $local_sql = "UPDATE db_value SET setting_value = '".$local_log_id."' where setting = 'log_processing'";
239        $dbms->query($local_sql);
240        
241
242        //Update the statistics for the object-table
243        
244
245      } else {
246        syslog (LOG_INFO, "Couldn't clone database connection.");
247        die ("Couldn't reconnect to the database.\n");
248     }     
249    }
250
251 }
252
253 function notificationstats () {
254
255 /* This routine will determine how many new notifications have arrived and will
256  * update the statistics in the object-table to keep the performance acceptable
257  * INPUT  : NONE
258  * OUTPUT : NONE
259  */
260             
261  global $dbms;
262
263  //Find records in log that still have to be processed.
264  $local_sql = "SELECT setting_value FROM db_value WHERE setting = 'last_notification'";
265  $dbms->query($local_sql);
266  $local_dbms = copy_db_class($dbms);
267
268  //Determine the last notification
269  if ($dbms->fetch_row() == "TRUE") {
270     $last_notification = $dbms->db_result_row[0];
271  }
272  
273  //Determine how many records we are going to analyse.
274  $local_sql = "SELECT MAX(notificationid) FROM notification " .
275    "WHERE notificationid > CAST ('" . $last_notification . "' AS BIGINT)";
276  $dbms->query($local_sql);  
277
278  //Only process data if there are new notifications
279  if ($dbms->fetch_row() == "TRUE") {
280   if (intval($dbms->db_result_row[0])>0) {
281      $local_upper = $dbms->db_result_row[0] + 1;
282      $local_max   = $dbms->db_result_row[0];
283      $local_sql   = "SELECT COUNT(objectid), objectid FROM notification " .
284        "WHERE notificationid > CAST ('" .  $last_notification ."' AS BIGINT) " .
285        "AND   notificationid < CAST ('" .  $local_upper .
286        "' AS BIGINT) GROUP BY objectid";
287      $dbms->query($local_sql);
288
289
290      for ($i=0; $i < $dbms->num_rows(); $i++) {
291        $dbms->fetch_row();
292     
293        $local_sql = "UPDATE object SET notification_count = notification_count + " . $dbms->db_result_row[0] .
294          " WHERE objectid = '" . $dbms->db_result_row[1] . "'";
295       $local_dbms->query($local_sql); 
296      }  
297
298      $local_sql = "UPDATE db_value SET setting_value = '" . $local_max . 
299        "' WHERE setting = 'last_notification'";
300      $dbms->query($local_sql);  
301   }   
302  } 
303 }
304
305 ?>
306