2023-11-04 19:51:35 +00:00
|
|
|
//go:build windows
|
|
|
|
|
|
|
|
package cs
|
|
|
|
|
|
|
|
import (
|
2024-09-10 22:34:10 +00:00
|
|
|
"log/slog"
|
|
|
|
|
2023-11-04 19:51:35 +00:00
|
|
|
"github.com/alecthomas/kingpin/v2"
|
2024-10-03 18:23:56 +00:00
|
|
|
"github.com/prometheus-community/windows_exporter/internal/headers/sysinfoapi"
|
2024-11-03 16:23:26 +00:00
|
|
|
"github.com/prometheus-community/windows_exporter/internal/mi"
|
2024-10-03 18:34:45 +00:00
|
|
|
"github.com/prometheus-community/windows_exporter/internal/types"
|
2023-11-04 19:51:35 +00:00
|
|
|
"github.com/prometheus/client_golang/prometheus"
|
|
|
|
)
|
|
|
|
|
|
|
|
const Name = "cs"
|
|
|
|
|
|
|
|
type Config struct{}
|
|
|
|
|
|
|
|
var ConfigDefaults = Config{}
|
|
|
|
|
2024-08-10 20:05:33 +00:00
|
|
|
// A Collector is a Prometheus Collector for WMI metrics.
|
2024-08-05 13:50:41 +00:00
|
|
|
type Collector struct {
|
2024-08-11 11:28:39 +00:00
|
|
|
config Config
|
2023-11-04 19:51:35 +00:00
|
|
|
|
2024-09-10 22:34:10 +00:00
|
|
|
// physicalMemoryBytes
|
|
|
|
// Deprecated: Use windows_cpu_logical_processor instead
|
2024-08-05 21:40:32 +00:00
|
|
|
physicalMemoryBytes *prometheus.Desc
|
2024-09-10 22:34:10 +00:00
|
|
|
// logicalProcessors
|
|
|
|
// Deprecated: Use windows_physical_memory_total_bytes instead
|
|
|
|
logicalProcessors *prometheus.Desc
|
|
|
|
// hostname
|
|
|
|
// Deprecated: Use windows_os_hostname instead
|
|
|
|
hostname *prometheus.Desc
|
2023-11-04 19:51:35 +00:00
|
|
|
}
|
|
|
|
|
2024-08-24 17:14:38 +00:00
|
|
|
func New(config *Config) *Collector {
|
2024-08-11 11:28:39 +00:00
|
|
|
if config == nil {
|
|
|
|
config = &ConfigDefaults
|
|
|
|
}
|
|
|
|
|
|
|
|
c := &Collector{
|
|
|
|
config: *config,
|
|
|
|
}
|
|
|
|
|
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-09-10 22:34:10 +00:00
|
|
|
func (c *Collector) GetPerfCounter(_ *slog.Logger) ([]string, error) {
|
2023-11-04 19:51:35 +00:00
|
|
|
return []string{}, nil
|
|
|
|
}
|
|
|
|
|
2024-09-10 22:34:10 +00:00
|
|
|
func (c *Collector) Close(_ *slog.Logger) error {
|
2024-08-05 13:50:41 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-11-03 16:23:26 +00:00
|
|
|
func (c *Collector) Build(logger *slog.Logger, _ *mi.Session) error {
|
2024-09-10 22:34:10 +00:00
|
|
|
logger.Warn("The cs collector is deprecated and will be removed in a future release. " +
|
|
|
|
"Logical processors has been moved to cpu_info collector. " +
|
|
|
|
"Physical memory has been moved to memory collector. " +
|
|
|
|
"Hostname has been moved to os collector.")
|
|
|
|
|
2024-08-05 21:40:32 +00:00
|
|
|
c.logicalProcessors = prometheus.NewDesc(
|
2024-10-03 18:34:45 +00:00
|
|
|
prometheus.BuildFQName(types.Namespace, Name, "logical_processors"),
|
2024-09-10 22:34:10 +00:00
|
|
|
"Deprecated: Use windows_cpu_logical_processor instead",
|
2023-11-04 19:51:35 +00:00
|
|
|
nil,
|
|
|
|
nil,
|
|
|
|
)
|
2024-08-05 21:40:32 +00:00
|
|
|
c.physicalMemoryBytes = prometheus.NewDesc(
|
2024-10-03 18:34:45 +00:00
|
|
|
prometheus.BuildFQName(types.Namespace, Name, "physical_memory_bytes"),
|
2024-09-10 22:34:10 +00:00
|
|
|
"Deprecated: Use windows_physical_memory_total_bytes instead",
|
2023-11-04 19:51:35 +00:00
|
|
|
nil,
|
|
|
|
nil,
|
|
|
|
)
|
2024-08-05 13:50:41 +00:00
|
|
|
c.hostname = prometheus.NewDesc(
|
2024-10-03 18:34:45 +00:00
|
|
|
prometheus.BuildFQName(types.Namespace, Name, "hostname"),
|
2024-09-10 22:34:10 +00:00
|
|
|
"Deprecated: Use windows_os_hostname instead",
|
2023-11-04 19:51:35 +00:00
|
|
|
[]string{
|
|
|
|
"hostname",
|
|
|
|
"domain",
|
|
|
|
"fqdn",
|
|
|
|
},
|
|
|
|
nil,
|
|
|
|
)
|
2024-09-10 22:34:10 +00:00
|
|
|
|
2023-11-04 19:51:35 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Collect sends the metric values for each metric
|
|
|
|
// to the provided prometheus Metric channel.
|
2024-10-03 18:34:45 +00:00
|
|
|
func (c *Collector) Collect(_ *types.ScrapeContext, logger *slog.Logger, ch chan<- prometheus.Metric) error {
|
2024-09-10 22:34:10 +00:00
|
|
|
logger = logger.With(slog.String("collector", Name))
|
2024-09-27 09:09:17 +00:00
|
|
|
|
2024-05-11 10:05:45 +00:00
|
|
|
if err := c.collect(ch); err != nil {
|
2024-09-10 22:34:10 +00:00
|
|
|
logger.Error("failed collecting cs metrics",
|
|
|
|
slog.Any("err", err),
|
|
|
|
)
|
|
|
|
|
2023-11-04 19:51:35 +00:00
|
|
|
return err
|
|
|
|
}
|
2024-09-10 22:34:10 +00:00
|
|
|
|
2023-11-04 19:51:35 +00:00
|
|
|
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(
|
2024-08-05 21:40:32 +00:00
|
|
|
c.logicalProcessors,
|
2023-11-04 19:51:35 +00:00
|
|
|
prometheus.GaugeValue,
|
|
|
|
float64(systemInfo.NumberOfProcessors),
|
|
|
|
)
|
|
|
|
|
|
|
|
ch <- prometheus.MustNewConstMetric(
|
2024-08-05 21:40:32 +00:00
|
|
|
c.physicalMemoryBytes,
|
2023-11-04 19:51:35 +00:00
|
|
|
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
|
|
|
}
|
2024-09-10 22:34:10 +00:00
|
|
|
|
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
|
|
|
}
|
2024-09-10 22:34:10 +00:00
|
|
|
|
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
|
|
|
}
|