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 ***********************************************************************************/
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
17 First : November 8th 2002
18 Gnucomo-0.0.3: December 6th 2002
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");
27 require_once "classes/gnucomo_config.class.php";
28 require_once "classes/db.class.php";
29 require_once "classes/gnucomo.process_log.php";
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 = 21; //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.
43 //Avoid time-limit issues
47 // Read the database settings //
48 $class_settings = new gnucomo_config();
49 $class_settings->read($project_name);
50 $class_settings->database();
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");
59 db_select($dbms_type);
61 $dbms->db_host = $dbms_host;
62 $dbms->db_name = $dbms_name;
63 $dbms->db_user = $dbms_user;
64 $dbms->db_password = $dbms_password;
67 if ($dbms->have_db_connection() == "FALSE") {
68 exit ("Database connection failed.");
70 //The database connection has been made.
71 $dbms_working = copy_db_class($dbms);
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);
77 if ($dbms->fetch_row() == "TRUE") {
78 $active_version = $dbms->db_result_row[0];
80 //Update the database to the most recent version.
81 if ($active_version < $db_version) {
82 include ("classes/gnucomo_db_version.php");
85 syslog (LOG_INFO, "Couldn't initialize database version. Is this a gnucomo database?");
86 die ("Couldn't initialize database version.\n");
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
92 $local_sql = " SELECT setting_value FROM db_value WHERE setting = 'gcm_daemon_version'";
93 $dbms->query($local_sql);
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);
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);
110 //Now we loop the tasks that we have to do.
114 //At this place we start processing new log-lines
117 $keep_running = 'FALSE';
119 } while ($keep_running == 'TRUE');
121 //Tell the log that we're ending our efforts in a nice way
122 syslog (LOG_INFO, "gcm_daemon ended nicely");
124 function process_log () {
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
134 global $dbms_working;
137 //Start a transaction for the processing/
138 $local_sql_working = "BEGIN TRANSACTION";
139 $dbms_working->query($local_sql_working);
141 //Find records in log that still have to be processed.
143 $local_sql = " SELECT setting_value FROM db_value WHERE setting = 'log_processing'";
144 $dbms->query($local_sql);
146 if ($dbms->fetch_row() == "TRUE") {
147 $last_log = $dbms->db_result_row[0];
151 $local_sql = "SELECT * FROM log WHERE logid > CAST(".$last_log." AS BIGINT) order by logid";
152 $dbms->query($local_sql);
156 if ($dbms->num_rows() > 0) {
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') {
163 $local_object_os = "";
164 $local_object_os_version = "";
166 //Walk through all the log-records.
168 while ($local_counter < $dbms->num_rows()) {
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];
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);
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";
189 $local_object_os_version = $local_findobject_db->db_result_row[1];
192 syslog (LOG_INFO, "Couldn't find object for log-records");
193 die("Couldn't match log-records to any existing object");
197 switch (strtolower($local_object_os)) {
199 $local_process_return = linux_log ();
202 syslog (LOG_INFO, "Couldn't find suitable OS for processing the logline");
206 if ($local_process_return <> 'TRUE') {
207 $local_process_return = 'FALSE';
218 $local_sql = "UPDATE db_value SET setting_value = '".$local_log_id."' where setting = 'log_processing'";
219 $dbms->query($local_sql);
223 syslog (LOG_INFO, "Couldn't clone database connection.");
224 die ("Couldn't reconnect to the database.\n");
230 //close the transaction
231 $local_working = "COMMIT";
232 $dbms_working->query($local_working);