From 46899d1643b57245dd154bbc1febd71f128c8ccb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20Gir=C3=B3n?= Date: Fri, 10 Jun 2022 23:24:50 +0200 Subject: [PATCH] Custom OID processing of numeric strings with filters (#13968) * Custom OID processing of numeric strings with filters * Fix syntax * Refactor ajax customoid code --- includes/html/forms/customoid.inc.php | 137 ++++++++++---------------- includes/polling/customoid.inc.php | 10 ++ 2 files changed, 64 insertions(+), 83 deletions(-) diff --git a/includes/html/forms/customoid.inc.php b/includes/html/forms/customoid.inc.php index 454925f128..e3dc73103c 100644 --- a/includes/html/forms/customoid.inc.php +++ b/includes/html/forms/customoid.inc.php @@ -3,15 +3,13 @@ header('Content-type: application/json'); if (! Auth::user()->hasGlobalAdmin()) { - $response = [ + exit(json_encode([ 'status' => 'error', 'message' => 'Need to be admin', - ]; - echo json_encode($response, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE); - exit; + ])); } -$status = 'ok'; +$status = 'error'; $message = ''; $device_id = $_POST['device_id']; @@ -20,51 +18,19 @@ $action = $_POST['action']; $name = strip_tags($_POST['name']); $oid = strip_tags($_POST['oid']); $datatype = strip_tags($_POST['datatype']); -if (empty(($_POST['unit']))) { - $unit = ['NULL']; -} else { +if (! empty(($_POST['unit']))) { $unit = $_POST['unit']; -} -if (! empty(($_POST['limit'])) && is_numeric($_POST['limit'])) { - $limit = $_POST['limit']; } else { - $limit = ['NULL']; -} -if (! empty(($_POST['limit_warn'])) && is_numeric($_POST['limit_warn'])) { - $limit_warn = $_POST['limit_warn']; -} else { - $limit_warn = ['NULL']; -} -if (! empty(($_POST['limit_low'])) && is_numeric($_POST['limit_low'])) { - $limit_low = $_POST['limit_low']; -} else { - $limit_low = ['NULL']; -} -if (! empty(($_POST['limit_low_warn'])) && is_numeric($_POST['limit_low_warn'])) { - $limit_low_warn = $_POST['limit_low_warn']; -} else { - $limit_low_warn = ['NULL']; -} -if ($_POST['alerts'] == 'on') { - $alerts = 1; -} else { - $alerts = 0; -} -if ($_POST['passed'] == 'on') { - $passed = 1; -} else { - $passed = 0; -} -if (! empty(($_POST['divisor'])) && is_numeric($_POST['divisor'])) { - $divisor = $_POST['divisor']; -} else { - $divisor = 1; -} -if (! empty(($_POST['multiplier'])) && is_numeric($_POST['multiplier'])) { - $multiplier = $_POST['multiplier']; -} else { - $multiplier = 1; + $unit = ['NULL']; } +$limit = set_numeric($_POST['limit'], ['NULL']); +$limit_warn = set_numeric($_POST['limit_warn'], ['NULL']); +$limit_low = set_numeric($_POST['limit_low'], ['NULL']); +$limit_low_warn = set_numeric($_POST['limit_low_warn'], ['NULL']); +$alerts = ($_POST['alerts'] == 'on' ? 1 : 0); +$passed = ($_POST['passed'] == 'on' ? 1 : 0); +$divisor = set_numeric($_POST['divisor'], 1); +$multiplier = set_numeric($_POST['multiplier'], 1); if (! empty(($_POST['user_func']))) { $user_func = $_POST['user_func']; } else { @@ -78,6 +44,18 @@ if ($action == 'test') { $rawdata = snmp_get($device, $oid, '-Oqv'); if (is_numeric($rawdata)) { + $oid_value = $rawdata; + } elseif ( + ! empty($_POST['unit']) && + str_i_contains($rawdata, $unit) && + is_numeric(trim(str_replace($unit, '', $rawdata))) + ) { + $oid_value = trim(str_replace($unit, '', $rawdata)); + } elseif (is_numeric(string_to_float($rawdata))) { + $oid_value = string_to_float($rawdata); + } + + if (is_numeric($oid_value)) { if (dbUpdate( [ 'customoid_passed' => 1, @@ -87,12 +65,11 @@ if ($action == 'test') { [$id] ) >= 0) { $message = "Test successful for $name, value $rawdata received"; + $status = 'ok'; } else { - $status = 'error'; $message = "Failed to set pass on OID $name"; } } else { - $status = 'error'; $message = "Invalid data in SNMP reply, value $rawdata received"; } } else { @@ -118,45 +95,39 @@ if ($action == 'test') { [$id] ) >= 0) { //end if condition $message = "Edited OID: $name"; + $status = 'ok'; } else { - $status = 'error'; $message = "Failed to edit OID $name"; } + } elseif (empty($name)) { + $message = 'No OID name provided'; + } elseif (dbFetchCell('SELECT 1 FROM `customoids` WHERE `customoid_descr` = ? AND `device_id`=?', [$name, $device_id])) { + $message = "OID named $name on this device already exists"; } else { - if (empty($name)) { - $status = 'error'; - $message = 'No OID name provided'; + $id = dbInsert( + [ + 'device_id' => $device_id, + 'customoid_descr' => $name, + 'customoid_oid' => $oid, + 'customoid_datatype' => $datatype, + 'customoid_unit' => $unit, + 'customoid_divisor' => $divisor, + 'customoid_multiplier' => $multiplier, + 'customoid_limit' => $limit, + 'customoid_limit_warn' => $limit_warn, + 'customoid_limit_low' => $limit_low, + 'customoid_limit_low_warn' => $limit_low_warn, + 'customoid_alert' => $alerts, + 'customoid_passed' => $passed, + 'user_func' => $user_func, + ], + 'customoids' + ); + if ($id) { + $message = "Added OID: $name"; + $status = 'ok'; } else { - if (dbFetchCell('SELECT 1 FROM `customoids` WHERE `customoid_descr` = ? AND `device_id`=?', [$name, $device_id])) { - $status = 'error'; - $message = "OID named $name on this device already exists"; - } else { - $id = dbInsert( - [ - 'device_id' => $device_id, - 'customoid_descr' => $name, - 'customoid_oid' => $oid, - 'customoid_datatype' => $datatype, - 'customoid_unit' => $unit, - 'customoid_divisor' => $divisor, - 'customoid_multiplier' => $multiplier, - 'customoid_limit' => $limit, - 'customoid_limit_warn' => $limit_warn, - 'customoid_limit_low' => $limit_low, - 'customoid_limit_low_warn' => $limit_low_warn, - 'customoid_alert' => $alerts, - 'customoid_passed' => $passed, - 'user_func' => $user_func, - ], - 'customoids' - ); - if ($id) { - $message = "Added OID: $name"; - } else { - $status = 'error'; - $message = "Failed to add OID: $name"; - } - } + $message = "Failed to add OID: $name"; } } } diff --git a/includes/polling/customoid.inc.php b/includes/polling/customoid.inc.php index 23fd423092..2b0633451b 100644 --- a/includes/polling/customoid.inc.php +++ b/includes/polling/customoid.inc.php @@ -18,6 +18,16 @@ foreach (dbFetchRows('SELECT * FROM `customoids` WHERE `customoid_passed` = 1 AN if (is_numeric($rawdata)) { $os->enableGraph('customoid'); $oid_value = $rawdata; + } elseif ( + $customoid['customoid_unit'] && + str_i_contains($rawdata, $customoid['customoid_unit']) && + is_numeric(trim(str_replace($customoid['customoid_unit'], '', $rawdata))) + ) { + $os->enableGraph('customoid'); + $oid_value = trim(str_replace($customoid['customoid_unit'], '', $rawdata)); + } elseif (is_numeric(string_to_float($rawdata))) { + $os->enableGraph('customoid'); + $oid_value = string_to_float($rawdata); } else { $oid_value = 0; $error = 'Invalid SNMP reply.';