From 19b1e73322524ebd338510966fcc692aa9ef1329 Mon Sep 17 00:00:00 2001 From: Sunnyka <71286632+Sunnyka98@users.noreply.github.com> Date: Thu, 29 Jun 2023 10:45:50 +0200 Subject: [PATCH 1/6] add tracking of memory --- SNMP/form.json | 65 ++++++++++++++++++++--------- SNMP/locale.json | 5 ++- SNMP/module.php | 106 ++++++++++++++++++++++++++++++++--------------- 3 files changed, 123 insertions(+), 53 deletions(-) diff --git a/SNMP/form.json b/SNMP/form.json index b0f9610..7bb0aef 100644 --- a/SNMP/form.json +++ b/SNMP/form.json @@ -185,25 +185,52 @@ ], "actions": [ { - "caption": "Start Walk", - "type": "Button", - "name": "StartWalk", - "visible": true, - "onClick": "SNMP_StartWalkingOIDs($id);" - }, - { - "caption": "Stop Walk", - "type": "Button", - "name": "StopWalk", - "visible": false, - "onClick": "SNMP_StopWalkingOIDs($id);" - }, - { - "caption": "", - "type": "ProgressBar", - "name": "Bar", - "indeterminate": true, - "visible": false + "type": "RowLayout", + "items": [ + { + "caption": "Start Walk", + "type": "PopupButton", + "name": "StartWalk", + "onClick": "SNMP_StartWalkingOIDs($id);", + "popup": { + "buttons": [ + { + "caption": "Cancel", + "type": "Button", + "name": "StopWalk", + "onClick": "SNMP_StopWalkingOIDs($id);" + } + ], + "items": [ + { + "caption": "", + "type": "ProgressBar", + "name": "Bar", + "indeterminate": true + } + ] + } + }, + { + "type": "PopupAlert", + "name": "Alert", + "visible": false, + "popup": { + "items": [ + { + "type": "Label", + "caption": "", + "name": "AlertLabel" + } + ] + } + }, + { + "type": "Label", + "name": "Count", + "caption": "" + } + ] }, { "caption": "OIDs", diff --git a/SNMP/locale.json b/SNMP/locale.json index 55d44f6..13023db 100644 --- a/SNMP/locale.json +++ b/SNMP/locale.json @@ -26,7 +26,10 @@ "seconds": "Sekunden", "Please configure Host before starting a walk!": "Bitte Host konfigurieren bevor ein Walk gestartet wird!", "Walking...": "Führe Walk durch...", - "Walking... %d": "Führe Walk durch... %d" + "Walking... %d": "Führe Walk durch... %d", + "Cancel": "Abbrechen", + "%d OIDs in the list": "%d in der Liste", + "To many OIDs in the OIDLib": "Zu viele OIDs in der OIDLib" } } } \ No newline at end of file diff --git a/SNMP/module.php b/SNMP/module.php index 0d8ec5e..181d8c8 100644 --- a/SNMP/module.php +++ b/SNMP/module.php @@ -119,6 +119,7 @@ public function StartWalkingOIDs() echo $this->Translate('Please configure Host before starting a walk!'); return; } + $this->SetBuffer('IsWalking', 'yes'); $this->UpdateOIDCache(); @@ -127,11 +128,8 @@ public function StartWalkingOIDs() // At this point we have at least some sort of communication going // Update progressbar and buttons - $this->SetBuffer('IsWalking', 'yes'); - $this->UpdateFormField('StartWalk', 'visible', false); - $this->UpdateFormField('StopWalk', 'visible', true); $this->UpdateFormField('Bar', 'caption', $this->Translate('Walking...')); - $this->UpdateFormField('Bar', 'visible', true); + $this->UpdateFormField('Bar', 'indeterminate', true); $this->UpdateFormField('Bar', 'maximum', 1); $this->UpdateFormField('Bar', 'current', 0); @@ -149,9 +147,41 @@ public function StartWalkingOIDs() // Counter for our progressbar $count = 0; + // Get the memory limit of php + $inBytes = function ($memoryLimit): int + { + preg_match('/([0-9]+)[\s]*([a-zA-Z]+)/', $memoryLimit, $matches); + $value = (isset($matches[1])) ? $matches[1] : 0; + $metric = (isset($matches[2])) ? strtolower($matches[2]) : 'b'; + + switch ($metric) { + case 'k': + case 'kb': + $value *= 1024; + break; + case 'm': + case 'mb': + $value *= (1024 ** 2); + break; + case 'g': + case 'gb': + $value *= (1024 ** 3); + break; + case't': + case 'tb': + $value *= (1024 ** 4); + break; + default: + $value = 0; + break; + } + return intval($value); + }; + $memoryMaxSize = $inBytes(ini_get('memory_limit')); + try { - // Keep the walk going until there are no more OIDs left - while ($walk->hasOids()) { + // Keep the walk going until there are no more OIDs left OR the memory usage is on 80 % + while ($walk->hasOids() && (memory_get_usage(true) / $memoryMaxSize < 0.8)) { $this->UpdateFormField('Bar', 'caption', sprintf($this->Translate('Walking... %d'), ++$count)); // Abort walk if IsWalking is set to false @@ -162,23 +192,7 @@ public function StartWalkingOIDs() // Get the next OID in the walk $oid = $walk->next(); - // Create Ident by replacing all dots with underscores - $ident = $this->OIDtoIdent($oid->getOID()); - - // Convert to HEX if it is not a valid UTF-8 value - $value = strval($oid->getValue()); - if (!$this->isValidUTF8($value)) { - $value = bin2hex($value); - } - - $value = [ - 'OID' => $oid->getOID(), - 'Name' => '', - 'Description' => '', - 'Value' => $value, - 'Active' => isset($childrenIdents[$ident]), - 'Writable' => isset($childrenIdents[$ident]) && $childrenIdents[$ident], - ]; + $value = ['OID' => $oid->getOID()]; // Check if we have more details in our OID cache $info = $this->getInformationFromOIDCache($oid->getOID()); @@ -190,21 +204,35 @@ public function StartWalkingOIDs() if ($this->ReadPropertyBoolean('OnlyShowKnownOIDs')) { continue; } + $value['Name'] = ''; + $value['Description'] = ''; + } + + // Create Ident by replacing all dots with underscores + $ident = $this->OIDtoIdent($oid->getOID()); + $value['Active'] = isset($childrenIdents[$ident]); + $value['Writeable'] = isset($childrenIdents[$ident]) && $childrenIdents[$ident]; + + // Convert to HEX if it is not a valid UTF-8 value + $value['Value'] = strval($oid->getValue()); + if (!$this->isValidUTF8($value['Value'])) { + $value['Value'] = bin2hex($value['Value']); } $values[] = $value; } $this->UpdateFormField('OIDList', 'values', json_encode($values)); + + $this->UpdateFormField('Bar', 'indeterminate', false); + $this->UpdateFormField('Bar', 'current', 1); + $this->UpdateFormField('Count', 'caption', sprintf($this->Translate('%d OIDs in the list.'), count($values))); } catch (\Exception $e) { // If we have an issue, display it here (network timeout, etc) echo $e->getMessage(); } $this->SetBuffer('IsWalking', 'no'); - $this->UpdateFormField('Bar', 'visible', false); - $this->UpdateFormField('StartWalk', 'visible', true); - $this->UpdateFormField('StopWalk', 'visible', false); } public function StopWalkingOIDs() @@ -259,18 +287,30 @@ public function UpdateOIDCache() return; } + $startOID = $this->ReadPropertyString('StartAt'); $OIDs = []; foreach ($list as $row) { $xml = simplexml_load_string(base64_decode($row['File'])); foreach ($xml->list->entry as $entry) { - $oid = trim(strval($entry->oid)); - $name = trim(strval($entry->indicator)); - $description = trim(strval($entry->description)); + // Check if the buffer is under 1024 kb + if (strlen(json_encode($OIDs, JSON_FORCE_OBJECT)) * 8 / 1024 > 1024) { + $this->UpdateFormField('Alert', "visible", true); + $this->UpdateFormField('AlertLabel', "caption", $this->Translate('To many OIDs in the OIDLib')); + $this->SetBuffer('IsWalking', 'no'); + break; + } - $OIDs[$oid] = [ - 'Name' => $name, - 'Description' => $description - ]; + //Only add OIDs in the space of the start OID + $oid = trim(strval($entry->oid)); + if (strpos($oid, $startOID) !== false) { + $name = trim(strval($entry->indicator)); + $description = trim(strval($entry->description)); + + $OIDs[$oid] = [ + 'Name' => $name, + 'Description' => $description + ]; + } } } $this->SetBuffer('OIDCache', json_encode($OIDs, JSON_FORCE_OBJECT)); From 0a5f9ccef51db02d20017996f7063048a8f66a94 Mon Sep 17 00:00:00 2001 From: Sunnyka <71286632+Sunnyka98@users.noreply.github.com> Date: Thu, 29 Jun 2023 10:56:36 +0200 Subject: [PATCH 2/6] :cookie: Fix style --- SNMP/module.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/SNMP/module.php b/SNMP/module.php index 181d8c8..f3afe41 100644 --- a/SNMP/module.php +++ b/SNMP/module.php @@ -294,8 +294,8 @@ public function UpdateOIDCache() foreach ($xml->list->entry as $entry) { // Check if the buffer is under 1024 kb if (strlen(json_encode($OIDs, JSON_FORCE_OBJECT)) * 8 / 1024 > 1024) { - $this->UpdateFormField('Alert', "visible", true); - $this->UpdateFormField('AlertLabel', "caption", $this->Translate('To many OIDs in the OIDLib')); + $this->UpdateFormField('Alert', 'visible', true); + $this->UpdateFormField('AlertLabel', 'caption', $this->Translate('To many OIDs in the OIDLib')); $this->SetBuffer('IsWalking', 'no'); break; } From c054d89ccad276241dfc535cf505ecce25fd743d Mon Sep 17 00:00:00 2001 From: Sunnyka <71286632+Sunnyka98@users.noreply.github.com> Date: Mon, 10 Mar 2025 16:20:27 +0100 Subject: [PATCH 3/6] Fix translation --- SNMP/locale.json | 5 +++-- SNMP/module.php | 3 ++- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/SNMP/locale.json b/SNMP/locale.json index 13023db..1bdddb9 100644 --- a/SNMP/locale.json +++ b/SNMP/locale.json @@ -28,8 +28,9 @@ "Walking...": "Führe Walk durch...", "Walking... %d": "Führe Walk durch... %d", "Cancel": "Abbrechen", - "%d OIDs in the list": "%d in der Liste", - "To many OIDs in the OIDLib": "Zu viele OIDs in der OIDLib" + "%d OIDs in the list": "%d OIDs in der Liste", + "To many OIDs in the OIDLib": "Zu viele OIDs in der OIDLib", + "Found %d OIDs": "%d gefundene OIDs" } } } \ No newline at end of file diff --git a/SNMP/module.php b/SNMP/module.php index f3afe41..0338e39 100644 --- a/SNMP/module.php +++ b/SNMP/module.php @@ -221,12 +221,13 @@ public function StartWalkingOIDs() $values[] = $value; } + $this->UpdateFormField('Bar', 'caption', sprintf($this->Translate('Found %d OIDs'), $count)); $this->UpdateFormField('OIDList', 'values', json_encode($values)); $this->UpdateFormField('Bar', 'indeterminate', false); $this->UpdateFormField('Bar', 'current', 1); - $this->UpdateFormField('Count', 'caption', sprintf($this->Translate('%d OIDs in the list.'), count($values))); + $this->UpdateFormField('Count', 'caption', sprintf($this->Translate('%d OIDs in the list'), count($values))); } catch (\Exception $e) { // If we have an issue, display it here (network timeout, etc) echo $e->getMessage(); From d7b830f4a9c2fb7719060b7829812fb019a38841 Mon Sep 17 00:00:00 2001 From: Sunnyka <71286632+Sunnyka98@users.noreply.github.com> Date: Mon, 10 Mar 2025 16:29:15 +0100 Subject: [PATCH 4/6] :cookie: fix style --- SNMP/module.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/SNMP/module.php b/SNMP/module.php index 0338e39..43aadc1 100644 --- a/SNMP/module.php +++ b/SNMP/module.php @@ -167,7 +167,7 @@ public function StartWalkingOIDs() case 'gb': $value *= (1024 ** 3); break; - case't': + case 't': case 'tb': $value *= (1024 ** 4); break; From 20e018b87be0d05a78423c4936995b0a5c240064 Mon Sep 17 00:00:00 2001 From: Sunnyka <71286632+Sunnyka98@users.noreply.github.com> Date: Mon, 30 Jun 2025 09:00:51 +0200 Subject: [PATCH 5/6] hide cancle button if walk is finished --- SNMP/module.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/SNMP/module.php b/SNMP/module.php index 43aadc1..cd800bb 100644 --- a/SNMP/module.php +++ b/SNMP/module.php @@ -132,6 +132,7 @@ public function StartWalkingOIDs() $this->UpdateFormField('Bar', 'indeterminate', true); $this->UpdateFormField('Bar', 'maximum', 1); $this->UpdateFormField('Bar', 'current', 0); + $this->UpdateFormField('StopWalk', 'visible', true); // Create a cache of all previously created Idents for faster "Active?" evaluation $childrenIdents = []; @@ -228,6 +229,7 @@ public function StartWalkingOIDs() $this->UpdateFormField('Bar', 'indeterminate', false); $this->UpdateFormField('Bar', 'current', 1); $this->UpdateFormField('Count', 'caption', sprintf($this->Translate('%d OIDs in the list'), count($values))); + $this->UpdateFormField('StopWalk', 'visible', false); } catch (\Exception $e) { // If we have an issue, display it here (network timeout, etc) echo $e->getMessage(); From 230de072ad11f1333511ed05972d530a382670a4 Mon Sep 17 00:00:00 2001 From: TillBrede <51406030+TillBrede@users.noreply.github.com> Date: Tue, 10 Nov 2026 15:51:00 +0100 Subject: [PATCH 6/6] Stop walk safely near memory limit and fix OID cache handling - Stop the walk at 80 % of the memory limit including the size needed to encode the list, and show a message when that happens - Reset progress bar and cancel button at the start of a walk and always stop the bar when the walk ends (finished, cancelled or failed) - Show connection errors in the progress bar - Abort the walk if the OIDLib cache exceeds 1 MB instead of storing it - Only cache OIDs below the start OID (no partial prefix matches) - Clear the OID cache when all OIDLibs are removed - Fix Writeable/Writable key mismatch so writable variables get an action - Simplify memory_limit parsing and fix "Too many" typo --- SNMP/locale.json | 3 +- SNMP/module.php | 121 ++++++++++++++++++++++++----------------------- 2 files changed, 65 insertions(+), 59 deletions(-) diff --git a/SNMP/locale.json b/SNMP/locale.json index a540051..4b7c0b7 100644 --- a/SNMP/locale.json +++ b/SNMP/locale.json @@ -29,8 +29,9 @@ "Walking... %d": "Führe Walk durch... %d", "Cancel": "Abbrechen", "%d OIDs in the list": "%d OIDs in der Liste", - "To many OIDs in the OIDLib": "Zu viele OIDs in der OIDLib", + "Too many OIDs in the OIDLib": "Zu viele OIDs in der OIDLib", "Found %d OIDs": "%d gefundene OIDs", + "Walk stopped after %d OIDs as the memory limit is almost reached": "Walk nach %d OIDs abgebrochen, da das Speicherlimit fast erreicht ist", "https://www.symcon.de/en/service/documentation/module-reference/snmp/": "https://www.symcon.de/de/service/dokumentation/modulreferenz/snmp/" } } diff --git a/SNMP/module.php b/SNMP/module.php index cd800bb..a43d580 100644 --- a/SNMP/module.php +++ b/SNMP/module.php @@ -121,19 +121,24 @@ public function StartWalkingOIDs() } $this->SetBuffer('IsWalking', 'yes'); - $this->UpdateOIDCache(); - - $snmp = $this->createSNMPClient(); - $walk = $snmp->walk($this->ReadPropertyString('StartAt')); - - // At this point we have at least some sort of communication going - // Update progressbar and buttons + // Reset progressbar and buttons, as the popup still shows the result of the last walk $this->UpdateFormField('Bar', 'caption', $this->Translate('Walking...')); $this->UpdateFormField('Bar', 'indeterminate', true); $this->UpdateFormField('Bar', 'maximum', 1); $this->UpdateFormField('Bar', 'current', 0); $this->UpdateFormField('StopWalk', 'visible', true); + if (!$this->UpdateOIDCache()) { + $this->UpdateFormField('Bar', 'caption', $this->Translate('Too many OIDs in the OIDLib')); + $this->UpdateFormField('Bar', 'indeterminate', false); + $this->UpdateFormField('StopWalk', 'visible', false); + $this->SetBuffer('IsWalking', 'no'); + return; + } + + $snmp = $this->createSNMPClient(); + $walk = $snmp->walk($this->ReadPropertyString('StartAt')); + // Create a cache of all previously created Idents for faster "Active?" evaluation $childrenIdents = []; foreach (IPS_GetChildrenIDs($this->InstanceID) as $id) { @@ -148,50 +153,29 @@ public function StartWalkingOIDs() // Counter for our progressbar $count = 0; - // Get the memory limit of php - $inBytes = function ($memoryLimit): int - { - preg_match('/([0-9]+)[\s]*([a-zA-Z]+)/', $memoryLimit, $matches); - $value = (isset($matches[1])) ? $matches[1] : 0; - $metric = (isset($matches[2])) ? strtolower($matches[2]) : 'b'; - - switch ($metric) { - case 'k': - case 'kb': - $value *= 1024; - break; - case 'm': - case 'mb': - $value *= (1024 ** 2); - break; - case 'g': - case 'gb': - $value *= (1024 ** 3); - break; - case 't': - case 'tb': - $value *= (1024 ** 4); - break; - default: - $value = 0; - break; - } - return intval($value); - }; - $memoryMaxSize = $inBytes(ini_get('memory_limit')); + // Stop the walk at 80 % of the memory limit, so we can still display what we found so far + $memoryLimit = $this->getMemoryLimit(); + $memoryLimitReached = false; - try { - // Keep the walk going until there are no more OIDs left OR the memory usage is on 80 % - while ($walk->hasOids() && (memory_get_usage(true) / $memoryMaxSize < 0.8)) { - $this->UpdateFormField('Bar', 'caption', sprintf($this->Translate('Walking... %d'), ++$count)); + // Encoding the list as JSON at the end needs up to twice its encoded size, so we need to keep that free + $listSize = 0; + try { + // Keep the walk going until there are no more OIDs left + while ($walk->hasOids()) { // Abort walk if IsWalking is set to false if ($this->GetBuffer('IsWalking') != 'yes') { break; } + if (($memoryLimit > 0) && (memory_get_usage(true) + 2 * $listSize > $memoryLimit * 0.8)) { + $memoryLimitReached = true; + break; + } + // Get the next OID in the walk $oid = $walk->next(); + $this->UpdateFormField('Bar', 'caption', sprintf($this->Translate('Walking... %d'), ++$count)); $value = ['OID' => $oid->getOID()]; @@ -212,7 +196,7 @@ public function StartWalkingOIDs() // Create Ident by replacing all dots with underscores $ident = $this->OIDtoIdent($oid->getOID()); $value['Active'] = isset($childrenIdents[$ident]); - $value['Writeable'] = isset($childrenIdents[$ident]) && $childrenIdents[$ident]; + $value['Writable'] = isset($childrenIdents[$ident]) && $childrenIdents[$ident]; // Convert to HEX if it is not a valid UTF-8 value $value['Value'] = strval($oid->getValue()); @@ -221,20 +205,27 @@ public function StartWalkingOIDs() } $values[] = $value; + $listSize += strlen(json_encode($value)); } - $this->UpdateFormField('Bar', 'caption', sprintf($this->Translate('Found %d OIDs'), $count)); $this->UpdateFormField('OIDList', 'values', json_encode($values)); + $this->UpdateFormField('Count', 'caption', sprintf($this->Translate('%d OIDs in the list'), count($values))); - $this->UpdateFormField('Bar', 'indeterminate', false); + if ($memoryLimitReached) { + $this->UpdateFormField('Bar', 'caption', sprintf($this->Translate('Walk stopped after %d OIDs as the memory limit is almost reached'), $count)); + } else { + $this->UpdateFormField('Bar', 'caption', sprintf($this->Translate('Found %d OIDs'), $count)); + } $this->UpdateFormField('Bar', 'current', 1); - $this->UpdateFormField('Count', 'caption', sprintf($this->Translate('%d OIDs in the list'), count($values))); - $this->UpdateFormField('StopWalk', 'visible', false); } catch (\Exception $e) { // If we have an issue, display it here (network timeout, etc) + $this->UpdateFormField('Bar', 'caption', $e->getMessage()); echo $e->getMessage(); } + // Stop the progressbar and hide the cancel button, regardless of how the walk ended + $this->UpdateFormField('Bar', 'indeterminate', false); + $this->UpdateFormField('StopWalk', 'visible', false); $this->SetBuffer('IsWalking', 'no'); } @@ -286,8 +277,11 @@ public function UpdateOIDCache() { $list = json_decode($this->ReadPropertyString('OIDLibs'), true); + // Clear the cache if there are no OIDLibs, so names of removed OIDLibs are not shown anymore if (count($list) == 0) { - return; + $this->SetBuffer('OIDCache', '{}'); + $this->OIDCache = null; + return true; } $startOID = $this->ReadPropertyString('StartAt'); @@ -295,17 +289,9 @@ public function UpdateOIDCache() foreach ($list as $row) { $xml = simplexml_load_string(base64_decode($row['File'])); foreach ($xml->list->entry as $entry) { - // Check if the buffer is under 1024 kb - if (strlen(json_encode($OIDs, JSON_FORCE_OBJECT)) * 8 / 1024 > 1024) { - $this->UpdateFormField('Alert', 'visible', true); - $this->UpdateFormField('AlertLabel', 'caption', $this->Translate('To many OIDs in the OIDLib')); - $this->SetBuffer('IsWalking', 'no'); - break; - } - //Only add OIDs in the space of the start OID $oid = trim(strval($entry->oid)); - if (strpos($oid, $startOID) !== false) { + if (($oid == $startOID) || (strpos($oid, $startOID . '.') === 0)) { $name = trim(strval($entry->indicator)); $description = trim(strval($entry->description)); @@ -316,10 +302,19 @@ public function UpdateOIDCache() } } } - $this->SetBuffer('OIDCache', json_encode($OIDs, JSON_FORCE_OBJECT)); + // Check if the buffer is under 1 MB + $cache = json_encode($OIDs, JSON_FORCE_OBJECT); + if (strlen($cache) > 1024 * 1024) { + $this->UpdateFormField('AlertLabel', 'caption', $this->Translate('Too many OIDs in the OIDLib')); + $this->UpdateFormField('Alert', 'visible', true); + return false; + } + $this->SetBuffer('OIDCache', $cache); // Reset fast internal cache $this->OIDCache = null; + + return true; } public function RequestAction($Ident, $Value) @@ -373,6 +368,16 @@ private function getInformationFromOIDCache($oid) return null; } + private function getMemoryLimit() + { + // The memory_limit is either -1 (unlimited), a number of bytes or a number with a K, M or G suffix + $memoryLimit = trim(ini_get('memory_limit')); + $units = ['k' => 1024, 'm' => 1024 ** 2, 'g' => 1024 ** 3]; + $unit = strtolower(substr($memoryLimit, -1)); + + return max(0, intval($memoryLimit) * ($units[$unit] ?? 1)); + } + private function OIDtoIdent($oid) { return str_replace('.', '_', $oid);