This repository has been archived by the owner on Oct 22, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
main.go
123 lines (104 loc) · 2.43 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
package main
import (
"fmt"
"math/rand"
"os"
"path"
"github.com/netdata/go-orchestrator/cli"
"github.com/netdata/go-orchestrator/module"
"github.com/netdata/go-orchestrator/pkg/logger"
"github.com/netdata/go-orchestrator/pkg/multipath"
"github.com/netdata/go-orchestrator/plugin"
"github.com/jessevdk/go-flags"
)
var version = "v0.0.1-example"
type example struct{ module.Base }
func (example) Cleanup() {}
func (example) Init() bool { return true }
func (example) Check() bool { return true }
func (example) Charts() *module.Charts {
return &module.Charts{
{
ID: "random",
Title: "A Random Number", Units: "random", Fam: "random",
Dims: module.Dims{
{ID: "random0", Name: "random 0"},
{ID: "random1", Name: "random 1"},
},
},
}
}
func (e *example) Collect() map[string]int64 {
return map[string]int64{
"random0": rand.Int63n(100),
"random1": rand.Int63n(100),
}
}
var (
cd, _ = os.Getwd()
name = "goplugin"
userDir = os.Getenv("NETDATA_USER_CONFIG_DIR")
stockDir = os.Getenv("NETDATA_STOCK_CONFIG_DIR")
)
func confDir(dirs []string) (mpath multipath.MultiPath) {
if len(dirs) > 0 {
return dirs
}
if userDir != "" && stockDir != "" {
return multipath.New(
userDir,
stockDir,
)
}
return multipath.New(
path.Join(cd, "/../../../../etc/netdata"),
path.Join(cd, "/../../../../usr/lib/netdata/conf.d"),
)
}
func modulesConfDir(dirs []string) multipath.MultiPath {
if len(dirs) > 0 {
return dirs
}
if userDir != "" && stockDir != "" {
return multipath.New(
path.Join(userDir, name),
path.Join(stockDir, name),
)
}
return multipath.New(
path.Join(cd, "/../../../../etc/netdata", name),
path.Join(cd, "/../../../../usr/lib/netdata/conf.d", name),
)
}
func main() {
opt := parseCLI()
if opt.Debug {
logger.SetSeverity(logger.DEBUG)
}
if opt.Version {
fmt.Println(version)
os.Exit(0)
}
module.Register("example", module.Creator{
Create: func() module.Module { return &example{} }},
)
p := plugin.New(plugin.Config{
Name: name,
ConfDir: confDir(opt.ConfDir),
ModulesConfDir: modulesConfDir(opt.ConfDir),
ModulesSDConfPath: opt.WatchPath,
RunModule: opt.Module,
MinUpdateEvery: opt.UpdateEvery,
})
p.Run()
}
func parseCLI() *cli.Option {
opt, err := cli.Parse(os.Args)
if err != nil {
if flagsErr, ok := err.(*flags.Error); ok && flagsErr.Type == flags.ErrHelp {
os.Exit(0)
}
os.Exit(1)
}
return opt
}