+<?PHP
+/**********************************************************************************
+** (c) Copyright 2002, Brenno J.S.A.A.F. de Winter, De Winter Information Solutions
+** This is free software; you can redistribute it and/or modify it under the
+** terms of the GNU General Public License, see the file COPYING.
+***********************************************************************************/
+
+
+/*
+ NAME : gcm_daemon
+ AUTHOR : Brenno J.S.A.A.F. de Winter
+ De Winter Information Solutions
+ COPYRIGHT : 2002 - De Winter Information Solutions,
+ Brenno J.S.A.A.F. de Winter
+
+ * DATES *
+ First : November 8th 2002
+
+*/
+
+//Tell the log that we're up.
+define_syslog_variables();
+openlog("gnucomo", LOG_PID, LOG_DAEMON);
+syslog(LOG_INFO, "gcm_daemon started");
+
+require_once "classes/gnucomo_config.class.php";
+require_once "classes/db.class.php";
+require_once "classes/gnucomo.process_log.php";
+
+// Set the standard variables //
+$project_name = "gnucomo"; //name of the entire project
+$app_name = "gcm_daemon"; //name of the application running
+$db_version = 13; //The db_version indicates what the level of
+ //the database should be. If the database is
+ //old an update will be generated.
+$gcmd_version = 2; //This value indicates the active version of the gcm_daemon,
+ //which is saved in the database. Log records that were not
+ //recognized before will now be recognized. The version doesn't
+ //mean anything in the overall gnucomo project.
+
+// Read the database settings //
+$class_settings = new gnucomo_config();
+$class_settings->read($project_name);
+$class_settings->database();
+
+//Open an connection to the database
+$dbms_type = $class_settings->find_parameter("database", "type");
+$dbms_host = $class_settings->find_parameter("database", "host");
+$dbms_name = $class_settings->find_parameter("database", "name");
+$dbms_user = $class_settings->find_parameter("gcm_daemon", "user");
+$dbms_password = $class_settings->find_parameter("gcm_daemon", "password");
+
+db_select($dbms_type);
+$dbms = new db();
+$dbms->db_host = $dbms_host;
+$dbms->db_name = $dbms_name;
+$dbms->db_user = $dbms_user;
+$dbms->db_password = $dbms_password;
+$dbms->db_connect();
+
+if ($dbms->have_db_connection() == "FALSE") {
+ exit ("Database connection failed.");
+}
+//The database connection has been made.
+
+//Verify if the database is up-to-date by checking the versionnumber
+$local_sql = "SELECT setting_value FROM db_value WHERE setting = 'db_version'";
+$dbms->query($local_sql);
+
+if ($dbms->fetch_row() == "TRUE") {
+ $active_version = $dbms->db_result_row[0];
+
+ //Update the database to the most recent version.
+ if ($active_version < $db_version) {
+ include ("classes/gnucomo_db_version.php");
+ }
+} else {
+ syslog (LOG_INFO, "Couldn't initialize database version. Is this a gnucomo database?");
+ die ("Couldn't initialize database version.\n");
+}
+
+//If there is a new gcm_daemon_version the logrecords that couldn't be understood can be
+//reprocessed. For this reason processed is now changed to false again for not recognized
+//records.
+$local_sql = " SELECT setting_value FROM db_value WHERE setting = 'gcm_daemon_version'";
+$dbms->query($local_sql);
+
+if ($dbms->fetch_row() == "TRUE") {
+ if ($dbms->db_result_row[0] < $gcmd_version) {
+ //Reactive log-records that weren't understood earlier.
+ $local_sql = "UPDATE log SET processed = false WHERE recognized = false";
+ $dbms->query($local_sql);
+
+ //Update de gcm_daemon version in the database
+ $local_sql = "UPDATE db_value SET setting_value = '".$gcmd_version;
+ $local_sql .= "' WHERE setting = 'gcm_daemon_version'";
+ $dbms->query($local_sql);
+
+ }
+
+}
+
+
+
+//At this place we start processing new log-lines
+process_log ();
+
+//Tell the log that we're ending our efforts in a nice way
+syslog (LOG_INFO, "gcm_daemon ended nicely");
+
+function process_log () {
+
+ /* This function will walk through the log-records that haven't been processed
+ * first a snapshot will be created of a the non-processed records.
+ * sequentially each record will dealt with. By doing that changes will be made
+ * in several log_adv_xxx tables
+ * INPUT : NONE
+ * OUTPUT : NONE
+ */
+ global $dbms;
+
+ //Find open records.
+ $local_sql = "SELECT * FROM log WHERE processed = FALSE";
+ $dbms->query($local_sql);
+ $local_counter = 0;
+
+ if ($dbms->num_rows() > 0) {
+
+ //Create a database connection for changes in the database.
+ $dbms_changes = copy_db_class($dbms);
+ if ($dbms_changes->have_db_connection() == 'TRUE') {
+
+ $local_sql = 0;
+ $local_object_os = "";
+ $local_object_os_version = "";
+
+ //Walk through all the log-records.
+
+ while ($local_counter < $dbms->num_rows()) {
+ $local_return_row = $dbms->fetch_row();
+ if ($local_return_row == 'TRUE') {
+ //Work on active rows
+ $local_log_id = $dbms->db_result_row[0];
+
+ //If this is the first time get information about the object
+ if ($local_object_os == "") {
+ //No OS has been given yet, so let's find the objectid.
+ $local_findobject_db = copy_db_class($dbms);
+
+ $local_sql_findobject = "SELECT os, os_version FROM object WHERE objectid = '".$dbms->db_result_row[0]."'";
+ $local_findobject_db->query($local_sql_findobject);
+ $local_findobject_result = $local_findobject_db->fetch_row();
+ if ($local_findobject_result == 'TRUE') {
+ $local_object_os = $local_findobject_db->db_result_row[0];
+ if ($local_object_os == "") {
+ $local_object_os = "Linux";
+ $local_object_os_version = "Unknown assuming Linux";
+ } else {
+ $local_object_os_version = $local_findobject_db->db_result_row[1];
+ }
+ } else {
+ syslog (LOG_INFO, "Couldn't find object for log-records");
+ die("Couldn't match log-records to any existing object");
+ }
+ }
+
+ switch (strtolower($local_object_os)) {
+ case "linux":
+ $local_process_return = linux_log ();
+ break;
+ default:
+ syslog (LOG_INFO, "Couldn't find suitable OS for processing the logline");
+ break;
+ }
+
+ //Now that the processing took place change the processing state if processing was
+ //completed successfully.
+ if ($local_process_return == 'TRUE') {
+ $local_sql_processed = "UPDATE log SET recognized = TRUE where logid = $local_log_id";
+ $dbms_changes->query($local_sql_processed);
+ }
+
+ //Change the status of the log-record to processed.
+ $local_sql_processed = "UPDATE log SET processed = TRUE WHERE logid = $local_log_id";
+ $dbms_changes->query($local_sql_processed);
+
+ } else {
+
+ break;
+
+ }
+ $local_counter++;
+ }
+ } else {
+ syslog (LOG_INFO, "Couldn't clone database connection.");
+ die ("Couldn't reconnect to the database.\n");
+ }
+ }
+}
+
+?>
+
+