github.com/netdata/go.d.plugin@v0.58.1/examples/simple/main.go (about)

     1  // SPDX-License-Identifier: GPL-3.0-or-later
     2  
     3  package main
     4  
     5  import (
     6  	"fmt"
     7  	"log/slog"
     8  	"math/rand"
     9  	"os"
    10  	"path"
    11  
    12  	"github.com/netdata/go.d.plugin/agent"
    13  	"github.com/netdata/go.d.plugin/agent/module"
    14  	"github.com/netdata/go.d.plugin/cli"
    15  	"github.com/netdata/go.d.plugin/logger"
    16  	"github.com/netdata/go.d.plugin/pkg/multipath"
    17  
    18  	"github.com/jessevdk/go-flags"
    19  )
    20  
    21  var version = "v0.0.1-example"
    22  
    23  type example struct{ module.Base }
    24  
    25  func (example) Cleanup() {}
    26  
    27  func (example) Init() bool { return true }
    28  
    29  func (example) Check() bool { return true }
    30  
    31  func (example) Charts() *module.Charts {
    32  	return &module.Charts{
    33  		{
    34  			ID:    "random",
    35  			Title: "A Random Number", Units: "random", Fam: "random",
    36  			Dims: module.Dims{
    37  				{ID: "random0", Name: "random 0"},
    38  				{ID: "random1", Name: "random 1"},
    39  			},
    40  		},
    41  	}
    42  }
    43  
    44  func (e *example) Collect() map[string]int64 {
    45  	return map[string]int64{
    46  		"random0": rand.Int63n(100),
    47  		"random1": rand.Int63n(100),
    48  	}
    49  }
    50  
    51  var (
    52  	cd, _    = os.Getwd()
    53  	name     = "goplugin"
    54  	userDir  = os.Getenv("NETDATA_USER_CONFIG_DIR")
    55  	stockDir = os.Getenv("NETDATA_STOCK_CONFIG_DIR")
    56  )
    57  
    58  func confDir(dirs []string) (mpath multipath.MultiPath) {
    59  	if len(dirs) > 0 {
    60  		return dirs
    61  	}
    62  	if userDir != "" && stockDir != "" {
    63  		return multipath.New(
    64  			userDir,
    65  			stockDir,
    66  		)
    67  	}
    68  	return multipath.New(
    69  		path.Join(cd, "/../../../../etc/netdata"),
    70  		path.Join(cd, "/../../../../usr/lib/netdata/conf.d"),
    71  	)
    72  }
    73  
    74  func modulesConfDir(dirs []string) multipath.MultiPath {
    75  	if len(dirs) > 0 {
    76  		return dirs
    77  	}
    78  	if userDir != "" && stockDir != "" {
    79  		return multipath.New(
    80  			path.Join(userDir, name),
    81  			path.Join(stockDir, name),
    82  		)
    83  	}
    84  	return multipath.New(
    85  		path.Join(cd, "/../../../../etc/netdata", name),
    86  		path.Join(cd, "/../../../../usr/lib/netdata/conf.d", name),
    87  	)
    88  }
    89  
    90  func main() {
    91  	opt := parseCLI()
    92  
    93  	if opt.Debug {
    94  		logger.Level.Set(slog.LevelDebug)
    95  	}
    96  	if opt.Version {
    97  		fmt.Println(version)
    98  		os.Exit(0)
    99  	}
   100  
   101  	module.Register("example", module.Creator{
   102  		Create: func() module.Module { return &example{} }},
   103  	)
   104  
   105  	p := agent.New(agent.Config{
   106  		Name:              name,
   107  		ConfDir:           confDir(opt.ConfDir),
   108  		ModulesConfDir:    modulesConfDir(opt.ConfDir),
   109  		ModulesSDConfPath: opt.WatchPath,
   110  		RunModule:         opt.Module,
   111  		MinUpdateEvery:    opt.UpdateEvery,
   112  	})
   113  
   114  	p.Run()
   115  }
   116  
   117  func parseCLI() *cli.Option {
   118  	opt, err := cli.Parse(os.Args)
   119  	if err != nil {
   120  		if flagsErr, ok := err.(*flags.Error); ok && flagsErr.Type == flags.ErrHelp {
   121  			os.Exit(0)
   122  		}
   123  		os.Exit(1)
   124  	}
   125  	return opt
   126  }