Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 46 additions & 19 deletions SNMP/form.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Comment thread
TillBrede marked this conversation as resolved.
Resolved
"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",
Expand Down
5 changes: 5 additions & 0 deletions SNMP/locale.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@
"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",
"Cancel": "Abbrechen",
"%d OIDs in the list": "%d OIDs in der Liste",
"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/"
}
}
Expand Down
132 changes: 90 additions & 42 deletions SNMP/module.php
Original file line number Diff line number Diff line change
Expand Up @@ -119,21 +119,25 @@ public function StartWalkingOIDs()
echo $this->Translate('Please configure Host before starting a walk!');
return;
}

$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
$this->SetBuffer('IsWalking', 'yes');
$this->UpdateFormField('StartWalk', 'visible', false);
$this->UpdateFormField('StopWalk', 'visible', true);

// 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', 'visible', true);
$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 = [];
Expand All @@ -149,36 +153,31 @@ public function StartWalkingOIDs()
// Counter for our progressbar
$count = 0;

// Stop the walk at 80 % of the memory limit, so we can still display what we found so far
$memoryLimit = $this->getMemoryLimit();
$memoryLimitReached = false;

// 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()) {
$this->UpdateFormField('Bar', 'caption', sprintf($this->Translate('Walking... %d'), ++$count));

// 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));

// 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());
Expand All @@ -190,21 +189,44 @@ 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['Writable'] = 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;
$listSize += strlen(json_encode($value));
}

$this->UpdateFormField('OIDList', 'values', json_encode($values));
$this->UpdateFormField('Count', 'caption', sprintf($this->Translate('%d OIDs in the list'), count($values)));

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);
} catch (\Exception $e) {
// If we have an issue, display it here (network timeout, etc)
$this->UpdateFormField('Bar', 'caption', $e->getMessage());
echo $e->getMessage();
}

$this->SetBuffer('IsWalking', 'no');
$this->UpdateFormField('Bar', 'visible', false);
$this->UpdateFormField('StartWalk', 'visible', true);
// 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');
}

public function StopWalkingOIDs()
Expand Down Expand Up @@ -255,28 +277,44 @@ 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');
$OIDs = [];
foreach ($list as $row) {
$xml = simplexml_load_string(base64_decode($row['File']));
foreach ($xml->list->entry as $entry) {
//Only add OIDs in the space of the start OID
$oid = trim(strval($entry->oid));
$name = trim(strval($entry->indicator));
$description = trim(strval($entry->description));

$OIDs[$oid] = [
'Name' => $name,
'Description' => $description
];
if (($oid == $startOID) || (strpos($oid, $startOID . '.') === 0)) {
$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));
// 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)
Expand Down Expand Up @@ -330,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);
Expand Down
Loading