Files
librenms-librenms/html/includes/modal/edit_alert_transport.inc.php

308 lines
14 KiB
PHP
Raw Normal View History

Added Alert Transports Mapping (#8660) Hello all, I guess this is the second version of a more fully fleshed out alert contact mapping feature. The old one was GH-8507 Transports to convert: - [x] API - [x] Cisco Spark - [x] Elasticsearch - [x] GitLab - [x] Philips Hue - [x] Jira - [x] Mail - [ ] ~~PagerDuty~~ - Requires a callback so leaving for now - [x] Nagios - [x] IRC - [x] Discord - [x] Rocket.chat - [x] Hipchat - [x] Pushover - [x] Boxcar - [x] Telegram - [x] Pushbullet - [x] VictorOps - [x] OpsGenie - [x] Clickatell - [x] PlaySMS - [x] Canopsis - [x] osTicket - [x] Microsoft Teams - [x] SMSEagle - [x] Syslog - [x] Slack The intention is for this feature to have three different levels to it: 1. Alert rule to an alert contact mapping (where the code is at now) 2. Alert rule to an alert group (made up of alert contacts) mapping 3. Alert contact mapping to different transport configurations. There will be three transport configuration types. 1. Default (the configuration that is held in the configs table) 2. None (no transport configuration - will explain later) 3. Other (a configuration that will be defined in a different able) Take Mail transport for example. It can either be of a "default" or "other" configuration. The hope is that in the future, users can send mail from different mail servers if they wish. However, for ciscospark which requires a room ID and an api-token, I've decided that it has no transport configuration. Most likely, every alert contact will contain a different room-id and an api-token - which is why it has the transport config of "none". For other transports : I am not familiar with them, so hopefully the community can add support for these. I can definitely help! To add support for each transport will require several things: - addition to the UI - addition to forms/alert-contacts.inc.php - modifications to its object class Screenshots ![image](https://user-images.githubusercontent.com/28970851/39594533-2092ce9e-4eca-11e8-9c5d-cd002ece1425.png) ![image](https://user-images.githubusercontent.com/28970851/39594544-276e9856-4eca-11e8-80cc-82789ee0b2b2.png) ![image](https://user-images.githubusercontent.com/28970851/39594553-2fdf528c-4eca-11e8-8a40-4f149e767054.png) I'm not sure if this is the best way to do things, so please let me know if there's a better way to structure the code! Any comments on code/db schema,/UI etc is welcome and encouraged! The UI is heavily based on alert rules (front end is not my strong suit). And parts of the code are based on the code that was written for alert rules. DO NOT DELETE THIS TEXT #### Please note > Please read this information carefully. You can run `./scripts/pre-commit.php` to check your code before submitting. - [x] Have you followed our [code guidelines?](http://docs.librenms.org/Developing/Code-Guidelines/) #### Testers If you would like to test this pull request then please run: `./scripts/github-apply <pr_id>`, i.e `./scripts/github-apply 5926`
2018-07-21 13:34:59 -06:00
<?php
/*
* LibreNMS
*
* Copyright (c) 2018 Vivia Nguyen-Tran <vivia@ualberta.ca>
*
* This program is free software: you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
* Free Software Foundation, either version 3 of the License, or (at your
* option) any later version. Please see LICENSE.txt at the top level of
* the source code distribution for details.
*/
use LibreNMS\Authentication\Auth;
use LibreNMS\Config;
if (Auth::user()->hasGlobalAdmin()) {
?>
<!--Modal for adding or updating an alert transport -->
<div class="modal fade" id="edit-alert-transport" tabindex="-1" role="dialog"
aria-labelledby="Edit-transport" aria-hidden="true">
<div class="modal-dialog modal-lg">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
<h5 class="modal-title" id="Edit-transport">Alert Transport :: <a href="https://docs.librenms.org/Alerting/">Docs <i class="fa fa-book fa-1x"></i></a> </h5>
</div>
<div class="modal-body">
<form method="post" role="form" id="transports" class="form-horizontal transports-form">
<input type="hidden" name="transport_id" id="transport_id" value="">
<input type="hidden" name="type" id="type" value="alert-transports">
<div class='form-group' title="The description of this alert transport.">
<label for='name' class='col-sm-3 col-md-2 control-label'>Transport name: </label>
<div class='col-sm-9 col-md-10'>
<input type='text' id='name' name='name' class='form-control validation' maxlength='200' required>
</div>
</div>
<div class="form-group" title="The type of transport.">
<label for='transport-choice' class='col-sm-3 col-md-2 control-label'>Transport type: </label>
<div class="col-sm-3">
<select name='transport-choice' id='transport-choice' class='form-control'>
<option value="api-form">API</option>
<option value="boxcar-form">Boxcar</option>
<option value="canopsis-form">Canopsis</option>
<option value="ciscospark-form">Cisco Spark</option>
<option value="clickatell-form">Clickatell</option>
<option value="discord-form">Discord</option>
<option value="elasticsearch-form">Elasticsearch</option>
<option value="gitlab-form">Gitlab</option>
<option value="hipchat-form">Hipchat</option>
<option value="irc-form">IRC</option>
<option value="jira-form">Jira</option>
<option value="kayako-form">Kayako Classic</option>
Added Alert Transports Mapping (#8660) Hello all, I guess this is the second version of a more fully fleshed out alert contact mapping feature. The old one was GH-8507 Transports to convert: - [x] API - [x] Cisco Spark - [x] Elasticsearch - [x] GitLab - [x] Philips Hue - [x] Jira - [x] Mail - [ ] ~~PagerDuty~~ - Requires a callback so leaving for now - [x] Nagios - [x] IRC - [x] Discord - [x] Rocket.chat - [x] Hipchat - [x] Pushover - [x] Boxcar - [x] Telegram - [x] Pushbullet - [x] VictorOps - [x] OpsGenie - [x] Clickatell - [x] PlaySMS - [x] Canopsis - [x] osTicket - [x] Microsoft Teams - [x] SMSEagle - [x] Syslog - [x] Slack The intention is for this feature to have three different levels to it: 1. Alert rule to an alert contact mapping (where the code is at now) 2. Alert rule to an alert group (made up of alert contacts) mapping 3. Alert contact mapping to different transport configurations. There will be three transport configuration types. 1. Default (the configuration that is held in the configs table) 2. None (no transport configuration - will explain later) 3. Other (a configuration that will be defined in a different able) Take Mail transport for example. It can either be of a "default" or "other" configuration. The hope is that in the future, users can send mail from different mail servers if they wish. However, for ciscospark which requires a room ID and an api-token, I've decided that it has no transport configuration. Most likely, every alert contact will contain a different room-id and an api-token - which is why it has the transport config of "none". For other transports : I am not familiar with them, so hopefully the community can add support for these. I can definitely help! To add support for each transport will require several things: - addition to the UI - addition to forms/alert-contacts.inc.php - modifications to its object class Screenshots ![image](https://user-images.githubusercontent.com/28970851/39594533-2092ce9e-4eca-11e8-9c5d-cd002ece1425.png) ![image](https://user-images.githubusercontent.com/28970851/39594544-276e9856-4eca-11e8-80cc-82789ee0b2b2.png) ![image](https://user-images.githubusercontent.com/28970851/39594553-2fdf528c-4eca-11e8-8a40-4f149e767054.png) I'm not sure if this is the best way to do things, so please let me know if there's a better way to structure the code! Any comments on code/db schema,/UI etc is welcome and encouraged! The UI is heavily based on alert rules (front end is not my strong suit). And parts of the code are based on the code that was written for alert rules. DO NOT DELETE THIS TEXT #### Please note > Please read this information carefully. You can run `./scripts/pre-commit.php` to check your code before submitting. - [x] Have you followed our [code guidelines?](http://docs.librenms.org/Developing/Code-Guidelines/) #### Testers If you would like to test this pull request then please run: `./scripts/github-apply <pr_id>`, i.e `./scripts/github-apply 5926`
2018-07-21 13:34:59 -06:00
<option value="mail-form" selected>Mail</option>
<option value="msteams-form">Microsoft Teams</option>
<option value="nagios-form">Nagios</option>
<option value="opsgenie-form">OpsGenie</option>
<option value="osticket-form">osTicket</option>
<option value="hue-form">Phillips Hue</option>
<option value="playsms-form">PlaySMS</option>
<option value="pushbullet-form">Pushbullet</option>
<option value="pushover-form">Pushover</option>
<option value="rocket-form">Rocket.chat</option>
<option value="slack-form">Slack</option>
<option value="smseagle-form">SMSEagle</option>
<option value="smsfeedback-form">SMSFeedback</option>
Added Alert Transports Mapping (#8660) Hello all, I guess this is the second version of a more fully fleshed out alert contact mapping feature. The old one was GH-8507 Transports to convert: - [x] API - [x] Cisco Spark - [x] Elasticsearch - [x] GitLab - [x] Philips Hue - [x] Jira - [x] Mail - [ ] ~~PagerDuty~~ - Requires a callback so leaving for now - [x] Nagios - [x] IRC - [x] Discord - [x] Rocket.chat - [x] Hipchat - [x] Pushover - [x] Boxcar - [x] Telegram - [x] Pushbullet - [x] VictorOps - [x] OpsGenie - [x] Clickatell - [x] PlaySMS - [x] Canopsis - [x] osTicket - [x] Microsoft Teams - [x] SMSEagle - [x] Syslog - [x] Slack The intention is for this feature to have three different levels to it: 1. Alert rule to an alert contact mapping (where the code is at now) 2. Alert rule to an alert group (made up of alert contacts) mapping 3. Alert contact mapping to different transport configurations. There will be three transport configuration types. 1. Default (the configuration that is held in the configs table) 2. None (no transport configuration - will explain later) 3. Other (a configuration that will be defined in a different able) Take Mail transport for example. It can either be of a "default" or "other" configuration. The hope is that in the future, users can send mail from different mail servers if they wish. However, for ciscospark which requires a room ID and an api-token, I've decided that it has no transport configuration. Most likely, every alert contact will contain a different room-id and an api-token - which is why it has the transport config of "none". For other transports : I am not familiar with them, so hopefully the community can add support for these. I can definitely help! To add support for each transport will require several things: - addition to the UI - addition to forms/alert-contacts.inc.php - modifications to its object class Screenshots ![image](https://user-images.githubusercontent.com/28970851/39594533-2092ce9e-4eca-11e8-9c5d-cd002ece1425.png) ![image](https://user-images.githubusercontent.com/28970851/39594544-276e9856-4eca-11e8-80cc-82789ee0b2b2.png) ![image](https://user-images.githubusercontent.com/28970851/39594553-2fdf528c-4eca-11e8-8a40-4f149e767054.png) I'm not sure if this is the best way to do things, so please let me know if there's a better way to structure the code! Any comments on code/db schema,/UI etc is welcome and encouraged! The UI is heavily based on alert rules (front end is not my strong suit). And parts of the code are based on the code that was written for alert rules. DO NOT DELETE THIS TEXT #### Please note > Please read this information carefully. You can run `./scripts/pre-commit.php` to check your code before submitting. - [x] Have you followed our [code guidelines?](http://docs.librenms.org/Developing/Code-Guidelines/) #### Testers If you would like to test this pull request then please run: `./scripts/github-apply <pr_id>`, i.e `./scripts/github-apply 5926`
2018-07-21 13:34:59 -06:00
<option value="syslog-form">Syslog</option>
<option value="telegram-form">Telegram</option>
<option value="victorops-form">Victorops</option>
<!--Insert more transport type options here has support is added. Value should be: [transport_name]-form -->
</select>
</div>
</div>
<div class="form-group" title="The transport is default.">
<label for="default" class="col-sm-3 col-md-2 control-label">Default Alert: </label>
<div class="col-sm-2">
<input type="checkbox" name="is_default" id="is_default">
</div>
</div>
</form>
<?php
// Fetch list of transport classes
$transport_dir = Config::get('install_dir').'/LibreNMS/Alert/Transport';
$switches = []; // store names of bootstrap switches
foreach (scandir($transport_dir) as $transport) {
$transport = strstr($transport, '.', true);
if (empty($transport)) {
continue;
}
$class = 'LibreNMS\\Alert\\Transport\\'.$transport;
if (!method_exists($class, 'configTemplate')) {
// Skip since support has not been added
continue;
}
echo '<form method="post" role="form" id="'.strtolower($transport).'-form" class="form-horizontal transport">';
echo '<input type="hidden" name="transport-type" id="transport-type" value="'.strtolower($transport).'">';
$tmp = call_user_func($class.'::configTemplate');
foreach ($tmp['config'] as $item) {
echo '<div class="form-group" title="'.$item['descr'].'">';
echo '<label for="'.$item['name'].'" class="col-sm-3 col-md-2 control-label">'.$item['title'].': </label>';
if ($item['type'] == 'text') {
echo '<div class="col-sm-9 col-md-10">';
echo '<input type="'.$item['type'].'" id="'.$item['name'].'" name="'.$item['name'].'" class="form-control" ';
if ($item['required']) {
echo 'required>';
} else {
echo '>';
}
echo '</div>';
} elseif ($item['type'] == 'checkbox') {
echo '<div class="col-sm-2">';
echo '<input type="checkbox" name="'.$item['name'].'" id="'.$item['name'].'">';
echo '</div>';
$switches[$item['name']] = $item['default'];
} elseif ($item['type'] == 'select') {
echo '<div class="col-sm-3">';
echo '<select name="'.$item['name'].'" id="'.$item['name'].'" class="form-control">';
foreach ($item['options'] as $descr => $opt) {
echo '<option value="'.$opt.'">'.$descr.'</option>';
}
echo '</select>';
echo '</div>';
} elseif ($item['type'] === 'textarea') {
echo '<div class="col-sm-9 col-md-10">';
echo '<textarea name="' . $item['name'] . '" id="' . $item['name'] . '" class="form-control" placeholder="'.$item['descr'].'">';
echo '</textarea>';
echo '</div>';
}
echo '</div>';
}
echo '<div class="form-group">';
echo '<div class="col-sm-12 text-center">';
echo '<button type="button" class="btn btn-success btn-save" name="save-transport">';
echo 'Save Transport';
echo '</button>';
echo '</div>';
echo '</div>';
echo '</form>';
}
?>
</div>
</div>
</div>
</div>
<!-- Modal end for adding or updating an alert tramsport-->
<!--Modal for deleting an alert transport -->
<div class="modal fade" id="delete-alert-transport" tabindex="-1" role="dialog"
aria-labelledby="Delete" aria-hidden="true">
<div class="modal-dialog modal-sm">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
<h5 class="modal-title" id="Delete">Confirm Transport Delete</h5>
</div>
<div class="modal-body">
<p>If you would like to remove this alert transport then please click Delete.</p>
</div>
<div class="modal-footer">
<form role="form" class="remove_transport_form">
<button type="button" class="btn btn-default" data-dismiss="modal">Cancel</button>
<button type="submit" class="btn btn-danger danger" id="remove-alert-transport" data-target="remove-alert-transport">Delete</button>
<input type="hidden" name="transport_id" id="delete_transport_id" value="">
<input type="hidden" name="confirm" id="confirm" value="yes">
</form>
</div>
</div>
</div>
</div>
<!--Modal end for deleting an alert transport -->
<script>
// Scripts related to editing/updating alert transports
// Display different form on selection
$("#transport-choice").change(function (){
$(".transport").hide();
$("#" + $(this).val()).show().find("input:text").val("");
});
$("#edit-alert-transport").on("show.bs.modal", function(e) {
// Get transport id of clicked element
var transport_id = $(e.relatedTarget).data("transport_id");
$("#transport_id").val(transport_id);
if(transport_id > 0) {
$.ajax({
type: "POST",
url: "ajax_form.php",
data: { type: "show-alert-transport", transport_id: transport_id },
success: function (data) {
loadTransport(data);
},
error: function () {
toastr.error("Failed to process alert transport");
}
});
} else {
// Resetting to default
$("#name").val("");
$("#transport-choice").val("mail-form");
$(".transport").hide();
$("#" + $("#transport-choice").val()).show().find("input:text").val("");
$("#is_default").bootstrapSwitch('state', false);
// Turn on all switches in form
var switches = <?php echo json_encode($switches);?>;
$.each(switches, function(name, state) {
$("input[name="+name+"]").bootstrapSwitch('state', state);
});
}
});
function loadTransport(transport) {
$("#name").val(transport.name);
$("#transport-choice").val(transport.type+"-form");
$("#is_default").bootstrapSwitch('state', transport.is_default);
$(".transport").hide();
$("#" + $("#transport-choice").val()).show().find("input:text").val("");
// Populate the field values
transport.details.forEach(function(config) {
var $field = $("#" + config.name);
if ($field.prop('type') == 'checkbox') {
$field.bootstrapSwitch('state', config.value);
} else {
$field.val(config.value);
}
});
}
// Save alert transport
$(".btn-save").on("click", function (e) {
e.preventDefault();
//Combine form data (general and transport specific)
data = $("form.transports-form").serializeArray();
data = data.concat($("#" + $("#transport-choice").val()).serializeArray());
if (data !== null) {
//post data to ajax form
$.ajax({
type: "POST",
url: "ajax_form.php",
data: data,
dataType: "json",
success: function (data) {
if (data.status == 'ok') {
toastr.success(data.message);
setTimeout(function (){
$("#edit-alert-transports").modal("hide");
window.location.reload();
}, 500);
} else {
toastr.error(data.message);
}
},
error: function () {
toastr.error("Failed to process alert transport");
}
});
}
});
// Scripts related to deleting an alert transport
// Populate transport id value
$("#delete-alert-transport").on("show.bs.modal", function(event) {
transport_id = $(event.relatedTarget).data("transport_id");
$("#delete_transport_id").val(transport_id);
});
// Delete the alert transport
$("#remove-alert-transport").click('', function(event) {
event.preventDefault();
var transport_id = $("#delete_transport_id").val();
$.ajax({
type: "POST",
url: "ajax_form.php",
data: { type: "delete-alert-transport", transport_id: transport_id },
dataType: "json",
success: function(data) {
if (data.status == 'ok') {
toastr.success(data.message);
$("#alert-transport-" + transport_id).remove();
$("#delete-alert-transport").modal("hide");
} else {
toastr.error(data.message);
}
},
error: function() {
toastr.error("The alert transport could not be deleted.");
}
});
});
</script>
<?php
}