2016-08-26 06:59:27 +00:00
|
|
|
// returns data points from Win32_OperatingSystem
|
2016-08-26 13:06:10 +00:00
|
|
|
// https://msdn.microsoft.com/en-us/library/aa394239 - Win32_OperatingSystem class
|
2016-08-26 06:59:27 +00:00
|
|
|
|
2016-09-01 12:55:35 +00:00
|
|
|
package collector
|
2016-08-26 06:59:27 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"log"
|
|
|
|
|
|
|
|
"github.com/StackExchange/wmi"
|
|
|
|
"github.com/prometheus/client_golang/prometheus"
|
|
|
|
)
|
|
|
|
|
2016-08-26 09:08:12 +00:00
|
|
|
// A OSCollector is a Prometheus collector for WMI Win32_OperatingSystem metrics
|
2016-08-26 06:59:27 +00:00
|
|
|
type OSCollector struct {
|
2016-08-26 12:19:08 +00:00
|
|
|
PhysicalMemoryFreeBytes *prometheus.Desc
|
|
|
|
PagingFreeBytes *prometheus.Desc
|
|
|
|
VirtualMemoryFreeBytes *prometheus.Desc
|
|
|
|
ProcessesMax *prometheus.Desc
|
|
|
|
ProcessMemoryMaxBytes *prometheus.Desc
|
|
|
|
Processes *prometheus.Desc
|
|
|
|
Users *prometheus.Desc
|
|
|
|
PagingMaxBytes *prometheus.Desc
|
|
|
|
VirtualMemoryBytes *prometheus.Desc
|
|
|
|
VisibleMemoryBytes *prometheus.Desc
|
2016-08-26 06:59:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewOSCollector ...
|
|
|
|
func NewOSCollector() *OSCollector {
|
|
|
|
|
|
|
|
return &OSCollector{
|
2016-08-26 12:19:08 +00:00
|
|
|
|
|
|
|
PagingMaxBytes: prometheus.NewDesc(
|
|
|
|
prometheus.BuildFQName(wmiNamespace, "os", "paging_max_bytes"),
|
|
|
|
"SizeStoredInPagingFiles",
|
2016-08-26 06:59:27 +00:00
|
|
|
nil,
|
|
|
|
nil,
|
|
|
|
),
|
|
|
|
|
2016-08-26 12:19:08 +00:00
|
|
|
PagingFreeBytes: prometheus.NewDesc(
|
|
|
|
prometheus.BuildFQName(wmiNamespace, "os", "paging_free_bytes"),
|
|
|
|
"FreeSpaceInPagingFiles",
|
2016-08-26 06:59:27 +00:00
|
|
|
nil,
|
|
|
|
nil,
|
|
|
|
),
|
|
|
|
|
2016-08-26 12:19:08 +00:00
|
|
|
PhysicalMemoryFreeBytes: prometheus.NewDesc(
|
|
|
|
prometheus.BuildFQName(wmiNamespace, "os", "physical_memory_free_bytes"),
|
|
|
|
"FreePhysicalMemory",
|
2016-08-26 06:59:27 +00:00
|
|
|
nil,
|
|
|
|
nil,
|
|
|
|
),
|
|
|
|
|
2016-08-26 12:19:08 +00:00
|
|
|
Processes: prometheus.NewDesc(
|
|
|
|
prometheus.BuildFQName(wmiNamespace, "os", "processes"),
|
|
|
|
"NumberOfProcesses",
|
2016-08-26 08:23:41 +00:00
|
|
|
nil,
|
|
|
|
nil,
|
|
|
|
),
|
|
|
|
|
2016-08-26 12:19:08 +00:00
|
|
|
ProcessesMax: prometheus.NewDesc(
|
|
|
|
prometheus.BuildFQName(wmiNamespace, "os", "processes_max"),
|
|
|
|
"MaxNumberOfProcesses",
|
2016-08-26 08:23:41 +00:00
|
|
|
nil,
|
|
|
|
nil,
|
|
|
|
),
|
|
|
|
|
2016-08-26 12:19:08 +00:00
|
|
|
ProcessMemoryMaxBytes: prometheus.NewDesc(
|
|
|
|
prometheus.BuildFQName(wmiNamespace, "os", "process_memory_max_bytes"),
|
|
|
|
"MaxProcessMemorySize",
|
2016-08-26 06:59:27 +00:00
|
|
|
nil,
|
|
|
|
nil,
|
|
|
|
),
|
|
|
|
|
2016-08-26 09:35:31 +00:00
|
|
|
Users: prometheus.NewDesc(
|
|
|
|
prometheus.BuildFQName(wmiNamespace, "os", "users"),
|
2016-08-26 12:19:08 +00:00
|
|
|
"NumberOfUsers",
|
2016-08-26 07:29:03 +00:00
|
|
|
nil,
|
|
|
|
nil,
|
|
|
|
),
|
|
|
|
|
2016-08-26 12:19:08 +00:00
|
|
|
VirtualMemoryBytes: prometheus.NewDesc(
|
|
|
|
prometheus.BuildFQName(wmiNamespace, "os", "virtual_memory_bytes"),
|
|
|
|
"TotalVirtualMemorySize",
|
2016-08-26 07:29:03 +00:00
|
|
|
nil,
|
|
|
|
nil,
|
|
|
|
),
|
|
|
|
|
2016-08-26 12:19:08 +00:00
|
|
|
VisibleMemoryBytes: prometheus.NewDesc(
|
|
|
|
prometheus.BuildFQName(wmiNamespace, "os", "visible_memory_bytes"),
|
|
|
|
"TotalVisibleMemorySize",
|
2016-08-26 07:29:03 +00:00
|
|
|
nil,
|
|
|
|
nil,
|
|
|
|
),
|
|
|
|
|
2016-08-26 12:19:08 +00:00
|
|
|
VirtualMemoryFreeBytes: prometheus.NewDesc(
|
|
|
|
prometheus.BuildFQName(wmiNamespace, "os", "virtual_memory_free_bytes"),
|
|
|
|
"FreeVirtualMemory",
|
2016-08-26 06:59:27 +00:00
|
|
|
nil,
|
|
|
|
nil,
|
|
|
|
),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Collect sends the metric values for each metric
|
|
|
|
// to the provided prometheus Metric channel.
|
|
|
|
func (c *OSCollector) Collect(ch chan<- prometheus.Metric) {
|
|
|
|
if desc, err := c.collect(ch); err != nil {
|
2016-08-26 09:08:12 +00:00
|
|
|
log.Println("[ERROR] failed collecting os metrics:", desc, err)
|
2016-08-26 06:59:27 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Describe sends the descriptors of each metric over to the provided channel.
|
|
|
|
// The corresponding metric values are sent separately.
|
|
|
|
func (c *OSCollector) Describe(ch chan<- *prometheus.Desc) {
|
|
|
|
|
2016-08-26 12:19:08 +00:00
|
|
|
ch <- c.PhysicalMemoryFreeBytes
|
|
|
|
ch <- c.PagingFreeBytes
|
|
|
|
ch <- c.VirtualMemoryFreeBytes
|
2016-08-26 09:35:31 +00:00
|
|
|
ch <- c.ProcessesMax
|
2016-08-26 12:19:08 +00:00
|
|
|
ch <- c.ProcessMemoryMaxBytes
|
2016-08-26 09:35:31 +00:00
|
|
|
ch <- c.Processes
|
|
|
|
ch <- c.Users
|
2016-08-26 12:19:08 +00:00
|
|
|
ch <- c.PagingMaxBytes
|
|
|
|
ch <- c.VirtualMemoryBytes
|
|
|
|
ch <- c.VisibleMemoryBytes
|
2016-08-26 06:59:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type Win32_OperatingSystem struct {
|
2016-08-26 07:29:03 +00:00
|
|
|
FreePhysicalMemory uint64
|
|
|
|
FreeSpaceInPagingFiles uint64
|
|
|
|
FreeVirtualMemory uint64
|
2016-08-26 08:23:41 +00:00
|
|
|
MaxNumberOfProcesses uint32
|
|
|
|
MaxProcessMemorySize uint64
|
2016-08-26 07:29:03 +00:00
|
|
|
NumberOfProcesses uint32
|
|
|
|
NumberOfUsers uint32
|
|
|
|
SizeStoredInPagingFiles uint64
|
|
|
|
TotalVirtualMemorySize uint64
|
|
|
|
TotalVisibleMemorySize uint64
|
2016-08-26 06:59:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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 {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
ch <- prometheus.MustNewConstMetric(
|
2016-08-26 12:19:08 +00:00
|
|
|
c.PhysicalMemoryFreeBytes,
|
2016-08-26 06:59:27 +00:00
|
|
|
prometheus.GaugeValue,
|
2016-08-26 09:35:31 +00:00
|
|
|
float64(dst[0].FreePhysicalMemory*1024), // KiB -> bytes
|
2016-08-26 06:59:27 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
ch <- prometheus.MustNewConstMetric(
|
2016-08-26 12:19:08 +00:00
|
|
|
c.PagingFreeBytes,
|
2016-08-26 06:59:27 +00:00
|
|
|
prometheus.GaugeValue,
|
2016-08-26 09:35:31 +00:00
|
|
|
float64(dst[0].FreeSpaceInPagingFiles*1024), // KiB -> bytes
|
2016-08-26 06:59:27 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
ch <- prometheus.MustNewConstMetric(
|
2016-08-26 12:19:08 +00:00
|
|
|
c.VirtualMemoryFreeBytes,
|
2016-08-26 06:59:27 +00:00
|
|
|
prometheus.GaugeValue,
|
2016-08-26 09:35:31 +00:00
|
|
|
float64(dst[0].FreeVirtualMemory*1024), // KiB -> bytes
|
2016-08-26 06:59:27 +00:00
|
|
|
)
|
|
|
|
|
2016-08-26 08:23:41 +00:00
|
|
|
ch <- prometheus.MustNewConstMetric(
|
2016-08-26 09:35:31 +00:00
|
|
|
c.ProcessesMax,
|
2016-08-26 08:23:41 +00:00
|
|
|
prometheus.GaugeValue,
|
|
|
|
float64(dst[0].MaxNumberOfProcesses),
|
|
|
|
)
|
|
|
|
|
|
|
|
ch <- prometheus.MustNewConstMetric(
|
2016-08-26 12:19:08 +00:00
|
|
|
c.ProcessMemoryMaxBytes,
|
2016-08-26 08:23:41 +00:00
|
|
|
prometheus.GaugeValue,
|
2016-08-26 09:35:31 +00:00
|
|
|
float64(dst[0].MaxProcessMemorySize*1024), // KiB -> bytes
|
2016-08-26 08:23:41 +00:00
|
|
|
)
|
|
|
|
|
2016-08-26 06:59:27 +00:00
|
|
|
ch <- prometheus.MustNewConstMetric(
|
2016-08-26 09:35:31 +00:00
|
|
|
c.Processes,
|
2016-08-26 06:59:27 +00:00
|
|
|
prometheus.GaugeValue,
|
|
|
|
float64(dst[0].NumberOfProcesses),
|
|
|
|
)
|
|
|
|
|
|
|
|
ch <- prometheus.MustNewConstMetric(
|
2016-08-26 09:35:31 +00:00
|
|
|
c.Users,
|
2016-08-26 06:59:27 +00:00
|
|
|
prometheus.GaugeValue,
|
|
|
|
float64(dst[0].NumberOfUsers),
|
|
|
|
)
|
|
|
|
|
2016-08-26 07:29:03 +00:00
|
|
|
ch <- prometheus.MustNewConstMetric(
|
2016-08-26 12:19:08 +00:00
|
|
|
c.PagingMaxBytes,
|
2016-08-26 07:29:03 +00:00
|
|
|
prometheus.GaugeValue,
|
2016-08-26 09:35:31 +00:00
|
|
|
float64(dst[0].SizeStoredInPagingFiles*1024), // KiB -> bytes
|
2016-08-26 07:29:03 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
ch <- prometheus.MustNewConstMetric(
|
2016-08-26 12:19:08 +00:00
|
|
|
c.VirtualMemoryBytes,
|
2016-08-26 07:29:03 +00:00
|
|
|
prometheus.GaugeValue,
|
2016-08-26 09:35:31 +00:00
|
|
|
float64(dst[0].TotalVirtualMemorySize*1024), // KiB -> bytes
|
2016-08-26 07:29:03 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
ch <- prometheus.MustNewConstMetric(
|
2016-08-26 12:19:08 +00:00
|
|
|
c.VisibleMemoryBytes,
|
2016-08-26 07:29:03 +00:00
|
|
|
prometheus.GaugeValue,
|
2016-08-26 09:35:31 +00:00
|
|
|
float64(dst[0].TotalVisibleMemorySize*1024), // KiB -> bytes
|
2016-08-26 07:29:03 +00:00
|
|
|
)
|
|
|
|
|
2016-08-26 06:59:27 +00:00
|
|
|
return nil, nil
|
|
|
|
}
|