github.com/netdata/go.d.plugin@v0.58.1/cmd/godplugin/main.go (about)

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