diff --git a/html/includes/forms/sensor-alert-reset.inc.php b/html/includes/forms/sensor-alert-reset.inc.php index aeef2e4e9d..db3591167e 100644 --- a/html/includes/forms/sensor-alert-reset.inc.php +++ b/html/includes/forms/sensor-alert-reset.inc.php @@ -4,6 +4,7 @@ * LibreNMS * * Copyright (c) 2014 Neil Lathwood + * Copyright (c) 2018 TheGreatDoc * * 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 @@ -14,23 +15,42 @@ use LibreNMS\Authentication\LegacyAuth; -header('Content-type: text/plain'); - -// FUA +header('Content-type: application/json'); if (!LegacyAuth::user()->hasGlobalAdmin()) { - die('ERROR: You need to be admin'); + $response = array( + 'status' => 'error', + 'message' => 'Need to be admin', + ); + echo _json_encode($response); + exit; } -for ($x = 0; $x < count($_POST['sensor_id']); $x++) { - dbUpdate( - array( - 'sensor_limit' => set_null($_POST['sensor_limit'][$x], array('NULL')), - 'sensor_limit_low' => set_null($_POST['sensor_limit_low'][$x], array('NULL')), - 'sensor_alert' => set_null($_POST['sensor_alert'][$x], array('NULL')) - ), - 'sensors', - '`sensor_id` = ?', - array($_POST['sensor_id'][$x]) - ); +$status = 'error'; +$message = 'Error resetting values'; +$sensor_limit = $_POST['sensor_limit']; +$sensor_limit_warn = $_POST['sensor_limit_warn']; +$sensor_limit_low = $_POST['sensor_limit_low']; +$sensor_limit_low_warn = $_POST['sensor_limit_low_warn']; +$sensor_alert = $_POST['sensor_alert']; +$sensor_id = $_POST['sensor_id']; +$sensor_count = count($sensor_id); + +if (is_array($sensor_id)) { + for ($x = 0; $x < $sensor_count; $x++) { + if (dbUpdate(array('sensor_limit' => set_null($sensor_limit[$x], array('NULL')), 'sensor_limit_warn' => set_null($sensor_limit_warn[$x], array('NULL')), 'sensor_limit_low_warn' => set_null($sensor_limit_low_warn[$x], array('NULL')), 'sensor_limit_low' => set_null($sensor_limit_low[$x], array('NULL'))), 'sensors', '`sensor_id` = ?', array($sensor_id[$x])) >= 0) { + $message = 'Sensor values resetted'; + $status = 'ok'; + } else { + $message = 'Could not reset sensors values'; + } + } +} else { + $status = 'error'; + $message = 'Invalid sensor id'; } +$response = array( + 'status' => $status, + 'message' => $message +); +echo _json_encode($response); diff --git a/html/includes/forms/sensor-alert-update.inc.php b/html/includes/forms/sensor-alert-update.inc.php index c625faab17..c2272af9eb 100644 --- a/html/includes/forms/sensor-alert-update.inc.php +++ b/html/includes/forms/sensor-alert-update.inc.php @@ -4,6 +4,7 @@ * LibreNMS * * Copyright (c) 2014 Neil Lathwood + * Copyright (c) 2018 TheGreatDoc * * 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 @@ -14,36 +15,51 @@ use LibreNMS\Authentication\LegacyAuth; -header('Content-type: text/plain'); - -// FUA +header('Content-type: application/json'); if (!LegacyAuth::user()->hasGlobalAdmin()) { - die('ERROR: You need to be admin'); + $response = array( + 'status' => 'error', + 'message' => 'Need to be admin', + ); + echo _json_encode($response); + exit; } if (isset($_POST['sub_type']) && !empty($_POST['sub_type'])) { - dbUpdate(array('sensor_custom' => 'No'), 'sensors', '`sensor_id` = ?', array($_POST['sensor_id'])); + $status = 'error'; + $message = 'Error removing custom'; + if (dbUpdate(array('sensor_custom' => 'No'), 'sensors', '`sensor_id` = ?', array($_POST['sensor_id'])) >= 0) { + $status = 'ok'; + $message = 'Custom limit removed. New one will be set up in rediscovery'; + } else { + $message = 'Couldn\'t not remove custom. Enable debug and check logfile'; + } } else { if (!is_numeric($_POST['device_id']) || !is_numeric($_POST['sensor_id'])) { - echo 'error with data'; - exit; + $message = "Invalid device or sensor id"; } else { if ($_POST['state'] == 'true') { $state = 1; + $state_string = "enabled"; } elseif ($_POST['state'] == 'false') { $state = 0; + $state_string = "disabled"; } else { $state = 0; + $state_string = "disabled"; } - - $update = dbUpdate(array('sensor_alert' => $state), 'sensors', '`sensor_id` = ? AND `device_id` = ?', array($_POST['sensor_id'], $_POST['device_id'])); - if (!empty($update) || $update == '0') { - echo 'success'; - exit; + if (dbUpdate(array('sensor_alert' => $state), 'sensors', '`sensor_id` = ? AND `device_id` = ?', array($_POST['sensor_id'], $_POST['device_id'])) >= 0) { + $status = ($state == 0) ? 'info' : 'ok'; + $message = 'Alerts ' . $state_string . ' for sensor ' . $_POST['sensor_desc']; } else { - echo 'error'; - exit; + $status = 'error'; + $message = 'Couldn\'t ' . substr($state_string, 0, -1) . ' alerts for sensor ' . $_POST['sensor_desc'] . '. Enable debug and check librenms.log'; } } } +$response = array( + 'status' => $status, + 'message' => $message +); +echo _json_encode($response); diff --git a/html/includes/forms/sensor-update.inc.php b/html/includes/forms/sensor-update.inc.php index 3a8215bb27..1492cf7374 100644 --- a/html/includes/forms/sensor-update.inc.php +++ b/html/includes/forms/sensor-update.inc.php @@ -4,6 +4,7 @@ * LibreNMS * * Copyright (c) 2014 Neil Lathwood + * Copyright (c) 2018 TheGreatDoc * * 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 @@ -14,29 +15,41 @@ use LibreNMS\Authentication\LegacyAuth; -header('Content-type: text/plain'); - -// FUA +header('Content-type: application/json'); if (!LegacyAuth::user()->hasGlobalAdmin()) { - die('ERROR: You need to be admin'); + $response = array( + 'status' => 'error', + 'message' => 'Need to be admin', + ); + echo _json_encode($response); + exit; } -if (!is_numeric($_POST['device_id']) || !is_numeric($_POST['sensor_id']) || !isset($_POST['data'])) { - echo 'error with data'; - exit; +$status = 'error'; +$message = 'Error updating sensor limit'; +$device_id = $_POST['device_id']; +$sensor_id = $_POST['sensor_id']; +$value_type = $_POST['value_type']; +$data = $_POST['data']; + +if (!is_numeric($device_id)) { + $message = 'Missing device id'; +} elseif (!is_numeric($sensor_id)) { + $message = 'Missing sensor id'; +} elseif (!isset($data)) { + $message = 'Missing data'; } else { - $update = dbUpdate( - array($_POST['value_type'] => set_null($_POST['data'], array('NULL')), 'sensor_custom' => 'Yes'), - 'sensors', - '`sensor_id` = ? AND `device_id` = ?', - array($_POST['sensor_id'], $_POST['device_id']) - ); - if (!empty($update) || $update == '0') { - echo 'success'; - exit; + if (dbUpdate(array($value_type => set_null($data, array('NULL')), 'sensor_custom' => 'Yes'), 'sensors', '`sensor_id` = ? AND `device_id` = ?', array($sensor_id, $device_id)) >= 0) { + $message = 'Sensor value updated'; + $status = 'ok'; } else { - echo 'error'; - exit; + $message = 'Could not update sensor value'; } } + +$response = array( + 'status' => $status, + 'message' => $message, +); +echo _json_encode($response); diff --git a/html/pages/device/edit/sensors-common.php b/html/pages/device/edit/sensors-common.php index 381cd71e45..dd895f7ba3 100644 --- a/html/pages/device/edit/sensors-common.php +++ b/html/pages/device/edit/sensors-common.php @@ -5,6 +5,7 @@ * * Copyright (c) 2014 Neil Lathwood * Copyright (c) 2017 Tony Murray + * Copyright (c) 2018 TheGreatDoc * * 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 @@ -25,6 +26,8 @@ echo "

$title

"; Desc Current High + High warn + Low warn Low Alerts @@ -35,6 +38,8 @@ foreach (dbFetchRows("SELECT * FROM `$table` WHERE `device_id` = ? AND `sensor_d $rollback[] = array( 'sensor_id' => $sensor['sensor_id'], 'sensor_limit' => $sensor['sensor_limit'], + 'sensor_limit_warn' => $sensor['sensor_limit_warn'], + 'sensor_limit_low_warn' => $sensor['sensor_limit_low_warn'], 'sensor_limit_low' => $sensor['sensor_limit_low'], 'sensor_alert' => $sensor['sensor_alert'], ); @@ -59,21 +64,25 @@ foreach (dbFetchRows("SELECT * FROM `$table` WHERE `device_id` = ? AND `sensor_d
- +
+ + +
+ +
+ + +
+
-
- + Clear custom @@ -90,6 +99,8 @@ foreach ($rollback as $reset_data) { echo ' + + '; @@ -101,18 +112,27 @@ foreach ($rollback as $reset_data) {