function process_alerts() { global $cacti_camm_components; $alerts = db_fetch_assoc("SELECT * FROM `plugin_camm_rule` where `rule_enable`=1 ORDER BY `is_delete` DESC, `count_triggered` DESC;"); //$use_syslog = (read_config_option("camm_use_syslog") == "1"); $camm_syslog_db_name = read_config_option("camm_syslog_db_name"); camm_debug("S1. Found " . sizeof($alerts) . " camm rule" . (sizeof($alerts) == 1 ? "" : "s") . " to process"); /* FLAG ALL THE CURRENT ITEMS TO WORK WITH */ if ($cacti_camm_components["snmptt"]) { camm_debug("S1.1 Use SNMPTT component"); db_execute("UPDATE `plugin_camm_snmptt` set status=1 where status=0"); $stat_ruleDeleTraps = mysql_affected_rows(); } else { $stat_ruleDeleTraps = 0; } if ($cacti_camm_components["syslog"]) { camm_debug("S1.1 Use SYSLOG component"); if (strlen(trim(read_config_option("camm_syslog_pretable_name"))) > 0 && read_config_option("camm_syslog_pretable_name") != "plugin_camm_syslog") { $syslog_use_pretable = true; $syslog_table = '`' . read_config_option("camm_syslog_db_name") . '`.`' . read_config_option("camm_syslog_pretable_name") . '`'; } else { $syslog_use_pretable = false; $syslog_table = '`' . read_config_option("camm_syslog_db_name") . '`.`plugin_camm_syslog`'; } db_execute("UPDATE " . $syslog_table . " set status=1 where status=0"); $stat_ruleDeleSys = mysql_affected_rows(); } else { $stat_ruleDeleSys = 0; } if (sizeof($alerts) > 0) { camm_debug("S1.2 Found " . $stat_ruleDeleTraps . " new trap" . ($stat_ruleDeleTraps == 1 ? "" : "s") . " to process"); camm_debug("S1.2 Found " . $stat_ruleDeleSys . " new syslog message" . ($stat_ruleDeleSys == 1 ? "" : "s") . " to process"); if ($stat_ruleDeleTraps > 0 || $stat_ruleDeleSys > 0) { foreach ($alerts as $alert) { if ($cacti_camm_components[$alert["rule_type"]]) { $rule_rezult = camm_process_rule($alert); } } } else { camm_debug("S1.2 No new records to process rules"); } } else { camm_debug("S1.2 No enabled rules found"); } if ($cacti_camm_components["snmptt"]) { $stat_ruleDeleTraps_end = db_fetch_cell("select count(*) FROM `plugin_camm_snmptt` where status=1"); $camm_stats = sprintf("%s", round($stat_ruleDeleTraps - $stat_ruleDeleTraps_end, 4)); db_execute("REPLACE INTO settings (name,value) VALUES ('camm_stats_ruledel_snmptt', '" . $camm_stats . "')"); db_execute("UPDATE `plugin_camm_snmptt` set status=2 where status=1"); } if ($cacti_camm_components["syslog"]) { $stat_ruleDeleSys_end = db_fetch_cell("select count(*) FROM " . $syslog_table . " where `status`=1"); $camm_stats = sprintf("%s", round($stat_ruleDeleSys - $stat_ruleDeleSys_end, 4)); db_execute("REPLACE INTO settings (name,value) VALUES ('camm_stats_ruledel_syslog', '" . $camm_stats . "')"); db_execute("UPDATE " . $syslog_table . " set status=2 where status=1"); //IF use syslog pre table - need copy processed message to main table if ($syslog_use_pretable) { camm_debug("S1.3 Use syslog pre table. Move processed records to main syslog table"); // db_execute("INSERT INTO `" . read_config_option("camm_syslog_db_name") . "`.`plugin_camm_syslog` " . // "SELECT * FROM " . $syslog_table . " WHERE status=2"); db_execute("INSERT INTO `" . read_config_option("camm_syslog_db_name") . "`.`plugin_camm_syslog` (`host`, `sourceip`, `facility`, `priority`, `sys_date`, `message`, `status`, `alert`) " . "SELECT `host`, `sourceip`, `facility`, `priority`, `sys_date`, `message`, `status`, `alert` FROM " . $syslog_table . " WHERE status=2"); db_execute("DELETE FROM " . $syslog_table . " where `status`=2"); } } }
function camm_execute_rule() { global $cacti_camm_components; $rezult = 1; /* ================= input validation ================= */ camm_input_validate_input_regex(stripslashes(get_request_var_request("id")), "^\\[([0-9]+,?)+\\]\$", "Uncorrect input value"); /* ==================================================== */ //error checking if (is_error_message()) { $rezult = "Input validation error."; } //business logic if ($rezult == 1) { $id = $_POST['id']; // Get our array back and translate it : $id = camm_JDecode(stripslashes($id)); if (sizeof($id) != 1) { $rezult = " no ID."; } else { if (sizeof($id) == 1) { $rule = db_fetch_row("SELECT * FROM `plugin_camm_rule` where `rule_enable`=1 AND `id`='" . $id[0] . "';"); if (isset($rule["id"]) && $rule["id"] == $id[0]) { if ($cacti_camm_components[$rule["rule_type"]]) { $rezult = camm_process_rule($rule, true); } else { $rezult = $rule["rule_type"] . " component disabled."; } } else { $rezult = "Got incorrect rule or it is disabled"; } } else { $rezult = " no ID."; } } } //output if ($rezult == 1) { echo camm_JEncode(array('success' => true)); } else { echo camm_JEncode(array('failure' => true, 'error' => $rezult)); } }