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 ***********************************************************************************/
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
18 First : November 8th 2002
19 Gnucomo-0.0.3: December 6th 2002
23 // $Id: gcm_daemon.php,v 1.5 2003-02-08 07:38:44 arjen Exp $
25 ini_set('include_path', '.:./classes:../phpclasses');
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");
32 require_once "gnucomo_config.php";
33 require_once "db.class.php";
34 require_once "gnucomo.process_log.php";
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.
48 //Avoid time-limit issues
52 // Read the database settings //
53 $class_settings = new gnucomo_config();
54 $class_settings->read($project_name);
55 $class_settings->database();
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");
64 db_select($dbms_type);
66 $dbms->db_host = $dbms_host;
67 $dbms->db_name = $dbms_name;
68 $dbms->db_user = $dbms_user;
69 $dbms->db_password = $dbms_password;
72 if ($dbms->have_db_connection() == "FALSE") {
73 exit ("Database connection failed.");
75 //The database connection has been made.
76 $dbms_working = copy_db_class($dbms);
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);
82 if ($dbms->fetch_row() == "TRUE") {
83 $active_version = $dbms->db_result_row[0];
85 //Update the database to the most recent version.
86 if ($active_version < $db_version) {
87 include ("gnucomo_db_version.php");
90 syslog (LOG_INFO, "Couldn't initialize database version. Is this a gnucomo database?");
91 die ("Couldn't initialize database version.\n");
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
97 $local_sql = " SELECT setting_value FROM db_value WHERE setting = 'gcm_daemon_version'";
98 $dbms->query($local_sql);
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);
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);
115 //Now we loop the tasks that we have to do.
119 //At this place we start processing new log-lines
122 $keep_running = 'FALSE';
124 } while ($keep_running == 'TRUE');
126 //Tell the log that we're ending our efforts in a nice way
127 syslog (LOG_INFO, "gcm_daemon ended nicely");
129 function process_log () {
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
139 global $dbms_working;
142 //Start a transaction for the processing/
143 $local_sql_working = "BEGIN TRANSACTION";
144 $dbms_working->query($local_sql_working);
146 //Find records in log that still have to be processed.
148 $local_sql = " SELECT setting_value FROM db_value WHERE setting = 'log_processing'";
149 $dbms->query($local_sql);
151 if ($dbms->fetch_row() == "TRUE") {
152 $last_log = $dbms->db_result_row[0];
156 $local_sql = "SELECT * FROM log WHERE logid > CAST(".$last_log." AS BIGINT) order by logid";
157 $dbms->query($local_sql);
161 if ($dbms->num_rows() > 0) {
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') {
168 $local_object_os = "";
169 $local_object_os_version = "";
171 //Walk through all the log-records.
173 while ($local_counter < $dbms->num_rows()) {
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];
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);
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";
194 $local_object_os_version = $local_findobject_db->db_result_row[1];
197 syslog (LOG_INFO, "Couldn't find object for log-records");
198 die("Couldn't match log-records to any existing object");
202 switch (strtolower($local_object_os)) {
204 $local_process_return = linux_log ();
207 syslog (LOG_INFO, "Couldn't find suitable OS for processing the logline");
211 if ($local_process_return <> 'TRUE') {
212 $local_process_return = 'FALSE';
223 $local_sql = "UPDATE db_value SET setting_value = '".$local_log_id."' where setting = 'log_processing'";
224 $dbms->query($local_sql);
228 syslog (LOG_INFO, "Couldn't clone database connection.");
229 die ("Couldn't reconnect to the database.\n");