Fix OID macro expansion for Zabbix 7.0+ templates in snmp_zabbix plugin#1351
Conversation
Co-authored-by: kongfei605 <12164762+kongfei605@users.noreply.github.com>
Co-authored-by: kongfei605 <12164762+kongfei605@users.noreply.github.com>
…zation Co-authored-by: kongfei605 <12164762+kongfei605@users.noreply.github.com>
There was a problem hiding this comment.
Pull request overview
This PR fixes a critical bug in the snmp_zabbix plugin where Zabbix 7.0+ templates fail to load due to unexpanded macros in OIDs. The root cause was that macro keys stored with braces (e.g., {#SNMPINDEX}) were being double-wrapped during replacement, searching for non-existent patterns like {#{#SNMPINDEX}}.
Key Changes:
- Introduced macro key normalization to strip existing
{#prefix and}suffix before constructing replacement patterns - Added comprehensive unit tests covering braced/unbraced keys, multiple macros, OIDs, and malformed edge cases
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
| inputs/snmp_zabbix/template.go | Added normalization logic to handle macro keys stored with or without braces, preventing double-wrapping that caused OID expansion failures |
| inputs/snmp_zabbix/template_test.go | New comprehensive test suite covering the reported bug scenario with Zabbix 7.0+ templates, plus edge cases for macro expansion |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| normalizedMacro = strings.TrimPrefix(normalizedMacro, "{#") | ||
| normalizedMacro = strings.TrimSuffix(normalizedMacro, "}") |
There was a problem hiding this comment.
The normalization can be simplified since both conditions are already checked. Instead of calling TrimPrefix and TrimSuffix separately, you can slice the string directly:
if strings.HasPrefix(normalizedMacro, "{#") && strings.HasSuffix(normalizedMacro, "}") {
normalizedMacro = normalizedMacro[2 : len(normalizedMacro)-1]
}This is more efficient as it performs a single allocation instead of two string operations.
| normalizedMacro = strings.TrimPrefix(normalizedMacro, "{#") | |
| normalizedMacro = strings.TrimSuffix(normalizedMacro, "}") | |
| normalizedMacro = normalizedMacro[2 : len(normalizedMacro)-1] |
| name: "malformed macro key only prefix", | ||
| text: "1.3.6.1.2.1.2.2.1.8.{#SNMPINDEX}", | ||
| context: map[string]string{ | ||
| "{#SNMPINDEX": "12345", // Missing closing brace - should not be normalized | ||
| }, | ||
| expected: "1.3.6.1.2.1.2.2.1.8.{#SNMPINDEX}", // No match, macro stays unexpanded | ||
| }, |
There was a problem hiding this comment.
Consider adding a test case for a macro key with only the suffix but missing the prefix (e.g., "SNMPINDEX}": "12345"). This would ensure the normalization logic correctly handles all malformed macro variations, not just the missing closing brace case.
…in (flashcatcloud#1351) * Initial plan * Fix OID macro expansion for Zabbix 7.0+ templates Co-authored-by: kongfei605 <12164762+kongfei605@users.noreply.github.com> * Use TrimPrefix/TrimSuffix for more precise macro normalization Co-authored-by: kongfei605 <12164762+kongfei605@users.noreply.github.com> * Address code review: require both prefix and suffix for macro normalization Co-authored-by: kongfei605 <12164762+kongfei605@users.noreply.github.com> --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: kongfei605 <12164762+kongfei605@users.noreply.github.com>
snmp_zabbix plugin fails with "Invalid object identifier" when loading Zabbix 7.0+ templates. Macros like
{#SNMPINDEX}in OIDs remain unexpanded, causing gosnmp to reject non-numeric OID components.Root Cause
template.ExpandMacrosassumed macro keys in context were bare names (SNMPINDEX), but they're stored with braces ({#SNMPINDEX}). The function searched for{#{#SNMPINDEX}}instead of{#SNMPINDEX}.Changes
{#prefix and}suffix before constructing search patternOriginal prompt
This section details on the original issue you should resolve
<issue_title>Categraf v0.4.29 版本的 snmp_zabbix 插件加载 7.0+ 模版报错不可用</issue_title>
<issue_description>
测试:/opt/categraf/categraf --test --debug --inputs snmp_zabbix
模版:https://git.zabbix.com/projects/ZBX/repos/zabbix/browse/templates/net/huawei_snmp/template_net_huawei_snmp.yaml
报错日志:
SEND Error on the first Request Error: marshal: marshalPDU: unable to marshal varbind list: unable to marshal OID: Invalid object identifier
2025/11/27 11:25:19 collector.go:153: D! bulk request failed for agent udp://172.18.48.2:161 (size: 2): marshal: marshalPDU: unable to marshal varbind list: unable to marshal OID: Invalid object identifier. Falling back to individual collection.
SEND INIT
SEND Error on the first Request Error: marshal: marshalPDU: unable to marshal varbind list: unable to marshal OID: Invalid object identifier
SEND INIT
SEND Error on the first Request Error: marshal: marshalPDU: unable to marshal varbind list: unable to marshal OID: Invalid object identifier
2025/11/27 11:25:19 collector.go:78: D! collected agent: udp://172.18.48.2:161, key: sensor.fan.status[hwEntityFanState.1.7], error: marshal: marshalPDU: unable to marshal varbind list: unable to marshal OID: Invalid object identifier
2025/11/27 11:25:19 collector.go:78: D! collected agent: udp://172.18.48.2:161, key: sensor.fan.status[hwEntityFanState.0.7], error: marshal: marshalPDU: unable to marshal varbind list: unable to marshal OID: Invalid object identifier
SEND INIT
SEND Error on the first Request Error: marshal: marshalPDU: unable to marshal varbind list: unable to marshal OID: Invalid object identifier
2025/11/27 11:25:19 collector.go:153: D! bulk request failed for agent udp://172.18.48.2:161 (size: 2): marshal: marshalPDU: unable to marshal varbind list: unable to marshal OID: Invalid object identifier. Falling back to individual collection.
SEND INIT
SEND Error on the first Request Error: marshal: marshalPDU: unable to marshal varbind list: unable to marshal OID: Invalid object identifier
SEND INIT
SEND Error on the first Request Error: marshal: marshalPDU: unable to marshal varbind list: unable to marshal OID: Invalid object identifier
2025/11/27 11:25:19 collector.go:78: D! collected agent: udp://172.18.48.2:161, key: sensor.temp.value[hwEntityTemperature.68157449], error: marshal: marshalPDU: unable to marshal varbind list: unable to marshal OID: Invalid object identifier
2025/11/27 11:25:19 collector.go:78: D! collected agent: udp://172.18.48.2:161, key: sensor.temp.value[hwEntityTemperature.67108873], error: marshal: marshalPDU: unable to marshal varbind list: unable to marshal OID: Invalid object identifier
SEND INIT
SEND Error on the first Request Error: marshal: marshalPDU: unable to marshal varbind list: unable to marshal OID: Invalid object identifier
2025/11/27 11:25:19 collector.go:153: D! bulk request failed for agent udp://172.18.48.2:161 (size: 6): marshal: marshalPDU: unable to marshal varbind list: unable to marshal OID: Invalid object identifier. Falling back to individual collection.
SEND INIT
SEND Error on the first Request Error: marshal: marshalPDU: unable to marshal varbind list: unable to marshal OID: Invalid object identifier
SEND INIT
SEND Error on the first Request Error: marshal: marshalPDU: unable to marshal varbind list: unable to marshal OID: Invalid object identifier
SEND INIT
SEND Error on the first Request Error: marshal: marshalPDU: unable to marshal varbind list: unable to marshal OID: Invalid object identifier
SEND INIT
SEND Error on the first Request Error: marshal: marshalPDU: unable to marshal varbind list: unable to marshal OID: Invalid object identifier
SEND INIT
SEND Error on the first Request Error: marshal: marshalPDU: unable to marshal varbind list: unable to marshal OID: Invalid object identifier
SEND INIT
SEND Error on the first Request Error: marshal: marshalPDU: unable to marshal varbind list: unable to marshal OID: Invalid object identifier
SEND INIT
SEND Error on the first Request Error: marshal: marshalPDU: unable to marshal varbind list: unable to marshal OID: Invalid object identifier
2025/11/27 11:25:19 collector.go:153: D! bulk request failed for agent udp://172.18.48.2:161 (size: 4): marshal: marshalPDU: unable to marshal varbind list: unable to marshal OID: Invalid object identifier. Falling back to individual collection.
SEND INIT
SEND Error on the first Request Error: marshal: marshalPDU: unable to marshal varbind list: unable to marshal OID: Invalid object identifier
SEND INIT
SEND Error on the first Request Error: marshal: marshalPDU: unable to marshal varbind list: unable to marshal OID: Invalid object identifier
SEND INIT
SEND Error on the first Request Error: marshal: marshalPDU: unable to marshal varbind list: unable to marshal OID: Invalid object identifier
SEND INIT
SEND Error on the first Request Error: marshal: marshalPDU: unable to marshal varbind list: unable to marshal OID: Invalid object ident...
✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.