mirror of
https://github.com/prometheus-community/windows_exporter.git
synced 2026-02-08 05:56:37 +00:00
Use perflib for net collector
This commit is contained in:
@@ -6,7 +6,6 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"regexp"
|
"regexp"
|
||||||
|
|
||||||
"github.com/StackExchange/wmi"
|
|
||||||
"github.com/prometheus/client_golang/prometheus"
|
"github.com/prometheus/client_golang/prometheus"
|
||||||
"github.com/prometheus/common/log"
|
"github.com/prometheus/common/log"
|
||||||
"gopkg.in/alecthomas/kingpin.v2"
|
"gopkg.in/alecthomas/kingpin.v2"
|
||||||
@@ -28,7 +27,7 @@ var (
|
|||||||
nicNameToUnderscore = regexp.MustCompile("[^a-zA-Z0-9]")
|
nicNameToUnderscore = regexp.MustCompile("[^a-zA-Z0-9]")
|
||||||
)
|
)
|
||||||
|
|
||||||
// A NetworkCollector is a Prometheus collector for WMI Win32_PerfRawData_Tcpip_NetworkInterface metrics
|
// A NetworkCollector is a Prometheus collector for Perflib Network Interface metrics
|
||||||
type NetworkCollector struct {
|
type NetworkCollector struct {
|
||||||
BytesReceivedTotal *prometheus.Desc
|
BytesReceivedTotal *prometheus.Desc
|
||||||
BytesSentTotal *prometheus.Desc
|
BytesSentTotal *prometheus.Desc
|
||||||
@@ -133,7 +132,7 @@ func NewNetworkCollector() (Collector, error) {
|
|||||||
// Collect sends the metric values for each metric
|
// Collect sends the metric values for each metric
|
||||||
// to the provided prometheus Metric channel.
|
// to the provided prometheus Metric channel.
|
||||||
func (c *NetworkCollector) Collect(ctx *ScrapeContext, ch chan<- prometheus.Metric) error {
|
func (c *NetworkCollector) Collect(ctx *ScrapeContext, ch chan<- prometheus.Metric) error {
|
||||||
if desc, err := c.collect(ch); err != nil {
|
if desc, err := c.collect(ctx, ch); err != nil {
|
||||||
log.Error("failed collecting net metrics:", desc, err)
|
log.Error("failed collecting net metrics:", desc, err)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@@ -141,34 +140,33 @@ func (c *NetworkCollector) Collect(ctx *ScrapeContext, ch chan<- prometheus.Metr
|
|||||||
}
|
}
|
||||||
|
|
||||||
// mangleNetworkName mangles Network Adapter name (non-alphanumeric to _)
|
// mangleNetworkName mangles Network Adapter name (non-alphanumeric to _)
|
||||||
// that is used in Win32_PerfRawData_Tcpip_NetworkInterface.
|
// that is used in networkInterface.
|
||||||
func mangleNetworkName(name string) string {
|
func mangleNetworkName(name string) string {
|
||||||
return nicNameToUnderscore.ReplaceAllString(name, "_")
|
return nicNameToUnderscore.ReplaceAllString(name, "_")
|
||||||
}
|
}
|
||||||
|
|
||||||
// Win32_PerfRawData_Tcpip_NetworkInterface docs:
|
// Win32_PerfRawData_Tcpip_NetworkInterface docs:
|
||||||
// - https://technet.microsoft.com/en-us/security/aa394340(v=vs.80)
|
// - https://technet.microsoft.com/en-us/security/aa394340(v=vs.80)
|
||||||
type Win32_PerfRawData_Tcpip_NetworkInterface struct {
|
type networkInterface struct {
|
||||||
BytesReceivedPerSec uint64
|
BytesReceivedPerSec float64 `perflib:"Bytes Received/sec"`
|
||||||
BytesSentPerSec uint64
|
BytesSentPerSec float64 `perflib:"Bytes Sent/sec"`
|
||||||
BytesTotalPerSec uint64
|
BytesTotalPerSec float64 `perflib:"Bytes Total/sec"`
|
||||||
Name string
|
Name string
|
||||||
PacketsOutboundDiscarded uint64
|
PacketsOutboundDiscarded float64 `perflib:"Packets Outbound Discarded"`
|
||||||
PacketsOutboundErrors uint64
|
PacketsOutboundErrors float64 `perflib:"Packets Outbound Errors"`
|
||||||
PacketsPerSec uint64
|
PacketsPerSec float64 `perflib:"Packets/sec"`
|
||||||
PacketsReceivedDiscarded uint64
|
PacketsReceivedDiscarded float64 `perflib:"Packets Received Discarded"`
|
||||||
PacketsReceivedErrors uint64
|
PacketsReceivedErrors float64 `perflib:"Packets Received Errors"`
|
||||||
PacketsReceivedPerSec uint64
|
PacketsReceivedPerSec float64 `perflib:"Packets Received/sec"`
|
||||||
PacketsReceivedUnknown uint64
|
PacketsReceivedUnknown float64 `perflib:"Packets Received Unknown"`
|
||||||
PacketsSentPerSec uint64
|
PacketsSentPerSec float64 `perflib:"Packets Sent/sec"`
|
||||||
CurrentBandwidth uint64
|
CurrentBandwidth float64 `perflib:"Current Bandwidth"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *NetworkCollector) collect(ch chan<- prometheus.Metric) (*prometheus.Desc, error) {
|
func (c *NetworkCollector) collect(ctx *ScrapeContext, ch chan<- prometheus.Metric) (*prometheus.Desc, error) {
|
||||||
var dst []Win32_PerfRawData_Tcpip_NetworkInterface
|
var dst []networkInterface
|
||||||
|
|
||||||
q := queryAll(&dst)
|
if err := unmarshalObject(ctx.perfObjects["Network Interface"], &dst); err != nil {
|
||||||
if err := wmi.Query(q, &dst); err != nil {
|
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -187,76 +185,75 @@ func (c *NetworkCollector) collect(ch chan<- prometheus.Metric) (*prometheus.Des
|
|||||||
ch <- prometheus.MustNewConstMetric(
|
ch <- prometheus.MustNewConstMetric(
|
||||||
c.BytesReceivedTotal,
|
c.BytesReceivedTotal,
|
||||||
prometheus.CounterValue,
|
prometheus.CounterValue,
|
||||||
float64(nic.BytesReceivedPerSec),
|
nic.BytesReceivedPerSec,
|
||||||
name,
|
name,
|
||||||
)
|
)
|
||||||
ch <- prometheus.MustNewConstMetric(
|
ch <- prometheus.MustNewConstMetric(
|
||||||
c.BytesSentTotal,
|
c.BytesSentTotal,
|
||||||
prometheus.CounterValue,
|
prometheus.CounterValue,
|
||||||
float64(nic.BytesSentPerSec),
|
nic.BytesSentPerSec,
|
||||||
name,
|
name,
|
||||||
)
|
)
|
||||||
ch <- prometheus.MustNewConstMetric(
|
ch <- prometheus.MustNewConstMetric(
|
||||||
c.BytesTotal,
|
c.BytesTotal,
|
||||||
prometheus.CounterValue,
|
prometheus.CounterValue,
|
||||||
float64(nic.BytesTotalPerSec),
|
nic.BytesTotalPerSec,
|
||||||
name,
|
name,
|
||||||
)
|
)
|
||||||
ch <- prometheus.MustNewConstMetric(
|
ch <- prometheus.MustNewConstMetric(
|
||||||
c.PacketsOutboundDiscarded,
|
c.PacketsOutboundDiscarded,
|
||||||
prometheus.CounterValue,
|
prometheus.CounterValue,
|
||||||
float64(nic.PacketsOutboundDiscarded),
|
nic.PacketsOutboundDiscarded,
|
||||||
name,
|
name,
|
||||||
)
|
)
|
||||||
ch <- prometheus.MustNewConstMetric(
|
ch <- prometheus.MustNewConstMetric(
|
||||||
c.PacketsOutboundErrors,
|
c.PacketsOutboundErrors,
|
||||||
prometheus.CounterValue,
|
prometheus.CounterValue,
|
||||||
float64(nic.PacketsOutboundErrors),
|
nic.PacketsOutboundErrors,
|
||||||
name,
|
name,
|
||||||
)
|
)
|
||||||
ch <- prometheus.MustNewConstMetric(
|
ch <- prometheus.MustNewConstMetric(
|
||||||
c.PacketsTotal,
|
c.PacketsTotal,
|
||||||
prometheus.CounterValue,
|
prometheus.CounterValue,
|
||||||
float64(nic.PacketsPerSec),
|
nic.PacketsPerSec,
|
||||||
name,
|
name,
|
||||||
)
|
)
|
||||||
ch <- prometheus.MustNewConstMetric(
|
ch <- prometheus.MustNewConstMetric(
|
||||||
c.PacketsReceivedDiscarded,
|
c.PacketsReceivedDiscarded,
|
||||||
prometheus.CounterValue,
|
prometheus.CounterValue,
|
||||||
float64(nic.PacketsReceivedDiscarded),
|
nic.PacketsReceivedDiscarded,
|
||||||
name,
|
name,
|
||||||
)
|
)
|
||||||
ch <- prometheus.MustNewConstMetric(
|
ch <- prometheus.MustNewConstMetric(
|
||||||
c.PacketsReceivedErrors,
|
c.PacketsReceivedErrors,
|
||||||
prometheus.CounterValue,
|
prometheus.CounterValue,
|
||||||
float64(nic.PacketsReceivedErrors),
|
nic.PacketsReceivedErrors,
|
||||||
name,
|
name,
|
||||||
)
|
)
|
||||||
ch <- prometheus.MustNewConstMetric(
|
ch <- prometheus.MustNewConstMetric(
|
||||||
c.PacketsReceivedTotal,
|
c.PacketsReceivedTotal,
|
||||||
prometheus.CounterValue,
|
prometheus.CounterValue,
|
||||||
float64(nic.PacketsReceivedPerSec),
|
nic.PacketsReceivedPerSec,
|
||||||
name,
|
name,
|
||||||
)
|
)
|
||||||
ch <- prometheus.MustNewConstMetric(
|
ch <- prometheus.MustNewConstMetric(
|
||||||
c.PacketsReceivedUnknown,
|
c.PacketsReceivedUnknown,
|
||||||
prometheus.CounterValue,
|
prometheus.CounterValue,
|
||||||
float64(nic.PacketsReceivedUnknown),
|
nic.PacketsReceivedUnknown,
|
||||||
name,
|
name,
|
||||||
)
|
)
|
||||||
ch <- prometheus.MustNewConstMetric(
|
ch <- prometheus.MustNewConstMetric(
|
||||||
c.PacketsSentTotal,
|
c.PacketsSentTotal,
|
||||||
prometheus.CounterValue,
|
prometheus.CounterValue,
|
||||||
float64(nic.PacketsSentPerSec),
|
nic.PacketsSentPerSec,
|
||||||
name,
|
name,
|
||||||
)
|
)
|
||||||
ch <- prometheus.MustNewConstMetric(
|
ch <- prometheus.MustNewConstMetric(
|
||||||
c.CurrentBandwidth,
|
c.CurrentBandwidth,
|
||||||
prometheus.GaugeValue,
|
prometheus.GaugeValue,
|
||||||
float64(nic.CurrentBandwidth),
|
nic.CurrentBandwidth,
|
||||||
name,
|
name,
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil, nil
|
return nil, nil
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,6 +5,7 @@ The net collector exposes metrics about network interfaces
|
|||||||
|||
|
|||
|
||||||
-|-
|
-|-
|
||||||
Metric name prefix | `net`
|
Metric name prefix | `net`
|
||||||
|
Data source | Perflib
|
||||||
Classes | [`Win32_PerfRawData_Tcpip_NetworkInterface`](https://technet.microsoft.com/en-us/security/aa394340(v=vs.80))
|
Classes | [`Win32_PerfRawData_Tcpip_NetworkInterface`](https://technet.microsoft.com/en-us/security/aa394340(v=vs.80))
|
||||||
Enabled by default? | Yes
|
Enabled by default? | Yes
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user