From 52592bb097fb4a9e833dc26cdf22911e100ad642 Mon Sep 17 00:00:00 2001 From: Martin Lindhe Date: Mon, 19 Sep 2016 09:02:05 +0200 Subject: [PATCH] os: expose Win32_ComputerSystem and Win32_PerfRawData_PerfOS_System --- collector/cs.go | 77 +++++++++++++++++++++++++++ collector/os.go | 60 +++++++++------------ collector/system.go | 125 ++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 226 insertions(+), 36 deletions(-) create mode 100644 collector/cs.go create mode 100644 collector/system.go diff --git a/collector/cs.go b/collector/cs.go new file mode 100644 index 00000000..49cbe77f --- /dev/null +++ b/collector/cs.go @@ -0,0 +1,77 @@ +// returns data points from Win32_ComputerSystem +// https://msdn.microsoft.com/en-us/library/aa394102 - Win32_ComputerSystem class + +package collector + +import ( + "log" + + "github.com/StackExchange/wmi" + "github.com/prometheus/client_golang/prometheus" +) + +func init() { + Factories["cs"] = NewCSCollector +} + +// A CSCollector is a Prometheus collector for WMI metrics +type CSCollector struct { + PhysicalMemoryBytes *prometheus.Desc + LogicalProcessors *prometheus.Desc +} + +// 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, + ), + }, nil +} + +// Collect sends the metric values for each metric +// to the provided prometheus Metric channel. +func (c *CSCollector) Collect(ch chan<- prometheus.Metric) error { + if desc, err := c.collect(ch); err != nil { + log.Println("[ERROR] failed collecting cs metrics:", desc, err) + return err + } + return nil +} + +type Win32_ComputerSystem struct { + NumberOfLogicalProcessors uint32 + TotalPhysicalMemory uint64 +} + +func (c *CSCollector) collect(ch chan<- prometheus.Metric) (*prometheus.Desc, error) { + var dst []Win32_ComputerSystem + if err := wmi.Query(wmi.CreateQuery(&dst, ""), &dst); err != nil { + return nil, err + } + + ch <- prometheus.MustNewConstMetric( + c.LogicalProcessors, + prometheus.GaugeValue, + float64(dst[0].NumberOfLogicalProcessors), + ) + + ch <- prometheus.MustNewConstMetric( + c.PhysicalMemoryBytes, + prometheus.GaugeValue, + float64(dst[0].TotalPhysicalMemory), + ) + + return nil, nil +} diff --git a/collector/os.go b/collector/os.go index d46f58ea..e4f290da 100644 --- a/collector/os.go +++ b/collector/os.go @@ -14,93 +14,82 @@ func init() { Factories["os"] = NewOSCollector } -// A OSCollector is a Prometheus collector for WMI Win32_OperatingSystem metrics +// A OSCollector is a Prometheus collector for WMI metrics type OSCollector struct { PhysicalMemoryFreeBytes *prometheus.Desc PagingFreeBytes *prometheus.Desc VirtualMemoryFreeBytes *prometheus.Desc - ProcessesMax *prometheus.Desc - ProcessMemoryMaxBytes *prometheus.Desc + ProcessesLimit *prometheus.Desc + ProcessMemoryLimitBytes *prometheus.Desc Processes *prometheus.Desc Users *prometheus.Desc - PagingMaxBytes *prometheus.Desc + PagingLimitBytes *prometheus.Desc VirtualMemoryBytes *prometheus.Desc VisibleMemoryBytes *prometheus.Desc } // NewOSCollector ... func NewOSCollector() (Collector, error) { - const subsystem = "os" return &OSCollector{ - - PagingMaxBytes: prometheus.NewDesc( - prometheus.BuildFQName(Namespace, subsystem, "paging_max_bytes"), - "SizeStoredInPagingFiles", + PagingLimitBytes: prometheus.NewDesc( + prometheus.BuildFQName(Namespace, subsystem, "paging_limit_bytes"), + "OperatingSystem.SizeStoredInPagingFiles", nil, nil, ), - PagingFreeBytes: prometheus.NewDesc( prometheus.BuildFQName(Namespace, subsystem, "paging_free_bytes"), - "FreeSpaceInPagingFiles", + "OperatingSystem.FreeSpaceInPagingFiles", nil, nil, ), - PhysicalMemoryFreeBytes: prometheus.NewDesc( prometheus.BuildFQName(Namespace, subsystem, "physical_memory_free_bytes"), - "FreePhysicalMemory", + "OperatingSystem.FreePhysicalMemory", nil, nil, ), - Processes: prometheus.NewDesc( prometheus.BuildFQName(Namespace, subsystem, "processes"), - "NumberOfProcesses", + "OperatingSystem.NumberOfProcesses", nil, nil, ), - - ProcessesMax: prometheus.NewDesc( - prometheus.BuildFQName(Namespace, subsystem, "processes_max"), - "MaxNumberOfProcesses", + ProcessesLimit: prometheus.NewDesc( + prometheus.BuildFQName(Namespace, subsystem, "processes_limit"), + "OperatingSystem.MaxNumberOfProcesses", nil, nil, ), - - ProcessMemoryMaxBytes: prometheus.NewDesc( - prometheus.BuildFQName(Namespace, subsystem, "process_memory_max_bytes"), - "MaxProcessMemorySize", + ProcessMemoryLimitBytes: prometheus.NewDesc( + prometheus.BuildFQName(Namespace, subsystem, "process_memory_limix_bytes"), + "OperatingSystem.MaxProcessMemorySize", nil, nil, ), - Users: prometheus.NewDesc( prometheus.BuildFQName(Namespace, subsystem, "users"), - "NumberOfUsers", + "OperatingSystem.NumberOfUsers", nil, nil, ), - VirtualMemoryBytes: prometheus.NewDesc( prometheus.BuildFQName(Namespace, subsystem, "virtual_memory_bytes"), - "TotalVirtualMemorySize", + "OperatingSystem.TotalVirtualMemorySize", nil, nil, ), - VisibleMemoryBytes: prometheus.NewDesc( prometheus.BuildFQName(Namespace, subsystem, "visible_memory_bytes"), - "TotalVisibleMemorySize", + "OperatingSystem.TotalVisibleMemorySize", nil, nil, ), - VirtualMemoryFreeBytes: prometheus.NewDesc( prometheus.BuildFQName(Namespace, subsystem, "virtual_memory_free_bytes"), - "FreeVirtualMemory", + "OperatingSystem.FreeVirtualMemory", nil, nil, ), @@ -132,8 +121,7 @@ type Win32_OperatingSystem struct { func (c *OSCollector) collect(ch chan<- prometheus.Metric) (*prometheus.Desc, error) { var dst []Win32_OperatingSystem - q := wmi.CreateQuery(&dst, "") - if err := wmi.Query(q, &dst); err != nil { + if err := wmi.Query(wmi.CreateQuery(&dst, ""), &dst); err != nil { return nil, err } @@ -156,13 +144,13 @@ func (c *OSCollector) collect(ch chan<- prometheus.Metric) (*prometheus.Desc, er ) ch <- prometheus.MustNewConstMetric( - c.ProcessesMax, + c.ProcessesLimit, prometheus.GaugeValue, float64(dst[0].MaxNumberOfProcesses), ) ch <- prometheus.MustNewConstMetric( - c.ProcessMemoryMaxBytes, + c.ProcessMemoryLimitBytes, prometheus.GaugeValue, float64(dst[0].MaxProcessMemorySize*1024), // KiB -> bytes ) @@ -180,7 +168,7 @@ func (c *OSCollector) collect(ch chan<- prometheus.Metric) (*prometheus.Desc, er ) ch <- prometheus.MustNewConstMetric( - c.PagingMaxBytes, + c.PagingLimitBytes, prometheus.GaugeValue, float64(dst[0].SizeStoredInPagingFiles*1024), // KiB -> bytes ) diff --git a/collector/system.go b/collector/system.go new file mode 100644 index 00000000..5cb7f159 --- /dev/null +++ b/collector/system.go @@ -0,0 +1,125 @@ +// returns data points from Win32_PerfRawData_PerfOS_System class (undocumented) + +package collector + +import ( + "log" + + "github.com/StackExchange/wmi" + "github.com/prometheus/client_golang/prometheus" +) + +func init() { + Factories["perfos"] = NewSystemCollector +} + +// A PerfOSCollector is a Prometheus collector for WMI metrics +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"), + "PerfOS_System.ContextSwitchesPersec", + nil, + nil, + ), + ExceptionDispatchesTotal: prometheus.NewDesc( + prometheus.BuildFQName(Namespace, subsystem, "exception_dispatches_total"), + "PerfOS_System.ExceptionDispatchesPersec", + nil, + nil, + ), + ProcessorQueueLength: prometheus.NewDesc( + prometheus.BuildFQName(Namespace, subsystem, "processor_queue_length"), + "PerfOS_System.ProcessorQueueLength", + nil, + nil, + ), + SystemCallsTotal: prometheus.NewDesc( + prometheus.BuildFQName(Namespace, subsystem, "system_calls_total"), + "PerfOS_System.SystemCallsPersec", + nil, + nil, + ), + SystemUpTime: prometheus.NewDesc( + prometheus.BuildFQName(Namespace, subsystem, "system_up_time"), + "PerfOS_System.SystemUpTime", + nil, + nil, + ), + Threads: prometheus.NewDesc( + prometheus.BuildFQName(Namespace, subsystem, "threads"), + "PerfOS_System.Threads", + 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 { + log.Println("[ERROR] failed collecting os metrics:", desc, err) + return err + } + return nil +} + +type Win32_PerfRawData_PerfOS_System struct { + ContextSwitchesPersec uint32 + ExceptionDispatchesPersec uint32 + ProcessorQueueLength uint32 + SystemCallsPersec uint32 + SystemUpTime uint64 + Threads uint32 +} + +func (c *SystemCollector) collect(ch chan<- prometheus.Metric) (*prometheus.Desc, error) { + var dst []Win32_PerfRawData_PerfOS_System + if err := wmi.Query(wmi.CreateQuery(&dst, ""), &dst); err != nil { + return nil, err + } + ch <- prometheus.MustNewConstMetric( + c.ContextSwitchesTotal, + prometheus.GaugeValue, + float64(dst[0].ContextSwitchesPersec), + ) + ch <- prometheus.MustNewConstMetric( + c.ExceptionDispatchesTotal, + prometheus.GaugeValue, + float64(dst[0].ExceptionDispatchesPersec), + ) + ch <- prometheus.MustNewConstMetric( + c.ProcessorQueueLength, + prometheus.GaugeValue, + float64(dst[0].ProcessorQueueLength), + ) + ch <- prometheus.MustNewConstMetric( + c.SystemCallsTotal, + prometheus.GaugeValue, + float64(dst[0].SystemCallsPersec), + ) + ch <- prometheus.MustNewConstMetric( + c.SystemUpTime, + prometheus.GaugeValue, + float64(dst[0].SystemUpTime), + ) + ch <- prometheus.MustNewConstMetric( + c.Threads, + prometheus.GaugeValue, + float64(dst[0].Threads), + ) + return nil, nil +}