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
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");
26 require_once "classes/gnucomo_config.class.php";
27 require_once "classes/db.class.php";
28 require_once "classes/gnucomo.process_log.php";
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.
41 // Read the database settings //
42 $class_settings = new gnucomo_config();
43 $class_settings->read($project_name);
44 $class_settings->database();
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");
53 db_select($dbms_type);
55 $dbms->db_host = $dbms_host;
56 $dbms->db_name = $dbms_name;
57 $dbms->db_user = $dbms_user;
58 $dbms->db_password = $dbms_password;
61 if ($dbms->have_db_connection() == "FALSE") {
62 exit ("Database connection failed.");
64 //The database connection has been made.
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);
70 if ($dbms->fetch_row() == "TRUE") {
71 $active_version = $dbms->db_result_row[0];
73 //Update the database to the most recent version.
74 if ($active_version < $db_version) {
75 include ("classes/gnucomo_db_version.php");
78 syslog (LOG_INFO, "Couldn't initialize database version. Is this a gnucomo database?");
79 die ("Couldn't initialize database version.\n");
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
85 $local_sql = " SELECT setting_value FROM db_value WHERE setting = 'gcm_daemon_version'";
86 $dbms->query($local_sql);
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);
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);
105 //At this place we start processing new log-lines
108 //Tell the log that we're ending our efforts in a nice way
109 syslog (LOG_INFO, "gcm_daemon ended nicely");
111 function process_log () {
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
123 $local_sql = "SELECT * FROM log WHERE processed = FALSE";
124 $dbms->query($local_sql);
127 if ($dbms->num_rows() > 0) {
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') {
134 $local_object_os = "";
135 $local_object_os_version = "";
137 //Walk through all the log-records.
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];
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);
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";
159 $local_object_os_version = $local_findobject_db->db_result_row[1];
162 syslog (LOG_INFO, "Couldn't find object for log-records");
163 die("Couldn't match log-records to any existing object");
167 switch (strtolower($local_object_os)) {
169 $local_process_return = linux_log ();
172 syslog (LOG_INFO, "Couldn't find suitable OS for processing the logline");
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);
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);
195 syslog (LOG_INFO, "Couldn't clone database connection.");
196 die ("Couldn't reconnect to the database.\n");