| 1 | // SPDX-License-Identifier: GPL-3.0-or-later |
| 2 | |
| 3 | //go:build linux |
| 4 | |
| 5 | package dnsmasq_dhcp |
| 6 | |
| 7 | import ( |
| 8 | "context" |
| 9 | _ "embed" |
| 10 | "errors" |
| 11 | "fmt" |
| 12 | "net/netip" |
| 13 | "time" |
| 14 | |
| 15 | "github.com/netdata/netdata/go/plugins/plugin/framework/collectorapi" |
| 16 | "github.com/netdata/netdata/go/plugins/plugin/go.d/pkg/iprange" |
| 17 | ) |
| 18 | |
| 19 | //go:embed "config_schema.json" |
| 20 | var configSchema string |
| 21 | |
| 22 | func init() { |
| 23 | collectorapi.Register("dnsmasq_dhcp", collectorapi.Creator{ |
| 24 | JobConfigSchema: configSchema, |
| 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 | // debian defaults |
| 34 | LeasesPath: "/var/lib/misc/dnsmasq.leases", |
| 35 | ConfPath: "/etc/dnsmasq.conf", |
| 36 | ConfDir: "/etc/dnsmasq.d,.dpkg-dist,.dpkg-old,.dpkg-new", |
| 37 | }, |
| 38 | charts: charts.Copy(), |
| 39 | parseConfigEvery: time.Minute, |
| 40 | cacheDHCPRanges: make(map[string]bool), |
| 41 | mx: make(map[string]int64), |
| 42 | } |
| 43 | } |
| 44 | |
| 45 | type Config struct { |
| 46 | UpdateEvery int `yaml:"update_every,omitempty" json:"update_every"` |
| 47 | LeasesPath string `yaml:"leases_path" json:"leases_path"` |
| 48 | ConfPath string `yaml:"conf_path,omitempty" json:"conf_path"` |
| 49 | ConfDir string `yaml:"conf_dir,omitempty" json:"conf_dir"` |
| 50 | } |
| 51 | |
| 52 | type Collector struct { |
| 53 | collectorapi.Base |
| 54 | Config `yaml:",inline" json:""` |
| 55 | |
| 56 | charts *collectorapi.Charts |
| 57 | |
| 58 | leasesModTime time.Time |
| 59 | parseConfigTime time.Time |
| 60 | parseConfigEvery time.Duration |
| 61 | dhcpRanges []iprange.Range |
| 62 | dhcpHosts []netip.Addr |
| 63 | cacheDHCPRanges map[string]bool |
| 64 | |
| 65 | mx map[string]int64 |
| 66 | } |
| 67 | |
| 68 | func (c *Collector) Configuration() any { |
| 69 | return c.Config |
| 70 | } |
| 71 | |
| 72 | func (c *Collector) Init(context.Context) error { |
| 73 | if err := c.validateConfig(); err != nil { |
| 74 | return fmt.Errorf("config validation: %v", err) |
| 75 | } |
| 76 | if err := c.checkLeasesPath(); err != nil { |
| 77 | return fmt.Errorf("leases path check: %v", err) |
| 78 | } |
| 79 | |
| 80 | return nil |
| 81 | } |
| 82 | |
| 83 | func (c *Collector) Check(context.Context) error { |
| 84 | mx, err := c.collect() |
| 85 | if err != nil { |
| 86 | return err |
| 87 | } |
| 88 | if len(mx) == 0 { |
| 89 | return errors.New("no metrics collected") |
| 90 | |
| 91 | } |
| 92 | return nil |
| 93 | } |
| 94 | |
| 95 | func (c *Collector) Charts() *collectorapi.Charts { |
| 96 | return c.charts |
| 97 | } |
| 98 | |
| 99 | func (c *Collector) Collect(context.Context) map[string]int64 { |
| 100 | mx, err := c.collect() |
| 101 | if err != nil { |
| 102 | c.Error(err) |
| 103 | } |
| 104 | |
| 105 | if len(mx) == 0 { |
| 106 | return nil |
| 107 | } |
| 108 | |
| 109 | return mx |
| 110 | } |
| 111 | |
| 112 | func (c *Collector) Cleanup(context.Context) {} |