Added user defined titles for alert templates

This commit is contained in:
f0o
2015-08-30 16:53:34 +01:00
parent 629d4f5dda
commit ac7d0c9420
4 changed files with 26 additions and 18 deletions

View File

@@ -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;

View File

@@ -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 {

View File

@@ -51,6 +51,8 @@ if(is_admin() === false) {
<textarea class="form-control" id="template" name="template" rows="15"></textarea><br /><br />
<strong><em>Give your template a name: </em></strong><br />
<input type="text" class="form-control input-sm" id="name" name="name"><br />
<em>Optionally, add custom titles: </em><br />
<input type="text" class="form-control input-sm" id="title" name="title" placeholder="Alert Title"><input type="text" class="form-control input-sm" id="title_rec" name="title_rec" placeholder="Recovery Title"><br />
<span id="error"></span><br />
<button type="button" class="btn btn-primary btn-sm" name="create-template" id="create-template">Create template</button>
</div>

2
sql-schema/066.sql Normal file
View File

@@ -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;