2023-11-04 19:51:35 +00:00
|
|
|
//go:build windows
|
|
|
|
|
|
|
|
package cs
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/alecthomas/kingpin/v2"
|
|
|
|
"github.com/go-kit/log"
|
|
|
|
"github.com/go-kit/log/level"
|
|
|
|
"github.com/prometheus-community/windows_exporter/pkg/headers/sysinfoapi"
|
|
|
|
"github.com/prometheus-community/windows_exporter/pkg/types"
|
|
|
|
"github.com/prometheus/client_golang/prometheus"
|
|
|
|
)
|
|
|
|
|
|
|
|
const Name = "cs"
|
|
|
|
|
|
|
|
type Config struct{}
|
|
|
|
|
|
|
|
var ConfigDefaults = Config{}
|
|
|
|
|
2024-08-05 13:50:41 +00:00
|
|
|
// A Collector is a Prometheus Collector for WMI metrics
|
|
|
|
type Collector struct {
|
2023-11-04 19:51:35 +00:00
|
|
|
logger log.Logger
|
|
|
|
|
|
|
|
PhysicalMemoryBytes *prometheus.Desc
|
|
|
|
LogicalProcessors *prometheus.Desc
|
2024-08-05 13:50:41 +00:00
|
|
|
hostname *prometheus.Desc
|
2023-11-04 19:51:35 +00:00
|
|
|
}
|
|
|
|
|
2024-08-05 13:50:41 +00:00
|
|
|
func New(logger log.Logger, _ *Config) *Collector {
|
|
|
|
c := &Collector{}
|
2023-11-04 19:51:35 +00:00
|
|
|
c.SetLogger(logger)
|
2024-08-05 13:50:41 +00:00
|
|
|
|
2023-11-04 19:51:35 +00:00
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
2024-08-05 13:50:41 +00:00
|
|
|
func NewWithFlags(_ *kingpin.Application) *Collector {
|
|
|
|
return &Collector{}
|
2023-11-04 19:51:35 +00:00
|
|
|
}
|
|
|
|
|
2024-08-05 13:50:41 +00:00
|
|
|
func (c *Collector) GetName() string {
|
2023-11-04 19:51:35 +00:00
|
|
|
return Name
|
|
|
|
}
|
|
|
|
|
2024-08-05 13:50:41 +00:00
|
|
|
func (c *Collector) SetLogger(logger log.Logger) {
|
2023-11-04 19:51:35 +00:00
|
|
|
c.logger = log.With(logger, "collector", Name)
|
|
|
|
}
|
|
|
|
|
2024-08-05 13:50:41 +00:00
|
|
|
func (c *Collector) GetPerfCounter() ([]string, error) {
|
2023-11-04 19:51:35 +00:00
|
|
|
return []string{}, nil
|
|
|
|
}
|
|
|
|
|
2024-08-05 13:50:41 +00:00
|
|
|
func (c *Collector) Close() error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Collector) Build() error {
|
2023-11-04 19:51:35 +00:00
|
|
|
c.LogicalProcessors = prometheus.NewDesc(
|
|
|
|
prometheus.BuildFQName(types.Namespace, Name, "logical_processors"),
|
|
|
|
"ComputerSystem.NumberOfLogicalProcessors",
|
|
|
|
nil,
|
|
|
|
nil,
|
|
|
|
)
|
|
|
|
c.PhysicalMemoryBytes = prometheus.NewDesc(
|
|
|
|
prometheus.BuildFQName(types.Namespace, Name, "physical_memory_bytes"),
|
|
|
|
"ComputerSystem.TotalPhysicalMemory",
|
|
|
|
nil,
|
|
|
|
nil,
|
|
|
|
)
|
2024-08-05 13:50:41 +00:00
|
|
|
c.hostname = prometheus.NewDesc(
|
2023-11-04 19:51:35 +00:00
|
|
|
prometheus.BuildFQName(types.Namespace, Name, "hostname"),
|
2024-02-01 08:46:33 +00:00
|
|
|
"Labelled system hostname information as provided by ComputerSystem.DNSHostName and ComputerSystem.Domain",
|
2023-11-04 19:51:35 +00:00
|
|
|
[]string{
|
|
|
|
"hostname",
|
|
|
|
"domain",
|
|
|
|
"fqdn",
|
|
|
|
},
|
|
|
|
nil,
|
|
|
|
)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Collect sends the metric values for each metric
|
|
|
|
// to the provided prometheus Metric channel.
|
2024-08-05 13:50:41 +00:00
|
|
|
func (c *Collector) Collect(_ *types.ScrapeContext, ch chan<- prometheus.Metric) error {
|
2024-05-11 10:05:45 +00:00
|
|
|
if err := c.collect(ch); err != nil {
|
|
|
|
_ = level.Error(c.logger).Log("msg", "failed collecting cs metrics", "err", err)
|
2023-11-04 19:51:35 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-08-05 13:50:41 +00:00
|
|
|
func (c *Collector) collect(ch chan<- prometheus.Metric) error {
|
2023-11-04 19:51:35 +00:00
|
|
|
// Get systeminfo for number of processors
|
|
|
|
systemInfo := sysinfoapi.GetSystemInfo()
|
|
|
|
|
|
|
|
// Get memory status for physical memory
|
|
|
|
mem, err := sysinfoapi.GlobalMemoryStatusEx()
|
|
|
|
if err != nil {
|
2024-05-11 10:05:45 +00:00
|
|
|
return err
|
2023-11-04 19:51:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ch <- prometheus.MustNewConstMetric(
|
|
|
|
c.LogicalProcessors,
|
|
|
|
prometheus.GaugeValue,
|
|
|
|
float64(systemInfo.NumberOfProcessors),
|
|
|
|
)
|
|
|
|
|
|
|
|
ch <- prometheus.MustNewConstMetric(
|
|
|
|
c.PhysicalMemoryBytes,
|
|
|
|
prometheus.GaugeValue,
|
|
|
|
float64(mem.TotalPhys),
|
|
|
|
)
|
|
|
|
|
|
|
|
hostname, err := sysinfoapi.GetComputerName(sysinfoapi.ComputerNameDNSHostname)
|
|
|
|
if err != nil {
|
2024-05-11 10:05:45 +00:00
|
|
|
return err
|
2023-11-04 19:51:35 +00:00
|
|
|
}
|
|
|
|
domain, err := sysinfoapi.GetComputerName(sysinfoapi.ComputerNameDNSDomain)
|
|
|
|
if err != nil {
|
2024-05-11 10:05:45 +00:00
|
|
|
return err
|
2023-11-04 19:51:35 +00:00
|
|
|
}
|
|
|
|
fqdn, err := sysinfoapi.GetComputerName(sysinfoapi.ComputerNameDNSFullyQualified)
|
|
|
|
if err != nil {
|
2024-05-11 10:05:45 +00:00
|
|
|
return err
|
2023-11-04 19:51:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ch <- prometheus.MustNewConstMetric(
|
2024-08-05 13:50:41 +00:00
|
|
|
c.hostname,
|
2023-11-04 19:51:35 +00:00
|
|
|
prometheus.GaugeValue,
|
|
|
|
1.0,
|
|
|
|
hostname,
|
|
|
|
domain,
|
|
|
|
fqdn,
|
|
|
|
)
|
|
|
|
|
2024-05-11 10:05:45 +00:00
|
|
|
return nil
|
2023-11-04 19:51:35 +00:00
|
|
|
}
|