|
| 1 | +// Copyright 2025 The Prometheus Authors |
| 2 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 3 | +// you may not use this file except in compliance with the License. |
| 4 | +// You may obtain a copy of the License at |
| 5 | +// |
| 6 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 7 | +// |
| 8 | +// Unless required by applicable law or agreed to in writing, software |
| 9 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 10 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 | +// See the License for the specific language governing permissions and |
| 12 | +// limitations under the License. |
| 13 | + |
| 14 | +//go:build !nonetinterface |
| 15 | +// +build !nonetinterface |
| 16 | + |
| 17 | +package collector |
| 18 | + |
| 19 | +import ( |
| 20 | + "log/slog" |
| 21 | + |
| 22 | + "github.com/power-devops/perfstat" |
| 23 | + "github.com/prometheus/client_golang/prometheus" |
| 24 | +) |
| 25 | + |
| 26 | +type netinterfaceCollector struct { |
| 27 | + logger *slog.Logger |
| 28 | + collisions *prometheus.Desc |
| 29 | + ibytes *prometheus.Desc |
| 30 | + ipackets *prometheus.Desc |
| 31 | + obytes *prometheus.Desc |
| 32 | + opackets *prometheus.Desc |
| 33 | +} |
| 34 | + |
| 35 | +const ( |
| 36 | + netinterfaceSubsystem = "netinterface" |
| 37 | +) |
| 38 | + |
| 39 | +func init() { |
| 40 | + registerCollector("netinterface", defaultEnabled, NewNetinterfaceCollector) |
| 41 | +} |
| 42 | + |
| 43 | +func NewNetinterfaceCollector(logger *slog.Logger) (Collector, error) { |
| 44 | + labels := []string{"interface"} |
| 45 | + return &netinterfaceCollector{ |
| 46 | + logger: logger, |
| 47 | + collisions: prometheus.NewDesc( |
| 48 | + prometheus.BuildFQName(namespace, netinterfaceSubsystem, "collisions_total"), |
| 49 | + "Total number of CSMA collisions on the interface.", labels, nil, |
| 50 | + ), |
| 51 | + ibytes: prometheus.NewDesc( |
| 52 | + prometheus.BuildFQName(namespace, netinterfaceSubsystem, "receive_bytes_total"), |
| 53 | + "Total number of bytes received on the interface.", labels, nil, |
| 54 | + ), |
| 55 | + ipackets: prometheus.NewDesc( |
| 56 | + prometheus.BuildFQName(namespace, netinterfaceSubsystem, "receive_packets_total"), |
| 57 | + "Total number of packets received on the interface.", labels, nil, |
| 58 | + ), |
| 59 | + obytes: prometheus.NewDesc( |
| 60 | + prometheus.BuildFQName(namespace, netinterfaceSubsystem, "transmit_bytes_total"), |
| 61 | + "Total number of bytes transmitted on the interface.", labels, nil, |
| 62 | + ), |
| 63 | + opackets: prometheus.NewDesc( |
| 64 | + prometheus.BuildFQName(namespace, netinterfaceSubsystem, "transmit_packets_total"), |
| 65 | + "Total number of packets transmitted on the interface.", labels, nil, |
| 66 | + ), |
| 67 | + }, nil |
| 68 | +} |
| 69 | + |
| 70 | +func (c *netinterfaceCollector) Update(ch chan<- prometheus.Metric) error { |
| 71 | + stats, err := perfstat.NetIfaceStat() |
| 72 | + if err != nil { |
| 73 | + return err |
| 74 | + } |
| 75 | + |
| 76 | + for _, stat := range stats { |
| 77 | + iface := stat.Name |
| 78 | + |
| 79 | + ch <- prometheus.MustNewConstMetric(c.collisions, prometheus.CounterValue, float64(stat.Collisions), iface) |
| 80 | + ch <- prometheus.MustNewConstMetric(c.ibytes, prometheus.CounterValue, float64(stat.IBytes), iface) |
| 81 | + ch <- prometheus.MustNewConstMetric(c.ipackets, prometheus.CounterValue, float64(stat.IPackets), iface) |
| 82 | + ch <- prometheus.MustNewConstMetric(c.obytes, prometheus.CounterValue, float64(stat.OBytes), iface) |
| 83 | + ch <- prometheus.MustNewConstMetric(c.opackets, prometheus.CounterValue, float64(stat.OPackets), iface) |
| 84 | + } |
| 85 | + return nil |
| 86 | +} |
0 commit comments