os: expose Win32_ComputerSystem and Win32_PerfRawData_PerfOS_System

This commit is contained in:
Martin Lindhe
2016-09-19 09:02:05 +02:00
parent 29beaa6c3b
commit 52592bb097
3 changed files with 226 additions and 36 deletions

77
collector/cs.go Normal file
View File

@@ -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
}

View File

@@ -14,93 +14,82 @@ func init() {
Factories["os"] = NewOSCollector 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 { type OSCollector struct {
PhysicalMemoryFreeBytes *prometheus.Desc PhysicalMemoryFreeBytes *prometheus.Desc
PagingFreeBytes *prometheus.Desc PagingFreeBytes *prometheus.Desc
VirtualMemoryFreeBytes *prometheus.Desc VirtualMemoryFreeBytes *prometheus.Desc
ProcessesMax *prometheus.Desc ProcessesLimit *prometheus.Desc
ProcessMemoryMaxBytes *prometheus.Desc ProcessMemoryLimitBytes *prometheus.Desc
Processes *prometheus.Desc Processes *prometheus.Desc
Users *prometheus.Desc Users *prometheus.Desc
PagingMaxBytes *prometheus.Desc PagingLimitBytes *prometheus.Desc
VirtualMemoryBytes *prometheus.Desc VirtualMemoryBytes *prometheus.Desc
VisibleMemoryBytes *prometheus.Desc VisibleMemoryBytes *prometheus.Desc
} }
// NewOSCollector ... // NewOSCollector ...
func NewOSCollector() (Collector, error) { func NewOSCollector() (Collector, error) {
const subsystem = "os" const subsystem = "os"
return &OSCollector{ return &OSCollector{
PagingLimitBytes: prometheus.NewDesc(
PagingMaxBytes: prometheus.NewDesc( prometheus.BuildFQName(Namespace, subsystem, "paging_limit_bytes"),
prometheus.BuildFQName(Namespace, subsystem, "paging_max_bytes"), "OperatingSystem.SizeStoredInPagingFiles",
"SizeStoredInPagingFiles",
nil, nil,
nil, nil,
), ),
PagingFreeBytes: prometheus.NewDesc( PagingFreeBytes: prometheus.NewDesc(
prometheus.BuildFQName(Namespace, subsystem, "paging_free_bytes"), prometheus.BuildFQName(Namespace, subsystem, "paging_free_bytes"),
"FreeSpaceInPagingFiles", "OperatingSystem.FreeSpaceInPagingFiles",
nil, nil,
nil, nil,
), ),
PhysicalMemoryFreeBytes: prometheus.NewDesc( PhysicalMemoryFreeBytes: prometheus.NewDesc(
prometheus.BuildFQName(Namespace, subsystem, "physical_memory_free_bytes"), prometheus.BuildFQName(Namespace, subsystem, "physical_memory_free_bytes"),
"FreePhysicalMemory", "OperatingSystem.FreePhysicalMemory",
nil, nil,
nil, nil,
), ),
Processes: prometheus.NewDesc( Processes: prometheus.NewDesc(
prometheus.BuildFQName(Namespace, subsystem, "processes"), prometheus.BuildFQName(Namespace, subsystem, "processes"),
"NumberOfProcesses", "OperatingSystem.NumberOfProcesses",
nil, nil,
nil, nil,
), ),
ProcessesLimit: prometheus.NewDesc(
ProcessesMax: prometheus.NewDesc( prometheus.BuildFQName(Namespace, subsystem, "processes_limit"),
prometheus.BuildFQName(Namespace, subsystem, "processes_max"), "OperatingSystem.MaxNumberOfProcesses",
"MaxNumberOfProcesses",
nil, nil,
nil, nil,
), ),
ProcessMemoryLimitBytes: prometheus.NewDesc(
ProcessMemoryMaxBytes: prometheus.NewDesc( prometheus.BuildFQName(Namespace, subsystem, "process_memory_limix_bytes"),
prometheus.BuildFQName(Namespace, subsystem, "process_memory_max_bytes"), "OperatingSystem.MaxProcessMemorySize",
"MaxProcessMemorySize",
nil, nil,
nil, nil,
), ),
Users: prometheus.NewDesc( Users: prometheus.NewDesc(
prometheus.BuildFQName(Namespace, subsystem, "users"), prometheus.BuildFQName(Namespace, subsystem, "users"),
"NumberOfUsers", "OperatingSystem.NumberOfUsers",
nil, nil,
nil, nil,
), ),
VirtualMemoryBytes: prometheus.NewDesc( VirtualMemoryBytes: prometheus.NewDesc(
prometheus.BuildFQName(Namespace, subsystem, "virtual_memory_bytes"), prometheus.BuildFQName(Namespace, subsystem, "virtual_memory_bytes"),
"TotalVirtualMemorySize", "OperatingSystem.TotalVirtualMemorySize",
nil, nil,
nil, nil,
), ),
VisibleMemoryBytes: prometheus.NewDesc( VisibleMemoryBytes: prometheus.NewDesc(
prometheus.BuildFQName(Namespace, subsystem, "visible_memory_bytes"), prometheus.BuildFQName(Namespace, subsystem, "visible_memory_bytes"),
"TotalVisibleMemorySize", "OperatingSystem.TotalVisibleMemorySize",
nil, nil,
nil, nil,
), ),
VirtualMemoryFreeBytes: prometheus.NewDesc( VirtualMemoryFreeBytes: prometheus.NewDesc(
prometheus.BuildFQName(Namespace, subsystem, "virtual_memory_free_bytes"), prometheus.BuildFQName(Namespace, subsystem, "virtual_memory_free_bytes"),
"FreeVirtualMemory", "OperatingSystem.FreeVirtualMemory",
nil, nil,
nil, nil,
), ),
@@ -132,8 +121,7 @@ type Win32_OperatingSystem struct {
func (c *OSCollector) collect(ch chan<- prometheus.Metric) (*prometheus.Desc, error) { func (c *OSCollector) collect(ch chan<- prometheus.Metric) (*prometheus.Desc, error) {
var dst []Win32_OperatingSystem var dst []Win32_OperatingSystem
q := wmi.CreateQuery(&dst, "") if err := wmi.Query(wmi.CreateQuery(&dst, ""), &dst); err != nil {
if err := wmi.Query(q, &dst); err != nil {
return nil, err return nil, err
} }
@@ -156,13 +144,13 @@ func (c *OSCollector) collect(ch chan<- prometheus.Metric) (*prometheus.Desc, er
) )
ch <- prometheus.MustNewConstMetric( ch <- prometheus.MustNewConstMetric(
c.ProcessesMax, c.ProcessesLimit,
prometheus.GaugeValue, prometheus.GaugeValue,
float64(dst[0].MaxNumberOfProcesses), float64(dst[0].MaxNumberOfProcesses),
) )
ch <- prometheus.MustNewConstMetric( ch <- prometheus.MustNewConstMetric(
c.ProcessMemoryMaxBytes, c.ProcessMemoryLimitBytes,
prometheus.GaugeValue, prometheus.GaugeValue,
float64(dst[0].MaxProcessMemorySize*1024), // KiB -> bytes 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( ch <- prometheus.MustNewConstMetric(
c.PagingMaxBytes, c.PagingLimitBytes,
prometheus.GaugeValue, prometheus.GaugeValue,
float64(dst[0].SizeStoredInPagingFiles*1024), // KiB -> bytes float64(dst[0].SizeStoredInPagingFiles*1024), // KiB -> bytes
) )

125
collector/system.go Normal file
View File

@@ -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
}