bc0547a7f44ecbd180e5e44dbc33a2938e497795
[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 */
22
23 // $Id: gcm_daemon.php,v 1.5 2003-02-08 07:38:44 arjen Exp $
24
25 ini_set('include_path', '.:./classes:../phpclasses');
26
27 //Tell the log that we're up.
28 define_syslog_variables();
29 openlog("gnucomo", LOG_PID, LOG_DAEMON);
30 syslog(LOG_INFO, "gcm_daemon started");
31
32 require_once "gnucomo_config.php";
33 require_once "db.class.php";
34 require_once "gnucomo.process_log.php";
35
36 // Set the standard variables //
37 $project_name   = "gnucomo";    //name of the entire project
38 $app_name       = "gcm_daemon"; //name of the application running
39 $developrelease = "TRUE";      //Indicates if special debug settings are needed
40 $db_version     = 24;           //The db_version indicates what the level of 
41                                 //the database should be. If the database is 
42                                 //old an update will be generated.
43 $gcmd_version   = 3;            //This value indicates the active version of the gcm_daemon,
44                                 //which is saved in the database. Log records that were not
45                                 //recognized before will now be recognized. The version doesn't
46                                 //mean anything in the overall gnucomo project.
47
48 //Avoid time-limit issues
49 set_time_limit(0);
50
51
52 // Read the database settings //
53 $class_settings = new gnucomo_config();
54 $class_settings->read($project_name);
55 $class_settings->database();
56
57 //Open an connection to the database
58 $dbms_type = $class_settings->find_parameter("database", "type");
59 $dbms_host = $class_settings->find_parameter("database", "host");
60 $dbms_name = $class_settings->find_parameter("database", "name");
61 $dbms_user = $class_settings->find_parameter("gcm_daemon", "user");
62 $dbms_password = $class_settings->find_parameter("gcm_daemon", "password");
63
64 db_select($dbms_type);
65 $dbms = new db();
66 $dbms->db_host = $dbms_host;
67 $dbms->db_name = $dbms_name;
68 $dbms->db_user = $dbms_user;
69 $dbms->db_password = $dbms_password;
70 $dbms->db_connect();
71
72 if ($dbms->have_db_connection() == "FALSE") {
73   exit ("Database connection failed.");
74 } else {
75   //The database connection has been made.
76   $dbms_working = copy_db_class($dbms);
77 }
78 //Verify if the database is up-to-date by checking the versionnumber
79 $local_sql = "SELECT setting_value FROM db_value WHERE setting = 'db_version' ";
80 $dbms->query($local_sql);
81
82 if ($dbms->fetch_row() == "TRUE") {
83   $active_version = $dbms->db_result_row[0];
84  
85   //Update the database to the most recent version.
86   if ($active_version < $db_version) { 
87      include ("gnucomo_db_version.php");
88   }
89 } else {
90   syslog (LOG_INFO, "Couldn't initialize database version. Is this a gnucomo database?");
91   die ("Couldn't initialize database version.\n");
92 }
93
94 //If there is a new gcm_daemon_version the logrecords that couldn't be understood can be
95 //reprocessed. For this reason processed is now changed to false again for not recognized
96 //records.
97 $local_sql = " SELECT setting_value FROM db_value WHERE setting = 'gcm_daemon_version'";
98 $dbms->query($local_sql);
99
100 if ($dbms->fetch_row() == "TRUE") {
101    if ($dbms->db_result_row[0] < $gcmd_version) {
102       //Reactive log-records that weren't understood earlier.
103       $local_sql = "UPDATE log SET processed = false WHERE logid NOT IN (SELECT DISTINCT logid FROM log_adv)";
104       $dbms->query($local_sql);
105
106       //Update de gcm_daemon version in the database
107       $local_sql = "UPDATE db_value SET setting_value = '".$gcmd_version;
108       $local_sql .= "' WHERE setting = 'gcm_daemon_version'";
109       $dbms->query($local_sql);
110
111    }
112       
113 }
114
115 //Now we loop the tasks that we have to do.
116
117 do {
118
119   //At this place we start processing new log-lines 
120   process_log ();
121
122   $keep_running = 'FALSE';
123
124 } while ($keep_running == 'TRUE');
125
126 //Tell the log that we're ending our efforts in a nice way
127 syslog (LOG_INFO, "gcm_daemon ended nicely");
128
129 function process_log () {
130  
131  /* This function will walk through the log-records that haven't been processed
132   * first a snapshot will be created of a the non-processed records. 
133   * sequentially each record will dealt with. By doing that changes will be made
134   * in several log_adv_xxx tables
135   * INPUT  : NONE
136   * OUTPUT : NONE
137   */
138   global $dbms;
139   global $dbms_working;
140
141 /*
142   //Start a transaction for the processing/
143   $local_sql_working = "BEGIN TRANSACTION";
144   $dbms_working->query($local_sql_working);
145 */  
146   //Find records in log that still have to be processed.
147
148   $local_sql = " SELECT setting_value FROM db_value WHERE setting = 'log_processing'";
149   $dbms->query($local_sql);
150
151   if ($dbms->fetch_row() == "TRUE") {
152      $last_log = $dbms->db_result_row[0];
153   }
154
155
156   $local_sql = "SELECT * FROM log WHERE logid > CAST(".$last_log." AS BIGINT) order by logid";
157   $dbms->query($local_sql);
158
159   $local_counter = 0;
160
161   if ($dbms->num_rows() > 0) {
162
163     //Create a database connection for changes in the database.
164     $dbms_changes = copy_db_class($dbms);
165     if ($dbms_changes->have_db_connection() == 'TRUE') {
166
167        $local_sql               = 0;     
168        $local_object_os         = "";
169        $local_object_os_version = "";
170
171        //Walk through all the log-records.
172
173        while ($local_counter < $dbms->num_rows()) {
174
175          $local_return_row = $dbms->fetch_row();
176          if ($local_return_row == 'TRUE') {
177             //Work on active rows
178             $local_log_id = $dbms->db_result_row[0];
179
180             //If this is the first time get information about the object
181             if ($local_object_os == "") {
182               //No OS has been given yet, so let's find the objectid.
183               $local_findobject_db = copy_db_class($dbms);
184
185               $local_sql_findobject = "SELECT os, os_version FROM object WHERE objectid = '".$dbms->db_result_row[0]."'";
186               $local_findobject_db->query($local_sql_findobject);
187               $local_findobject_result = $local_findobject_db->fetch_row();
188               if ($local_findobject_result == 'TRUE') {
189                 $local_object_os = $local_findobject_db->db_result_row[0];
190                 if  ($local_object_os == "") {
191                     $local_object_os = "Linux";
192                     $local_object_os_version = "Unknown assuming Linux";
193                 } else {
194                   $local_object_os_version = $local_findobject_db->db_result_row[1];
195                 }
196               } else {
197                 syslog (LOG_INFO, "Couldn't find object for log-records");
198                 die("Couldn't match log-records to any existing object");
199               }
200              }
201
202             switch (strtolower($local_object_os)) {
203               case "linux":
204                 $local_process_return = linux_log ();
205                 break;
206               default:
207                 syslog (LOG_INFO, "Couldn't find suitable OS for processing the logline");
208                 break;
209              }
210             
211             if ($local_process_return <> 'TRUE') {
212                $local_process_return = 'FALSE';
213             }
214
215          } else {
216
217            break;
218
219          }
220          $local_counter++;
221        } 
222
223        $local_sql = "UPDATE db_value SET setting_value = '".$local_log_id."' where setting = 'log_processing'";
224        $dbms->query($local_sql);
225        
226
227      } else {
228        syslog (LOG_INFO, "Couldn't clone database connection.");
229        die ("Couldn't reconnect to the database.\n");
230     }     
231    } else {
232      die ("done");
233    }
234 }
235
236 ?>
237
238