fix(go.d/snmp/ddsnmp): correct profile directory path (#19887)
Ilya Mashchenko committed
Mar 17, 2025 at 21:37 UTC
a82ba6f9681bffd9ff17e33263ba2b54032071fd
4 files changed
+40
-37
src/go/plugin/go.d/agent/agent.go
+1
@@ -21,6 +21,7 @@ import (
21
"github.com/netdata/netdata/go/plugins/plugin/go.d/agent/functions"
22
"github.com/netdata/netdata/go/plugins/plugin/go.d/agent/jobmgr"
23
"github.com/netdata/netdata/go/plugins/plugin/go.d/agent/module"
24
+ _ "github.com/netdata/netdata/go/plugins/plugin/go.d/collector/snmp/ddsnmp"
25
26
"github.com/mattn/go-isatty"
27
)
src/go/plugin/go.d/collector/snmp/ddsnmp/load.go
+39
-1
@@ -3,6 +3,7 @@
3
package ddsnmp
4
5
import (
6
+ "errors"
7
"io/fs"
8
"os"
9
"path/filepath"
@@ -11,15 +12,44 @@ import (
12
"gopkg.in/yaml.v2"
13
14
"github.com/netdata/netdata/go/plugins/logger"
15
+ "github.com/netdata/netdata/go/plugins/pkg/executable"
16
)
17
18
var log = logger.New().With("component", "snmp/ddsnmp")
19
20
+var ddProfiles []*Profile
21
+
22
+func init() {
23
+ dir := os.Getenv("NETDATA_STOCK_CONFIG_DIR")
24
+ if dir != "" {
25
+ dir = filepath.Join(dir, "go.d/snmp.profiles/default")
26
+ } else {
27
+ if dir, _ = filepath.Abs("../../../config/go.d/snmp.profiles/default"); !isDirExists(dir) {
28
+ dir = filepath.Join(executable.Directory, "../../../../usr/lib/netdata/conf.d/go.d/snmp.profiles/default")
29
+ }
30
+ }
31
+ profiles, err := load(dir)
32
+ if err != nil {
33
+ log.Errorf("failed to load dd snmp profiles: %v", err)
34
+ return
35
+ }
36
+ if len(profiles) == 0 {
37
+ log.Warningf("no dd snmp profiles found in '%s'", dir)
38
+ return
39
+ }
40
+
41
+ log.Infof("found %d profiles in '%s'", len(profiles), dir)
42
+ ddProfiles = profiles
43
+}
44
+
45
func load(dirpath string) ([]*Profile, error) {
46
var profiles []*Profile
47
48
if err := filepath.WalkDir(dirpath, func(path string, d fs.DirEntry, err error) error {
22
- if !(strings.HasSuffix(path, ".yaml") || strings.HasSuffix(path, ".yml")) {
49
+ if err != nil {
50
+ return err
51
+ }
52
+ if !(strings.HasSuffix(d.Name(), ".yaml") || strings.HasSuffix(d.Name(), ".yml")) {
53
return nil
54
}
55
@@ -71,3 +101,11 @@ func loadProfile(filename string) (*Profile, error) {
101
102
return &prof, nil
103
}
104
+
105
+func isDirExists(dir string) bool {
106
+ fi, err := os.Stat(dir)
107
+ if err != nil {
108
+ return !errors.Is(err, fs.ErrNotExist)
109
+ }
110
+ return fi.Mode().IsDir()
111
+}
src/go/plugin/go.d/collector/snmp/ddsnmp/profile.go
-26
@@ -4,39 +4,13 @@ package ddsnmp
4
5
import (
6
"errors"
7
- "os"
8
- "path/filepath"
9
- "sync"
7
8
"github.com/DataDog/datadog-agent/pkg/networkdevice/profile/profiledefinition"
9
13
- "github.com/netdata/netdata/go/plugins/pkg/executable"
10
"github.com/netdata/netdata/go/plugins/pkg/matcher"
11
)
12
17
-var once sync.Once
18
-var ddProfiles []*Profile
19
-
13
func Find(sysObjId string) []*Profile {
21
- once.Do(func() {
22
- dir := os.Getenv("NETDATA_STOCK_CONFIG_DIR")
23
- if dir == "" {
24
- dir = filepath.Join(executable.Directory, "../../../../usr/lib/netdata/conf.d/go.d/snmp.profiles/default/")
25
- }
26
-
27
- profiles, err := load(dir)
28
- if err != nil {
29
- log.Errorf("failed to load dd snmp profiles: %v", err)
30
- return
31
- }
32
- if len(profiles) == 0 {
33
- log.Warningf("no dd snmp profiles found in '%s'", dir)
34
- return
35
- }
36
-
37
- ddProfiles = profiles
38
- })
39
-
14
var profiles []*Profile
15
16
for _, prof := range ddProfiles {
src/go/plugin/go.d/collector/snmp/ddsnmp/profile_test.go
-10
@@ -3,21 +3,11 @@
3
package ddsnmp
4
5
import (
6
- "os"
7
- "path/filepath"
6
"testing"
7
8
"github.com/stretchr/testify/require"
9
)
10
13
-func TestMain(m *testing.M) {
14
- dir, _ := filepath.Abs("../../../config/go.d/snmp.profiles/default")
15
- _ = os.Setenv("NETDATA_STOCK_CONFIG_DIR", dir)
16
- code := m.Run()
17
- _ = os.Unsetenv("NETDATA_STOCK_CONFIG_DIR")
18
- os.Exit(code)
19
-}
20
-
11
func Test_Find(t *testing.T) {
12
test := map[string]struct {
13
sysObjOId string