-
Notifications
You must be signed in to change notification settings - Fork 5.9k
/
main.go
166 lines (144 loc) · 4 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
// SPDX-License-Identifier: GPL-3.0-or-later
package main
import (
"errors"
"fmt"
"log/slog"
"os"
"os/user"
"path/filepath"
"strings"
"github.com/netdata/netdata/go/go.d.plugin/agent"
"github.com/netdata/netdata/go/go.d.plugin/agent/executable"
"github.com/netdata/netdata/go/go.d.plugin/cli"
"github.com/netdata/netdata/go/go.d.plugin/logger"
"github.com/netdata/netdata/go/go.d.plugin/pkg/buildinfo"
"github.com/netdata/netdata/go/go.d.plugin/pkg/multipath"
"github.com/jessevdk/go-flags"
"golang.org/x/net/http/httpproxy"
_ "github.com/netdata/netdata/go/go.d.plugin/modules"
)
var (
cd, _ = os.Getwd()
name = "go.d"
userDir = os.Getenv("NETDATA_USER_CONFIG_DIR")
stockDir = os.Getenv("NETDATA_STOCK_CONFIG_DIR")
varLibDir = os.Getenv("NETDATA_LIB_DIR")
lockDir = os.Getenv("NETDATA_LOCK_DIR")
watchPath = os.Getenv("NETDATA_PLUGINS_GOD_WATCH_PATH")
envLogLevel = os.Getenv("NETDATA_LOG_LEVEL")
)
func confDir(opts *cli.Option) multipath.MultiPath {
if len(opts.ConfDir) > 0 {
return opts.ConfDir
}
if userDir != "" || stockDir != "" {
return multipath.New(
userDir,
stockDir,
)
}
if executable.Directory != "" {
return multipath.New(
filepath.Join(executable.Directory, "/../../../../etc/netdata"),
filepath.Join(executable.Directory, "/../../../../usr/lib/netdata/conf.d"),
)
}
return multipath.New(
filepath.Join(cd, "/../../../../etc/netdata"),
filepath.Join(cd, "/../../../../usr/lib/netdata/conf.d"),
)
}
func modulesConfDir(opts *cli.Option) (mpath multipath.MultiPath) {
if len(opts.ConfDir) > 0 {
return opts.ConfDir
}
if userDir != "" || stockDir != "" {
if userDir != "" {
mpath = append(mpath, filepath.Join(userDir, name))
}
if stockDir != "" {
mpath = append(mpath, filepath.Join(stockDir, name))
}
return multipath.New(mpath...)
}
if executable.Directory != "" {
return multipath.New(
filepath.Join(executable.Directory, "/../../../../etc/netdata", name),
filepath.Join(executable.Directory, "/../../../../usr/lib/netdata/conf.d", name),
)
}
return multipath.New(
filepath.Join(cd, "/../../../../etc/netdata", name),
filepath.Join(cd, "/../../../../usr/lib/netdata/conf.d", name),
)
}
func modulesConfSDDir(confDir multipath.MultiPath) (mpath multipath.MultiPath) {
for _, v := range confDir {
mpath = append(mpath, filepath.Join(v, "sd"))
}
return mpath
}
func watchPaths(opts *cli.Option) []string {
if watchPath == "" {
return opts.WatchPath
}
return append(opts.WatchPath, watchPath)
}
func stateFile() string {
if varLibDir == "" {
return ""
}
return filepath.Join(varLibDir, "god-jobs-statuses.json")
}
func init() {
// https://github.com/netdata/netdata/issues/8949#issuecomment-638294959
if v := os.Getenv("TZ"); strings.HasPrefix(v, ":") {
_ = os.Unsetenv("TZ")
}
}
func main() {
opts := parseCLI()
if opts.Version {
fmt.Printf("go.d.plugin, version: %s\n", buildinfo.Version)
return
}
if envLogLevel != "" {
logger.Level.SetByName(envLogLevel)
}
if opts.Debug {
logger.Level.Set(slog.LevelDebug)
}
dir := modulesConfDir(opts)
a := agent.New(agent.Config{
Name: name,
ConfDir: confDir(opts),
ModulesConfDir: dir,
ModulesConfSDDir: modulesConfSDDir(dir),
ModulesConfWatchPath: watchPaths(opts),
VnodesConfDir: confDir(opts),
StateFile: stateFile(),
LockDir: lockDir,
RunModule: opts.Module,
MinUpdateEvery: opts.UpdateEvery,
})
a.Debugf("plugin: name=%s, version=%s", a.Name, buildinfo.Version)
if u, err := user.Current(); err == nil {
a.Debugf("current user: name=%s, uid=%s", u.Username, u.Uid)
}
cfg := httpproxy.FromEnvironment()
a.Infof("env HTTP_PROXY '%s', HTTPS_PROXY '%s'", cfg.HTTPProxy, cfg.HTTPSProxy)
a.Run()
}
func parseCLI() *cli.Option {
opt, err := cli.Parse(os.Args)
if err != nil {
var flagsErr *flags.Error
if errors.As(err, &flagsErr) && errors.Is(flagsErr.Type, flags.ErrHelp) {
os.Exit(0)
} else {
os.Exit(1)
}
}
return opt
}