From b19cf9d5d556e15f7e889e341b76f26d75092440 Mon Sep 17 00:00:00 2001 From: laf Date: Sun, 19 Jul 2015 21:13:06 +0100 Subject: [PATCH] Added boxcar web ui support --- html/forms/config-item.inc.php | 28 ++++- html/forms/update-config-item.inc.php | 6 + html/pages/settings/alerting.inc.php | 151 ++++++++++++++++++++++++++ 3 files changed, 184 insertions(+), 1 deletion(-) diff --git a/html/forms/config-item.inc.php b/html/forms/config-item.inc.php index 8e35c10ccd..084c612316 100644 --- a/html/forms/config-item.inc.php +++ b/html/forms/config-item.inc.php @@ -33,7 +33,7 @@ $config_userkey = mres($_POST['config_userkey']); $status = 'error'; $message = 'Error with config'; -if ($action == 'remove' || $action == 'remove-slack' || $action == 'remove-hipchat' || $action == 'remove-pushover') { +if ($action == 'remove' || $action == 'remove-slack' || $action == 'remove-hipchat' || $action == 'remove-pushover' || $action == 'remove-boxcar') { $config_id = mres($_POST['config_id']); if (empty($config_id)) { $message = 'No config id passed'; @@ -49,6 +49,9 @@ if ($action == 'remove' || $action == 'remove-slack' || $action == 'remove-hipch else if ($action == 'remove-pushover') { dbDelete('config', "`config_name` LIKE 'alert.transports.pushover.$config_id.%'"); } + elseif ($action == 'remove-boxcar') { + dbDelete('config', "`config_name` LIKE 'alert.transports.boxcar.$config_id.%'"); + } $status = 'ok'; $message = 'Config item removed'; @@ -130,6 +133,29 @@ else if ($action == 'add-pushover') { } } } +else if ($action == 'add-boxcar') { + if (empty($config_value)) { + $message = 'No Boxcar access token provided'; + } + else { + $config_id = dbInsert(array('config_name' => 'alert.transports.boxcar.', 'config_value' => $config_value, 'config_group' => $config_group, 'config_sub_group' => $config_sub_group, 'config_default' => $config_value, 'config_descr' => 'Boxcar Transport'), 'config'); + if ($config_id > 0) { + dbUpdate(array('config_name' => 'alert.transports.boxcar.'.$config_id.'.access_token'), 'config', 'config_id=?', array($config_id)); + $status = 'ok'; + $message = 'Config item created'; + $extras = explode('\n', $config_extra); + foreach ($extras as $option) { + list($k,$v) = explode('=', $option, 2); + if (!empty($k) || !empty($v)) { + dbInsert(array('config_name' => 'alert.transports.boxcar.'.$config_id.'.'.$k, 'config_value' => $v, 'config_group' => $config_group, 'config_sub_group' => $config_sub_group, 'config_default' => $v, 'config_descr' => 'Boxcar '.$v), 'config'); + } + } + } + else { + $message = 'Could not create config item'; + } + } +} else { if (empty($config_group) || empty($config_sub_group) || empty($config_name) || empty($config_value)) { $message = 'Missing config name or value'; diff --git a/html/forms/update-config-item.inc.php b/html/forms/update-config-item.inc.php index edcaf8cb00..b15444d9e9 100644 --- a/html/forms/update-config-item.inc.php +++ b/html/forms/update-config-item.inc.php @@ -39,6 +39,9 @@ else if ($action == 'update-textarea') { else if ($config_type == 'pushover') { $db_id[] = dbInsert(array('config_name' => 'alert.transports.pushover.'.$config_id.'.'.$k, 'config_value' => $v, 'config_group' => 'alerting', 'config_sub_group' => 'transports', 'config_default' => $v, 'config_descr' => 'Pushover Transport'), 'config'); } + elseif ($config_type == 'boxcar') { + $db_id[] = dbInsert(array('config_name' => 'alert.transports.boxcar.'.$config_id.'.'.$k, 'config_value' => $v, 'config_group' => 'alerting', 'config_sub_group' => 'transports', 'config_default' => $v, 'config_descr' => 'Boxcar Transport'), 'config'); + } } } @@ -57,6 +60,9 @@ else if ($action == 'update-textarea') { else if ($config_type == 'pushover') { dbDelete('config', "(`config_name` LIKE 'alert.transports.pushover.$config_id.%' AND (`config_name` != 'alert.transports.pushover.$config_id.appkey' AND `config_name` != 'alert.transports.pushover.$config_id.userkey') AND `config_id` NOT IN ($db_inserts))"); } + else if ($config_type == 'boxcar') { + dbDelete('config', "(`config_name` LIKE 'alert.transports.boxcar.$config_id.%' AND (`config_name` != 'alert.transports.boxcar.$config_id.access_token' AND `config_name` != 'alert.transports.boxcar.$config_id.userkey') AND `config_id` NOT IN ($db_inserts))"); + } } $message = 'Config item has been updated:'; diff --git a/html/pages/settings/alerting.inc.php b/html/pages/settings/alerting.inc.php index b04073aca3..9282284eea 100644 --- a/html/pages/settings/alerting.inc.php +++ b/html/pages/settings/alerting.inc.php @@ -145,6 +145,34 @@ $no_refresh = true; + + + + +
+ +
+
+
+
+ +
+
'; +$boxcar_appkeys = get_config_like_name('alert.transports.boxcar.%.access_token'); +foreach ($boxcar_appkeys as $boxcar_appkey) { + unset($upd_boxcar_extra); + $new_boxcar_extra = array(); + $boxcar_extras = get_config_like_name('alert.transports.boxcar.'.$boxcar_appkey['config_id'].'.%'); + foreach ($boxcar_extras as $extra) { + $split_extra = explode('.', $extra['config_name']); + if ($split_extra[4] != 'access_token') { + $new_boxcar_extra[] = $split_extra[4].'='.$extra['config_value']; + } + } + + $upd_boxcar_extra = implode(PHP_EOL, $new_boxcar_extra); + echo '
+
+ +
+ + +
+
+ +
+
+
+
+ + +
+
+
'; +}//end foreach + +echo '
+
+ +
+ + +
+
+ +
+
+
+
+ +
+
+
+
+
+
'; @@ -873,6 +967,42 @@ echo '
}); });// End Add Pushover config + // Add Boxcar config + itemIndex = 0; + $("button#submit-boxcar").click(function(){ + var config_value = $('#boxcar_value').val(); + var config_extra = $('#boxcar_extra').val(); + $.ajax({ + type: "POST", + url: "/ajax_form.php", + data: {type: "config-item", action: 'add-boxcar', config_group: "alerting", config_sub_group: "transports", config_extra: config_extra, config_value: config_value}, + dataType: "json", + success: function(data){ + if (data.status == 'ok') { + itemIndex++; + var $template = $('#boxcar_appkey_template'), + $clone = $template + .clone() + .removeClass('hide') + .attr('id',data.config_id) + .attr('boxcar-appkey-index', itemIndex) + .insertBefore($template); + $clone.find('[id="boxcar_access_token"]').attr('data-config_id',data.config_id); + $clone.find('[id="del-boxcar-call"]').attr('data-config_id',data.config_id); + $clone.find('[name="global-config-input"]').attr('value', config_value); + $clone.find('[id="upd_boxcar_extra"]').val(config_extra); + $clone.find('[id="upd_boxcar_extra"]').attr('data-config_id',data.config_id); + $("#new-config-boxcar").modal('hide'); + } else { + $("#message").html('
' + data.message + '
'); + } + }, + error: function(){ + $("#message").html('
Error creating config item
'); + } + }); + });// End Add Boxcar config + // Delete api config $(document).on('click', 'button[name="del-api-call"]', function(event) { var config_id = $(this).data('config_id'); @@ -957,6 +1087,27 @@ echo '
}); });// End delete pushover config + // Delete Boxcar config + $(document).on('click', 'button[name="del-boxcar-call"]', function(event) { + var config_id = $(this).data('config_id'); + $.ajax({ + type: 'POST', + url: '/ajax_form.php', + data: {type: "config-item", action: 'remove-boxcar', config_id: config_id}, + dataType: "json", + success: function (data) { + if (data.status == 'ok') { + $("#"+config_id).remove(); + } else { + $("#message").html('
' + data.message + '
'); + } + }, + error: function () { + $("#message").html('
An error occurred.
'); + } + }); + });// End delete Boxcar config + $("[name='global-config-check']").bootstrapSwitch('offColor','danger'); $('input[name="global-config-check"]').on('switchChange.bootstrapSwitch', function(event, state) { event.preventDefault();