2021-12-18 18:01:29 +00:00
|
|
|
//go:build windows
|
2018-11-30 00:51:12 +00:00
|
|
|
// +build windows
|
|
|
|
|
2016-09-27 14:17:03 +00:00
|
|
|
package collector
|
|
|
|
|
|
|
|
import (
|
2023-04-01 07:48:23 +00:00
|
|
|
"errors"
|
2016-09-27 14:17:03 +00:00
|
|
|
"fmt"
|
|
|
|
"regexp"
|
|
|
|
|
2023-03-12 23:32:17 +00:00
|
|
|
"github.com/alecthomas/kingpin/v2"
|
2023-04-22 10:17:51 +00:00
|
|
|
"github.com/go-kit/log"
|
|
|
|
"github.com/go-kit/log/level"
|
2016-09-27 14:17:03 +00:00
|
|
|
"github.com/prometheus/client_golang/prometheus"
|
|
|
|
)
|
|
|
|
|
2023-04-16 09:45:07 +00:00
|
|
|
const (
|
2023-04-01 07:48:23 +00:00
|
|
|
FlagNicOldExclude = "collector.net.nic-blacklist"
|
|
|
|
FlagNicOldInclude = "collector.net.nic-whitelist"
|
|
|
|
|
|
|
|
FlagNicExclude = "collector.net.nic-exclude"
|
|
|
|
FlagNicInclude = "collector.net.nic-include"
|
2023-04-16 09:45:07 +00:00
|
|
|
)
|
|
|
|
|
2016-09-27 14:17:03 +00:00
|
|
|
var (
|
2023-04-01 07:48:23 +00:00
|
|
|
nicOldInclude *string
|
|
|
|
nicOldExclude *string
|
|
|
|
|
|
|
|
nicInclude *string
|
|
|
|
nicExclude *string
|
|
|
|
|
|
|
|
nicIncludeSet bool
|
|
|
|
nicExcludeSet bool
|
|
|
|
|
2016-09-27 14:17:03 +00:00
|
|
|
nicNameToUnderscore = regexp.MustCompile("[^a-zA-Z0-9]")
|
|
|
|
)
|
|
|
|
|
2019-09-24 09:40:53 +00:00
|
|
|
// A NetworkCollector is a Prometheus collector for Perflib Network Interface metrics
|
2016-09-27 14:17:03 +00:00
|
|
|
type NetworkCollector struct {
|
2023-04-22 10:17:51 +00:00
|
|
|
logger log.Logger
|
|
|
|
|
2016-09-27 14:17:03 +00:00
|
|
|
BytesReceivedTotal *prometheus.Desc
|
|
|
|
BytesSentTotal *prometheus.Desc
|
|
|
|
BytesTotal *prometheus.Desc
|
2023-03-25 20:27:07 +00:00
|
|
|
OutputQueueLength *prometheus.Desc
|
2016-09-27 14:17:03 +00:00
|
|
|
PacketsOutboundDiscarded *prometheus.Desc
|
|
|
|
PacketsOutboundErrors *prometheus.Desc
|
|
|
|
PacketsTotal *prometheus.Desc
|
|
|
|
PacketsReceivedDiscarded *prometheus.Desc
|
|
|
|
PacketsReceivedErrors *prometheus.Desc
|
|
|
|
PacketsReceivedTotal *prometheus.Desc
|
|
|
|
PacketsReceivedUnknown *prometheus.Desc
|
|
|
|
PacketsSentTotal *prometheus.Desc
|
2018-02-21 16:06:07 +00:00
|
|
|
CurrentBandwidth *prometheus.Desc
|
2016-09-27 14:17:03 +00:00
|
|
|
|
2023-04-01 07:48:23 +00:00
|
|
|
nicIncludePattern *regexp.Regexp
|
|
|
|
nicExcludePattern *regexp.Regexp
|
2016-09-27 14:17:03 +00:00
|
|
|
}
|
|
|
|
|
2023-04-16 09:45:07 +00:00
|
|
|
// newNetworkCollectorFlags ...
|
|
|
|
func newNetworkCollectorFlags(app *kingpin.Application) {
|
2023-04-01 07:48:23 +00:00
|
|
|
nicInclude = app.Flag(
|
|
|
|
FlagNicInclude,
|
|
|
|
"Regexp of NIC:s to include. NIC name must both match include and not match exclude to be included.",
|
|
|
|
).Default(".+").PreAction(func(c *kingpin.ParseContext) error {
|
|
|
|
nicIncludeSet = true
|
|
|
|
return nil
|
|
|
|
}).String()
|
|
|
|
|
|
|
|
nicExclude = app.Flag(
|
|
|
|
FlagNicExclude,
|
|
|
|
"Regexp of NIC:s to exclude. NIC name must both match include and not match exclude to be included.",
|
|
|
|
).Default("").PreAction(func(c *kingpin.ParseContext) error {
|
|
|
|
nicExcludeSet = true
|
|
|
|
return nil
|
|
|
|
}).String()
|
|
|
|
|
|
|
|
nicOldInclude = app.Flag(
|
|
|
|
FlagNicOldInclude,
|
|
|
|
"DEPRECATED: Use --collector.net.nic-include",
|
|
|
|
).Hidden().String()
|
|
|
|
nicOldExclude = app.Flag(
|
|
|
|
FlagNicOldExclude,
|
|
|
|
"DEPRECATED: Use --collector.net.nic-exclude",
|
|
|
|
).Hidden().String()
|
|
|
|
|
2023-04-16 09:45:07 +00:00
|
|
|
}
|
|
|
|
|
2022-12-21 05:44:29 +00:00
|
|
|
// newNetworkCollector ...
|
2023-04-22 10:17:51 +00:00
|
|
|
func newNetworkCollector(logger log.Logger) (Collector, error) {
|
|
|
|
const subsystem = "net"
|
|
|
|
logger = log.With(logger, "collector", subsystem)
|
|
|
|
|
2023-04-01 07:48:23 +00:00
|
|
|
if *nicOldExclude != "" {
|
|
|
|
if !nicExcludeSet {
|
2023-06-08 00:29:50 +00:00
|
|
|
_ = level.Warn(logger).Log("msg", "--collector.net.nic-blacklist is DEPRECATED and will be removed in a future release, use --collector.net.nic-exclude")
|
2023-04-01 07:48:23 +00:00
|
|
|
*nicExclude = *nicOldExclude
|
|
|
|
} else {
|
|
|
|
return nil, errors.New("--collector.net.nic-blacklist and --collector.net.nic-exclude are mutually exclusive")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if *nicOldInclude != "" {
|
|
|
|
if !nicIncludeSet {
|
2023-06-08 00:29:50 +00:00
|
|
|
_ = level.Warn(logger).Log("msg", "--collector.net.nic-whitelist is DEPRECATED and will be removed in a future release, use --collector.net.nic-include")
|
2023-04-01 07:48:23 +00:00
|
|
|
*nicInclude = *nicOldInclude
|
|
|
|
} else {
|
|
|
|
return nil, errors.New("--collector.net.nic-whitelist and --collector.net.nic-include are mutually exclusive")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-27 14:17:03 +00:00
|
|
|
return &NetworkCollector{
|
2023-04-22 10:17:51 +00:00
|
|
|
logger: logger,
|
2016-09-27 14:17:03 +00:00
|
|
|
BytesReceivedTotal: prometheus.NewDesc(
|
|
|
|
prometheus.BuildFQName(Namespace, subsystem, "bytes_received_total"),
|
|
|
|
"(Network.BytesReceivedPerSec)",
|
|
|
|
[]string{"nic"},
|
|
|
|
nil,
|
|
|
|
),
|
|
|
|
BytesSentTotal: prometheus.NewDesc(
|
|
|
|
prometheus.BuildFQName(Namespace, subsystem, "bytes_sent_total"),
|
|
|
|
"(Network.BytesSentPerSec)",
|
|
|
|
[]string{"nic"},
|
|
|
|
nil,
|
|
|
|
),
|
|
|
|
BytesTotal: prometheus.NewDesc(
|
|
|
|
prometheus.BuildFQName(Namespace, subsystem, "bytes_total"),
|
|
|
|
"(Network.BytesTotalPerSec)",
|
|
|
|
[]string{"nic"},
|
|
|
|
nil,
|
|
|
|
),
|
2023-03-25 20:27:07 +00:00
|
|
|
OutputQueueLength: prometheus.NewDesc(
|
|
|
|
prometheus.BuildFQName(Namespace, subsystem, "output_queue_length_packets"),
|
|
|
|
"(Network.OutputQueueLength)",
|
|
|
|
[]string{"nic"},
|
|
|
|
nil,
|
|
|
|
),
|
2016-09-27 14:17:03 +00:00
|
|
|
PacketsOutboundDiscarded: prometheus.NewDesc(
|
2020-11-14 02:38:01 +00:00
|
|
|
prometheus.BuildFQName(Namespace, subsystem, "packets_outbound_discarded_total"),
|
2016-09-27 14:17:03 +00:00
|
|
|
"(Network.PacketsOutboundDiscarded)",
|
|
|
|
[]string{"nic"},
|
|
|
|
nil,
|
|
|
|
),
|
|
|
|
PacketsOutboundErrors: prometheus.NewDesc(
|
2020-11-14 02:38:01 +00:00
|
|
|
prometheus.BuildFQName(Namespace, subsystem, "packets_outbound_errors_total"),
|
2016-09-27 14:17:03 +00:00
|
|
|
"(Network.PacketsOutboundErrors)",
|
|
|
|
[]string{"nic"},
|
|
|
|
nil,
|
|
|
|
),
|
|
|
|
PacketsReceivedDiscarded: prometheus.NewDesc(
|
2020-11-14 02:38:01 +00:00
|
|
|
prometheus.BuildFQName(Namespace, subsystem, "packets_received_discarded_total"),
|
2016-09-27 14:17:03 +00:00
|
|
|
"(Network.PacketsReceivedDiscarded)",
|
|
|
|
[]string{"nic"},
|
|
|
|
nil,
|
|
|
|
),
|
|
|
|
PacketsReceivedErrors: prometheus.NewDesc(
|
2020-11-14 02:38:01 +00:00
|
|
|
prometheus.BuildFQName(Namespace, subsystem, "packets_received_errors_total"),
|
2016-09-27 14:17:03 +00:00
|
|
|
"(Network.PacketsReceivedErrors)",
|
|
|
|
[]string{"nic"},
|
|
|
|
nil,
|
|
|
|
),
|
|
|
|
PacketsReceivedTotal: prometheus.NewDesc(
|
|
|
|
prometheus.BuildFQName(Namespace, subsystem, "packets_received_total"),
|
|
|
|
"(Network.PacketsReceivedPerSec)",
|
|
|
|
[]string{"nic"},
|
|
|
|
nil,
|
|
|
|
),
|
|
|
|
PacketsReceivedUnknown: prometheus.NewDesc(
|
2020-11-14 02:38:01 +00:00
|
|
|
prometheus.BuildFQName(Namespace, subsystem, "packets_received_unknown_total"),
|
2016-09-27 14:17:03 +00:00
|
|
|
"(Network.PacketsReceivedUnknown)",
|
|
|
|
[]string{"nic"},
|
|
|
|
nil,
|
|
|
|
),
|
|
|
|
PacketsTotal: prometheus.NewDesc(
|
|
|
|
prometheus.BuildFQName(Namespace, subsystem, "packets_total"),
|
|
|
|
"(Network.PacketsPerSec)",
|
|
|
|
[]string{"nic"},
|
|
|
|
nil,
|
|
|
|
),
|
|
|
|
PacketsSentTotal: prometheus.NewDesc(
|
|
|
|
prometheus.BuildFQName(Namespace, subsystem, "packets_sent_total"),
|
|
|
|
"(Network.PacketsSentPerSec)",
|
|
|
|
[]string{"nic"},
|
|
|
|
nil,
|
|
|
|
),
|
2018-02-21 16:06:07 +00:00
|
|
|
CurrentBandwidth: prometheus.NewDesc(
|
2021-06-14 15:33:27 +00:00
|
|
|
prometheus.BuildFQName(Namespace, subsystem, "current_bandwidth_bytes"),
|
2018-02-21 16:06:07 +00:00
|
|
|
"(Network.CurrentBandwidth)",
|
|
|
|
[]string{"nic"},
|
|
|
|
nil,
|
|
|
|
),
|
2016-09-27 14:17:03 +00:00
|
|
|
|
2023-04-01 07:48:23 +00:00
|
|
|
nicIncludePattern: regexp.MustCompile(fmt.Sprintf("^(?:%s)$", *nicInclude)),
|
|
|
|
nicExcludePattern: regexp.MustCompile(fmt.Sprintf("^(?:%s)$", *nicExclude)),
|
2016-09-27 14:17:03 +00:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Collect sends the metric values for each metric
|
|
|
|
// to the provided prometheus Metric channel.
|
2019-04-05 13:59:40 +00:00
|
|
|
func (c *NetworkCollector) Collect(ctx *ScrapeContext, ch chan<- prometheus.Metric) error {
|
2019-09-24 09:40:53 +00:00
|
|
|
if desc, err := c.collect(ctx, ch); err != nil {
|
2023-06-08 00:29:50 +00:00
|
|
|
_ = level.Error(c.logger).Log("failed collecting net metrics", "desc", desc, "err", err)
|
2016-09-27 14:17:03 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// mangleNetworkName mangles Network Adapter name (non-alphanumeric to _)
|
2019-09-24 09:40:53 +00:00
|
|
|
// that is used in networkInterface.
|
2016-09-27 14:17:03 +00:00
|
|
|
func mangleNetworkName(name string) string {
|
|
|
|
return nicNameToUnderscore.ReplaceAllString(name, "_")
|
|
|
|
}
|
|
|
|
|
2018-10-04 19:35:33 +00:00
|
|
|
// Win32_PerfRawData_Tcpip_NetworkInterface docs:
|
|
|
|
// - https://technet.microsoft.com/en-us/security/aa394340(v=vs.80)
|
2019-09-24 09:40:53 +00:00
|
|
|
type networkInterface struct {
|
|
|
|
BytesReceivedPerSec float64 `perflib:"Bytes Received/sec"`
|
|
|
|
BytesSentPerSec float64 `perflib:"Bytes Sent/sec"`
|
|
|
|
BytesTotalPerSec float64 `perflib:"Bytes Total/sec"`
|
2016-09-27 14:17:03 +00:00
|
|
|
Name string
|
2023-03-25 20:27:07 +00:00
|
|
|
OutputQueueLength float64 `perflib:"Output Queue Length"`
|
2019-09-24 09:40:53 +00:00
|
|
|
PacketsOutboundDiscarded float64 `perflib:"Packets Outbound Discarded"`
|
|
|
|
PacketsOutboundErrors float64 `perflib:"Packets Outbound Errors"`
|
|
|
|
PacketsPerSec float64 `perflib:"Packets/sec"`
|
|
|
|
PacketsReceivedDiscarded float64 `perflib:"Packets Received Discarded"`
|
|
|
|
PacketsReceivedErrors float64 `perflib:"Packets Received Errors"`
|
|
|
|
PacketsReceivedPerSec float64 `perflib:"Packets Received/sec"`
|
|
|
|
PacketsReceivedUnknown float64 `perflib:"Packets Received Unknown"`
|
|
|
|
PacketsSentPerSec float64 `perflib:"Packets Sent/sec"`
|
|
|
|
CurrentBandwidth float64 `perflib:"Current Bandwidth"`
|
2016-09-27 14:17:03 +00:00
|
|
|
}
|
|
|
|
|
2019-09-24 09:40:53 +00:00
|
|
|
func (c *NetworkCollector) collect(ctx *ScrapeContext, ch chan<- prometheus.Metric) (*prometheus.Desc, error) {
|
|
|
|
var dst []networkInterface
|
2016-09-27 14:17:03 +00:00
|
|
|
|
2023-04-22 10:17:51 +00:00
|
|
|
if err := unmarshalObject(ctx.perfObjects["Network Interface"], &dst, c.logger); err != nil {
|
2016-09-27 14:17:03 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, nic := range dst {
|
2023-04-01 07:48:23 +00:00
|
|
|
if c.nicExcludePattern.MatchString(nic.Name) ||
|
|
|
|
!c.nicIncludePattern.MatchString(nic.Name) {
|
2016-09-27 14:17:03 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
name := mangleNetworkName(nic.Name)
|
|
|
|
if name == "" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// Counters
|
|
|
|
ch <- prometheus.MustNewConstMetric(
|
|
|
|
c.BytesReceivedTotal,
|
|
|
|
prometheus.CounterValue,
|
2019-09-24 09:40:53 +00:00
|
|
|
nic.BytesReceivedPerSec,
|
2016-09-27 14:17:03 +00:00
|
|
|
name,
|
|
|
|
)
|
|
|
|
ch <- prometheus.MustNewConstMetric(
|
|
|
|
c.BytesSentTotal,
|
|
|
|
prometheus.CounterValue,
|
2019-09-24 09:40:53 +00:00
|
|
|
nic.BytesSentPerSec,
|
2016-09-27 14:17:03 +00:00
|
|
|
name,
|
|
|
|
)
|
|
|
|
ch <- prometheus.MustNewConstMetric(
|
|
|
|
c.BytesTotal,
|
|
|
|
prometheus.CounterValue,
|
2019-09-24 09:40:53 +00:00
|
|
|
nic.BytesTotalPerSec,
|
2016-09-27 14:17:03 +00:00
|
|
|
name,
|
|
|
|
)
|
2023-03-25 20:27:07 +00:00
|
|
|
ch <- prometheus.MustNewConstMetric(
|
|
|
|
c.OutputQueueLength,
|
|
|
|
prometheus.GaugeValue,
|
|
|
|
nic.OutputQueueLength,
|
|
|
|
name,
|
|
|
|
)
|
2016-09-27 14:17:03 +00:00
|
|
|
ch <- prometheus.MustNewConstMetric(
|
|
|
|
c.PacketsOutboundDiscarded,
|
|
|
|
prometheus.CounterValue,
|
2019-09-24 09:40:53 +00:00
|
|
|
nic.PacketsOutboundDiscarded,
|
2016-09-27 14:17:03 +00:00
|
|
|
name,
|
|
|
|
)
|
|
|
|
ch <- prometheus.MustNewConstMetric(
|
|
|
|
c.PacketsOutboundErrors,
|
|
|
|
prometheus.CounterValue,
|
2019-09-24 09:40:53 +00:00
|
|
|
nic.PacketsOutboundErrors,
|
2016-09-27 14:17:03 +00:00
|
|
|
name,
|
|
|
|
)
|
|
|
|
ch <- prometheus.MustNewConstMetric(
|
|
|
|
c.PacketsTotal,
|
|
|
|
prometheus.CounterValue,
|
2019-09-24 09:40:53 +00:00
|
|
|
nic.PacketsPerSec,
|
2016-09-27 14:17:03 +00:00
|
|
|
name,
|
|
|
|
)
|
|
|
|
ch <- prometheus.MustNewConstMetric(
|
|
|
|
c.PacketsReceivedDiscarded,
|
|
|
|
prometheus.CounterValue,
|
2019-09-24 09:40:53 +00:00
|
|
|
nic.PacketsReceivedDiscarded,
|
2016-09-27 14:17:03 +00:00
|
|
|
name,
|
|
|
|
)
|
|
|
|
ch <- prometheus.MustNewConstMetric(
|
|
|
|
c.PacketsReceivedErrors,
|
|
|
|
prometheus.CounterValue,
|
2019-09-24 09:40:53 +00:00
|
|
|
nic.PacketsReceivedErrors,
|
2016-09-27 14:17:03 +00:00
|
|
|
name,
|
|
|
|
)
|
|
|
|
ch <- prometheus.MustNewConstMetric(
|
|
|
|
c.PacketsReceivedTotal,
|
|
|
|
prometheus.CounterValue,
|
2019-09-24 09:40:53 +00:00
|
|
|
nic.PacketsReceivedPerSec,
|
2016-09-27 14:17:03 +00:00
|
|
|
name,
|
|
|
|
)
|
|
|
|
ch <- prometheus.MustNewConstMetric(
|
|
|
|
c.PacketsReceivedUnknown,
|
|
|
|
prometheus.CounterValue,
|
2019-09-24 09:40:53 +00:00
|
|
|
nic.PacketsReceivedUnknown,
|
2016-09-27 14:17:03 +00:00
|
|
|
name,
|
|
|
|
)
|
|
|
|
ch <- prometheus.MustNewConstMetric(
|
|
|
|
c.PacketsSentTotal,
|
|
|
|
prometheus.CounterValue,
|
2019-09-24 09:40:53 +00:00
|
|
|
nic.PacketsSentPerSec,
|
2016-09-27 14:17:03 +00:00
|
|
|
name,
|
|
|
|
)
|
2018-02-21 16:06:07 +00:00
|
|
|
ch <- prometheus.MustNewConstMetric(
|
|
|
|
c.CurrentBandwidth,
|
2019-08-28 11:12:14 +00:00
|
|
|
prometheus.GaugeValue,
|
2021-06-14 15:33:27 +00:00
|
|
|
nic.CurrentBandwidth/8,
|
2018-02-21 16:06:07 +00:00
|
|
|
name,
|
|
|
|
)
|
2016-09-27 14:17:03 +00:00
|
|
|
}
|
|
|
|
return nil, nil
|
|
|
|
}
|