Skip to content

Commit c4dbce3

Browse files
Sergei Semenchukoblitorum
Sergei Semenchuk
authored andcommitted
add node_network_address_info collector (prometheus#2105)
* add node_network_address_info collector emit (interface, addr, netmask, scope) labels for every network interface Signed-off-by: binjip978 <[email protected]>
1 parent 1684f05 commit c4dbce3

File tree

1 file changed

+67
-0
lines changed

1 file changed

+67
-0
lines changed

collector/netdev_common.go

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
// See the License for the specific language governing permissions and
1212
// limitations under the License.
1313

14+
//go:build !nonetdev && (linux || freebsd || openbsd || dragonfly || darwin)
1415
// +build !nonetdev
1516
// +build linux freebsd openbsd dragonfly darwin
1617

@@ -19,6 +20,8 @@ package collector
1920
import (
2021
"errors"
2122
"fmt"
23+
"net"
24+
"strconv"
2225

2326
"github.com/go-kit/log"
2427
"github.com/go-kit/log/level"
@@ -31,6 +34,7 @@ var (
3134
oldNetdevDeviceInclude = kingpin.Flag("collector.netdev.device-whitelist", "DEPRECATED: Use collector.netdev.device-include").Hidden().String()
3235
netdevDeviceExclude = kingpin.Flag("collector.netdev.device-exclude", "Regexp of net devices to exclude (mutually exclusive to device-include).").String()
3336
oldNetdevDeviceExclude = kingpin.Flag("collector.netdev.device-blacklist", "DEPRECATED: Use collector.netdev.device-exclude").Hidden().String()
37+
netdevAddressInfo = kingpin.Flag("collector.netdev.address-info", "Collect address-info for every device").Bool()
3438
)
3539

3640
type netDevCollector struct {
@@ -106,5 +110,68 @@ func (c *netDevCollector) Update(ch chan<- prometheus.Metric) error {
106110
ch <- prometheus.MustNewConstMetric(desc, prometheus.CounterValue, float64(value), dev)
107111
}
108112
}
113+
if *netdevAddressInfo {
114+
interfaces, err := net.Interfaces()
115+
if err != nil {
116+
return fmt.Errorf("could not get network interfaces: %w", err)
117+
}
118+
119+
desc := prometheus.NewDesc(prometheus.BuildFQName(namespace, "network_address",
120+
"info"), "node network address by device",
121+
[]string{"device", "address", "netmask", "scope"}, nil)
122+
123+
for _, addr := range getAddrsInfo(interfaces) {
124+
ch <- prometheus.MustNewConstMetric(desc, prometheus.GaugeValue, 1,
125+
addr.device, addr.addr, addr.netmask, addr.scope)
126+
}
127+
}
109128
return nil
110129
}
130+
131+
type addrInfo struct {
132+
device string
133+
addr string
134+
scope string
135+
netmask string
136+
}
137+
138+
func scope(ip net.IP) string {
139+
if ip.IsLoopback() || ip.IsLinkLocalUnicast() || ip.IsLinkLocalMulticast() {
140+
return "link-local"
141+
}
142+
143+
if ip.IsInterfaceLocalMulticast() {
144+
return "interface-local"
145+
}
146+
147+
if ip.IsGlobalUnicast() {
148+
return "global"
149+
}
150+
151+
return ""
152+
}
153+
154+
// getAddrsInfo returns interface name, address, scope and netmask for all interfaces.
155+
func getAddrsInfo(interfaces []net.Interface) []addrInfo {
156+
var res []addrInfo
157+
158+
for _, ifs := range interfaces {
159+
addrs, _ := ifs.Addrs()
160+
for _, addr := range addrs {
161+
ip, ipNet, err := net.ParseCIDR(addr.String())
162+
if err != nil {
163+
continue
164+
}
165+
size, _ := ipNet.Mask.Size()
166+
167+
res = append(res, addrInfo{
168+
device: ifs.Name,
169+
addr: ip.String(),
170+
scope: scope(ip),
171+
netmask: strconv.Itoa(size),
172+
})
173+
}
174+
}
175+
176+
return res
177+
}

0 commit comments

Comments
 (0)