chore(go.d/ddsnmp): use plugincofng for loading profiles (#20878)
Ilya Mashchenko committed
Aug 23, 2025 at 20:42 UTC
512a4a95190d2959f009b641facb7ebf56f3f99b
2 files changed
+25
-52
src/go/plugin/go.d/agent/module/job.go
+1
-1
@@ -214,7 +214,7 @@ func (j *Job) Vnode() vnodes.VirtualNode {
214
func (j *Job) AutoDetection() (err error) {
215
defer func() {
216
if r := recover(); r != nil {
217
- err = fmt.Errorf("panic %v", err)
217
+ err = fmt.Errorf("panic %v", r)
218
j.panicked = true
219
j.disableAutoDetection()
220
src/go/plugin/go.d/collector/snmp/ddsnmp/load.go
+24
-51
@@ -8,6 +8,7 @@ import (
8
"io/fs"
9
"os"
10
"path/filepath"
11
+ "runtime"
12
"slices"
13
"strings"
14
"sync"
@@ -18,6 +19,7 @@ import (
19
"github.com/netdata/netdata/go/plugins/pkg/executable"
20
"github.com/netdata/netdata/go/plugins/pkg/multipath"
21
"github.com/netdata/netdata/go/plugins/plugin/go.d/collector/snmp/ddsnmp/ddprofiledefinition"
22
+ "github.com/netdata/netdata/go/plugins/plugin/go.d/pkg/pluginconfig"
23
)
24
25
var log = logger.New().With("component", "snmp/ddsnmp")
@@ -33,6 +35,7 @@ var (
35
func loadProfiles() {
36
loadOnce.Do(func() {
37
profilesPaths := getProfilesDirs()
38
+ log.Infof("Loading SNMP profiles from %v", profilesPaths)
39
seen := make(map[string]bool)
40
41
for _, dir := range profilesPaths {
@@ -168,56 +171,20 @@ func loadProfileWithExtendsMap(filename string, extendsPaths multipath.MultiPath
171
172
func getProfilesDirs() multipath.MultiPath {
173
if executable.Name == "test" {
171
- dir, _ := filepath.Abs("../../../config/go.d/snmp.profiles/default")
172
- return multipath.New(dir)
173
- }
174
-
175
- var userDir, stockDir string
176
-
177
- if userDir = handleDirOnWin(os.Getenv("NETDATA_USER_CONFIG_DIR")); userDir != "" {
178
- if dir := filepath.Join(userDir, "go.d/snmp.profiles"); isDirExists(dir) {
179
- userDir = dir
180
- }
181
- }
182
- if stockDir = handleDirOnWin(os.Getenv("NETDATA_STOCK_CONFIG_DIR")); stockDir != "" {
183
- if dir := filepath.Join(stockDir, "go.d/snmp.profiles/default"); isDirExists(dir) {
184
- stockDir = dir
185
- }
186
- }
187
-
188
- if userDir != "" || stockDir != "" {
189
- return multipath.New(userDir, stockDir)
174
+ return multipath.New(snmpProfilesDirFromThisFile())
175
}
176
192
- // Development: When running from source (netdata/src/go/plugin/go.d/bin)
193
- // Looks for profiles in the local git repository
177
if dir := filepath.Join(executable.Directory, "../config/go.d/snmp.profiles/default"); isDirExists(dir) {
178
return multipath.New(dir)
179
}
180
198
- possibleDirs := []string{
199
- filepath.Join(executable.Directory, "../../../../etc/netdata/go.d/snmp.profiles"),
200
- // User Standard installation paths
201
- handleDirOnWin("/etc/netdata/go.d/snmp.profiles"),
202
- handleDirOnWin("/opt/netdata/etc/netdata/go.d/snmp.profiles"),
203
-
204
- filepath.Join(executable.Directory, "../../../lib/netdata/conf.d/go.d/snmp.profiles/default"),
205
- // Stock standard installation paths
206
- handleDirOnWin("/usr/lib/netdata/conf.d/go.d/snmp.profiles/default"),
207
- handleDirOnWin("/opt/netdata/usr/lib/netdata/conf.d/go.d/snmp.profiles/default"),
208
- }
209
-
210
- for _, dir := range possibleDirs {
211
- isStock := strings.HasSuffix(filepath.Base(dir), "default")
212
- switch {
213
- case userDir == "" && !isStock && isDirExists(dir):
214
- userDir = dir
215
- case stockDir == "" && isStock && isDirExists(dir):
216
- stockDir = dir
217
- }
181
+ var dirs []string
182
+ for _, dir := range pluginconfig.CollectorsUserDirs() {
183
+ dirs = append(dirs, filepath.Join(dir, "snmp.profiles"))
184
}
185
+ dirs = append(dirs, filepath.Join(pluginconfig.CollectorsStockDir(), "snmp.profiles", "default"))
186
220
- return multipath.New(userDir, stockDir)
187
+ return multipath.New(dirs...)
188
}
189
190
func isDirExists(dir string) bool {
@@ -228,17 +195,23 @@ func isDirExists(dir string) bool {
195
return fi.Mode().IsDir()
196
}
197
231
-func handleDirOnWin(path string) string {
232
- base := os.Getenv("NETDATA_CYGWIN_BASE_PATH")
233
-
234
- // TODO: temp workaround for debug mode
235
- if base == "" && strings.HasPrefix(executable.Directory, "C:\\msys64") {
236
- base = "C:\\msys64"
198
+func snmpProfilesDirFromThisFile() string {
199
+ // runtime.Caller(0) returns the absolute path to THIS .go file at build time.
200
+ _, thisFile, _, ok := runtime.Caller(0)
201
+ if !ok {
202
+ return ""
203
}
204
+ base := filepath.Dir(thisFile)
205
239
- if base == "" || !strings.HasPrefix(path, "/") {
240
- return path
206
+ candidates := []string{
207
+ filepath.Join(base, "..", "..", "..", "config", "go.d", "snmp.profiles", "default"),
208
}
209
243
- return filepath.Join(base, path)
210
+ for _, p := range candidates {
211
+ if isDirExists(p) {
212
+ abs, _ := filepath.Abs(p)
213
+ return abs
214
+ }
215
+ }
216
+ return ""
217
}