@cryptotaxi247 / netdata-1 / commits / ee1969a4a

go.d isc_dhcpd create a chart for each pool (#17629)

Ilya Mashchenko committed May 9, 2024 at 17:11 UTC ee1969a4a80641fa084d4a2c30bc2c8f8680d6df
6 files changed +143 -116
src/go/collectors/go.d.plugin/modules/isc_dhcpd/charts.go
+39 -17
@@ -6,30 +6,52 @@ import (
6 "github.com/netdata/netdata/go/go.d.plugin/agent/module"
7 )
8
9 +const (
10 + prioLeasesTotal = module.Priority + iota
11 +
12 + prioDHCPPoolUtilization
13 + prioDHCPPoolActiveLeases
14 +)
15 +
16 var activeLeasesTotalChart = module.Chart{
10 - ID: "active_leases_total",
11 - Title: "Active Leases Total",
12 - Units: "leases",
13 - Fam: "summary",
14 - Ctx: "isc_dhcpd.active_leases_total",
17 + ID: "active_leases_total",
18 + Title: "Active Leases Total",
19 + Units: "leases",
20 + Fam: "summary",
21 + Ctx: "isc_dhcpd.active_leases_total",
22 + Priority: prioLeasesTotal,
23 Dims: module.Dims{
24 {ID: "active_leases_total", Name: "active"},
25 },
26 }
27
28 +var dhcpPoolChartsTmpl = module.Charts{
29 + dhcpPoolActiveLeasesChartTmpl.Copy(),
30 + dhcpPoolUtilizationChartTmpl.Copy(),
31 +}
32 +
33 var (
21 - poolActiveLeasesChart = module.Chart{
22 - ID: "pool_active_leases",
23 - Title: "Pool Active Leases",
24 - Units: "leases",
25 - Fam: "pools",
26 - Ctx: "isc_dhcpd.pool_active_leases",
34 + dhcpPoolUtilizationChartTmpl = module.Chart{
35 + ID: "dhcp_pool_%s_utilization",
36 + Title: "DHCP Pool Utilization",
37 + Units: "percent",
38 + Fam: "pools",
39 + Ctx: "isc_dhcpd.dhcp_pool_utilization",
40 + Priority: prioDHCPPoolUtilization,
41 + Type: module.Area,
42 + Dims: module.Dims{
43 + {ID: "dhcp_pool_%s_utilization", Name: "utilization"},
44 + },
45 }
28 - poolUtilizationChart = module.Chart{
29 - ID: "pool_utilization",
30 - Title: "Pool Utilization",
31 - Units: "percentage",
32 - Fam: "pools",
33 - Ctx: "isc_dhcpd.pool_utilization",
46 + dhcpPoolActiveLeasesChartTmpl = module.Chart{
47 + ID: "dhcp_pool_%s_active_leases",
48 + Title: "DHCP Pool Active Leases",
49 + Units: "leases",
50 + Fam: "pools",
51 + Ctx: "isc_dhcpd.dhcp_pool_active_leases",
52 + Priority: prioDHCPPoolActiveLeases,
53 + Dims: module.Dims{
54 + {ID: "dhcp_pool_%s_active_leases", Name: "active"},
55 + },
56 }
57 )
src/go/collectors/go.d.plugin/modules/isc_dhcpd/collect.go
+2 -2
@@ -54,8 +54,8 @@ const precision = 100
54
55 func collectPool(collected map[string]int64, pool ipPool, leases []leaseEntry) {
56 n := calcPoolActiveLeases(pool, leases)
57 - collected["pool_"+pool.name+"_active_leases"] = n
58 - collected["pool_"+pool.name+"_utilization"] = int64(calcPoolUtilizationPercentage(pool, n) * precision)
57 + collected["dhcp_pool_"+pool.name+"_active_leases"] = n
58 + collected["dhcp_pool_"+pool.name+"_utilization"] = int64(calcPoolUtilizationPercentage(pool, n) * precision)
59 }
60
61 func calcPoolActiveLeases(pool ipPool, leases []leaseEntry) (num int64) {
src/go/collectors/go.d.plugin/modules/isc_dhcpd/init.go
+26 -28
@@ -5,6 +5,7 @@ package isc_dhcpd
5 import (
6 "errors"
7 "fmt"
8 + "strings"
9
10 "github.com/netdata/netdata/go/go.d.plugin/agent/module"
11 "github.com/netdata/netdata/go/go.d.plugin/pkg/iprange"
@@ -35,18 +36,20 @@ func (d *DHCPd) validateConfig() error {
36
37 func (d *DHCPd) initPools() ([]ipPool, error) {
38 var pools []ipPool
39 +
40 for i, cfg := range d.Pools {
39 - rs, err := iprange.ParseRanges(cfg.Networks)
41 + ipRange, err := iprange.ParseRanges(cfg.Networks)
42 if err != nil {
43 return nil, fmt.Errorf("parse pools[%d]->pool.networks '%s' ('%s'): %v", i+1, cfg.Name, cfg.Networks, err)
44 }
43 - if len(rs) != 0 {
44 - pools = append(pools, ipPool{
45 - name: cfg.Name,
46 - addresses: rs,
47 - })
45 + if len(ipRange) == 0 {
46 + continue
47 }
48 +
49 + pool := ipPool{name: cfg.Name, addresses: ipRange}
50 + pools = append(pools, pool)
51 }
52 +
53 return pools, nil
54 }
55
@@ -57,34 +60,29 @@ func (d *DHCPd) initCharts(pools []ipPool) (*module.Charts, error) {
60 return nil, err
61 }
62
60 - chart := poolActiveLeasesChart.Copy()
61 - if err := charts.Add(chart); err != nil {
62 - return nil, err
63 - }
63 for _, pool := range pools {
65 - dim := &module.Dim{
66 - ID: "pool_" + pool.name + "_active_leases",
67 - Name: pool.name,
68 - }
69 - if err := chart.AddDim(dim); err != nil {
70 - return nil, err
71 - }
72 - }
64 + poolCharts := dhcpPoolChartsTmpl.Copy()
65
74 - chart = poolUtilizationChart.Copy()
75 - if err := charts.Add(chart); err != nil {
76 - return nil, err
77 - }
78 - for _, pool := range pools {
79 - dim := &module.Dim{
80 - ID: "pool_" + pool.name + "_utilization",
81 - Name: pool.name,
82 - Div: precision,
66 + for _, chart := range *poolCharts {
67 + chart.ID = fmt.Sprintf(chart.ID, cleanPoolNameForChart(pool.name))
68 + chart.Labels = []module.Label{
69 + {Key: "dhcp_pool_name", Value: pool.name},
70 + }
71 + for _, dim := range chart.Dims {
72 + dim.ID = fmt.Sprintf(dim.ID, pool.name)
73 + }
74 }
84 - if err := chart.AddDim(dim); err != nil {
75 +
76 + if err := charts.Add(*poolCharts...); err != nil {
77 return nil, err
78 }
79 }
80
81 return charts, nil
82 }
83 +
84 +func cleanPoolNameForChart(name string) string {
85 + name = strings.ReplaceAll(name, " ", "_")
86 + name = strings.ReplaceAll(name, ".", "_")
87 + return name
88 +}
src/go/collectors/go.d.plugin/modules/isc_dhcpd/isc_dhcpd.go
+4 -3
@@ -35,9 +35,10 @@ func New() *DHCPd {
35
36 type (
37 Config struct {
38 - UpdateEvery int `yaml:"update_every" json:"update_every"`
39 - LeasesPath string `yaml:"leases_path" json:"leases_path"`
40 - Pools []PoolConfig `yaml:"pools" json:"pools"`
38 + UpdateEvery int `yaml:"update_every" json:"update_every"`
39 + LeasesPath string `yaml:"leases_path" json:"leases_path"`
40 + // TODO: parse config file to extract configured pool
41 + Pools []PoolConfig `yaml:"pools" json:"pools"`
42 }
43 PoolConfig struct {
44 Name string `yaml:"name" json:"name"`
src/go/collectors/go.d.plugin/modules/isc_dhcpd/isc_dhcpd_test.go
+57 -57
@@ -140,83 +140,83 @@ func TestDHCPd_Collect(t *testing.T) {
140 "lease db is an empty file": {
141 prepare: prepareDHCPdLeasesEmpty,
142 wantCollected: map[string]int64{
143 - "active_leases_total": 0,
144 - "pool_net1_active_leases": 0,
145 - "pool_net1_utilization": 0,
146 - "pool_net2_active_leases": 0,
147 - "pool_net2_utilization": 0,
148 - "pool_net3_active_leases": 0,
149 - "pool_net3_utilization": 0,
150 - "pool_net4_active_leases": 0,
151 - "pool_net4_utilization": 0,
152 - "pool_net5_active_leases": 0,
153 - "pool_net5_utilization": 0,
154 - "pool_net6_active_leases": 0,
155 - "pool_net6_utilization": 0,
143 + "active_leases_total": 0,
144 + "dhcp_pool_net1_active_leases": 0,
145 + "dhcp_pool_net1_utilization": 0,
146 + "dhcp_pool_net2_active_leases": 0,
147 + "dhcp_pool_net2_utilization": 0,
148 + "dhcp_pool_net3_active_leases": 0,
149 + "dhcp_pool_net3_utilization": 0,
150 + "dhcp_pool_net4_active_leases": 0,
151 + "dhcp_pool_net4_utilization": 0,
152 + "dhcp_pool_net5_active_leases": 0,
153 + "dhcp_pool_net5_utilization": 0,
154 + "dhcp_pool_net6_active_leases": 0,
155 + "dhcp_pool_net6_utilization": 0,
156 },
157 },
158 "lease db ipv4": {
159 prepare: prepareDHCPdLeasesIPv4,
160 wantCollected: map[string]int64{
161 - "active_leases_total": 5,
162 - "pool_net1_active_leases": 2,
163 - "pool_net1_utilization": 158,
164 - "pool_net2_active_leases": 1,
165 - "pool_net2_utilization": 39,
166 - "pool_net3_active_leases": 0,
167 - "pool_net3_utilization": 0,
168 - "pool_net4_active_leases": 1,
169 - "pool_net4_utilization": 79,
170 - "pool_net5_active_leases": 0,
171 - "pool_net5_utilization": 0,
172 - "pool_net6_active_leases": 1,
173 - "pool_net6_utilization": 39,
161 + "active_leases_total": 5,
162 + "dhcp_pool_net1_active_leases": 2,
163 + "dhcp_pool_net1_utilization": 158,
164 + "dhcp_pool_net2_active_leases": 1,
165 + "dhcp_pool_net2_utilization": 39,
166 + "dhcp_pool_net3_active_leases": 0,
167 + "dhcp_pool_net3_utilization": 0,
168 + "dhcp_pool_net4_active_leases": 1,
169 + "dhcp_pool_net4_utilization": 79,
170 + "dhcp_pool_net5_active_leases": 0,
171 + "dhcp_pool_net5_utilization": 0,
172 + "dhcp_pool_net6_active_leases": 1,
173 + "dhcp_pool_net6_utilization": 39,
174 },
175 },
176 "lease db ipv4 with only inactive leases": {
177 prepare: prepareDHCPdLeasesIPv4Inactive,
178 wantCollected: map[string]int64{
179 - "active_leases_total": 0,
180 - "pool_net1_active_leases": 0,
181 - "pool_net1_utilization": 0,
182 - "pool_net2_active_leases": 0,
183 - "pool_net2_utilization": 0,
184 - "pool_net3_active_leases": 0,
185 - "pool_net3_utilization": 0,
186 - "pool_net4_active_leases": 0,
187 - "pool_net4_utilization": 0,
188 - "pool_net5_active_leases": 0,
189 - "pool_net5_utilization": 0,
190 - "pool_net6_active_leases": 0,
191 - "pool_net6_utilization": 0,
179 + "active_leases_total": 0,
180 + "dhcp_pool_net1_active_leases": 0,
181 + "dhcp_pool_net1_utilization": 0,
182 + "dhcp_pool_net2_active_leases": 0,
183 + "dhcp_pool_net2_utilization": 0,
184 + "dhcp_pool_net3_active_leases": 0,
185 + "dhcp_pool_net3_utilization": 0,
186 + "dhcp_pool_net4_active_leases": 0,
187 + "dhcp_pool_net4_utilization": 0,
188 + "dhcp_pool_net5_active_leases": 0,
189 + "dhcp_pool_net5_utilization": 0,
190 + "dhcp_pool_net6_active_leases": 0,
191 + "dhcp_pool_net6_utilization": 0,
192 },
193 },
194 "lease db ipv4 with backup leases": {
195 prepare: prepareDHCPdLeasesIPv4Backup,
196 wantCollected: map[string]int64{
197 - "active_leases_total": 2,
198 - "pool_net1_active_leases": 1,
199 - "pool_net1_utilization": 79,
200 - "pool_net2_active_leases": 0,
201 - "pool_net2_utilization": 0,
202 - "pool_net3_active_leases": 0,
203 - "pool_net3_utilization": 0,
204 - "pool_net4_active_leases": 1,
205 - "pool_net4_utilization": 79,
206 - "pool_net5_active_leases": 0,
207 - "pool_net5_utilization": 0,
208 - "pool_net6_active_leases": 0,
209 - "pool_net6_utilization": 0,
197 + "active_leases_total": 2,
198 + "dhcp_pool_net1_active_leases": 1,
199 + "dhcp_pool_net1_utilization": 79,
200 + "dhcp_pool_net2_active_leases": 0,
201 + "dhcp_pool_net2_utilization": 0,
202 + "dhcp_pool_net3_active_leases": 0,
203 + "dhcp_pool_net3_utilization": 0,
204 + "dhcp_pool_net4_active_leases": 1,
205 + "dhcp_pool_net4_utilization": 79,
206 + "dhcp_pool_net5_active_leases": 0,
207 + "dhcp_pool_net5_utilization": 0,
208 + "dhcp_pool_net6_active_leases": 0,
209 + "dhcp_pool_net6_utilization": 0,
210 },
211 },
212 "lease db ipv6": {
213 prepare: prepareDHCPdLeasesIPv6,
214 wantCollected: map[string]int64{
215 - "active_leases_total": 6,
216 - "pool_net1_active_leases": 6,
217 - "pool_net1_utilization": 5454,
218 - "pool_net2_active_leases": 0,
219 - "pool_net2_utilization": 0,
215 + "active_leases_total": 6,
216 + "dhcp_pool_net1_active_leases": 6,
217 + "dhcp_pool_net1_utilization": 5454,
218 + "dhcp_pool_net2_active_leases": 0,
219 + "dhcp_pool_net2_utilization": 0,
220 },
221 },
222 }
src/go/collectors/go.d.plugin/modules/isc_dhcpd/metadata.yaml
+15 -9
@@ -115,15 +115,21 @@ modules:
115 chart_type: line
116 dimensions:
117 - name: active
118 - - name: isc_dhcpd.pool_active_leases
119 - description: Pool Active Leases
120 - unit: leases
121 - chart_type: line
118 + - name: global
119 + description: These metrics refer to the DHCP pool.
120 + labels:
121 + - name: dhcp_pool_name
122 + description: The DHCP pool name defined in the collector configuration.
123 + metrics:
124 + - name: isc_dhcpd.dhcp_pool_utilization
125 + description: DHCP Pool Utilization
126 + unit: percent
127 + chart_type: area
128 dimensions:
123 - - name: a dimension per DHCP pool
124 - - name: isc_dhcpd.pool_utilization
125 - description: Pool Utilization
126 - unit: percentage
129 + - name: utilization
130 + - name: isc_dhcpd.dhcp_pool_active_leases
131 + description: Active Leases Total
132 + unit: leases
133 chart_type: line
134 dimensions:
129 - - name: a dimension per DHCP pool
135 + - name: active