Add service templates

This commit is contained in:
Anthony F McInerney
2020-09-14 13:04:49 +01:00
parent 661a518dea
commit 32b2593cfe
7 changed files with 557 additions and 0 deletions
@@ -0,0 +1,78 @@
<?php
/*
* LibreNMS
*
* Copyright (c) 2016 Aaron Daniels <[email protected]>
*
* 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.
*/
if (Auth::user()->hasGlobalAdmin()) {
?>
<div class="modal fade" id="confirm-delete" 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 Delete</h5>
</div>
<div class="modal-body">
<p>Please confirm that you would like to delete this service template.</p>
</div>
<div class="modal-footer">
<form role="form" class="remove_token_form">
<?php echo csrf_field() ?>
<button type="button" class="btn btn-default" data-dismiss="modal">Cancel</button>
<button type="submit" class="btn btn-danger danger" id="service-template-removal"
data-target="service-template-removal">Delete
</button>
<input type="hidden" name="service_template_id" id="service_template_id" value="">
<input type="hidden" name="confirm" id="confirm" value="yes">
</form>
</div>
</div>
</div>
</div>
<script>
$('#confirm-delete').on('show.bs.modal', function (e) {
service_id = $(e.relatedTarget).data('service_template_id');
$("#service_template_id").val(service_template_id);
});
$('#service-template-removal').click('', function (e) {
e.preventDefault();
var service_template_id = $("#service_template_id").val();
$.ajax({
type: 'POST',
url: 'ajax_form.php',
data: {type: "delete-service-template", service_template_id: service_template_id},
success: function (result) {
if (result.status == 0) {
// Yay.
$('#message').html('<div class="alert alert-info">' + result.message + '</div>');
$("#row_" + service_template_id).remove();
$("#" + service_template_id).remove();
$("#confirm-delete").modal('hide');
}
else {
// Nay.
$("#message").html('<div class="alert alert-danger">' + result.message + '</div>');
$("#confirm-delete").modal('hide');
}
},
error: function () {
$("#message").html('<div class="alert alert-info">An error occurred deleting this service.</div>');
$("#confirm-delete").modal('hide');
}
});
});
</script>
<?php
}
?>
@@ -0,0 +1,169 @@
<?php
/*
* LibreNMS
*
* Copyright (c) 2016 Aaron Daniels <[email protected]>
*
* 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.
*/
if (Auth::user()->hasGlobalAdmin()) {
// Build the types list.
$dir = \LibreNMS\Config::get('nagios_plugins');
if (file_exists($dir) && is_dir($dir)) {
$files = scandir($dir);
$dir .= DIRECTORY_SEPARATOR;
foreach ($files as $file) {
if (is_executable($dir.$file) && is_file($dir.$file) && strstr($file, 'check_')) {
list(,$check_name) = explode('_', $file, 2);
$stype .= "<option value='$check_name'>$check_name</option>";
}
}
}
?>
<div class="modal fade bs-example-modal-sm" id="create-service-template" tabindex="-1" role="dialog" aria-labelledby="Create" aria-hidden="true">
<div class="modal-dialog">
<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="Create">Services</h5>
</div>
<div class="modal-body">
<form method="post" role="form" id="service" class="form-horizontal service-form">
<?php echo csrf_field() ?>
<input type="hidden" name="service_template_id" id="service_template_id" value="">
<input type="hidden" name="device_group_id" id="device_group_id" value="<?php echo $device_group['device_group_id']?>">
<input type="hidden" name="type" id="type" value="create-service-template">
<div class="form-service">
<div class="col-sm-12">
<span id="ajax_response">&nbsp;</span>
</div>
</div>
<div class="form-service row">
<label for='stype' class='col-sm-3 control-label'>Type: </label>
<div class="col-sm-9">
<select id='stype' name='stype' placeholder='type' class='form-control has-feedback'>
<?php echo $stype?>
</select>
</div>
</div>
<div class='form-service row'>
<label for='desc' class='col-sm-3 control-label'>Description: </label>
<div class='col-sm-9'>
<textarea id='desc' name='desc' class='form-control'></textarea>
</div>
</div>
<div class="form-service row">
<label for='ip' class='col-sm-3 control-label'>IP Address: </label>
<div class="col-sm-9">
<input type='text' id='ip' name='ip' class='form-control has-feedback' placeholder='<?php echo $device_group['name']?>'/>
</div>
</div>
<div class="form-service row">
<label for='param' class='col-sm-3 control-label'>Parameters: </label>
<div class="col-sm-9">
<input type='text' id='param' name='param' class='form-control has-feedback' placeholder=''/>
</div>
</div>
<div class="form-service row">
<label for='ignore' class='col-sm-3 control-label'>Ignore alert tag: </label>
<div class="col-sm-9">
<input type='checkbox' id='ignore' name='ignore'>
</div>
</div>
<div class="form-service row">
<label for='disabled' class='col-sm-3 control-label'>Disable polling and alerting: </label>
<div class="col-sm-9">
<input type='checkbox' id='disabled' name='disabled'>
</div>
</div>
<div class="form-service row">
<div class="col-sm-offset-3 col-sm-9">
<button class="btn btn-success btn-sm" type="submit" name="service-submit" id="service-submit" value="save">Save Service</button>
</div>
</div>
<div class="clearfix"></div>
</form>
</div>
</div>
</div>
</div>
<script>
// on-hide
$('#create-service-template').on('hide.bs.modal', function (event) {
$('#stype').val('');
$("#stype").prop("disabled", false);
$('#ip').val('');
$('#desc').val('');
$('#param').val('');
$('#ignore').val('');
$('#disabled').val('');
});
// on-load
$('#create-service-template').on('show.bs.modal', function (e) {
var button = $(e.relatedTarget);
var service_template_id = button.data('service_template_id');
var modal = $(this)
$('#service_template_id').val(service_template_id);
$.ajax({
type: "POST",
url: "ajax_form.php",
data: { type: "parse-service", service_template_id: service_template_id },
dataType: "json",
success: function(output) {
$('#stype').val(output['stype']);
$("#stype").prop("disabled", true);
$('#ip').val(output['ip']);
$('#desc').val(output['desc']);
$('#param').val(output['param']);
$('#ignore').val(output['ignore']);
$('#disabled').val(output['disabled']);
if ($('#ignore').attr('value') == 1) {
$('#ignore').prop("checked", true);
}
if ($('#disabled').attr('value') == 1) {
$('#disabled').prop("checked", true);
}
}
});
});
// on-submit
$('#service-template-submit').click('', function(e) {
e.preventDefault();
$.ajax({
type: "POST",
url: "ajax_form.php",
data: $('form.service-form').serialize(),
success: function(result){
if (result.status == 0) {
// Yay.
$("#create-service-template").modal('hide');
$('#message').html('<div class="alert alert-info">' + result.message + '</div>');
setTimeout(function() {
location.reload(1);
}, 1500);
}
else {
// Nay.
$("#ajax_response").html('<div class="alert alert-danger">'+result.message+'</div>');
}
},
error: function(){
$("#ajax_response").html('<div class="alert alert-info">An error occurred creating this service template.</div>');
}
});
});
</script>
<?php
}
@@ -0,0 +1,40 @@
<?php
$no_refresh = true;
if (!Auth::user()->hasGlobalAdmin()) {
include 'includes/html/error-no-perm.inc.php';
} else {
if ($vars['addsrvtp']) {
if (Auth::user()->hasGlobalAdmin()) {
$updated = '1';
$service_template_id = add_service_template($vars['device_group'], $vars['type'], $vars['descr'], $vars['params'], 0);
if ($service_template_id) {
$message .= $message_break.'Service Template added ('.$service_template_id.')!';
$message_break .= '<br />';
}
}
}
foreach (list_available_services() as $current_service) {
$servicesform .= "<option value='$current_service'>$current_service</option>";
}
foreach (dbFetchRows('SELECT * FROM `device_groups` ORDER BY `name`') as $device_group) {
$devicegroupsform .= "<option value='".$device_group['id']."'>".format_hostname($device_group).'</option>';
}
if ($updated) {
print_message('Device Settings Saved');
}
$pagetitle[] = 'Add service template';
echo "<div class='row'>
<div class='col-sm-6'>";
include_once 'includes/html/print-service-add-template.inc.php';
echo '</div>
</div>';
}//end if
@@ -0,0 +1,156 @@
<?php
/*
* LibreNMS
*
* 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.
*
* @package LibreNMS
* @subpackage webui
* @link http://librenms.org
* @copyright 2017 LibreNMS
* @author LibreNMS Contributors
*/
$pagetitle[] = 'Services';
require_once 'includes/html/modal/new_service_template.inc.php';
require_once 'includes/html/modal/delete_service_template.inc.php';
?>
<div class="container-fluid">
<div class="row">
<div class="panel panel-default">
<div class="panel-heading">
<span style="font-weight: bold;">Services</span> &#187;
<?php
$menu_options = array(
'basic' => 'Basic',
);
if (!$vars['view']) {
$vars['view'] = 'basic';
}
// The menu option - on the left
$sep = '';
foreach ($menu_options as $option => $text) {
if (empty($vars['view'])) {
$vars['view'] = $option;
}
echo $sep;
if ($vars['view'] == $option) {
echo "<span class='pagemenu-selected'>";
}
echo generate_link($text, $vars, array(
'view' => $option
));
if ($vars['view'] == $option) {
echo '</span>';
}
$sep = ' | ';
}
unset($sep);
// The status option - on the right
echo '<div class="pull-right">';
$sep = '';
unset($sep);
echo '</div>';
echo '</div>';
echo '<div style="margin:10px 10px 0px 10px;" id="message"></div>';
echo '<div class="panel-body">';
$sql_param = array();
$host_par = array();
$perms_sql = null;
if (!Auth::user()->hasGlobalRead()) {
$device_group_ids = Permissions::devicesForUser()->toArray() ?: [0];
$perms_sql .= " AND `D`.`device_group_id` IN " .dbGenPlaceholders(count($device_group_ids));
$host_par = $device_group_ids;
}
$host_sql = 'SELECT `D`.`device_group_id`,`D`.`name` FROM device_groups AS D, services_template AS S WHERE D.device_group_id = S.device_group_id ' . $perms_sql . ' GROUP BY `D`.`name`, `D`.`device_group_id`, `D`.`sysName` ORDER BY D.name';
$shift = 1;
foreach (dbFetchRows($host_sql, $host_par) as $device_group) {
$device_group_id = $device_group['device_group_id'];
$device_group_name = $device_group['name'];
#$device_sysName = $device_group['name'];
$devlink = generate_device_link($device_group, null, array('tab' => 'services'));
if ($shift == 1) {
array_unshift($sql_param, $device_group_id);
$shift = 0;
} else {
$sql_param[0] = $device_group_id;
}
$header = true;
$footer = false;
$service_template_iteration = 0;
$services_template = dbFetchRows("SELECT * FROM `services_template` WHERE `device_group_id` = ? $where ORDER BY service_template_type", $sql_param);
$services_template_count = count($services_template);
foreach ($services_template as $service_template) {
$service_template_iteration++;
if ($service_template_iteration < 2 && $header) {
echo '<div class="panel panel-default">';
echo '<div class="panel-heading"><h3 class="panel-title">' . $devlink . '</h3>' . $device_group_name . '</div>';
echo '<div class="panel-body">';
echo '<table class="table table-hover table-condensed">';
echo '<thead>';
echo '<th style="width:1%;max-width:1%;"></th>';
echo '<th style="width:10%;max-width: 10%;">Service</th>';
echo '<th style="width:15%;max-width: 15%;">Last Changed</th>';
echo '<th style="width:15%;max-width: 15%;">Description</th>';
echo '<th >Message</th>';
echo '<th style="width:5%;max-width:5%;"></th>';
echo '</thead>';
}
$header = false;
echo '<tr id="row_' . $service_template['service_template_id'] . '">';
echo '<td><span data-toggle="tooltip" title="' . $title . '" class="alert-status ' . $label . '"></span></td>';
echo '<td>' . nl2br(display($service_template['service_template_type'])) . '</td>';
echo '<td>' . formatUptime(time() - $service_template['service_template_changed']) . '</td>';
echo '<td>' . nl2br(display($service_template['service_template_desc'])) . '</td>';
echo '<td>' . nl2br(display($service_template['service_template_message'])) . '</td>';
if (Auth::user()->hasGlobalAdmin()) {
echo "<td>
<button type='button' class='btn btn-primary btn-sm' aria-label='Edit' data-toggle='modal' data-target='#create-service-template' data-service_template_id='{$service_template['service_template_id']}' name='edit-service-template'><i class='fa fa-pencil' aria-hidden='true'></i></button>
<button type='button' class='btn btn-danger btn-sm' aria-label='Delete' data-toggle='modal' data-target='#confirm-delete' data-service_template_id='{$service_template['service_template_id']}' name='delete-service-template'><i class='fa fa-trash' aria-hidden='true'></i></button>
</td>";
}
echo '</tr>';
if ($service_template_iteration >= $services_template_count) {
$footer = true;
}
if ($footer) {
echo '</table>';
echo '</div>';
echo '</div>';
}
}
}
unset($samehost);
?>
</div>
</div>
</div>
</div>
@@ -0,0 +1,48 @@
<?php
echo "
<h3><span class='label label-success threeqtr-width'>Add Service Template</span></h3>
<form id='addsrv-template' name='addsrv-template' method='post' action='' class='form-horizontal' role='form'>
" . csrf_field() . "
<div class='well well-lg'>
<div class='form-group'>
<input type='hidden' name='addsrv-template' value='yes'>
<label for='device_group' class='col-sm-2 control-label'>Device Group</label>
<div class='col-sm-5'>
<select name='device_group' class='form-control input-sm'>
$devicesform
</select>
</div>
<div class='col-sm-5'>
</div>
</div>
<div class='form-group'>
<label for='type' class='col-sm-2 control-label'>Type</label>
<div class='col-sm-5'>
<select name='type' id='type' class='form-control input-sm'>
$servicesform
</select>
</div>
<div class='col-sm-5'>
</div>
</div>
<div class='form-group'>
<label for='descr' class='col-sm-2 control-label'>Description</label>
<div class='col-sm-5'>
<textarea name='descr' id='descr' class='form-control input-sm' rows='5'></textarea>
</div>
<div class='col-sm-5'>
</div>
</div>
<div class='form-group'>
<label for='params' class='col-sm-2 control-label'>Parameters</label>
<div class='col-sm-5'>
<input name='params' id='params' class='form-control input-sm'>
</div>
<div class='col-sm-5'>
This may be required based on the service check.
</div>
</div>
<button type='submit' name='Submit' class='btn btn-success input-sm'>Add Service Template</button>
</div>
</form>";
+65
View File
@@ -105,6 +105,71 @@ function delete_service($service = null)
return dbDelete('services', '`service_id` = ?', array($service));
}
function add_service_template($device_group, $type, $desc, $param = "", $ignore = 0, $disabled = 0)
{
if (!is_array($device_group)) {
$device_group = device_group_by_id_cache($device_group);
}
$insert = array('device_group_id' => $device_group['device_group_id'], 'service_template_type' => $type, 'service_template_changed' => array('UNIX_TIMESTAMP(NOW())'), 'service_template_desc' => $desc, 'service_template_param' => $param, 'service_template_ignore' => $ignore, 'service_template_disabled' => $disabled);
return dbInsert($insert, 'services_template');
}
function service_template_get($device_group = null, $service = null)
{
$sql_query = "SELECT `service_template_id`,`device_group_id`,`service_template_type`,`service_template_desc`,`service_template_param`,`service_template_ignore`,`service_template_changed`,`service_template_disabled` FROM `services_template` WHERE";
$sql_param = array();
$add = 0;
d_echo("SQL Query: ".$sql_query);
if (!is_null($service)) {
// Add a service filter to the SQL query.
$sql_query .= " `service_template_id` = ? AND";
$sql_param[] = $service;
$add++;
}
if (!is_null($device)) {
// Add a device filter to the SQL query.
$sql_query .= " `device_group_id` = ? AND";
$sql_param[] = $device_group;
$add++;
}
if ($add == 0) {
// No filters, remove " WHERE" -6
$sql_query = substr($sql_query, 0, strlen($sql_query)-6);
} else {
// We have filters, remove " AND" -4
$sql_query = substr($sql_query, 0, strlen($sql_query)-4);
}
d_echo("SQL Query: ".$sql_query);
// $service is not null, get only what we want.
$services = dbFetchRows($sql_query, $sql_param);
d_echo("Service Array: ".print_r($services, true)."\n");
return $services;
}
function edit_service_template($update = array(), $service = null)
{
if (!is_numeric($service)) {
return false;
}
return dbUpdate($update, 'services_template', '`service_template_id`=?', array($service));
}
function delete_service_template($service = null)
{
if (!is_numeric($service)) {
return false;
}
return dbDelete('services_template', '`service_template_id` = ?', array($service));
}
function discover_service($device, $service)
{
if (! dbFetchCell('SELECT COUNT(service_id) FROM `services` WHERE `service_type`= ? AND `device_id` = ?', array($service, $device['device_id']))) {
+1
View File
@@ -0,0 +1 @@
CREATE TABLE IF NOT EXISTS `services_template` ( `service_template_id` int unsigned NOT NULL AUTO_INCREMENT, `device_group_id` int NOT NULL, `service_template_type` varchar(255) NOT NULL, `service_template_desc` text NOT NULL, `service_template_param` text NOT NULL, `service_template_ignore` tinyint(1) NOT NULL, `service_template_changed` int unsigned NOT NULL DEFAULT '0', `service_template_disabled` tinyint(1) NOT NULL DEFAULT '0', PRIMARY KEY (`service_tempate_id`), KEY `service_template_devicegroup` (`device_group_id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE utf8_unicode_ci;