improve(go.d/snmp-profiles): add DHCP tags transform to bluecat profile (#20547)
Ilya Mashchenko committed
Jun 23, 2025 at 10:33 UTC
e1d627b6114cbae9420cd3f011f93b015d31a940
2 files changed
+65
-4
src/go/plugin/go.d/collector/snmp/ddsnmp/transform.go
+17
@@ -5,6 +5,8 @@ package ddsnmp
5
import (
6
"errors"
7
"fmt"
8
+ "net"
9
+ "strconv"
10
"text/template"
11
12
"github.com/Masterminds/sprig/v3"
@@ -119,6 +121,21 @@ func newMetricTransformFuncMap() template.FuncMap {
121
}
122
return m
123
},
124
+ "mask2cidr": func(mask string) string {
125
+ ip := net.ParseIP(mask)
126
+ if ip == nil {
127
+ return ""
128
+ }
129
+ ip = ip.To4()
130
+ if ip == nil {
131
+ return ""
132
+ }
133
+ prefixLen, bits := net.IPv4Mask(ip[0], ip[1], ip[2], ip[3]).Size()
134
+ if bits == 0 {
135
+ return ""
136
+ }
137
+ return strconv.Itoa(prefixLen)
138
+ },
139
}
140
141
for name, fn := range extra {
src/go/plugin/go.d/config/go.d/snmp.profiles/default/bluecat-server.yaml
+48
-4
@@ -26,16 +26,40 @@ metrics:
26
description: The number of IPs addresses available in this subnet.
27
family: DHCP/Subnets
28
unit: "{ip_address}"
29
+ # Transform combines subnet IP and mask into CIDR notation (e.g., 192.168.1.0/24)
30
+ # Converts mask to CIDR if possible, otherwise keeps original mask
31
+ transform: |
32
+ {{- $subnetIP := index .Metric.Tags "bcn_dhcpv4_subnet_ip" | default "" -}}
33
+ {{- $subnetMask := index .Metric.Tags "bcn_dhcpv4_subnet_mask" | default "" -}}
34
+ {{- if and $subnetIP $subnetMask -}}
35
+ {{- $cidr := mask2cidr $subnetMask -}}
36
+ {{- setTag .Metric "bcn_dhcpv4_subnet" (printf "%s/%s" $subnetIP (ternary $cidr $subnetMask $cidr)) -}}
37
+ {{- end -}}
38
+ {{- deleteTag .Metric "bcn_dhcpv4_subnet_ip" -}}
39
+ {{- deleteTag .Metric "bcn_dhcpv4_subnet_mask" -}}
40
- OID: 1.3.6.1.4.1.13315.3.1.1.2.2.2.1.3
41
name: bcnDhcpv4SubnetSize
42
description: Size of the subnet
43
family: DHCP/Subnets
44
unit: "{ip_address}"
45
+ # Transform combines subnet IP and mask into CIDR notation (e.g., 192.168.1.0/24)
46
+ # Converts mask to CIDR if possible, otherwise keeps original mask
47
+ transform: |
48
+ {{- $subnetIP := index .Metric.Tags "bcn_dhcpv4_subnet_ip" | default "" -}}
49
+ {{- $subnetMask := index .Metric.Tags "bcn_dhcpv4_subnet_mask" | default "" -}}
50
+ {{- if and $subnetIP $subnetMask -}}
51
+ {{- $cidr := mask2cidr $subnetMask -}}
52
+ {{- setTag .Metric "bcn_dhcpv4_subnet" (printf "%s/%s" $subnetIP (ternary $cidr $subnetMask $cidr)) -}}
53
+ {{- end -}}
54
+ {{- deleteTag .Metric "bcn_dhcpv4_subnet_ip" -}}
55
+ {{- deleteTag .Metric "bcn_dhcpv4_subnet_mask" -}}
56
metric_tags:
57
+ # Note: This tag will be removed by transform and combined into bcn_dhcpv4_subnet
58
- symbol:
59
OID: 1.3.6.1.4.1.13315.3.1.1.2.2.2.1.1
60
name: bcnDhcpv4SubnetIP
61
tag: bcn_dhcpv4_subnet_ip
62
+ # Note: This tag will be removed by transform and combined into bcn_dhcpv4_subnet
63
- symbol:
64
OID: 1.3.6.1.4.1.13315.3.1.1.2.2.2.1.2
65
name: bcnDhcpv4SubnetMask
@@ -50,24 +74,44 @@ metrics:
74
description: The number of IPs addresses available in this pool
75
family: DHCP/Pools
76
unit: "{ip_address}"
77
+ # Transform combines start and end IPs into a single range tag (e.g., 192.168.1.10-192.168.1.100)
78
+ transform: |
79
+ {{- $startIP := index .Metric.Tags "bcn_dhcpv4_pool_start_ip" | default "" -}}
80
+ {{- $endIP := index .Metric.Tags "bcn_dhcpv4_pool_end_ip" | default "" -}}
81
+ {{- if and $startIP $endIP -}}
82
+ {{- setTag .Metric "bcn_dhcpv4_pool_range" (printf "%s-%s" $startIP $endIP) -}}
83
+ {{- end -}}
84
+ {{- deleteTag .Metric "bcn_dhcpv4_pool_start_ip" -}}
85
+ {{- deleteTag .Metric "bcn_dhcpv4_pool_end_ip" -}}
86
- OID: 1.3.6.1.4.1.13315.3.1.1.2.2.3.1.4
87
name: bcnDhcpv4PoolSize
88
description: Pool size
89
family: DHCP/Pools
90
unit: "{ip_address}"
91
+ # Transform combines start and end IPs into a single range tag (e.g., 192.168.1.10-192.168.1.100)
92
+ transform: |
93
+ {{- $startIP := index .Metric.Tags "bcn_dhcpv4_pool_start_ip" | default "" -}}
94
+ {{- $endIP := index .Metric.Tags "bcn_dhcpv4_pool_end_ip" | default "" -}}
95
+ {{- if and $startIP $endIP -}}
96
+ {{- setTag .Metric "bcn_dhcpv4_pool_range" (printf "%s-%s" $startIP $endIP) -}}
97
+ {{- end -}}
98
+ {{- deleteTag .Metric "bcn_dhcpv4_pool_start_ip" -}}
99
+ {{- deleteTag .Metric "bcn_dhcpv4_pool_end_ip" -}}
100
metric_tags:
101
+ - symbol:
102
+ OID: 1.3.6.1.4.1.13315.3.1.1.2.2.3.1.3
103
+ name: bcnDhcpv4PoolSubnetIP
104
+ tag: bcn_dhcpv4_pool_subnet_ip
105
+ # Note: This tag will be removed by transform and combined into bcn_dhcpv4_pool_range
106
- symbol:
107
OID: 1.3.6.1.4.1.13315.3.1.1.2.2.3.1.1
108
name: bcnDhcpv4PoolStartIP
109
tag: bcn_dhcpv4_pool_start_ip
110
+ # Note: This tag will be removed by transform and combined into bcn_dhcpv4_pool_range
111
- symbol:
112
OID: 1.3.6.1.4.1.13315.3.1.1.2.2.3.1.2
113
name: bcnDhcpv4PoolEndIP
114
tag: bcn_dhcpv4_pool_end_ip
67
- - symbol:
68
- OID: 1.3.6.1.4.1.13315.3.1.1.2.2.3.1.3
69
- name: bcnDhcpv4PoolSubnetIP
70
- tag: bcn_dhcpv4_pool_subnet_ip
115
116
metric_tags:
117
- OID: 1.3.6.1.4.1.13315.3.2.2.1.1.0