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
11 changes: 9 additions & 2 deletions inputs/snmp_zabbix/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -707,9 +707,16 @@ func (t *ZabbixTemplate) ExpandMacros(text string, context map[string]string) st
result := text

// 展开上下文宏 (discovery macros)
// Note: macro keys may be stored with or without braces (e.g., "{#SNMPINDEX}" or "SNMPINDEX")
for macro, value := range context {
result = strings.ReplaceAll(result, fmt.Sprintf("{#%s}", macro), value)
result = strings.ReplaceAll(result, fmt.Sprintf("{%s}", macro), value)
// Normalize the macro name by removing the {# prefix and } suffix if present
normalizedMacro := macro
if strings.HasPrefix(normalizedMacro, "{#") && strings.HasSuffix(normalizedMacro, "}") {
normalizedMacro = strings.TrimPrefix(normalizedMacro, "{#")
normalizedMacro = strings.TrimSuffix(normalizedMacro, "}")
Comment on lines +715 to +716

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.
}
result = strings.ReplaceAll(result, fmt.Sprintf("{#%s}", normalizedMacro), value)
result = strings.ReplaceAll(result, fmt.Sprintf("{%s}", normalizedMacro), value)
}

// 展开模板宏 {$MACRO}
Expand Down
89 changes: 89 additions & 0 deletions inputs/snmp_zabbix/template_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
package snmp_zabbix

import "testing"

func TestZabbixTemplate_ExpandMacros(t *testing.T) {
template := &ZabbixTemplate{}

tests := []struct {
name string
text string
context map[string]string
expected string
}{
{
name: "macro with braces in context key",
text: "1.3.6.1.4.1.2011.5.25.31.1.1.10.1.7.{#SNMPINDEX}",
context: map[string]string{
"{#SNMPINDEX}": "67108873",
},
expected: "1.3.6.1.4.1.2011.5.25.31.1.1.10.1.7.67108873",
},
{
name: "macro without braces in context key",
text: "1.3.6.1.2.1.2.2.1.8.{#SNMPINDEX}",
context: map[string]string{
"SNMPINDEX": "12345",
},
expected: "1.3.6.1.2.1.2.2.1.8.12345",
},
{
name: "multiple macros with braces",
text: "{#ENT_NAME}: Temperature ({#SNMPINDEX})",
context: map[string]string{
"{#ENT_NAME}": "MPU Board",
"{#SNMPINDEX}": "67108873",
},
expected: "MPU Board: Temperature (67108873)",
},
{
name: "interface name expansion",
text: "Interface {#IFNAME}({#IFALIAS}): Bits received",
context: map[string]string{
"{#IFNAME}": "eth0",
"{#IFALIAS}": "LAN",
},
expected: "Interface eth0(LAN): Bits received",
},
{
name: "OID with interface index macro",
text: "1.3.6.1.2.1.31.1.1.1.6.{#SNMPINDEX}",
context: map[string]string{
"{#SNMPINDEX}": "1",
"{#IFINDEX}": "1",
},
expected: "1.3.6.1.2.1.31.1.1.1.6.1",
},
{
name: "no macro in text",
text: "1.3.6.1.2.1.1.1.0",
context: map[string]string{
"{#SNMPINDEX}": "1",
},
expected: "1.3.6.1.2.1.1.1.0",
},
{
name: "empty context",
text: "1.3.6.1.2.1.2.2.1.8.{#SNMPINDEX}",
context: map[string]string{},
expected: "1.3.6.1.2.1.2.2.1.8.{#SNMPINDEX}",
},
{
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
},
Comment on lines +72 to +78

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.
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := template.ExpandMacros(tt.text, tt.context)
if result != tt.expected {
t.Errorf("ExpandMacros() = %q, want %q", result, tt.expected)
}
})
}
}
Loading