master
go 105 lines 2.23 KB
Raw
1 // SPDX-License-Identifier: GPL-3.0-or-later
2
3 package whoisquery
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/plugin/framework/collectorapi"
14 )
15
16 //go:embed "config_schema.json"
17 var configSchema string
18
19 func init() {
20 collectorapi.Register("whoisquery", collectorapi.Creator{
21 JobConfigSchema: configSchema,
22 Defaults: collectorapi.Defaults{
23 UpdateEvery: 60,
24 },
25 Create: func() collectorapi.CollectorV1 { return New() },
26 Config: func() any { return &Config{} },
27 })
28 }
29
30 func New() *Collector {
31 return &Collector{
32 Config: Config{
33 Timeout: confopt.Duration(time.Second * 5),
34 DaysUntilWarn: 30,
35 DaysUntilCrit: 15,
36 },
37 }
38 }
39
40 type Config struct {
41 Vnode string `yaml:"vnode,omitempty" json:"vnode"`
42 UpdateEvery int `yaml:"update_every,omitempty" json:"update_every"`
43 Source string `yaml:"source" json:"source"`
44 Timeout confopt.Duration `yaml:"timeout,omitempty" json:"timeout"`
45 DaysUntilWarn int64 `yaml:"days_until_expiration_warning,omitempty" json:"days_until_expiration_warning"`
46 DaysUntilCrit int64 `yaml:"days_until_expiration_critical,omitempty" json:"days_until_expiration_critical"`
47 }
48
49 type Collector struct {
50 collectorapi.Base
51 Config `yaml:",inline" json:""`
52
53 charts *collectorapi.Charts
54
55 prov provider
56 }
57
58 func (c *Collector) Configuration() any {
59 return c.Config
60 }
61
62 func (c *Collector) Init(context.Context) error {
63 if err := c.validateConfig(); err != nil {
64 return fmt.Errorf("config validation: %v", err)
65 }
66
67 prov, err := c.initProvider()
68 if err != nil {
69 return fmt.Errorf("init whois provider: %v", err)
70 }
71 c.prov = prov
72
73 c.charts = c.initCharts()
74
75 return nil
76 }
77
78 func (c *Collector) Check(context.Context) error {
79 mx, err := c.collect()
80 if err != nil {
81 return err
82 }
83 if len(mx) == 0 {
84 return errors.New("no metrics collected")
85 }
86 return nil
87 }
88
89 func (c *Collector) Charts() *collectorapi.Charts {
90 return c.charts
91 }
92
93 func (c *Collector) Collect(context.Context) map[string]int64 {
94 mx, err := c.collect()
95 if err != nil {
96 c.Error(err)
97 }
98
99 if len(mx) == 0 {
100 return nil
101 }
102 return mx
103 }
104
105 func (c *Collector) Cleanup(context.Context) {}