| 1 | // SPDX-License-Identifier: GPL-3.0-or-later |
| 2 | |
| 3 | //go:build linux |
| 4 | |
| 5 | package dnsmasq_dhcp |
| 6 | |
| 7 | import ( |
| 8 | "fmt" |
| 9 | "strings" |
| 10 | |
| 11 | "github.com/netdata/netdata/go/plugins/plugin/framework/collectorapi" |
| 12 | ) |
| 13 | |
| 14 | const ( |
| 15 | prioDHCPRangeUtilization = collectorapi.Priority + iota |
| 16 | prioDHCPRangeAllocatesLeases |
| 17 | prioDHCPRanges |
| 18 | prioDHCPHosts |
| 19 | ) |
| 20 | |
| 21 | var charts = collectorapi.Charts{ |
| 22 | { |
| 23 | ID: "dhcp_ranges", |
| 24 | Title: "Number of DHCP Ranges", |
| 25 | Units: "ranges", |
| 26 | Fam: "dhcp ranges", |
| 27 | Ctx: "dnsmasq_dhcp.dhcp_ranges", |
| 28 | Type: collectorapi.Stacked, |
| 29 | Priority: prioDHCPRanges, |
| 30 | Dims: collectorapi.Dims{ |
| 31 | {ID: "ipv4_dhcp_ranges", Name: "ipv4"}, |
| 32 | {ID: "ipv6_dhcp_ranges", Name: "ipv6"}, |
| 33 | }, |
| 34 | }, |
| 35 | { |
| 36 | ID: "dhcp_hosts", |
| 37 | Title: "Number of DHCP Hosts", |
| 38 | Units: "hosts", |
| 39 | Fam: "dhcp hosts", |
| 40 | Ctx: "dnsmasq_dhcp.dhcp_host", |
| 41 | Type: collectorapi.Stacked, |
| 42 | Priority: prioDHCPHosts, |
| 43 | Dims: collectorapi.Dims{ |
| 44 | {ID: "ipv4_dhcp_hosts", Name: "ipv4"}, |
| 45 | {ID: "ipv6_dhcp_hosts", Name: "ipv6"}, |
| 46 | }, |
| 47 | }, |
| 48 | } |
| 49 | |
| 50 | var ( |
| 51 | chartsTmpl = collectorapi.Charts{ |
| 52 | chartTmplDHCPRangeUtilization.Copy(), |
| 53 | chartTmplDHCPRangeAllocatedLeases.Copy(), |
| 54 | } |
| 55 | ) |
| 56 | |
| 57 | var ( |
| 58 | chartTmplDHCPRangeUtilization = collectorapi.Chart{ |
| 59 | ID: "dhcp_range_%s_utilization", |
| 60 | Title: "DHCP Range utilization", |
| 61 | Units: "percentage", |
| 62 | Fam: "dhcp range utilization", |
| 63 | Ctx: "dnsmasq_dhcp.dhcp_range_utilization", |
| 64 | Type: collectorapi.Area, |
| 65 | Priority: prioDHCPRangeUtilization, |
| 66 | Dims: collectorapi.Dims{ |
| 67 | {ID: "dhcp_range_%s_utilization", Name: "used"}, |
| 68 | }, |
| 69 | } |
| 70 | chartTmplDHCPRangeAllocatedLeases = collectorapi.Chart{ |
| 71 | ID: "dhcp_range_%s_allocated_leases", |
| 72 | Title: "DHCP Range Allocated Leases", |
| 73 | Units: "leases", |
| 74 | Fam: "dhcp range leases", |
| 75 | Ctx: "dnsmasq_dhcp.dhcp_range_allocated_leases", |
| 76 | Priority: prioDHCPRangeAllocatesLeases, |
| 77 | Dims: collectorapi.Dims{ |
| 78 | {ID: "dhcp_range_%s_allocated_leases", Name: "leases"}, |
| 79 | }, |
| 80 | } |
| 81 | ) |
| 82 | |
| 83 | func newDHCPRangeCharts(dhcpRange string) *collectorapi.Charts { |
| 84 | charts := chartsTmpl.Copy() |
| 85 | |
| 86 | for _, c := range *charts { |
| 87 | c.ID = fmt.Sprintf(c.ID, dhcpRange) |
| 88 | c.Labels = []collectorapi.Label{ |
| 89 | {Key: "dhcp_range", Value: dhcpRange}, |
| 90 | } |
| 91 | for _, d := range c.Dims { |
| 92 | d.ID = fmt.Sprintf(d.ID, dhcpRange) |
| 93 | } |
| 94 | } |
| 95 | return charts |
| 96 | } |
| 97 | |
| 98 | func (c *Collector) addDHCPRangeCharts(dhcpRange string) { |
| 99 | charts := newDHCPRangeCharts(dhcpRange) |
| 100 | if err := c.Charts().Add(*charts...); err != nil { |
| 101 | c.Warning(err) |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | func (c *Collector) removeDHCPRangeCharts(dhcpRange string) { |
| 106 | p := "dhcp_range_" + dhcpRange |
| 107 | for _, c := range *c.Charts() { |
| 108 | if strings.HasSuffix(c.ID, p) { |
| 109 | c.MarkRemove() |
| 110 | c.MarkNotCreated() |
| 111 | } |
| 112 | } |
| 113 | } |