master
go 103 lines 2.08 KB
Raw
1 // SPDX-License-Identifier: GPL-3.0-or-later
2
3 package tor
4
5 import (
6 "context"
7 _ "embed"
8 "errors"
9 "time"
10
11 "github.com/netdata/netdata/go/plugins/pkg/confopt"
12 "github.com/netdata/netdata/go/plugins/plugin/framework/collectorapi"
13 )
14
15 //go:embed "config_schema.json"
16 var configSchema string
17
18 func init() {
19 collectorapi.Register("tor", collectorapi.Creator{
20 JobConfigSchema: configSchema,
21 Create: func() collectorapi.CollectorV1 { return New() },
22 Config: func() any { return &Config{} },
23 })
24 }
25
26 func New() *Collector {
27 return &Collector{
28 Config: Config{
29 Address: "127.0.0.1:9051",
30 Timeout: confopt.Duration(time.Second * 1),
31 },
32 newConn: newControlConn,
33 charts: charts.Copy(),
34 }
35 }
36
37 type Config struct {
38 Vnode string `yaml:"vnode,omitempty" json:"vnode"`
39 UpdateEvery int `yaml:"update_every,omitempty" json:"update_every"`
40 AutoDetectionRetry int `yaml:"autodetection_retry,omitempty" json:"autodetection_retry"`
41 Address string `yaml:"address" json:"address"`
42 Timeout confopt.Duration `yaml:"timeout" json:"timeout"`
43 Password string `yaml:"password" json:"password"`
44 }
45
46 type Collector struct {
47 collectorapi.Base
48 Config `yaml:",inline" json:""`
49
50 charts *collectorapi.Charts
51
52 newConn func(Config) controlConn
53 conn controlConn
54 }
55
56 func (c *Collector) Configuration() any {
57 return c.Config
58 }
59
60 func (c *Collector) Init(context.Context) error {
61 if c.Address == "" {
62 return errors.New("config: address not set")
63 }
64
65 return nil
66 }
67
68 func (c *Collector) Check(context.Context) error {
69 mx, err := c.collect()
70 if err != nil {
71 return err
72 }
73
74 if len(mx) == 0 {
75 return errors.New("no metrics collected")
76 }
77
78 return nil
79 }
80
81 func (c *Collector) Charts() *collectorapi.Charts {
82 return c.charts
83 }
84
85 func (c *Collector) Collect(context.Context) map[string]int64 {
86 mx, err := c.collect()
87 if err != nil {
88 c.Error(err)
89 }
90
91 if len(mx) == 0 {
92 return nil
93 }
94
95 return mx
96 }
97
98 func (c *Collector) Cleanup(context.Context) {
99 if c.conn != nil {
100 c.conn.disconnect()
101 c.conn = nil
102 }
103 }