@cryptotaxi247 / netdata-1 / commits / 7cc66e5d8

go.d update config dirs (#17661)

Ilya Mashchenko committed May 15, 2024 at 10:51 UTC 7cc66e5d818e4b832d71fb85e60563d4d4efae69
1 file changed +41 -35
src/go/collectors/go.d.plugin/cmd/godplugin/main.go
+41 -35
@@ -5,6 +5,7 @@ package main
5 import (
6 "errors"
7 "fmt"
8 + "io/fs"
9 "log/slog"
10 "os"
11 "os/user"
@@ -25,7 +26,6 @@ import (
26 )
27
28 var (
28 - cd, _ = os.Getwd()
29 name = "go.d"
30 userDir = os.Getenv("NETDATA_USER_CONFIG_DIR")
31 stockDir = os.Getenv("NETDATA_STOCK_CONFIG_DIR")
@@ -39,47 +39,45 @@ func confDir(opts *cli.Option) multipath.MultiPath {
39 if len(opts.ConfDir) > 0 {
40 return opts.ConfDir
41 }
42 +
43 if userDir != "" || stockDir != "" {
43 - return multipath.New(
44 - userDir,
45 - stockDir,
46 - )
47 - }
48 - if executable.Directory != "" {
49 - return multipath.New(
50 - filepath.Join(executable.Directory, "/../../../../etc/netdata"),
51 - filepath.Join(executable.Directory, "/../../../../usr/lib/netdata/conf.d"),
52 - )
53 - }
54 - return multipath.New(
55 - filepath.Join(cd, "/../../../../etc/netdata"),
56 - filepath.Join(cd, "/../../../../usr/lib/netdata/conf.d"),
57 - )
44 + return multipath.New(userDir, stockDir)
45 + }
46 +
47 + var dirs []string
48 +
49 + dirs = append(dirs, filepath.Join(executable.Directory, "/../../../../etc/netdata"))
50 +
51 + for _, dir := range []string{"/etc/netdata", "/opt/netdata/etc/netdata"} {
52 + if isDirExists(dir) {
53 + dirs = append(dirs, dir)
54 + break
55 + }
56 + }
57 +
58 + dirs = append(dirs, filepath.Join(executable.Directory, "/../../../../usr/lib/netdata/conf.d"))
59 +
60 + for _, dir := range []string{"/usr/lib/netdata/conf.d", "/opt/netdata/usr/lib/netdata/conf.d"} {
61 + if isDirExists(dir) {
62 + dirs = append(dirs, dir)
63 + break
64 + }
65 + }
66 +
67 + return multipath.New(dirs...)
68 }
69
70 func modulesConfDir(opts *cli.Option) (mpath multipath.MultiPath) {
71 if len(opts.ConfDir) > 0 {
72 return opts.ConfDir
73 }
64 - if userDir != "" || stockDir != "" {
65 - if userDir != "" {
66 - mpath = append(mpath, filepath.Join(userDir, name))
67 - }
68 - if stockDir != "" {
69 - mpath = append(mpath, filepath.Join(stockDir, name))
70 - }
71 - return multipath.New(mpath...)
72 - }
73 - if executable.Directory != "" {
74 - return multipath.New(
75 - filepath.Join(executable.Directory, "/../../../../etc/netdata", name),
76 - filepath.Join(executable.Directory, "/../../../../usr/lib/netdata/conf.d", name),
77 - )
78 - }
79 - return multipath.New(
80 - filepath.Join(cd, "/../../../../etc/netdata", name),
81 - filepath.Join(cd, "/../../../../usr/lib/netdata/conf.d", name),
82 - )
74 +
75 + dirs := confDir(opts)
76 + for _, dir := range dirs {
77 + mpath = append(mpath, filepath.Join(dir, name))
78 + }
79 +
80 + return multipath.New(mpath...)
81 }
82
83 func modulesConfSDDir(confDir multipath.MultiPath) (mpath multipath.MultiPath) {
@@ -164,3 +162,11 @@ func parseCLI() *cli.Option {
162 }
163 return opt
164 }
165 +
166 +func isDirExists(dir string) bool {
167 + fi, err := os.Stat(dir)
168 + if err == nil {
169 + return fi.Mode().IsDir()
170 + }
171 + return !errors.Is(err, fs.ErrNotExist)
172 +}