2018-11-30 00:51:12 +00:00
|
|
|
// +build windows
|
|
|
|
|
2016-09-19 07:02:05 +00:00
|
|
|
package collector
|
|
|
|
|
|
|
|
import (
|
2018-08-07 19:42:16 +00:00
|
|
|
"errors"
|
2016-09-19 07:02:05 +00:00
|
|
|
"github.com/StackExchange/wmi"
|
|
|
|
"github.com/prometheus/client_golang/prometheus"
|
2018-04-05 05:27:26 +00:00
|
|
|
"github.com/prometheus/common/log"
|
2016-09-19 07:02:05 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
2016-10-14 08:14:32 +00:00
|
|
|
Factories["system"] = NewSystemCollector
|
2016-09-19 07:02:05 +00:00
|
|
|
}
|
|
|
|
|
2016-11-14 07:55:51 +00:00
|
|
|
// A SystemCollector is a Prometheus collector for WMI metrics
|
2016-09-19 07:02:05 +00:00
|
|
|
type SystemCollector struct {
|
|
|
|
ContextSwitchesTotal *prometheus.Desc
|
|
|
|
ExceptionDispatchesTotal *prometheus.Desc
|
|
|
|
ProcessorQueueLength *prometheus.Desc
|
|
|
|
SystemCallsTotal *prometheus.Desc
|
|
|
|
SystemUpTime *prometheus.Desc
|
|
|
|
Threads *prometheus.Desc
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewSystemCollector ...
|
|
|
|
func NewSystemCollector() (Collector, error) {
|
|
|
|
const subsystem = "system"
|
|
|
|
|
|
|
|
return &SystemCollector{
|
|
|
|
ContextSwitchesTotal: prometheus.NewDesc(
|
|
|
|
prometheus.BuildFQName(Namespace, subsystem, "context_switches_total"),
|
2018-04-29 15:14:53 +00:00
|
|
|
"Total number of context switches (WMI source: PerfOS_System.ContextSwitchesPersec)",
|
2016-09-19 07:02:05 +00:00
|
|
|
nil,
|
|
|
|
nil,
|
|
|
|
),
|
|
|
|
ExceptionDispatchesTotal: prometheus.NewDesc(
|
|
|
|
prometheus.BuildFQName(Namespace, subsystem, "exception_dispatches_total"),
|
2018-04-29 15:14:53 +00:00
|
|
|
"Total number of exceptions dispatched (WMI source: PerfOS_System.ExceptionDispatchesPersec)",
|
2016-09-19 07:02:05 +00:00
|
|
|
nil,
|
|
|
|
nil,
|
|
|
|
),
|
|
|
|
ProcessorQueueLength: prometheus.NewDesc(
|
|
|
|
prometheus.BuildFQName(Namespace, subsystem, "processor_queue_length"),
|
2018-04-29 15:14:53 +00:00
|
|
|
"Length of processor queue (WMI source: PerfOS_System.ProcessorQueueLength)",
|
2016-09-19 07:02:05 +00:00
|
|
|
nil,
|
|
|
|
nil,
|
|
|
|
),
|
|
|
|
SystemCallsTotal: prometheus.NewDesc(
|
|
|
|
prometheus.BuildFQName(Namespace, subsystem, "system_calls_total"),
|
2018-04-29 15:14:53 +00:00
|
|
|
"Total number of system calls (WMI source: PerfOS_System.SystemCallsPersec)",
|
2016-09-19 07:02:05 +00:00
|
|
|
nil,
|
|
|
|
nil,
|
|
|
|
),
|
|
|
|
SystemUpTime: prometheus.NewDesc(
|
|
|
|
prometheus.BuildFQName(Namespace, subsystem, "system_up_time"),
|
2018-04-29 15:14:53 +00:00
|
|
|
"System boot time (WMI source: PerfOS_System.SystemUpTime)",
|
2016-09-19 07:02:05 +00:00
|
|
|
nil,
|
|
|
|
nil,
|
|
|
|
),
|
|
|
|
Threads: prometheus.NewDesc(
|
|
|
|
prometheus.BuildFQName(Namespace, subsystem, "threads"),
|
2018-04-29 15:14:53 +00:00
|
|
|
"Current number of threads (WMI source: PerfOS_System.Threads)",
|
2016-09-19 07:02:05 +00:00
|
|
|
nil,
|
|
|
|
nil,
|
|
|
|
),
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Collect sends the metric values for each metric
|
|
|
|
// to the provided prometheus Metric channel.
|
|
|
|
func (c *SystemCollector) Collect(ch chan<- prometheus.Metric) error {
|
|
|
|
if desc, err := c.collect(ch); err != nil {
|
2018-04-05 05:27:26 +00:00
|
|
|
log.Error("failed collecting system metrics:", desc, err)
|
2016-09-19 07:02:05 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-10-04 19:35:33 +00:00
|
|
|
// Win32_PerfRawData_PerfOS_System docs:
|
|
|
|
// - https://web.archive.org/web/20050830140516/http://msdn.microsoft.com/library/en-us/wmisdk/wmi/win32_perfrawdata_perfos_system.asp
|
2016-09-19 07:02:05 +00:00
|
|
|
type Win32_PerfRawData_PerfOS_System struct {
|
|
|
|
ContextSwitchesPersec uint32
|
|
|
|
ExceptionDispatchesPersec uint32
|
2016-11-15 08:07:04 +00:00
|
|
|
Frequency_Object uint64
|
2016-09-19 07:02:05 +00:00
|
|
|
ProcessorQueueLength uint32
|
|
|
|
SystemCallsPersec uint32
|
|
|
|
SystemUpTime uint64
|
|
|
|
Threads uint32
|
2016-11-15 08:07:04 +00:00
|
|
|
Timestamp_Object uint64
|
2016-09-19 07:02:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *SystemCollector) collect(ch chan<- prometheus.Metric) (*prometheus.Desc, error) {
|
|
|
|
var dst []Win32_PerfRawData_PerfOS_System
|
2018-06-06 09:35:13 +00:00
|
|
|
q := queryAll(&dst)
|
|
|
|
if err := wmi.Query(q, &dst); err != nil {
|
2016-09-19 07:02:05 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
2018-08-07 19:42:16 +00:00
|
|
|
if len(dst) == 0 {
|
|
|
|
return nil, errors.New("WMI query returned empty result set")
|
|
|
|
}
|
2016-11-15 08:07:04 +00:00
|
|
|
|
2016-09-19 07:02:05 +00:00
|
|
|
ch <- prometheus.MustNewConstMetric(
|
|
|
|
c.ContextSwitchesTotal,
|
2018-04-29 15:14:53 +00:00
|
|
|
prometheus.CounterValue,
|
2016-09-19 07:02:05 +00:00
|
|
|
float64(dst[0].ContextSwitchesPersec),
|
|
|
|
)
|
|
|
|
ch <- prometheus.MustNewConstMetric(
|
|
|
|
c.ExceptionDispatchesTotal,
|
2018-04-29 15:14:53 +00:00
|
|
|
prometheus.CounterValue,
|
2016-09-19 07:02:05 +00:00
|
|
|
float64(dst[0].ExceptionDispatchesPersec),
|
|
|
|
)
|
|
|
|
ch <- prometheus.MustNewConstMetric(
|
|
|
|
c.ProcessorQueueLength,
|
|
|
|
prometheus.GaugeValue,
|
|
|
|
float64(dst[0].ProcessorQueueLength),
|
|
|
|
)
|
|
|
|
ch <- prometheus.MustNewConstMetric(
|
|
|
|
c.SystemCallsTotal,
|
2018-04-29 15:14:53 +00:00
|
|
|
prometheus.CounterValue,
|
2016-09-19 07:02:05 +00:00
|
|
|
float64(dst[0].SystemCallsPersec),
|
|
|
|
)
|
|
|
|
ch <- prometheus.MustNewConstMetric(
|
|
|
|
c.SystemUpTime,
|
|
|
|
prometheus.GaugeValue,
|
2016-11-15 08:07:04 +00:00
|
|
|
// convert from Windows timestamp (1 jan 1601) to unix timestamp (1 jan 1970)
|
|
|
|
float64(dst[0].SystemUpTime-116444736000000000)/float64(dst[0].Frequency_Object),
|
2016-09-19 07:02:05 +00:00
|
|
|
)
|
|
|
|
ch <- prometheus.MustNewConstMetric(
|
|
|
|
c.Threads,
|
|
|
|
prometheus.GaugeValue,
|
|
|
|
float64(dst[0].Threads),
|
|
|
|
)
|
|
|
|
return nil, nil
|
|
|
|
}
|