diff --git a/alerts.php b/alerts.php index 8cbeae55d2..b23f96fa02 100755 --- a/alerts.php +++ b/alerts.php @@ -96,24 +96,14 @@ function IssueAlert($alert) { 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); + $msg = FormatAlertTpl($obj); $obj['msg'] = $msg; if (!empty($config['alert']['transports'])) { ExtTransports($obj); @@ -349,12 +339,11 @@ function ExtTransports($obj) { /** * Format Alert - * @param string $tpl Template * @param array $obj Alert-Array * @return string */ -function FormatAlertTpl($tpl, $obj) { - $msg = '$ret .= "'.str_replace(array('{else}', '{/if}', '{/foreach}'), array('"; } else { $ret .= "', '"; } $ret .= "', '"; } $ret .= "'), addslashes($tpl)).'";'; +function FormatAlertTpl($obj) { + $msg = '$ret .= "'.str_replace(array('{else}', '{/if}', '{/foreach}'), array('"; } else { $ret .= "', '"; } $ret .= "', '"; } $ret .= "'), addslashes($obj["template"])).'";'; $parsed = $msg; $s = strlen($msg); $x = $pos = -1; @@ -426,11 +415,21 @@ function DescribeAlert($alert) { $obj = array(); $i = 0; $device = dbFetchRow('SELECT hostname FROM devices WHERE device_id = ?', array($alert['device_id'])); + $tpl = dbFetchRow('SELECT `template`,`title`,`title_rec` 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'])); + $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}"; $obj['hostname'] = $device['hostname']; $obj['device_id'] = $alert['device_id']; $extra = $alert['details']; + if (!isset($tpl['template'])) { + $tpl['template'] = $default_tpl; + } if ($alert['state'] >= 1) { - $obj['title'] = 'Alert for device '.$device['hostname'].' - '.($alert['name'] ? $alert['name'] : $alert['rule']); + if (!empty($tpl['title'])) { + $obj['title'] = $tpl['title']; + } + else { + $obj['title'] = 'Alert for device '.$device['hostname'].' - '.($alert['name'] ? $alert['name'] : $alert['rule']); + } if ($alert['state'] == 2) { $obj['title'] .= ' got acknowledged'; } @@ -458,7 +457,12 @@ function DescribeAlert($alert) { } $extra = json_decode(gzuncompress($id['details']), true); - $obj['title'] = 'Device '.$device['hostname'].' recovered from '.($alert['name'] ? $alert['name'] : $alert['rule']); + if (!empty($tpl['title_rec'])) { + $obj['title'] = $tpl['title_rec']; + } + else { + $obj['title'] = 'Device '.$device['hostname'].' recovered from '.($alert['name'] ? $alert['name'] : $alert['rule']); + } $obj['elapsed'] = TimeFormat(strtotime($alert['time_logged']) - strtotime($id['time_logged'])); $obj['id'] = $id['id']; $obj['faults'] = false; diff --git a/html/includes/forms/alert-templates.inc.php b/html/includes/forms/alert-templates.inc.php index 67182b1f2e..74fda1b8c1 100644 --- a/html/includes/forms/alert-templates.inc.php +++ b/html/includes/forms/alert-templates.inc.php @@ -52,7 +52,7 @@ if(!empty($name)) { elseif( $_REQUEST['template'] && is_numeric($_REQUEST['template_id']) ) { //Update template-text - if($ret = dbUpdate(array('template' => $_REQUEST['template'], 'name' => $name), "alert_templates", "id = ?", array($_REQUEST['template_id']))) { + if($ret = dbUpdate(array('template' => $_REQUEST['template'], 'name' => $name, 'title' => $_REQUEST['title'], 'title_rec' => $_REQUEST['title_rec']), "alert_templates", "id = ?", array($_REQUEST['template_id']))) { $ok = "Updated template"; } else { @@ -62,7 +62,7 @@ if(!empty($name)) { elseif( $_REQUEST['template'] ) { //Create new template - if(dbInsert(array('template' => $_REQUEST['template'], 'name' => $name), "alert_templates")) { + if(dbInsert(array('template' => $_REQUEST['template'], 'name' => $name, 'title' => $_REQUEST['title'], 'title_rec' => $_REQUEST['title_rec']), "alert_templates")) { $ok = "Alert template has been created."; } else { diff --git a/html/includes/modal/alert_template.inc.php b/html/includes/modal/alert_template.inc.php index 71574ba6f7..dceab82cff 100644 --- a/html/includes/modal/alert_template.inc.php +++ b/html/includes/modal/alert_template.inc.php @@ -51,6 +51,8 @@ if(is_admin() === false) {

Give your template a name:

+ Optionally, add custom titles:
+

diff --git a/sql-schema/066.sql b/sql-schema/066.sql new file mode 100644 index 0000000000..37b4dc6ede --- /dev/null +++ b/sql-schema/066.sql @@ -0,0 +1,2 @@ +ALTER TABLE `alert_templates` ADD `title` VARCHAR(255) NULL DEFAULT NULL; +ALTER TABLE `alert_templates` ADD `title_rec` VARCHAR(255) NULL DEFAULT NULL;