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() {
|
2020-02-09 20:09:26 +00:00
|
|
|
registerCollector("cs", NewCSCollector)
|
2016-09-19 07:02:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// A CSCollector is a Prometheus collector for WMI metrics
|
|
|
|
type CSCollector struct {
|
|
|
|
PhysicalMemoryBytes *prometheus.Desc
|
|
|
|
LogicalProcessors *prometheus.Desc
|
2020-02-24 16:00:22 +00:00
|
|
|
Hostname *prometheus.Desc
|
2016-09-19 07:02:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewCSCollector ...
|
|
|
|
func NewCSCollector() (Collector, error) {
|
|
|
|
const subsystem = "cs"
|
|
|
|
|
|
|
|
return &CSCollector{
|
|
|
|
LogicalProcessors: prometheus.NewDesc(
|
|
|
|
prometheus.BuildFQName(Namespace, subsystem, "logical_processors"),
|
|
|
|
"ComputerSystem.NumberOfLogicalProcessors",
|
|
|
|
nil,
|
|
|
|
nil,
|
|
|
|
),
|
|
|
|
PhysicalMemoryBytes: prometheus.NewDesc(
|
|
|
|
prometheus.BuildFQName(Namespace, subsystem, "physical_memory_bytes"),
|
|
|
|
"ComputerSystem.TotalPhysicalMemory",
|
|
|
|
nil,
|
|
|
|
nil,
|
|
|
|
),
|
2020-02-24 16:00:22 +00:00
|
|
|
Hostname: prometheus.NewDesc(
|
|
|
|
prometheus.BuildFQName(Namespace, subsystem, "hostname"),
|
|
|
|
"Labeled system hostname information as provided by ComputerSystem.DNSHostName and ComputerSystem.Domain",
|
|
|
|
[]string{
|
|
|
|
"hostname",
|
|
|
|
"domain",
|
|
|
|
"fqdn"},
|
|
|
|
nil,
|
|
|
|
),
|
2016-09-19 07:02:05 +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 *CSCollector) Collect(ctx *ScrapeContext, ch chan<- prometheus.Metric) error {
|
2016-09-19 07:02:05 +00:00
|
|
|
if desc, err := c.collect(ch); err != nil {
|
2018-04-05 05:27:26 +00:00
|
|
|
log.Error("failed collecting cs metrics:", desc, err)
|
2016-09-19 07:02:05 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-10-04 19:35:33 +00:00
|
|
|
// Win32_ComputerSystem docs:
|
|
|
|
// - https://msdn.microsoft.com/en-us/library/aa394102
|
2016-09-19 07:02:05 +00:00
|
|
|
type Win32_ComputerSystem struct {
|
|
|
|
NumberOfLogicalProcessors uint32
|
|
|
|
TotalPhysicalMemory uint64
|
2020-02-24 16:00:22 +00:00
|
|
|
DNSHostname string
|
|
|
|
Domain string
|
2020-03-04 13:49:59 +00:00
|
|
|
Workgroup *string
|
2016-09-19 07:02:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *CSCollector) collect(ch chan<- prometheus.Metric) (*prometheus.Desc, error) {
|
|
|
|
var dst []Win32_ComputerSystem
|
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-09-19 07:02:05 +00:00
|
|
|
|
|
|
|
ch <- prometheus.MustNewConstMetric(
|
|
|
|
c.LogicalProcessors,
|
|
|
|
prometheus.GaugeValue,
|
|
|
|
float64(dst[0].NumberOfLogicalProcessors),
|
|
|
|
)
|
|
|
|
|
|
|
|
ch <- prometheus.MustNewConstMetric(
|
|
|
|
c.PhysicalMemoryBytes,
|
|
|
|
prometheus.GaugeValue,
|
|
|
|
float64(dst[0].TotalPhysicalMemory),
|
|
|
|
)
|
|
|
|
|
2020-02-24 16:00:22 +00:00
|
|
|
var fqdn string
|
2020-03-04 13:49:59 +00:00
|
|
|
if dst[0].Workgroup == nil || dst[0].Domain != *dst[0].Workgroup {
|
2020-02-24 16:00:22 +00:00
|
|
|
fqdn = dst[0].DNSHostname + "." + dst[0].Domain
|
|
|
|
} else {
|
|
|
|
fqdn = dst[0].DNSHostname
|
|
|
|
}
|
|
|
|
|
|
|
|
ch <- prometheus.MustNewConstMetric(
|
|
|
|
c.Hostname,
|
|
|
|
prometheus.GaugeValue,
|
|
|
|
1.0,
|
|
|
|
dst[0].DNSHostname,
|
|
|
|
dst[0].Domain,
|
|
|
|
fqdn,
|
|
|
|
)
|
|
|
|
|
2016-09-19 07:02:05 +00:00
|
|
|
return nil, nil
|
|
|
|
}
|