Skip to content

Fix OID macro expansion for Zabbix 7.0+ templates in snmp_zabbix plugin#1351

Merged
kongfei605 merged 4 commits into
mainfrom
copilot/fix-snmp-zabbix-plugin-error
Dec 1, 2025
Merged

Fix OID macro expansion for Zabbix 7.0+ templates in snmp_zabbix plugin#1351
kongfei605 merged 4 commits into
mainfrom
copilot/fix-snmp-zabbix-plugin-error

Conversation

Copilot AI commented Nov 27, 2025

Copy link
Copy Markdown
Contributor

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.ExpandMacros assumed macro keys in context were bare names (SNMPINDEX), but they're stored with braces ({#SNMPINDEX}). The function searched for {#{#SNMPINDEX}} instead of {#SNMPINDEX}.

// Before: double-wraps the macro
result = strings.ReplaceAll(result, fmt.Sprintf("{#%s}", macro), value)
// With macro = "{#SNMPINDEX}", searches for "{#{#SNMPINDEX}}"

// After: normalize first
if strings.HasPrefix(macro, "{#") && strings.HasSuffix(macro, "}") {
    normalizedMacro = strings.TrimPrefix(macro, "{#")
    normalizedMacro = strings.TrimSuffix(normalizedMacro, "}")
}
result = strings.ReplaceAll(result, fmt.Sprintf("{#%s}", normalizedMacro), value)

Changes

  • template.go: Normalize macro keys by stripping {# prefix and } suffix before constructing search pattern
  • template_test.go: Add unit tests covering braced/unbraced keys, multiple macros, and edge cases
Original 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.

Copilot AI and others added 3 commits November 27, 2025 06:28
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>
Copilot AI changed the title [WIP] Fix SNMP Zabbix plugin loading error for Categraf v0.4.29 Fix OID macro expansion for Zabbix 7.0+ templates in snmp_zabbix plugin Nov 27, 2025
Copilot AI requested a review from kongfei605 November 27, 2025 06:37
@kongfei605 kongfei605 marked this pull request as ready for review November 29, 2025 08:23
Copilot AI review requested due to automatic review settings November 29, 2025 08:23

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment on lines +715 to +716
normalizedMacro = strings.TrimPrefix(normalizedMacro, "{#")
normalizedMacro = strings.TrimSuffix(normalizedMacro, "}")

Copilot AI Nov 29, 2025

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Suggested change
normalizedMacro = strings.TrimPrefix(normalizedMacro, "{#")
normalizedMacro = strings.TrimSuffix(normalizedMacro, "}")
normalizedMacro = normalizedMacro[2 : len(normalizedMacro)-1]

Copilot uses AI. Check for mistakes.
Comment on lines +72 to +78
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
},

Copilot AI Nov 29, 2025

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copilot uses AI. Check for mistakes.
@kongfei605 kongfei605 merged commit f563114 into main Dec 1, 2025
12 of 15 checks passed
@kongfei605 kongfei605 deleted the copilot/fix-snmp-zabbix-plugin-error branch December 1, 2025 01:12
laiwei pushed a commit to laiwei/categraf that referenced this pull request Mar 3, 2026
…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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Categraf v0.4.29 版本的 snmp_zabbix 插件加载 7.0+ 模版报错不可用

3 participants