0, 'internal_counter' => 0), array ('id' => $id)); if ($result > 0) { create_event ("Manual validation of alert for ". get_alert_template_description ($alert["id_alert_template"]), $group_id, $agent_id, 1, $config["id_user"], "alert_manual_validation", 1, $alert["id_agent_module"], $id); } elseif ($result === false) { return false; } } return true; } /** * Copy an alert defined in a module agent to other module agent. * * This function avoid duplicated insertion. * * @param int Source agent module id. * @param int Detiny agent module id. * * @return New alert id on success. Existing alert id if it already exists. * False on error. */ function copy_alert_agent_module_to_agent_module ($id_agent_alert, $id_destiny_module) { $alert = get_alert_agent_module ($id_agent_alert); if ($alert === false) return false; $alerts = get_alerts_agent_module ($id_destiny_module, false, array ('id_alert_template' => $alert['id_alert_template'])); if (! empty ($alerts)) { return $alerts[0]['id']; } /* PHP copy arrays on assignment */ $new_alert = array (); $new_alert['id_agent_module'] = $id_destiny_module; $new_alert['id_alert_template'] = $alert['id_alert_template']; $id_new_alert = @process_sql_insert ('talert_template_modules', $new_alert); if ($id_new_alert === false) { return false; } $actions = get_alert_agent_module_actions ($id_agent_alert); if (empty ($actions)) return $id_new_alert; foreach ($actions as $action) { $result = add_alert_agent_module_action ($id_new_alert, $action['id'], array ('fires_min' => $action['fires_min'], 'fires_max' => $action['fires_max'])); if ($result === false) return false; } return $id_new_alert; } /* Compound alerts */ function get_alert_compound_threshold_values () { /* At this moment we don't need different threshold values */ return get_alert_template_threshold_values (); } function get_alert_compound_operations () { $operations = array (); $operations['OR'] = 'OR'; $operations['AND'] = 'AND'; $operations['NOR'] = 'NOR'; $operations['NAND'] = 'NAND'; $operations['NXOR'] = 'NXOR'; return $operations; } function clean_alert_compound_values ($values, $set_empty = true) { $retvalues = array (); if ($set_empty) { $retvalues['description'] = ''; $retvalues['time_threshold'] = 0; $retvalues['max_alerts'] = 0; $retvalues['min_alerts'] = 0; $retvalues['monday'] = 0; $retvalues['tuesday'] = 0; $retvalues['wednesday'] = 0; $retvalues['thursday'] = 0; $retvalues['friday'] = 0; $retvalues['saturday'] = 0; $retvalues['sunday'] = 0; $retvalues['time_from'] = '00:00'; $retvalues['time_to'] = '00:00'; $retvalues['time_threshold'] = '300'; $retvalues['recovery_notify'] = ''; $retvalues['field2_recovery'] = ''; $retvalues['field2_recovery'] = ''; } if (empty ($values)) return $retvalues; if (isset ($values['name'])) $retvalues['name'] = (string) $values['name']; if (isset ($values['description'])) $retvalues['description'] = (string) $values['description']; if (isset ($values['id_agent'])) $retvalues['id_agent'] = (int) $values['id_agent']; if (isset ($values['time_threshold'])) $retvalues['time_threshold'] = (int) $values['time_threshold']; if (isset ($values['max_alerts'])) $retvalues['max_alerts'] = (int) $values['max_alerts']; if (isset ($values['min_alerts'])) $retvalues['min_alerts'] = (int) $values['min_alerts']; /* Ensure max an min orders */ if (isset ($values['min_alerts']) && isset ($values['max_alerts'])) { $max = max ($retvalues['max_alerts'], $retvalues['min_alerts']); $min = min ($retvalues['max_alerts'], $retvalues['min_alerts']); $retvalues['max_alerts'] = $max; $retvalues['min_alerts'] = $min; } if (isset ($values['monday'])) $retvalues['monday'] = (bool) $values['monday']; if (isset ($values['tuesday'])) $retvalues['tuesday'] = (bool) $values['tuesday']; if (isset ($values['wednesday'])) $retvalues['wednesday'] = (bool) $values['wednesday']; if (isset ($values['thursday'])) $retvalues['thursday'] = (bool) $values['thursday']; if (isset ($values['friday'])) $retvalues['friday'] = (bool) $values['friday']; if (isset ($values['saturday'])) $retvalues['saturday'] = (bool) $values['saturday']; if (isset ($values['sunday'])) $retvalues['sunday'] = (bool) $values['sunday']; if (isset ($values['time_from'])) $retvalues['time_from'] = (string) $values['time_from']; if (isset ($values['time_to'])) $retvalues['time_to'] = (string) $values['time_to']; if (isset ($values['time_threshold'])) $retvalues['time_threshold'] = (int) $values['time_threshold']; if (isset ($values['recovery_notify'])) $retvalues['recovery_notify'] = (bool) $values['recovery_notify']; if (isset ($values['field2_recovery'])) $retvalues['field2_recovery'] = (string) $values['field2_recovery']; if (isset ($values['field3_recovery'])) $retvalues['field3_recovery'] = (string) $values['field3_recovery']; return $retvalues; } function create_alert_compound ($name, $id_agent, $values = false) { if (empty ($name)) return false; $values = clean_alert_compound_values ($values); $values['name'] = $name; $values['id_agent'] = $id_agent; return @process_sql_insert ('talert_compound', $values); } function update_alert_compound ($id_alert_compound, $values = false) { if (empty ($id_alert_compound)) return false; $values = clean_alert_compound_values ($values, false); return @process_sql_update ('talert_compound', $values, array ('id' => $id_alert_compound)) !== false; } function delete_alert_compound_elements ($id_alert_compound) { if (empty ($id_alert_compound)) return false; $sql = sprintf ('DELETE FROM talert_compound_elements WHERE id_alert_compound = %d', $id_alert_compound); return @process_sql ($sql); } function add_alert_compound_element ($id_alert_compound, $id_alert_template_module, $operation) { if (empty ($id_alert_compound)) return false; if (empty ($id_alert_template_module)) return false; if (empty ($operation)) return false; $values = array (); $values['id_alert_compound'] = $id_alert_compound; $values['id_alert_template_module'] = $id_alert_template_module; $values['operation'] = $operation; return @process_sql_insert ('talert_compound_elements', $values); } function get_alert_compound ($id_alert_compound) { return get_db_row ('talert_compound', 'id', $id_alert_compound); } function get_alert_compound_actions ($id_alert_compound) { if (empty ($id_alert_compound)) return false; $sql = sprintf ('SELECT id_alert_action id, fires_min, fires_max FROM talert_compound_actions WHERE id_alert_compound = %d', $id_alert_compound); $actions = get_db_all_rows_sql ($sql); if ($actions === false) return array (); $retval = array (); foreach ($actions as $element) { $action = get_alert_action ($element['id']); $action['fires_min'] = $element['fires_min']; $action['fires_max'] = $element['fires_max']; array_push ($retval, $action); } return $retval; } function get_alert_compound_name ($id_alert_compound) { return (string) get_db_value ('name', 'talert_compund', 'id', $id_alert_compound); } function get_alert_compound_elements ($id_alert_compound) { return get_db_all_rows_field_filter ('talert_compound_elements', 'id_alert_compound', $id_alert_compound); } function add_alert_compound_action ($id_alert_compound, $id_alert_action, $options = false) { if (empty ($id_alert_compound)) return false; if (empty ($id_alert_action)) return false; $fires_max = 0; $fires_min = 0; if ($options) { $max = 0; $min = 0; if (isset ($options['fires_max'])) $max = (int) $options['fires_max']; if (isset ($options['fires_min'])) $min = (int) $options['fires_min']; $fires_max = max ($max, $min); $fires_min = min ($max, $min); } $sql = sprintf ('INSERT INTO talert_compound_actions VALUES (%d, %d, %d, %d)', $id_alert_compound, $id_alert_action, $fires_min, $fires_max); return @process_sql ($sql) !== false; } function set_alerts_compound_disable ($id_alert_compound, $disabled) { $id_alert_agent_module = safe_int ($id_alert_compound, 0); $sql = sprintf ('UPDATE talert_compound SET disabled = %d WHERE id = %d', $disabled ? 1 : 0, $id_alert_compound); return @process_sql ($sql) !== false; } /** * Validates a compound alert id or an array of alert id's * * @param mixed Array of compound alert ids or single id * * @return bool True if it was successful, false otherwise. */ function validate_alert_compound ($id_alert_compound) { global $config; require_once ("include/functions_events.php"); $alerts = safe_int ($id_alert_compound, 1); if (empty ($alerts)) { return false; } $alerts = (array) $alerts; foreach ($alerts as $id) { $alert = get_alert_compound ($id); $agent_id = $alert["id_agent"]; $group_id = get_agent_group ($agent_id); if (! give_acl ($config['id_user'], $group_id, "AW")) { continue; } $result = process_sql_update ('talert_compound', array ('times_fired' => 0, 'internal_counter' => 0), array ('id' => $id)); if ($result > 0) { create_event ("Manual validation of compound alert for ". $alert["name"], $group_id, $agent_id, 1, $config["id_user"], "alert_manual_validation", 1, $alert["id"], $id); } elseif ($result === false) { return false; } } return true; } function delete_alert_compound ($id_alert_compound) { $id_alert_compound = safe_int ($id_alert_compound, 1); if (empty ($id_alert_compound)) return false; $sql = sprintf ('DELETE FROM talert_compound WHERE id = %d', $id_alert_compound); return @process_sql ($sql); } ?>