| 1 | // SPDX-License-Identifier: GPL-3.0-or-later |
| 2 | |
| 3 | package main |
| 4 | |
| 5 | import ( |
| 6 | "fmt" |
| 7 | "log/slog" |
| 8 | "os" |
| 9 | "os/user" |
| 10 | "path/filepath" |
| 11 | "strings" |
| 12 | |
| 13 | "github.com/netdata/netdata/go/plugins/cmd/internal/agenthost" |
| 14 | "github.com/netdata/netdata/go/plugins/cmd/internal/discoveryproviders" |
| 15 | "github.com/netdata/netdata/go/plugins/plugin/agent" |
| 16 | "github.com/netdata/netdata/go/plugins/plugin/agent/discovery" |
| 17 | "github.com/netdata/netdata/go/plugins/plugin/agent/policy" |
| 18 | "go.uber.org/automaxprocs/maxprocs" |
| 19 | "golang.org/x/net/http/httpproxy" |
| 20 | |
| 21 | "github.com/netdata/netdata/go/plugins/logger" |
| 22 | "github.com/netdata/netdata/go/plugins/pkg/buildinfo" |
| 23 | "github.com/netdata/netdata/go/plugins/pkg/cli" |
| 24 | "github.com/netdata/netdata/go/plugins/pkg/executable" |
| 25 | "github.com/netdata/netdata/go/plugins/pkg/hostinfo" |
| 26 | "github.com/netdata/netdata/go/plugins/pkg/pluginconfig" |
| 27 | "github.com/netdata/netdata/go/plugins/pkg/terminal" |
| 28 | "github.com/netdata/netdata/go/plugins/plugin/framework/collectorapi" |
| 29 | _ "github.com/netdata/netdata/go/plugins/plugin/scripts.d/collector/nagios" |
| 30 | ) |
| 31 | |
| 32 | func init() { |
| 33 | executable.Name = "scripts.d" |
| 34 | if v := os.Getenv("TZ"); strings.HasPrefix(v, ":") { |
| 35 | _ = os.Unsetenv("TZ") |
| 36 | } |
| 37 | } |
| 38 | |
| 39 | func main() { |
| 40 | _, _ = maxprocs.Set(maxprocs.Logger(func(string, ...any) {})) |
| 41 | |
| 42 | opts := parseCLI() |
| 43 | |
| 44 | if opts.Version { |
| 45 | fmt.Printf("%s.plugin, version: %s\n", executable.Name, buildinfo.Version) |
| 46 | return |
| 47 | } |
| 48 | |
| 49 | pluginconfig.MustInit(pluginconfig.InitInput{ |
| 50 | ConfDir: opts.ConfDir, |
| 51 | WatchPath: opts.WatchPath, |
| 52 | }) |
| 53 | |
| 54 | watchPaths := pluginconfig.CollectorsConfigWatchPaths() |
| 55 | if len(watchPaths) == 0 { |
| 56 | for _, dir := range pluginconfig.CollectorsDir() { |
| 57 | watchPaths = append(watchPaths, filepath.Join(dir, "*.conf")) |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | if lvl := pluginconfig.EnvLogLevel(); lvl != "" { |
| 62 | logger.Level.SetByName(lvl) |
| 63 | } |
| 64 | if opts.Debug { |
| 65 | logger.Level.Set(slog.LevelDebug) |
| 66 | } |
| 67 | isTerminal := terminal.IsTerminal() |
| 68 | |
| 69 | a := agent.New(agent.Config{ |
| 70 | Name: executable.Name, |
| 71 | PluginConfigDir: pluginconfig.ConfigDir(), |
| 72 | CollectorsConfigDir: pluginconfig.CollectorsDir(), |
| 73 | ServiceDiscoveryConfigDir: nil, |
| 74 | CollectorsConfigWatchPath: watchPaths, |
| 75 | VarLibDir: pluginconfig.VarLibDir(), |
| 76 | ModuleRegistry: collectorapi.DefaultRegistry, |
| 77 | IsInsideK8s: hostinfo.IsInsideK8sCluster(), |
| 78 | RunModePolicy: policy.Agent(isTerminal), |
| 79 | DiscoveryProviders: []discovery.ProviderFactory{ |
| 80 | discoveryproviders.File(), |
| 81 | discoveryproviders.Dummy(), |
| 82 | }, |
| 83 | RunModule: opts.Module, |
| 84 | RunJob: opts.Job, |
| 85 | MinUpdateEvery: opts.UpdateEvery, |
| 86 | DisableServiceDiscovery: true, |
| 87 | }) |
| 88 | |
| 89 | a.Debugf("plugin: name=%s, %s", a.Name, buildinfo.Info()) |
| 90 | if u, err := user.Current(); err == nil { |
| 91 | a.Debugf("current user: name=%s, uid=%s", u.Username, u.Uid) |
| 92 | } |
| 93 | |
| 94 | proxyCfg := httpproxy.FromEnvironment() |
| 95 | a.Infof("env proxy settings: HTTP_PROXY set=%t, HTTPS_PROXY set=%t", proxyCfg.HTTPProxy != "", proxyCfg.HTTPSProxy != "") |
| 96 | |
| 97 | a.Infof("directories → config: %s | collectors: %s | varlib: %s", |
| 98 | a.ConfigDir, a.CollectorsConfDir, a.VarLibDir) |
| 99 | |
| 100 | agenthost.Run(a) |
| 101 | } |
| 102 | |
| 103 | func parseCLI() *cli.Option { |
| 104 | opt, err := cli.Parse(os.Args) |
| 105 | if err != nil { |
| 106 | if cli.IsHelp(err) { |
| 107 | os.Exit(0) |
| 108 | } |
| 109 | os.Exit(1) |
| 110 | } |
| 111 | |
| 112 | return opt |
| 113 | } |