-
Notifications
You must be signed in to change notification settings - Fork 350
Fix OID macro expansion for Zabbix 7.0+ templates in snmp_zabbix plugin #1351
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
63310c4
Initial plan
Copilot b2c2146
Fix OID macro expansion for Zabbix 7.0+ templates
Copilot a0b344a
Use TrimPrefix/TrimSuffix for more precise macro normalization
Copilot f6f842b
Address code review: require both prefix and suffix for macro normali…
Copilot File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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
|
||
| } | ||
|
|
||
| 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) | ||
| } | ||
| }) | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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
TrimPrefixandTrimSuffixseparately, you can slice the string directly:This is more efficient as it performs a single allocation instead of two string operations.