2021-12-18 18:01:29 +00:00
|
|
|
//go:build windows
|
2018-11-30 00:51:12 +00:00
|
|
|
// +build windows
|
|
|
|
|
2016-09-19 07:02:05 +00:00
|
|
|
package collector
|
|
|
|
|
|
|
|
import (
|
2021-01-20 00:20:23 +00:00
|
|
|
"github.com/prometheus-community/windows_exporter/headers/sysinfoapi"
|
2021-01-30 10:16:53 +00:00
|
|
|
"github.com/prometheus-community/windows_exporter/log"
|
2021-01-20 00:20:23 +00:00
|
|
|
|
2016-09-19 07:02:05 +00:00
|
|
|
"github.com/prometheus/client_golang/prometheus"
|
|
|
|
)
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *CSCollector) collect(ch chan<- prometheus.Metric) (*prometheus.Desc, error) {
|
2021-01-20 00:20:23 +00:00
|
|
|
// Get systeminfo for number of processors
|
|
|
|
systemInfo := sysinfoapi.GetSystemInfo()
|
2020-12-18 07:02:33 +00:00
|
|
|
|
2021-01-20 00:20:23 +00:00
|
|
|
// Get memory status for physical memory
|
|
|
|
mem, err := sysinfoapi.GlobalMemoryStatusEx()
|
2020-12-18 07:02:33 +00:00
|
|
|
if err != nil {
|
2016-09-19 07:02:05 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
ch <- prometheus.MustNewConstMetric(
|
|
|
|
c.LogicalProcessors,
|
|
|
|
prometheus.GaugeValue,
|
2021-03-03 04:35:04 +00:00
|
|
|
float64(systemInfo.NumberOfProcessors),
|
2016-09-19 07:02:05 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
ch <- prometheus.MustNewConstMetric(
|
|
|
|
c.PhysicalMemoryBytes,
|
|
|
|
prometheus.GaugeValue,
|
2021-03-03 04:35:04 +00:00
|
|
|
float64(mem.TotalPhys),
|
2016-09-19 07:02:05 +00:00
|
|
|
)
|
|
|
|
|
2020-12-18 07:02:33 +00:00
|
|
|
hostname, err := sysinfoapi.GetComputerName(sysinfoapi.ComputerNameDNSHostname)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
domain, err := sysinfoapi.GetComputerName(sysinfoapi.ComputerNameDNSDomain)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
fqdn, err := sysinfoapi.GetComputerName(sysinfoapi.ComputerNameDNSFullyQualified)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2020-02-24 16:00:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ch <- prometheus.MustNewConstMetric(
|
|
|
|
c.Hostname,
|
|
|
|
prometheus.GaugeValue,
|
|
|
|
1.0,
|
2020-12-18 07:02:33 +00:00
|
|
|
hostname,
|
|
|
|
domain,
|
2020-02-24 16:00:22 +00:00
|
|
|
fqdn,
|
|
|
|
)
|
|
|
|
|
2016-09-19 07:02:05 +00:00
|
|
|
return nil, nil
|
|
|
|
}
|