github.com/netdata/go.d.plugin@v0.58.1/modules/dnsmasq_dhcp/dhcp.go (about)

     1  // SPDX-License-Identifier: GPL-3.0-or-later
     2  
     3  package dnsmasq_dhcp
     4  
     5  import (
     6  	_ "embed"
     7  	"net"
     8  	"time"
     9  
    10  	"github.com/netdata/go.d.plugin/agent/module"
    11  	"github.com/netdata/go.d.plugin/pkg/iprange"
    12  )
    13  
    14  //go:embed "config_schema.json"
    15  var configSchema string
    16  
    17  func init() {
    18  	module.Register("dnsmasq_dhcp", module.Creator{
    19  		JobConfigSchema: configSchema,
    20  		Create:          func() module.Module { return New() },
    21  	})
    22  }
    23  
    24  func New() *DnsmasqDHCP {
    25  	config := Config{
    26  		// debian defaults
    27  		LeasesPath: "/var/lib/misc/dnsmasq.leases",
    28  		ConfPath:   "/etc/dnsmasq.conf",
    29  		ConfDir:    "/etc/dnsmasq.d,.dpkg-dist,.dpkg-old,.dpkg-new",
    30  	}
    31  
    32  	return &DnsmasqDHCP{
    33  		Config:           config,
    34  		charts:           charts.Copy(),
    35  		parseConfigEvery: time.Minute,
    36  		cacheDHCPRanges:  make(map[string]bool),
    37  		mx:               make(map[string]int64),
    38  	}
    39  }
    40  
    41  type Config struct {
    42  	LeasesPath string `yaml:"leases_path"`
    43  	ConfPath   string `yaml:"conf_path"`
    44  	ConfDir    string `yaml:"conf_dir"`
    45  }
    46  
    47  type DnsmasqDHCP struct {
    48  	module.Base
    49  	Config `yaml:",inline"`
    50  
    51  	charts *module.Charts
    52  
    53  	leasesModTime time.Time
    54  
    55  	parseConfigTime  time.Time
    56  	parseConfigEvery time.Duration
    57  
    58  	dhcpRanges []iprange.Range
    59  	dhcpHosts  []net.IP
    60  
    61  	cacheDHCPRanges map[string]bool
    62  
    63  	mx map[string]int64
    64  }
    65  
    66  func (d *DnsmasqDHCP) Init() bool {
    67  	if err := d.validateConfig(); err != nil {
    68  		d.Errorf("config validation: %v", err)
    69  		return false
    70  	}
    71  	if err := d.checkLeasesPath(); err != nil {
    72  		d.Errorf("leases path check: %v", err)
    73  		return false
    74  	}
    75  
    76  	return true
    77  }
    78  
    79  func (d *DnsmasqDHCP) Check() bool {
    80  	return len(d.Collect()) > 0
    81  }
    82  
    83  func (d *DnsmasqDHCP) Charts() *module.Charts {
    84  	return d.charts
    85  }
    86  
    87  func (d *DnsmasqDHCP) Collect() map[string]int64 {
    88  	mx, err := d.collect()
    89  	if err != nil {
    90  		d.Error(err)
    91  	}
    92  
    93  	if len(mx) == 0 {
    94  		return nil
    95  	}
    96  
    97  	return mx
    98  }
    99  
   100  func (d *DnsmasqDHCP) Cleanup() {}