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