#!/usr/bin/php read($project_name)) { echo "Can not read Gnucomo configuration file for $project_name.\n"; exit(); } openlog("gnucomo", LOG_PID, LOG_DAEMON); syslog(LOG_INFO, "gcm_daemon started"); //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($class_settings->database()); if ($dbms->have_db_connection() == "FALSE") { exit ("Database connection failed."); } else { // The database connection has been made. $dbms_working = copy_db_class($dbms, $class_settings->database()); } // 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 ("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 logid NOT IN (SELECT DISTINCT logid FROM log_adv)"; $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); } } // Now we loop the tasks that we have to do. do { //At this place we start processing new log-lines process_log (); service_check(); find_notifications(); // Gather the statistics for each object $obj_result = $dbms->query("SELECT objectid FROM object"); for ($obj = 0; $obj < $dbms->num_rows($obj_result); $obj++) { $object = $dbms->fetch_object($obj_result, $obj); echo "Gathering statistics for object " . $object->objectid . "\n"; GatherStatistics($object->objectid); } $keep_running = false; } while ($keep_running == true); //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; global $dbms_working; global $class_settings; $last_log = 0; // Find records in log that still have to be processed. $local_sql = "SELECT setting_value FROM db_value WHERE setting = 'log_processing'"; $dbms->query($local_sql); if ($dbms->fetch_row() == "TRUE") { $last_log = $dbms->db_result_row[0]; } //Query the log-table $local_sql = "SELECT * FROM log WHERE logid > CAST(".$last_log." AS BIGINT) ORDER BY logid"; $dbms->query($local_sql); //Update the log-statistics in the object-table $local_statistics_db = copy_db_class($dbms, $class_settings->database()); $local_findobject_db = copy_db_class($dbms, $class_settings->database()); //Make totals $local_upper_row = $dbms->num_rows() + $last_log + 1; $local_sql = "SELECT COUNT(logid), objectid from log WHERE logid > CAST(". $last_log . " AS BIGINT) AND logid < CAST (" . $local_upper_row . " AS BIGINT) GROUP BY objectid"; $local_statistics_db->query ($local_sql); // Loop the objects for ($i = 1; $i <= $local_statistics_db->num_rows(); $i++) { $local_object_row = $local_statistics_db->fetch_row(); $local_sql = "UPDATE object SET log_count = log_count + " . $local_statistics_db->db_result_row[0] . " WHERE objectid = '" . $local_statistics_db->db_result_row[1] . "'"; $local_findobject_db->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, $class_settings->database()); if ($dbms_changes->have_db_connection() == 'TRUE') { $local_sql = 0 ; $local_sql_statistics = ""; $local_object_os = ""; $local_object_os_version = ""; 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]; $local_sql_findobject = "SELECT os, os_version FROM object WHERE objectid = '".$dbms->db_result_row[1]."'"; $local_findobject_db->query($local_sql_findobject); $local_findobject_result = $local_findobject_db->fetch_row(); if ($local_findobject_result == 'TRUE') { // Now work on the OS again $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]; } } 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; } if ($local_process_return != 'TRUE') { $local_process_return = 'FALSE'; } } else { break; } $local_counter++; } // Register that the logrecords have been processed. $local_sql = "UPDATE db_value SET setting_value = '" .$local_log_id."' where setting = 'log_processing'"; $dbms->query($local_sql); // Update the statistics for the object-table } else { syslog (LOG_INFO, "Couldn't clone database connection."); die ("Couldn't reconnect to the database.\n"); } } } /* * Update a single statistic for some object. * If it does not yet exist, it will be created. */ function UpdateStatistic($objectid, $name, $value) { global $dbms; $result = $dbms->query("SELECT objectid FROM object_statistics WHERE objectid='$objectid' AND statname='$name'"); if ($dbms->num_rows() == 0) { $dbms->query("INSERT INTO object_statistics VALUES ('$objectid', '$name', '$value')"); } else { $dbms->query("UPDATE object_statistics SET statvalue='$value' WHERE statname='$name' AND objectid='$objectid'"); } } /* * Gather the statistics for a single object ($objectid). * We count the number of parameters, removed parameters, notifications * closed notifications and log entries. The totals of these are * maintained in a separate table: object_statistics. */ function GatherStatistics($objectid) { global $dbms; // Gather statistics on parameters $r = $dbms->query("SELECT paramid FROM parameter WHERE objectid=CAST('" . $objectid . "' AS BIGINT)"); $nr_parameters = $dbms->num_rows($r); $removed_parameters = 0; for ($p = 0; $p < $nr_parameters; $p++) { $param = pg_fetch_object($r, $p); $qry ="select change_nature from history where paramid= CAST('"; $qry .= $param->paramid . "' AS BIGINT) order by modified desc"; $rhist = $dbms->query($qry); if ($dbms->num_rows($rhist) == 0) { echo "ERROR: No history for parameter id " . $param->paramid . "\n"; } else { $hist = $dbms->fetch_object($rhist, 0); if ($hist->change_nature == "REMOVED") { $removed_parameters++; } } } UpdateStatistic($objectid, 'parameters', $nr_parameters); UpdateStatistic($objectid, 'removed_parameters', $removed_parameters); // Gather statistics on notifications $r = $dbms->query("SELECT count(notificationid) FROM notification WHERE objectid = CAST('" . $objectid . "' AS BIGINT)"); $cnt = $dbms->fetch_object($r, 0); UpdateStatistic($objectid, 'notifications', $cnt->count); $r = $dbms->query("SELECT count(notificationid) FROM notification WHERE objectid = CAST('" . $objectid . "' AS BIGINT) AND statuscode ='cls'"); $cnt = $dbms->fetch_object($r, 0); UpdateStatistic($objectid, 'closed_notifications', $cnt->count); // Gather statistics on log entries $r = $dbms->query("SELECT count(logid) FROM log WHERE objectid = CAST('" . $objectid . "' AS BIGINT)"); $cnt = $dbms->fetch_object($r, 0); UpdateStatistic($objectid, 'logs', $cnt->count); } /* * Service_check - Check the log entries if there are any unknown * services. */ function service_check() { global $dbms; $unknown_notification = 0; $unused_notification = 0; $last_log = 0; // How far did we get last time ? $lastlogres = $dbms->query("SELECT setting_value FROM db_value WHERE setting = 'log_servicecheck'"); if ($dbms->num_rows($lastlogres) == 1) { $last_log = $dbms->Field($lastlogres, 0, 'setting_value'); } else { $dbms->query("INSERT INTO db_value (setting, setting_value) VALUES ('log_servicecheck', '0')"); } // Query the log-table $qry = "SELECT logid, objectid, servicecode FROM log WHERE logid > CAST(".$last_log." AS BIGINT) ORDER BY logid"; $log_res = $dbms->query($qry); //$log_res = $dbms->query("SELECT logid, objectid, servicecode,rawdata FROM log"); for ($log_row = 0; $log_row < $dbms->num_rows($log_res); $log_row++) { $log_entry = $dbms->fetch_object($log_res, $log_row); $last_log = $log_entry->logid; // Check if the service is used on the object. $qry = "SELECT * FROM object_service WHERE objectid='"; $qry .= $log_entry->objectid . "' AND servicecode='"; $qry .= $log_entry->servicecode . "'"; $os_res = $dbms->query($qry); if ($dbms->num_rows($os_res) == 0) { // Service is not found for the object, check if the service // exists at all. $qry = "SELECT * FROM service WHERE servicecode='"; $qry .= $log_entry->servicecode . "'"; if ($dbms->num_rows($dbms->query($qry)) == 0) { if ($unknown_notification == 0) { $remark = "One or more log entries from a service that is not in the database"; $unknown_notification = $dbms->new_notification($log_entry->objectid, 'service unknown', $remark); } if ($unknown_notification != 0) { $insertion = "INSERT INTO log_notification (notificationid, logid) VALUES ('"; $insertion .= $unknown_notification . "', '"; $insertion .= $log_entry->logid . "')"; $dbms->query($insertion); } } else { if ($unused_notification == 0) { $remark = "One or more log entries from a service not running on this object"; $unused_notification = $dbms->new_notification($log_entry->objectid, 'service not used', $remark); } if ($unused_notification != 0) { $insertion = "INSERT INTO log_notification (notificationid, logid) VALUES ('"; $insertion .= $unused_notification . "', '"; $insertion .= $log_entry->logid . "')"; $dbms->query($insertion); } } } } $qry = "UPDATE db_value SET setting_value = '" . $last_log . "' WHERE setting = 'log_servicecheck'"; $dbms->query($qry); } function find_notifications () { /* * Do something with notification checks. * * INPUT : NONE * OUTPUT : NONE */ global $dbms; // Find checks that have to be executed. $local_sql = "select * from notification_check where age(last_execution) > time_between_executions"; $dbms->query($local_sql); for ($i=0; $i<$dbms->num_rows(); $i++) { // A check has been found that has to be executed $dbms->fetch_row(); } } ?>