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