[ZBXNEXT-9518] Support uneven OID depths in "SNMP walk to JSON" Created: 2024 Oct 02 Updated: 2025 Jan 30 |
|
Status: | Open |
Project: | ZABBIX FEATURE REQUESTS |
Component/s: | Frontend (F), Server (S) |
Affects Version/s: | 7.0.4 |
Fix Version/s: | None |
Type: | New Feature Request | Priority: | Minor |
Reporter: | user185953 | Assignee: | Valdis Murzins |
Resolution: | Unresolved | Votes: | 3 |
Labels: | LLD, SNMP, lld, sensors | ||
Remaining Estimate: | Not Specified | ||
Time Spent: | Not Specified | ||
Original Estimate: | Not Specified |
Description |
While trying to process sensor MIBs (eg ZBXNEXT-9517) I often run into situation where I need data from two tables. $ snmpwalk -On -c foobar -v2c r1.brueve 1.3.6.1.2.1.31.1.1.1.1.4671 .1.3.6.1.2.1.31.1.1.1.1.4671 = STRING: "et-8/0/5" $ snmpwalk -On -c foobar -v2c r1.brueve 1.3.6.1.4.1.2636.3.60.1.2.1.1.6.4671 .1.3.6.1.4.1.2636.3.60.1.2.1.1.6.4671.0 = INTEGER: -230 .1.3.6.1.4.1.2636.3.60.1.2.1.1.6.4671.1 = INTEGER: -146 .1.3.6.1.4.1.2636.3.60.1.2.1.1.6.4671.2 = INTEGER: -209 .1.3.6.1.4.1.2636.3.60.1.2.1.1.6.4671.3 = INTEGER: -265 Notice how table with interface names has one level (4671), but its sensors have two levels (4671.x).
|
Comments |
Comment by user185953 [ 2024 Oct 02 ] | |||||||||
Aaa. I really wish I can edit my issues. Table merged with code after it and I can't fix it
| |||||||||
Comment by user185953 [ 2024 Oct 02 ] | |||||||||
Current output of "SNMP walk to JSON": [ { "{#SNMPINDEX}": "4671.2", "{#SENSOR_VALUE}": "-209" }, { "{#SNMPINDEX}": "4671.1", "{#SENSOR_VALUE}": "-146" }, { "{#SNMPINDEX}": "4671.0", "{#SENSOR_VALUE}": "-230" }, { "{#SNMPINDEX}": "4671", "{#IFNAME}": "et-8/0/5" }, { "{#SNMPINDEX}": "4671.3", "{#SENSOR_VALUE}": "-265" } ] | |||||||||
Comment by user185953 [ 2024 Oct 02 ] | |||||||||
It would be more useful if shorter SNMPINDEX values get expanded and give this: [ { "{#SNMPINDEX}": "4671.2", "{#IFNAME}": "et-8/0/5", "{#SNMPINDEX_ARRAY}": ["4671", "2"], "{#SENSOR_VALUE}": "-209" }, { "{#SNMPINDEX}": "4671.1", "{#IFNAME}": "et-8/0/5", "{#SNMPINDEX_ARRAY}": ["4671", "1"], "{#SENSOR_VALUE}": "-146" }, { "{#SNMPINDEX}": "4671.0", "{#IFNAME}": "et-8/0/5", "{#SNMPINDEX_ARRAY}": ["4671", "0"], "{#SENSOR_VALUE}": "-230" }, { "{#SNMPINDEX}": "4671.3", "{#IFNAME}": "et-8/0/5", "{#SNMPINDEX_ARRAY}": ["4671", "3"], "{#SENSOR_VALUE}": "-265" }, { "{#SNMPINDEX}": "4671", "{#IFNAME}": "et-8/0/5", "{#SNMPINDEX_ARRAY}": ["4671"] } ] | |||||||||
Comment by user185953 [ 2024 Oct 02 ] | |||||||||
Losing information that {#IFNAME} came from shorter OID (not {#SNMPINDEX}) is no problem, But if this turns out to be needed, I propose adding {#SNMPINDEX_ARRAY} that will have javascript array like [4671.3] instead of text like "4671.3". | |||||||||
Comment by user185953 [ 2024 Oct 02 ] | |||||||||
In UI this can work as "expand suffix if needed" checkbox on either on top of "SNMP walk to JSON" step or per each line. Thank you! | |||||||||
Comment by user185953 [ 2024 Oct 11 ] | |||||||||
Here is javascript solution target_depth = 1; // ZBXNEXT-9518 try { var sensors = JSON.parse(value); } catch (e) { throw "Failed to parse JSON of \"SNMP walk to JSON\"."; } byDepth = []; // Sort elements by depth and create SNMPINDEX_ARRAY sensors.forEach(function (element) { index = element['{#SNMPINDEX}']; index_array = index.split('.'); depth = index_array.length; if (byDepth[depth] == null) { byDepth[depth] = {}; } element['{#SNMPINDEX_ARRAY}'] = index_array; byDepth[depth][index] = element; }); result = []; // Go through elements from longest depth to target_depth for(var depth = byDepth.length-1; depth >= target_depth; depth--){ if (byDepth[depth] == null) { continue; } Object.keys(byDepth[depth]).forEach(function (index) { new_element = {}; for(var sub_depth = depth-1, sub_index = index; sub_depth >= 1; sub_depth--){ // Cut away last level from sub_index and merge to new_element sub_index = sub_index.replace(/[.][^.]*$/, "");; if (byDepth[sub_depth] != null) { Object.assign(new_element, byDepth[sub_depth][sub_index]); } } // Merge object itself last for correct SNMPINDEX and SNMPINDEX_ARRAY Object.assign(new_element, byDepth[depth][index]); result.push(new_element); }); } return JSON.stringify(result); | |||||||||
Comment by Ken Buska [ 2025 Jan 30 ] | |||||||||
Thank you for this. I've been looking for a way to reference different SNMP tables but have common indices but perhaps one extends further. I'm working to determine if your JS script works in my case. |