/** * Issue Alert-Object * @param array $alert * @return boolean */ function IssueAlert($alert) { global $config; if (dbFetchCell('SELECT attrib_value FROM devices_attribs WHERE attrib_type = "disable_notify" && device_id = ?', array($alert['device_id'])) == '1') { return true; } if ($config['alert']['fixed-contacts'] == false) { $alert['details']['contacts'] = GetContacts($alert['details']['rule']); } $obj = DescribeAlert($alert); if (is_array($obj)) { echo 'Issuing Alert-UID #' . $alert['id'] . '/' . $alert['state'] . ': '; if (!empty($config['alert']['transports'])) { ExtTransports($obj); } echo "\r\n"; } return true; }
echo "\r\n"; echo ' SQL : ' . GenSQL($rule); echo "\r\n"; echo " ---\r\n"; } ///////////////////////////////////////////////////// echo "End.\r\n\r\n"; ///////////////////////////////// ///////////////////////////////////////////////////// ///////////////////////////////////////////////////// echo "Alert-Object and Formating Test:\r\n"; ///////// ///////////////////////////////////////////////////// $default_tpl = "%title\r\nSeverity: %severity\r\n{if %state == 0}Time elapsed: %elapsed\r\n{/if}Timestamp: %timestamp\r\nUnique-ID: %uid\r\nRule: {if %name}%name{else}%rule{/if}\r\n{if %faults}Faults:\r\n{foreach %faults} {if isset(%value.ifDescr)}%value.ifDescr{else}#%key: %value.string{/if}\r\n{/foreach}{/if}Alert sent to: {foreach %contacts}%value <%key> {/foreach}"; $alert = dbFetchRow("SELECT alert_log.id,alert_log.rule_id,alert_log.device_id,alert_log.state,alert_log.details,alert_log.time_logged,alert_rules.rule,alert_rules.severity,alert_rules.extra,alert_rules.name FROM alert_log,alert_rules WHERE alert_log.rule_id = alert_rules.id && alert_log.device_id = ? && alert_log.rule_id = ? ORDER BY alert_log.id DESC LIMIT 1", array(23, 1)); $alert['details'] = json_decode(gzuncompress($alert['details']), true); echo "Raw-Object:\r\n"; var_dump($alert); echo "----------\r\n"; echo "Post-Processed Object:\r\n"; $obj = DescribeAlert($alert); var_dump($obj); echo "----------\r\n"; echo "Formated Object:\r\n"; $msg = FormatAlertTpl($default_tpl, $obj); var_dump($msg); echo "----------\r\n"; ///////////////////////////////////////////////////// echo "End.\r\n\r\n"; ///////////////////////////////// /////////////////////////////////////////////////////
/** * Issue Alert-Object * @param array $alert * @return boolean */ function IssueAlert($alert) { global $config; if (dbFetchCell('SELECT attrib_value FROM devices_attribs WHERE attrib_type = "disable_notify" && device_id = ?', array($alert['device_id'])) == '1') { return true; } $default_tpl = "%title\r\nSeverity: %severity\r\n{if %state == 0}Time elapsed: %elapsed\r\n{/if}Timestamp: %timestamp\r\nUnique-ID: %uid\r\nRule: {if %name}%name{else}%rule{/if}\r\n{if %faults}Faults:\r\n{foreach %faults} #%key: %value.string\r\n{/foreach}{/if}Alert sent to: {foreach %contacts}%value <%key> {/foreach}"; // FIXME: Put somewhere else? if ($config['alert']['fixed-contacts'] == false) { $alert['details']['contacts'] = GetContacts($alert['details']['rule']); } $obj = DescribeAlert($alert); if (is_array($obj)) { $tpl = dbFetchRow('SELECT `template` FROM `alert_templates` JOIN `alert_template_map` ON `alert_template_map`.`alert_templates_id`=`alert_templates`.`id` WHERE `alert_template_map`.`alert_rule_id`=?', array($alert['rule_id'])); if (isset($tpl['template'])) { $tpl = $tpl['template']; } else { $tpl = $default_tpl; } echo 'Issuing Alert-UID #' . $alert['id'] . '/' . $alert['state'] . ': '; $msg = FormatAlertTpl($tpl, $obj); $obj['msg'] = $msg; if (!empty($config['alert']['transports'])) { ExtTransports($obj); } echo "\r\n"; } return true; }
/** * Run all alerts * @return void */ function RunAlerts() { global $config; $default_tpl = "%title\r\nSeverity: %severity\r\n{if %state == 0}Time elapsed: %elapsed\r\n{/if}Timestamp: %timestamp\r\nUnique-ID: %uid\r\nRule: %rule\r\n{if %faults}Faults:\r\n{foreach %faults} #%key: %value\r\n{/foreach}{/if}Alert sent to: {foreach %contacts}%value <%key> {/foreach}"; //FIXME: Put somewhere else? foreach (dbFetchRows("SELECT alerts.device_id, alerts.rule_id, alerts.state FROM alerts WHERE alerts.state != 2 && alerts.open = 1") as $alert) { $alert = dbFetchRow("SELECT alert_log.id,alert_log.rule_id,alert_log.device_id,alert_log.state,alert_log.details,alert_log.time_logged,alert_rules.rule,alert_rules.severity,alert_rules.extra FROM alert_log,alert_rules WHERE alert_log.rule_id = alert_rules.id && alert_log.device_id = ? && alert_log.rule_id = ? ORDER BY alert_log.id DESC LIMIT 1", array($alert['device_id'], $alert['rule_id'])); $alert['details'] = json_decode(gzuncompress($alert['details']), true); $noiss = false; $noacc = false; $updet = false; $rextra = json_decode($alert['extra'], true); $chk = dbFetchRow('SELECT alerted FROM alerts WHERE device_id = ? && rule_id = ?', array($alert['device_id'], $alert['rule_id'])); if ($chk['alerted'] == $alert['state']) { $noiss = true; } if (!empty($rextra['delay'])) { if (time() - strtotime($alert['time_logged']) < $rextra['delay'] || !empty($alert['details']['delay']) && time() - $alert['details']['delay'] < $rextra['delay']) { continue; } else { $alert['details']['delay'] = time(); $updet = true; } } if ($alert['state'] == 1 && !empty($rextra['count']) && ($rextra['count'] == -1 || $alert['details']['count']++ < $rextra['count'])) { if ($alert['details']['count'] < $rextra['count']) { $noacc = true; } $updet = true; $noiss = false; } if ($updet) { dbUpdate(array('details' => gzcompress(json_encode($alert['details']), 9)), 'alert_log', 'id = ?', array($alert['id'])); } if (!empty($rextra['muted'])) { echo "Muted Alert-UID #" . $alert['id'] . "\r\n"; $noiss = true; } if (!$noiss) { $obj = DescribeAlert($alert); if (is_array($obj)) { $tpl = dbFetchRow('SELECT template FROM alert_templates WHERE rule_id LIKE "%,' . $alert['rule_id'] . ',%"'); if (isset($tpl['template'])) { $tpl = $tpl['template']; } else { $tpl = $default_tpl; } echo "Issuing Alert-UID #" . $alert['id'] . ": "; $msg = FormatAlertTpl($tpl, $obj); $obj['msg'] = $msg; if (!empty($config['alert']['transports'])) { ExtTransports($obj); } echo "\r\n"; dbUpdate(array('alerted' => $alert['state']), 'alerts', 'rule_id = ? && device_id = ?', array($alert['rule_id'], $alert['device_id'])); } } if (!$noacc) { dbUpdate(array('open' => 0), 'alerts', 'rule_id = ? && device_id = ?', array($alert['rule_id'], $alert['device_id'])); } } }