2021-12-18 18:01:29 +00:00
|
|
|
//go:build windows
|
2018-11-30 00:51:12 +00:00
|
|
|
// +build windows
|
|
|
|
|
2017-12-20 13:26:30 +00:00
|
|
|
package collector
|
|
|
|
|
|
|
|
import (
|
2021-01-30 10:16:53 +00:00
|
|
|
"github.com/prometheus-community/windows_exporter/log"
|
2017-12-20 13:26:30 +00:00
|
|
|
"github.com/prometheus/client_golang/prometheus"
|
|
|
|
)
|
|
|
|
|
2020-10-08 18:43:39 +00:00
|
|
|
// A TCPCollector is a Prometheus collector for WMI Win32_PerfRawData_Tcpip_TCPv{4,6} metrics
|
2017-12-20 13:26:30 +00:00
|
|
|
type TCPCollector struct {
|
|
|
|
ConnectionFailures *prometheus.Desc
|
|
|
|
ConnectionsActive *prometheus.Desc
|
|
|
|
ConnectionsEstablished *prometheus.Desc
|
|
|
|
ConnectionsPassive *prometheus.Desc
|
|
|
|
ConnectionsReset *prometheus.Desc
|
|
|
|
SegmentsTotal *prometheus.Desc
|
|
|
|
SegmentsReceivedTotal *prometheus.Desc
|
|
|
|
SegmentsRetransmittedTotal *prometheus.Desc
|
|
|
|
SegmentsSentTotal *prometheus.Desc
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewTCPCollector ...
|
|
|
|
func NewTCPCollector() (Collector, error) {
|
|
|
|
const subsystem = "tcp"
|
|
|
|
|
|
|
|
return &TCPCollector{
|
|
|
|
ConnectionFailures: prometheus.NewDesc(
|
2022-01-02 17:47:17 +00:00
|
|
|
prometheus.BuildFQName(Namespace, subsystem, "connection_failures_total"),
|
2017-12-20 13:26:30 +00:00
|
|
|
"(TCP.ConnectionFailures)",
|
2020-10-08 18:43:39 +00:00
|
|
|
[]string{"af"},
|
2017-12-20 13:26:30 +00:00
|
|
|
nil,
|
|
|
|
),
|
|
|
|
ConnectionsActive: prometheus.NewDesc(
|
2022-01-02 17:47:17 +00:00
|
|
|
prometheus.BuildFQName(Namespace, subsystem, "connections_active_total"),
|
2017-12-20 13:26:30 +00:00
|
|
|
"(TCP.ConnectionsActive)",
|
2020-10-08 18:43:39 +00:00
|
|
|
[]string{"af"},
|
2017-12-20 13:26:30 +00:00
|
|
|
nil,
|
|
|
|
),
|
|
|
|
ConnectionsEstablished: prometheus.NewDesc(
|
|
|
|
prometheus.BuildFQName(Namespace, subsystem, "connections_established"),
|
|
|
|
"(TCP.ConnectionsEstablished)",
|
2020-10-08 18:43:39 +00:00
|
|
|
[]string{"af"},
|
2017-12-20 13:26:30 +00:00
|
|
|
nil,
|
|
|
|
),
|
|
|
|
ConnectionsPassive: prometheus.NewDesc(
|
2022-01-02 17:47:17 +00:00
|
|
|
prometheus.BuildFQName(Namespace, subsystem, "connections_passive_total"),
|
2017-12-20 13:26:30 +00:00
|
|
|
"(TCP.ConnectionsPassive)",
|
2020-10-08 18:43:39 +00:00
|
|
|
[]string{"af"},
|
2017-12-20 13:26:30 +00:00
|
|
|
nil,
|
|
|
|
),
|
|
|
|
ConnectionsReset: prometheus.NewDesc(
|
2022-01-02 17:47:17 +00:00
|
|
|
prometheus.BuildFQName(Namespace, subsystem, "connections_reset_total"),
|
2017-12-20 13:26:30 +00:00
|
|
|
"(TCP.ConnectionsReset)",
|
2020-10-08 18:43:39 +00:00
|
|
|
[]string{"af"},
|
2017-12-20 13:26:30 +00:00
|
|
|
nil,
|
|
|
|
),
|
|
|
|
SegmentsTotal: prometheus.NewDesc(
|
|
|
|
prometheus.BuildFQName(Namespace, subsystem, "segments_total"),
|
|
|
|
"(TCP.SegmentsTotal)",
|
2020-10-08 18:43:39 +00:00
|
|
|
[]string{"af"},
|
2017-12-20 13:26:30 +00:00
|
|
|
nil,
|
|
|
|
),
|
|
|
|
SegmentsReceivedTotal: prometheus.NewDesc(
|
|
|
|
prometheus.BuildFQName(Namespace, subsystem, "segments_received_total"),
|
|
|
|
"(TCP.SegmentsReceivedTotal)",
|
2020-10-08 18:43:39 +00:00
|
|
|
[]string{"af"},
|
2017-12-20 13:26:30 +00:00
|
|
|
nil,
|
|
|
|
),
|
|
|
|
SegmentsRetransmittedTotal: prometheus.NewDesc(
|
|
|
|
prometheus.BuildFQName(Namespace, subsystem, "segments_retransmitted_total"),
|
|
|
|
"(TCP.SegmentsRetransmittedTotal)",
|
2020-10-08 18:43:39 +00:00
|
|
|
[]string{"af"},
|
2017-12-20 13:26:30 +00:00
|
|
|
nil,
|
|
|
|
),
|
|
|
|
SegmentsSentTotal: prometheus.NewDesc(
|
|
|
|
prometheus.BuildFQName(Namespace, subsystem, "segments_sent_total"),
|
|
|
|
"(TCP.SegmentsSentTotal)",
|
2020-10-08 18:43:39 +00:00
|
|
|
[]string{"af"},
|
2017-12-20 13:26:30 +00:00
|
|
|
nil,
|
|
|
|
),
|
|
|
|
}, 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 *TCPCollector) Collect(ctx *ScrapeContext, ch chan<- prometheus.Metric) error {
|
2020-10-08 05:25:58 +00:00
|
|
|
if desc, err := c.collect(ctx, ch); err != nil {
|
2018-04-05 05:27:26 +00:00
|
|
|
log.Error("failed collecting tcp metrics:", desc, err)
|
2017-12-20 13:26:30 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-10-04 19:35:33 +00:00
|
|
|
// Win32_PerfRawData_Tcpip_TCPv4 docs
|
|
|
|
// - https://msdn.microsoft.com/en-us/library/aa394341(v=vs.85).aspx
|
2020-10-08 18:43:39 +00:00
|
|
|
// The TCPv6 performance object uses the same fields.
|
2020-10-08 05:25:58 +00:00
|
|
|
type tcp struct {
|
|
|
|
ConnectionFailures float64 `perflib:"Connection Failures"`
|
|
|
|
ConnectionsActive float64 `perflib:"Connections Active"`
|
|
|
|
ConnectionsEstablished float64 `perflib:"Connections Established"`
|
|
|
|
ConnectionsPassive float64 `perflib:"Connections Passive"`
|
|
|
|
ConnectionsReset float64 `perflib:"Connections Reset"`
|
|
|
|
SegmentsPersec float64 `perflib:"Segments/sec"`
|
|
|
|
SegmentsReceivedPersec float64 `perflib:"Segments Received/sec"`
|
|
|
|
SegmentsRetransmittedPersec float64 `perflib:"Segments Retransmitted/sec"`
|
|
|
|
SegmentsSentPersec float64 `perflib:"Segments Sent/sec"`
|
2017-12-20 13:26:30 +00:00
|
|
|
}
|
|
|
|
|
2020-10-08 18:43:39 +00:00
|
|
|
func writeTCPCounters(metrics tcp, labels []string, c *TCPCollector, ch chan<- prometheus.Metric) {
|
2017-12-20 13:26:30 +00:00
|
|
|
ch <- prometheus.MustNewConstMetric(
|
|
|
|
c.ConnectionFailures,
|
|
|
|
prometheus.CounterValue,
|
2020-10-08 18:43:39 +00:00
|
|
|
metrics.ConnectionFailures,
|
|
|
|
labels...,
|
2017-12-20 13:26:30 +00:00
|
|
|
)
|
|
|
|
ch <- prometheus.MustNewConstMetric(
|
|
|
|
c.ConnectionsActive,
|
|
|
|
prometheus.CounterValue,
|
2020-10-08 18:43:39 +00:00
|
|
|
metrics.ConnectionsActive,
|
|
|
|
labels...,
|
2017-12-20 13:26:30 +00:00
|
|
|
)
|
|
|
|
ch <- prometheus.MustNewConstMetric(
|
|
|
|
c.ConnectionsEstablished,
|
2019-09-27 09:48:16 +00:00
|
|
|
prometheus.GaugeValue,
|
2020-10-08 18:43:39 +00:00
|
|
|
metrics.ConnectionsEstablished,
|
|
|
|
labels...,
|
2017-12-20 13:26:30 +00:00
|
|
|
)
|
|
|
|
ch <- prometheus.MustNewConstMetric(
|
|
|
|
c.ConnectionsPassive,
|
|
|
|
prometheus.CounterValue,
|
2020-10-08 18:43:39 +00:00
|
|
|
metrics.ConnectionsPassive,
|
|
|
|
labels...,
|
2017-12-20 13:26:30 +00:00
|
|
|
)
|
|
|
|
ch <- prometheus.MustNewConstMetric(
|
|
|
|
c.ConnectionsReset,
|
|
|
|
prometheus.CounterValue,
|
2020-10-08 18:43:39 +00:00
|
|
|
metrics.ConnectionsReset,
|
|
|
|
labels...,
|
2017-12-20 13:26:30 +00:00
|
|
|
)
|
|
|
|
ch <- prometheus.MustNewConstMetric(
|
|
|
|
c.SegmentsTotal,
|
|
|
|
prometheus.CounterValue,
|
2020-10-08 18:43:39 +00:00
|
|
|
metrics.SegmentsPersec,
|
|
|
|
labels...,
|
2017-12-20 13:26:30 +00:00
|
|
|
)
|
|
|
|
ch <- prometheus.MustNewConstMetric(
|
|
|
|
c.SegmentsReceivedTotal,
|
|
|
|
prometheus.CounterValue,
|
2020-10-08 18:43:39 +00:00
|
|
|
metrics.SegmentsReceivedPersec,
|
|
|
|
labels...,
|
2017-12-20 13:26:30 +00:00
|
|
|
)
|
|
|
|
ch <- prometheus.MustNewConstMetric(
|
|
|
|
c.SegmentsRetransmittedTotal,
|
|
|
|
prometheus.CounterValue,
|
2020-10-08 18:43:39 +00:00
|
|
|
metrics.SegmentsRetransmittedPersec,
|
|
|
|
labels...,
|
2017-12-20 13:26:30 +00:00
|
|
|
)
|
|
|
|
ch <- prometheus.MustNewConstMetric(
|
|
|
|
c.SegmentsSentTotal,
|
|
|
|
prometheus.CounterValue,
|
2020-10-08 18:43:39 +00:00
|
|
|
metrics.SegmentsSentPersec,
|
|
|
|
labels...,
|
2017-12-20 13:26:30 +00:00
|
|
|
)
|
2020-10-08 18:43:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *TCPCollector) collect(ctx *ScrapeContext, ch chan<- prometheus.Metric) (*prometheus.Desc, error) {
|
|
|
|
var dst []tcp
|
|
|
|
|
|
|
|
// TCPv4 counters
|
|
|
|
if err := unmarshalObject(ctx.perfObjects["TCPv4"], &dst); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if len(dst) != 0 {
|
|
|
|
writeTCPCounters(dst[0], []string{"ipv4"}, c, ch)
|
|
|
|
}
|
|
|
|
|
|
|
|
// TCPv6 counters
|
|
|
|
if err := unmarshalObject(ctx.perfObjects["TCPv6"], &dst); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if len(dst) != 0 {
|
|
|
|
writeTCPCounters(dst[0], []string{"ipv6"}, c, ch)
|
|
|
|
}
|
2017-12-20 13:26:30 +00:00
|
|
|
|
|
|
|
return nil, nil
|
|
|
|
}
|