From 01fd9654ad9c2c658c605f90ef1df6f770a91476 Mon Sep 17 00:00:00 2001 From: abbas ali chezgi Date: Wed, 18 Dec 2024 08:11:15 +0330 Subject: [PATCH 1/3] fixed crash: on some tun/tap network devices in linux `NameAssignType` is nil. --- collector/netclass_linux.go | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/collector/netclass_linux.go b/collector/netclass_linux.go index 87763c3b6e..648cd234da 100644 --- a/collector/netclass_linux.go +++ b/collector/netclass_linux.go @@ -116,10 +116,13 @@ func (c *netClassCollector) netClassSysfsUpdate(ch chan<- prometheus.Metric) err pushMetric(ch, c.getFieldDesc("iface_id"), "iface_id", ifaceInfo.IfIndex, prometheus.GaugeValue, ifaceInfo.Name) pushMetric(ch, c.getFieldDesc("iface_link"), "iface_link", ifaceInfo.IfLink, prometheus.GaugeValue, ifaceInfo.Name) pushMetric(ch, c.getFieldDesc("iface_link_mode"), "iface_link_mode", ifaceInfo.LinkMode, prometheus.GaugeValue, ifaceInfo.Name) - pushMetric(ch, c.getFieldDesc("mtu_bytes"), "mtu_bytes", ifaceInfo.MTU, prometheus.GaugeValue, ifaceInfo.Name) - pushMetric(ch, c.getFieldDesc("name_assign_type"), "name_assign_type", ifaceInfo.NameAssignType, prometheus.GaugeValue, ifaceInfo.Name) + pushMetric(ch, c.getFieldDesc("mtu_bytes"), "mtu_bytes", ifaceInfo.MTU, prometheus.GaugeValue, ifaceInfo.Name) pushMetric(ch, c.getFieldDesc("net_dev_group"), "net_dev_group", ifaceInfo.NetDevGroup, prometheus.GaugeValue, ifaceInfo.Name) - + + if ifaceInfo.NameAssignType != nil { + pushMetric(ch, c.getFieldDesc("name_assign_type"), "name_assign_type", ifaceInfo.NameAssignType, prometheus.GaugeValue, ifaceInfo.Name) + } + if ifaceInfo.Speed != nil { // Some devices return -1 if the speed is unknown. if *ifaceInfo.Speed >= 0 || !*netclassInvalidSpeed { From e983219cf7afa9fe846ff28ab17c0cec326cb2ac Mon Sep 17 00:00:00 2001 From: abbas ali chezgi Date: Wed, 18 Dec 2024 08:23:27 +0330 Subject: [PATCH 2/3] also fix the bug in rtnl_linux --- collector/netclass_rtnl_linux.go | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/collector/netclass_rtnl_linux.go b/collector/netclass_rtnl_linux.go index 44d89f8f05..2402b4cb64 100644 --- a/collector/netclass_rtnl_linux.go +++ b/collector/netclass_rtnl_linux.go @@ -133,9 +133,12 @@ func (c *netClassCollector) netClassRTNLUpdate(ch chan<- prometheus.Metric) erro } else { pushMetric(ch, c.getFieldDesc("iface_link"), "iface_link", msg.Index, prometheus.GaugeValue, msg.Attributes.Name) } + + if ifaceInfo.NameAssignType != nil { + pushMetric(ch, c.getFieldDesc("name_assign_type"), "name_assign_type", ifaceInfo.NameAssignType, prometheus.GaugeValue, msg.Attributes.Name) + } pushMetric(ch, c.getFieldDesc("mtu_bytes"), "mtu_bytes", msg.Attributes.MTU, prometheus.GaugeValue, msg.Attributes.Name) - pushMetric(ch, c.getFieldDesc("name_assign_type"), "name_assign_type", ifaceInfo.NameAssignType, prometheus.GaugeValue, msg.Attributes.Name) pushMetric(ch, c.getFieldDesc("net_dev_group"), "net_dev_group", msg.Attributes.NetDevGroup, prometheus.GaugeValue, msg.Attributes.Name) pushMetric(ch, c.getFieldDesc("transmit_queue_length"), "transmit_queue_length", msg.Attributes.TxQueueLen, prometheus.GaugeValue, msg.Attributes.Name) pushMetric(ch, c.getFieldDesc("protocol_type"), "protocol_type", msg.Type, prometheus.GaugeValue, msg.Attributes.Name) From 4d5d836d8e25518f2e81fc4272dae1335fdac161 Mon Sep 17 00:00:00 2001 From: chezgi Date: Wed, 18 Dec 2024 10:05:14 +0330 Subject: [PATCH 3/3] fixed format errors --- collector/netclass_linux.go | 6 +++--- collector/netclass_rtnl_linux.go | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/collector/netclass_linux.go b/collector/netclass_linux.go index 648cd234da..b95bb0bb49 100644 --- a/collector/netclass_linux.go +++ b/collector/netclass_linux.go @@ -116,13 +116,13 @@ func (c *netClassCollector) netClassSysfsUpdate(ch chan<- prometheus.Metric) err pushMetric(ch, c.getFieldDesc("iface_id"), "iface_id", ifaceInfo.IfIndex, prometheus.GaugeValue, ifaceInfo.Name) pushMetric(ch, c.getFieldDesc("iface_link"), "iface_link", ifaceInfo.IfLink, prometheus.GaugeValue, ifaceInfo.Name) pushMetric(ch, c.getFieldDesc("iface_link_mode"), "iface_link_mode", ifaceInfo.LinkMode, prometheus.GaugeValue, ifaceInfo.Name) - pushMetric(ch, c.getFieldDesc("mtu_bytes"), "mtu_bytes", ifaceInfo.MTU, prometheus.GaugeValue, ifaceInfo.Name) + pushMetric(ch, c.getFieldDesc("mtu_bytes"), "mtu_bytes", ifaceInfo.MTU, prometheus.GaugeValue, ifaceInfo.Name) pushMetric(ch, c.getFieldDesc("net_dev_group"), "net_dev_group", ifaceInfo.NetDevGroup, prometheus.GaugeValue, ifaceInfo.Name) - + if ifaceInfo.NameAssignType != nil { pushMetric(ch, c.getFieldDesc("name_assign_type"), "name_assign_type", ifaceInfo.NameAssignType, prometheus.GaugeValue, ifaceInfo.Name) } - + if ifaceInfo.Speed != nil { // Some devices return -1 if the speed is unknown. if *ifaceInfo.Speed >= 0 || !*netclassInvalidSpeed { diff --git a/collector/netclass_rtnl_linux.go b/collector/netclass_rtnl_linux.go index 2402b4cb64..7f1f1a4b21 100644 --- a/collector/netclass_rtnl_linux.go +++ b/collector/netclass_rtnl_linux.go @@ -133,7 +133,7 @@ func (c *netClassCollector) netClassRTNLUpdate(ch chan<- prometheus.Metric) erro } else { pushMetric(ch, c.getFieldDesc("iface_link"), "iface_link", msg.Index, prometheus.GaugeValue, msg.Attributes.Name) } - + if ifaceInfo.NameAssignType != nil { pushMetric(ch, c.getFieldDesc("name_assign_type"), "name_assign_type", ifaceInfo.NameAssignType, prometheus.GaugeValue, msg.Attributes.Name) }