Device Maintenance configurable duration (#11821)

* Device Maintenance configurable duration

* increase hour range

* positionize button

* remove wrench icon
This commit is contained in:
SourceDoctor 2020-06-16 14:13:19 +02:00 committed by GitHub
parent 998cdc39fc
commit 6a0366c1e8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 132 additions and 36 deletions

View File

@ -43,12 +43,12 @@ if ($sub_type == 'new-maintenance') {
$end_recurring_hr = mres($_POST['end_recurring_hr']);
$recurring_day = mres($_POST['recurring_day']);
$start = mres($_POST['start']);
$duration = mres($_POST['duration']);
list($duration_hour, $duration_min) = explode(':', mres($_POST['duration']));
$end = mres($_POST['end']);
$maps = mres($_POST['maps']);
if (!empty($duration)) {
$end = date('Y-m-d H:i:00', strtotime('+'.$duration.' hour', strtotime($start)));
if (isset($duration_hour) && isset($duration_min)) {
$end = date('Y-m-d H:i:00', strtotime('+'.intval($duration_hour).' hour '.intval($duration_min).' minute', strtotime($start)));
}
if (empty($title)) {

View File

@ -0,0 +1,124 @@
<?php
/**
* device_maintenance.inc.php
*
* LibreNMS device maintenance modal
*
* 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.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* @package LibreNMS
* @link http://librenms.org
* @copyright 2020 Thomas Berberich
* @author Thomas Berberich <sourcehhdoctor@gmail.com>
*/
use LibreNMS\Alerting\QueryBuilderParser;
if (!Auth::user()->hasGlobalAdmin()) {
die('ERROR: You need to be admin');
}
$hour_steps = range(0, 23, 1);
$minute_steps = [0, 30];
$exclude_durations = ['0:00'];
$maintenance_duration_list = [];
foreach ($hour_steps as $hour) {
foreach ($minute_steps as $min) {
if (empty($hour) && empty($min)) {
continue;
}
$str_hour = $hour;
$str_min = $min < 10 ? '0'.$min : $min;
$duration = $str_hour.':'.$str_min;
if (in_array($duration, $exclude_durations)) {
continue;
}
$maintenance_duration_list[] = $duration;
}
}
?>
<div class="modal fade" id="device_maintenance_modal" tabindex="-1" role="dialog" aria-labelledby="device_edit" 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="search_alert_rule_list">Device Maintenance</h5>
</div>
<div class="modal-body">
<form method="post" role="form" id="sched-form" class="form-horizontal schedule-maintenance-form">
<?php echo csrf_field() ?>
<div class="form-group">
<label for="notes" class="col-sm-4 control-label">Notes: </label>
<div class="col-sm-8">
<textarea class="form-control" id="notes" name="notes" placeholder="Maintenance notes"></textarea>
</div>
</div>
<div class="form-group">
<label for="duration" class="col-sm-4 control-label">Duration: </label>
<div class="col-sm-8">
<select name='duration' id='duration' class='form-control input-sm'>
<?php foreach ($maintenance_duration_list as $dur) { ?>
<option value='<?=$dur?>'><?=$dur?>h</option>
<?php }?>
</select>
</div>
</div>
<div class="form-group">
<label for="maintenance-submit" class="col-sm-4 control-label"></label>
<div class="col-sm-8">
<button type="submit" id="maintenance-submit" data-device_id="<?php echo($device['device_id']); ?>" <?php echo(\LibreNMS\Alert\AlertUtil::isMaintenance($device['device_id']) ? 'disabled class="btn btn-warning"' : 'class="btn btn-success"')?> name="maintenance-submit">Start Maintenance</button>
</div>
</div>
</form>
</div>
</div>
</div>
</div>
<script>
$("#maintenance-submit").click(function() {
var device_id = $(this).data("device_id");
var title = '<?=display($device['hostname']);?>';
var notes = $('#notes').val();
var recurring = 0;
var start = '<?=date("Y-m-d H:i:00");?>';
var duration = $('#duration').val();
$.ajax({
type: 'POST',
url: 'ajax_form.php',
data: { type: "schedule-maintenance",
sub_type: 'new-maintenance',
title: title,
notes: notes,
recurring: recurring,
start: start,
duration: duration,
maps: [device_id]
},
dataType: "json",
success: function(data){
if(data['status'] == 'ok') {
toastr.success(data['message']);
} else {
toastr.error(data['message']);
}
},
error:function(){
toastr.error('An error occured setting this device into maintenance mode');
}
});
});
</script>

View File

@ -3,6 +3,8 @@
use App\Models\Device;
use App\Models\Location;
require_once 'includes/html/modal/device_maintenance.inc.php';
$device_model = Device::find($device['device_id']);
if ($_POST['editing']) {
@ -269,7 +271,7 @@ if (\LibreNMS\Config::get('distributed_poller') === true) {
<div class="form-group">
<label for="maintenance" class="col-sm-2 control-label"></label>
<div class="col-sm-6">
<button type="submit" id="maintenance" data-device_id="<?php echo($device['device_id']); ?>" <?php echo(\LibreNMS\Alert\AlertUtil::isMaintenance($device['device_id']) ? 'disabled class="btn btn-warning"' : 'class="btn btn-success"')?> name="maintenance"><i class="fa fa-wrench"></i> Maintenance Mode (1 hour)</button>
<button type="button" id="maintenance" data-device_id="<?php echo($device['device_id']); ?>" <?php echo(\LibreNMS\Alert\AlertUtil::isMaintenance($device['device_id']) ? 'disabled class="btn btn-warning"' : 'class="btn btn-success"')?> name="maintenance"><i class="fa fa-wrench"></i> Maintenance Mode</button>
</div>
</div>
@ -307,38 +309,8 @@ If `devices.ignore = 0` or `macros.device = 1` condition is is set and ignore al
<script>
$('[type="checkbox"]').bootstrapSwitch('offColor', 'danger');
$("#maintenance").click(function(event) {
event.preventDefault();
var device_id = $(this).data("device_id");
var title = '<?=display($device['hostname']);?>';
var notes = '';
var recurring = 0;
var start = '<?=date("Y-m-d H:i:00");?>';
var duration = 1;
$.ajax({
type: 'POST',
url: 'ajax_form.php',
data: { type: "schedule-maintenance",
sub_type: 'new-maintenance',
title: title,
notes: notes,
recurring: recurring,
start: start,
duration: duration,
maps: [device_id]
},
dataType: "json",
success: function(data){
if(data['status'] == 'ok') {
toastr.success(data['message']);
} else {
toastr.error(data['message']);
}
},
error:function(){
toastr.error('An error occured setting this device into maintenance mode');
}
});
$("#maintenance").click(function() {
$("#device_maintenance_modal").modal('show');
});
$("#rediscover").click(function() {
var device_id = $(this).data("device_id");