| 1 | // SPDX-License-Identifier: GPL-3.0-or-later |
| 2 | |
| 3 | package dnsdist |
| 4 | |
| 5 | import ( |
| 6 | "context" |
| 7 | _ "embed" |
| 8 | "errors" |
| 9 | "fmt" |
| 10 | "net/http" |
| 11 | "time" |
| 12 | |
| 13 | "github.com/netdata/netdata/go/plugins/pkg/confopt" |
| 14 | "github.com/netdata/netdata/go/plugins/pkg/web" |
| 15 | "github.com/netdata/netdata/go/plugins/plugin/framework/collectorapi" |
| 16 | ) |
| 17 | |
| 18 | //go:embed "config_schema.json" |
| 19 | var configSchema string |
| 20 | |
| 21 | func init() { |
| 22 | collectorapi.Register("dnsdist", collectorapi.Creator{ |
| 23 | JobConfigSchema: configSchema, |
| 24 | Defaults: collectorapi.Defaults{ |
| 25 | UpdateEvery: 1, |
| 26 | }, |
| 27 | Create: func() collectorapi.CollectorV1 { return New() }, |
| 28 | Config: func() any { return &Config{} }, |
| 29 | }) |
| 30 | } |
| 31 | |
| 32 | func New() *Collector { |
| 33 | return &Collector{ |
| 34 | Config: Config{ |
| 35 | HTTPConfig: web.HTTPConfig{ |
| 36 | RequestConfig: web.RequestConfig{ |
| 37 | URL: "http://127.0.0.1:8083", |
| 38 | }, |
| 39 | ClientConfig: web.ClientConfig{ |
| 40 | Timeout: confopt.Duration(time.Second), |
| 41 | }, |
| 42 | }, |
| 43 | }, |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | type Config struct { |
| 48 | Vnode string `yaml:"vnode,omitempty" json:"vnode"` |
| 49 | UpdateEvery int `yaml:"update_every,omitempty" json:"update_every"` |
| 50 | AutoDetectionRetry int `yaml:"autodetection_retry,omitempty" json:"autodetection_retry"` |
| 51 | web.HTTPConfig `yaml:",inline" json:""` |
| 52 | } |
| 53 | |
| 54 | type Collector struct { |
| 55 | collectorapi.Base |
| 56 | Config `yaml:",inline" json:""` |
| 57 | |
| 58 | charts *collectorapi.Charts |
| 59 | |
| 60 | httpClient *http.Client |
| 61 | } |
| 62 | |
| 63 | func (c *Collector) Configuration() any { |
| 64 | return c.Config |
| 65 | } |
| 66 | |
| 67 | func (c *Collector) Init(context.Context) error { |
| 68 | err := c.validateConfig() |
| 69 | if err != nil { |
| 70 | return fmt.Errorf("config validation: %v", err) |
| 71 | } |
| 72 | |
| 73 | client, err := c.initHTTPClient() |
| 74 | if err != nil { |
| 75 | return fmt.Errorf("init HTTP client: %v", err) |
| 76 | } |
| 77 | c.httpClient = client |
| 78 | |
| 79 | cs, err := c.initCharts() |
| 80 | if err != nil { |
| 81 | return fmt.Errorf("init charts: %v", err) |
| 82 | } |
| 83 | c.charts = cs |
| 84 | |
| 85 | return nil |
| 86 | } |
| 87 | |
| 88 | func (c *Collector) Check(context.Context) error { |
| 89 | mx, err := c.collect() |
| 90 | if err != nil { |
| 91 | return err |
| 92 | } |
| 93 | if len(mx) == 0 { |
| 94 | return errors.New("no metrics collected") |
| 95 | |
| 96 | } |
| 97 | return nil |
| 98 | } |
| 99 | |
| 100 | func (c *Collector) Charts() *collectorapi.Charts { |
| 101 | return c.charts |
| 102 | } |
| 103 | |
| 104 | func (c *Collector) Collect(context.Context) map[string]int64 { |
| 105 | ms, err := c.collect() |
| 106 | if err != nil { |
| 107 | c.Error(err) |
| 108 | } |
| 109 | |
| 110 | if len(ms) == 0 { |
| 111 | return nil |
| 112 | } |
| 113 | |
| 114 | return ms |
| 115 | } |
| 116 | |
| 117 | func (c *Collector) Cleanup(context.Context) { |
| 118 | if c.httpClient == nil { |
| 119 | return |
| 120 | } |
| 121 | c.httpClient.CloseIdleConnections() |
| 122 | } |