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
21 $Log: gcm_daemon.php,v $
22 Revision 1.9 2003-02-21 08:37:59 arjen
23 Added new table to the database: log_adv_daemon_email.
28 // $Id: gcm_daemon.php,v 1.9 2003-02-21 08:37:59 arjen Exp $
30 ini_set('include_path', '.:./classes:../phpclasses');
32 //Tell the log that we're up.
33 define_syslog_variables();
34 openlog("gnucomo", LOG_PID, LOG_DAEMON);
35 syslog(LOG_INFO, "gcm_daemon started");
37 require_once "gnucomo_config.php";
38 require_once "db.class.php";
39 require_once "gnucomo.process_log.php";
41 // Set the standard variables //
42 $project_name = "gnucomo"; //name of the entire project
43 $app_name = "gcm_daemon"; //name of the application running
44 $developrelease = "TRUE"; //Indicates if special debug settings are needed
45 $db_version = 32; //The db_version indicates what the level of
46 //the database should be. If the database is
47 //old an update will be generated.
48 $gcmd_version = 3; //This value indicates the active version of the gcm_daemon,
49 //which is saved in the database. Log records that were not
50 //recognized before will now be recognized. The version doesn't
51 //mean anything in the overall gnucomo project.
53 //Avoid time-limit issues
57 // Read the database settings //
58 $class_settings = new gnucomo_config();
59 $class_settings->read($project_name);
60 $class_settings->database();
62 //Open an connection to the database
63 $dbms_type = $class_settings->find_parameter("database", "type");
64 $dbms_host = $class_settings->find_parameter("database", "host");
65 $dbms_name = $class_settings->find_parameter("database", "name");
66 $dbms_user = $class_settings->find_parameter("gcm_daemon", "user");
67 $dbms_password = $class_settings->find_parameter("gcm_daemon", "password");
69 db_select($dbms_type);
71 $dbms->db_host = $dbms_host;
72 $dbms->db_name = $dbms_name;
73 $dbms->db_user = $dbms_user;
74 $dbms->db_password = $dbms_password;
77 if ($dbms->have_db_connection() == "FALSE") {
78 exit ("Database connection failed.");
80 //The database connection has been made.
81 $dbms_working = copy_db_class($dbms);
84 //Verify if the database is up-to-date by checking the versionnumber
85 $local_sql = "SELECT setting_value FROM db_value WHERE setting = 'db_version' ";
86 $dbms->query($local_sql);
88 if ($dbms->fetch_row() == "TRUE") {
89 $active_version = $dbms->db_result_row[0];
91 //Update the database to the most recent version.
92 if ($active_version < $db_version) {
93 include ("gnucomo_db_version.php");
96 syslog (LOG_INFO, "Couldn't initialize database version. Is this a gnucomo database?");
97 die ("Couldn't initialize database version.\n");
100 //If there is a new gcm_daemon_version the logrecords that couldn't be understood can be
101 //reprocessed. For this reason processed is now changed to false again for not recognized
103 $local_sql = "SELECT setting_value FROM db_value WHERE setting = 'gcm_daemon_version'";
104 $dbms->query($local_sql);
106 if ($dbms->fetch_row() == "TRUE") {
107 if ($dbms->db_result_row[0] < $gcmd_version) {
108 //Reactive log-records that weren't understood earlier.
109 $local_sql = "UPDATE log SET processed = false WHERE logid NOT IN (SELECT DISTINCT logid FROM log_adv)";
110 $dbms->query($local_sql);
112 //Update de gcm_daemon version in the database
113 $local_sql = "UPDATE db_value SET setting_value = '".$gcmd_version;
114 $local_sql .= "' WHERE setting = 'gcm_daemon_version'";
115 $dbms->query($local_sql);
121 //Now we loop the tasks that we have to do.
126 //At this place we start processing new log-lines
130 $keep_running = 'FALSE';
132 } while ($keep_running == 'TRUE');
134 //Tell the log that we're ending our efforts in a nice way
135 syslog (LOG_INFO, "gcm_daemon ended nicely");
137 function process_log () {
139 /* This function will walk through the log-records that haven't been processed
140 * first a snapshot will be created of a the non-processed records.
141 * sequentially each record will dealt with. By doing that changes will be made
142 * in several log_adv_xxx tables
147 global $dbms_working;
149 //Find records in log that still have to be processed.
151 $local_sql = "SELECT setting_value FROM db_value WHERE setting = 'log_processing'";
152 $dbms->query($local_sql);
154 if ($dbms->fetch_row() == "TRUE") {
155 $last_log = $dbms->db_result_row[0];
158 //Query the log-table
159 $local_sql = "SELECT * FROM log WHERE logid > CAST(".$last_log." AS BIGINT) order by logid";
160 $dbms->query($local_sql);
162 //Update the log-statistics in the object-table
163 $local_statistics_db = copy_db_class($dbms);
164 $local_findobject_db = copy_db_class($dbms);
167 $local_upper_row = $dbms->num_rows() + $last_log + 1;
168 $local_sql = "SELECT COUNT(logid), objectid from log WHERE logid > CAST(". $last_log .
169 " AS BIGINT) AND logid < CAST (" . $local_upper_row . " AS BIGINT) GROUP BY objectid";
170 $local_statistics_db->query ($local_sql);
173 for ($i = 1; $i <= $local_statistics_db->num_rows(); $i++) {
174 $local_object_row = $local_statistics_db->fetch_row();
175 $local_sql = "UPDATE object SET log_count = log_count + " .
176 $local_statistics_db->db_result_row[0] . " WHERE objectid = '" .
177 $local_statistics_db->db_result_row[1] . "'";
178 $local_findobject_db->query($local_sql);
183 if ($dbms->num_rows() > 0) {
185 //Create a database connection for changes in the database.
186 $dbms_changes = copy_db_class($dbms);
187 if ($dbms_changes->have_db_connection() == 'TRUE') {
190 $local_sql_statistics = "";
191 $local_object_os = "";
192 $local_object_os_version = "";
194 while ($local_counter < $dbms->num_rows()) {
196 $local_return_row = $dbms->fetch_row();
197 if ($local_return_row == 'TRUE') {
198 //Work on active rows
199 $local_log_id = $dbms->db_result_row[0];
201 $local_sql_findobject = "SELECT os, os_version FROM object WHERE objectid = '".$dbms->db_result_row[1]."'";
202 $local_findobject_db->query($local_sql_findobject);
203 $local_findobject_result = $local_findobject_db->fetch_row();
204 if ($local_findobject_result == 'TRUE') {
206 //Now work on the OS again
207 $local_object_os = $local_findobject_db->db_result_row[0];
208 if ($local_object_os == "") {
209 $local_object_os = "Linux";
210 $local_object_os_version = "Unknown assuming Linux";
212 $local_object_os_version = $local_findobject_db->db_result_row[1];
216 switch (strtolower($local_object_os)) {
218 $local_process_return = linux_log ();
221 syslog (LOG_INFO, "Couldn't find suitable OS for processing the logline");
225 if ($local_process_return <> 'TRUE') {
226 $local_process_return = 'FALSE';
237 //Register that the logrecords have been processed.
238 $local_sql = "UPDATE db_value SET setting_value = '".$local_log_id."' where setting = 'log_processing'";
239 $dbms->query($local_sql);
242 //Update the statistics for the object-table
246 syslog (LOG_INFO, "Couldn't clone database connection.");
247 die ("Couldn't reconnect to the database.\n");
253 function notificationstats () {
255 /* This routine will determine how many new notifications have arrived and will
256 * update the statistics in the object-table to keep the performance acceptable
263 //Find records in log that still have to be processed.
264 $local_sql = "SELECT setting_value FROM db_value WHERE setting = 'last_notification'";
265 $dbms->query($local_sql);
266 $local_dbms = copy_db_class($dbms);
268 //Determine the last notification
269 if ($dbms->fetch_row() == "TRUE") {
270 $last_notification = $dbms->db_result_row[0];
273 //Determine how many records we are going to analyse.
274 $local_sql = "SELECT MAX(notificationid) FROM notification " .
275 "WHERE notificationid > CAST ('" . $last_notification . "' AS BIGINT)";
276 $dbms->query($local_sql);
278 //Only process data if there are new notifications
279 if ($dbms->fetch_row() == "TRUE") {
280 if (intval($dbms->db_result_row[0])>0) {
281 $local_upper = $dbms->db_result_row[0] + 1;
282 $local_max = $dbms->db_result_row[0];
283 $local_sql = "SELECT COUNT(objectid), objectid FROM notification " .
284 "WHERE notificationid > CAST ('" . $last_notification ."' AS BIGINT) " .
285 "AND notificationid < CAST ('" . $local_upper .
286 "' AS BIGINT) GROUP BY objectid";
287 $dbms->query($local_sql);
290 for ($i=0; $i < $dbms->num_rows(); $i++) {
293 $local_sql = "UPDATE object SET notification_count = notification_count + " . $dbms->db_result_row[0] .
294 " WHERE objectid = '" . $dbms->db_result_row[1] . "'";
295 $local_dbms->query($local_sql);
298 $local_sql = "UPDATE db_value SET setting_value = '" . $local_max .
299 "' WHERE setting = 'last_notification'";
300 $dbms->query($local_sql);