master
go 128 lines 3.23 KB
Raw
1 // SPDX-License-Identifier: GPL-3.0-or-later
2
3 package unbound
4
5 import (
6 "context"
7 _ "embed"
8 "errors"
9 "fmt"
10 "time"
11
12 "github.com/netdata/netdata/go/plugins/pkg/confopt"
13 "github.com/netdata/netdata/go/plugins/pkg/tlscfg"
14 "github.com/netdata/netdata/go/plugins/plugin/framework/collectorapi"
15 "github.com/netdata/netdata/go/plugins/plugin/go.d/pkg/socket"
16 )
17
18 //go:embed "config_schema.json"
19 var configSchema string
20
21 func init() {
22 collectorapi.Register("unbound", collectorapi.Creator{
23 JobConfigSchema: configSchema,
24 Create: func() collectorapi.CollectorV1 { return New() },
25 Config: func() any { return &Config{} },
26 })
27 }
28
29 func New() *Collector {
30 return &Collector{
31 Config: Config{
32 Address: "127.0.0.1:8953",
33 ConfPath: "/etc/unbound/unbound.conf",
34 Timeout: confopt.Duration(time.Second),
35 Cumulative: false,
36 UseTLS: true,
37 TLSConfig: tlscfg.TLSConfig{
38 TLSCert: "/etc/unbound/unbound_control.pem",
39 TLSKey: "/etc/unbound/unbound_control.key",
40 InsecureSkipVerify: true,
41 },
42 },
43 curCache: newCollectCache(),
44 cache: newCollectCache(),
45 }
46 }
47
48 type Config struct {
49 Vnode string `yaml:"vnode,omitempty" json:"vnode"`
50 UpdateEvery int `yaml:"update_every,omitempty" json:"update_every"`
51 AutoDetectionRetry int `yaml:"autodetection_retry,omitempty" json:"autodetection_retry"`
52 Address string `yaml:"address" json:"address"`
53 ConfPath string `yaml:"conf_path,omitempty" json:"conf_path"`
54 Timeout confopt.Duration `yaml:"timeout,omitempty" json:"timeout"`
55 Cumulative bool `yaml:"cumulative_stats" json:"cumulative_stats"`
56 UseTLS bool `yaml:"use_tls,omitempty" json:"use_tls"`
57 tlscfg.TLSConfig `yaml:",inline" json:""`
58 }
59
60 type Collector struct {
61 collectorapi.Base
62 Config `yaml:",inline" json:""`
63
64 charts *collectorapi.Charts
65
66 client socket.Client
67
68 cache collectCache
69 curCache collectCache
70 prevCacheMiss float64 // needed for cumulative mode
71 extChartsCreated bool
72 }
73
74 func (c *Collector) Configuration() any {
75 return c.Config
76 }
77
78 func (c *Collector) Init(context.Context) error {
79 if enabled := c.initConfig(); !enabled {
80 return errors.New("remote control is disabled in the configuration file")
81 }
82
83 if err := c.initClient(); err != nil {
84 return fmt.Errorf("creating client: %v", err)
85 }
86
87 c.charts = charts(c.Cumulative)
88
89 c.Debugf("using address: %s, cumulative: %v, use_tls: %v, timeout: %s", c.Address, c.Cumulative, c.UseTLS, c.Timeout)
90 if c.UseTLS {
91 c.Debugf("using tls_skip_verify: %v, tls_key: %s, tls_cert: %s", c.InsecureSkipVerify, c.TLSKey, c.TLSCert)
92 }
93
94 return nil
95 }
96
97 func (c *Collector) Check(context.Context) error {
98 mx, err := c.collect()
99 if err != nil {
100 return err
101 }
102 if len(mx) == 0 {
103 return errors.New("no metrics collected")
104 }
105 return nil
106 }
107
108 func (c *Collector) Charts() *collectorapi.Charts {
109 return c.charts
110 }
111
112 func (c *Collector) Collect(context.Context) map[string]int64 {
113 mx, err := c.collect()
114 if err != nil {
115 c.Error(err)
116 }
117
118 if len(mx) == 0 {
119 return nil
120 }
121 return mx
122 }
123
124 func (c *Collector) Cleanup(context.Context) {
125 if c.client != nil {
126 _ = c.client.Disconnect()
127 }
128 }