2018-11-30 00:51:12 +00:00
|
|
|
// +build windows
|
|
|
|
|
2016-09-01 12:55:35 +00:00
|
|
|
package collector
|
2016-08-26 06:59:27 +00:00
|
|
|
|
|
|
|
import (
|
2018-08-07 19:42:16 +00:00
|
|
|
"errors"
|
2017-03-31 05:56:42 +00:00
|
|
|
"time"
|
2018-04-05 05:11:36 +00:00
|
|
|
|
2016-08-26 06:59:27 +00:00
|
|
|
"github.com/StackExchange/wmi"
|
2021-01-30 10:16:53 +00:00
|
|
|
"github.com/prometheus-community/windows_exporter/log"
|
2016-08-26 06:59:27 +00:00
|
|
|
"github.com/prometheus/client_golang/prometheus"
|
|
|
|
)
|
|
|
|
|
2016-09-01 14:04:43 +00:00
|
|
|
func init() {
|
2020-02-09 20:09:26 +00:00
|
|
|
registerCollector("os", NewOSCollector)
|
2016-09-01 14:04:43 +00:00
|
|
|
}
|
|
|
|
|
2016-09-19 07:02:05 +00:00
|
|
|
// A OSCollector is a Prometheus collector for WMI metrics
|
2016-08-26 06:59:27 +00:00
|
|
|
type OSCollector struct {
|
2019-10-01 16:54:50 +00:00
|
|
|
OSInformation *prometheus.Desc
|
2016-08-26 12:19:08 +00:00
|
|
|
PhysicalMemoryFreeBytes *prometheus.Desc
|
|
|
|
PagingFreeBytes *prometheus.Desc
|
|
|
|
VirtualMemoryFreeBytes *prometheus.Desc
|
2016-09-19 07:02:05 +00:00
|
|
|
ProcessesLimit *prometheus.Desc
|
|
|
|
ProcessMemoryLimitBytes *prometheus.Desc
|
2016-08-26 12:19:08 +00:00
|
|
|
Processes *prometheus.Desc
|
|
|
|
Users *prometheus.Desc
|
2016-09-19 07:02:05 +00:00
|
|
|
PagingLimitBytes *prometheus.Desc
|
2016-08-26 12:19:08 +00:00
|
|
|
VirtualMemoryBytes *prometheus.Desc
|
|
|
|
VisibleMemoryBytes *prometheus.Desc
|
2018-04-05 05:11:36 +00:00
|
|
|
Time *prometheus.Desc
|
|
|
|
Timezone *prometheus.Desc
|
2016-08-26 06:59:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewOSCollector ...
|
2016-09-01 14:04:43 +00:00
|
|
|
func NewOSCollector() (Collector, error) {
|
|
|
|
const subsystem = "os"
|
2016-08-26 06:59:27 +00:00
|
|
|
|
|
|
|
return &OSCollector{
|
2019-10-01 16:54:50 +00:00
|
|
|
OSInformation: prometheus.NewDesc(
|
|
|
|
prometheus.BuildFQName(Namespace, subsystem, "info"),
|
|
|
|
"OperatingSystem.Caption, OperatingSystem.Version",
|
|
|
|
[]string{"product", "version"},
|
|
|
|
nil,
|
|
|
|
),
|
2016-09-19 07:02:05 +00:00
|
|
|
PagingLimitBytes: prometheus.NewDesc(
|
|
|
|
prometheus.BuildFQName(Namespace, subsystem, "paging_limit_bytes"),
|
|
|
|
"OperatingSystem.SizeStoredInPagingFiles",
|
2016-08-26 06:59:27 +00:00
|
|
|
nil,
|
|
|
|
nil,
|
|
|
|
),
|
2016-08-26 12:19:08 +00:00
|
|
|
PagingFreeBytes: prometheus.NewDesc(
|
2016-09-01 14:04:43 +00:00
|
|
|
prometheus.BuildFQName(Namespace, subsystem, "paging_free_bytes"),
|
2016-09-19 07:02:05 +00:00
|
|
|
"OperatingSystem.FreeSpaceInPagingFiles",
|
2016-08-26 06:59:27 +00:00
|
|
|
nil,
|
|
|
|
nil,
|
|
|
|
),
|
2016-08-26 12:19:08 +00:00
|
|
|
PhysicalMemoryFreeBytes: prometheus.NewDesc(
|
2016-09-01 14:04:43 +00:00
|
|
|
prometheus.BuildFQName(Namespace, subsystem, "physical_memory_free_bytes"),
|
2016-09-19 07:02:05 +00:00
|
|
|
"OperatingSystem.FreePhysicalMemory",
|
2016-08-26 06:59:27 +00:00
|
|
|
nil,
|
|
|
|
nil,
|
|
|
|
),
|
2017-03-31 05:56:42 +00:00
|
|
|
Time: prometheus.NewDesc(
|
|
|
|
prometheus.BuildFQName(Namespace, subsystem, "time"),
|
|
|
|
"OperatingSystem.LocalDateTime",
|
|
|
|
nil,
|
|
|
|
nil,
|
|
|
|
),
|
|
|
|
Timezone: prometheus.NewDesc(
|
|
|
|
prometheus.BuildFQName(Namespace, subsystem, "timezone"),
|
|
|
|
"OperatingSystem.LocalDateTime",
|
|
|
|
[]string{"timezone"},
|
|
|
|
nil,
|
|
|
|
),
|
2016-08-26 12:19:08 +00:00
|
|
|
Processes: prometheus.NewDesc(
|
2016-09-01 14:04:43 +00:00
|
|
|
prometheus.BuildFQName(Namespace, subsystem, "processes"),
|
2016-09-19 07:02:05 +00:00
|
|
|
"OperatingSystem.NumberOfProcesses",
|
2016-08-26 08:23:41 +00:00
|
|
|
nil,
|
|
|
|
nil,
|
|
|
|
),
|
2016-09-19 07:02:05 +00:00
|
|
|
ProcessesLimit: prometheus.NewDesc(
|
|
|
|
prometheus.BuildFQName(Namespace, subsystem, "processes_limit"),
|
|
|
|
"OperatingSystem.MaxNumberOfProcesses",
|
2016-08-26 08:23:41 +00:00
|
|
|
nil,
|
|
|
|
nil,
|
|
|
|
),
|
2016-09-19 07:02:05 +00:00
|
|
|
ProcessMemoryLimitBytes: prometheus.NewDesc(
|
|
|
|
prometheus.BuildFQName(Namespace, subsystem, "process_memory_limix_bytes"),
|
|
|
|
"OperatingSystem.MaxProcessMemorySize",
|
2016-08-26 06:59:27 +00:00
|
|
|
nil,
|
|
|
|
nil,
|
|
|
|
),
|
2016-08-26 09:35:31 +00:00
|
|
|
Users: prometheus.NewDesc(
|
2016-09-01 14:04:43 +00:00
|
|
|
prometheus.BuildFQName(Namespace, subsystem, "users"),
|
2016-09-19 07:02:05 +00:00
|
|
|
"OperatingSystem.NumberOfUsers",
|
2016-08-26 07:29:03 +00:00
|
|
|
nil,
|
|
|
|
nil,
|
|
|
|
),
|
2016-08-26 12:19:08 +00:00
|
|
|
VirtualMemoryBytes: prometheus.NewDesc(
|
2016-09-01 14:04:43 +00:00
|
|
|
prometheus.BuildFQName(Namespace, subsystem, "virtual_memory_bytes"),
|
2016-09-19 07:02:05 +00:00
|
|
|
"OperatingSystem.TotalVirtualMemorySize",
|
2016-08-26 07:29:03 +00:00
|
|
|
nil,
|
|
|
|
nil,
|
|
|
|
),
|
2016-08-26 12:19:08 +00:00
|
|
|
VisibleMemoryBytes: prometheus.NewDesc(
|
2016-09-01 14:04:43 +00:00
|
|
|
prometheus.BuildFQName(Namespace, subsystem, "visible_memory_bytes"),
|
2016-09-19 07:02:05 +00:00
|
|
|
"OperatingSystem.TotalVisibleMemorySize",
|
2016-08-26 07:29:03 +00:00
|
|
|
nil,
|
|
|
|
nil,
|
|
|
|
),
|
2016-08-26 12:19:08 +00:00
|
|
|
VirtualMemoryFreeBytes: prometheus.NewDesc(
|
2016-09-01 14:04:43 +00:00
|
|
|
prometheus.BuildFQName(Namespace, subsystem, "virtual_memory_free_bytes"),
|
2016-09-19 07:02:05 +00:00
|
|
|
"OperatingSystem.FreeVirtualMemory",
|
2016-08-26 06:59:27 +00:00
|
|
|
nil,
|
|
|
|
nil,
|
|
|
|
),
|
2016-09-01 14:04:43 +00:00
|
|
|
}, nil
|
2016-08-26 06:59:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Collect sends the metric values for each metric
|
|
|
|
// to the provided prometheus Metric channel.
|
2019-04-05 13:59:40 +00:00
|
|
|
func (c *OSCollector) Collect(ctx *ScrapeContext, ch chan<- prometheus.Metric) error {
|
2016-08-26 06:59:27 +00:00
|
|
|
if desc, err := c.collect(ch); err != nil {
|
2018-04-05 05:27:26 +00:00
|
|
|
log.Error("failed collecting os metrics:", desc, err)
|
2016-09-01 14:04:43 +00:00
|
|
|
return err
|
2016-08-26 06:59:27 +00:00
|
|
|
}
|
2016-09-01 14:04:43 +00:00
|
|
|
return nil
|
2016-08-26 06:59:27 +00:00
|
|
|
}
|
|
|
|
|
2018-10-04 19:35:33 +00:00
|
|
|
// Win32_OperatingSystem docs:
|
|
|
|
// - https://msdn.microsoft.com/en-us/library/aa394239 - Win32_OperatingSystem class
|
2016-08-26 06:59:27 +00:00
|
|
|
type Win32_OperatingSystem struct {
|
2019-10-01 16:54:50 +00:00
|
|
|
Caption string
|
2016-08-26 07:29:03 +00:00
|
|
|
FreePhysicalMemory uint64
|
|
|
|
FreeSpaceInPagingFiles uint64
|
|
|
|
FreeVirtualMemory uint64
|
2019-10-01 16:54:50 +00:00
|
|
|
LocalDateTime time.Time
|
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
|
2019-10-01 16:54:50 +00:00
|
|
|
Version string
|
2016-08-26 06:59:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *OSCollector) collect(ch chan<- prometheus.Metric) (*prometheus.Desc, error) {
|
|
|
|
var dst []Win32_OperatingSystem
|
2018-06-06 09:35:13 +00:00
|
|
|
q := queryAll(&dst)
|
|
|
|
if err := wmi.Query(q, &dst); err != nil {
|
2016-08-26 06:59:27 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2018-08-07 19:42:16 +00:00
|
|
|
if len(dst) == 0 {
|
|
|
|
return nil, errors.New("WMI query returned empty result set")
|
|
|
|
}
|
|
|
|
|
2019-10-01 16:54:50 +00:00
|
|
|
ch <- prometheus.MustNewConstMetric(
|
|
|
|
c.OSInformation,
|
|
|
|
prometheus.GaugeValue,
|
|
|
|
1.0,
|
|
|
|
dst[0].Caption,
|
|
|
|
dst[0].Version,
|
|
|
|
)
|
|
|
|
|
2016-08-26 06:59:27 +00:00
|
|
|
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
|
|
|
)
|
|
|
|
|
2020-12-21 06:11:11 +00:00
|
|
|
currentTime := time.Now()
|
2018-04-05 05:11:36 +00:00
|
|
|
|
2017-03-31 05:56:42 +00:00
|
|
|
ch <- prometheus.MustNewConstMetric(
|
|
|
|
c.Time,
|
|
|
|
prometheus.GaugeValue,
|
2020-12-21 06:11:11 +00:00
|
|
|
float64(currentTime.Unix()),
|
2017-03-31 05:56:42 +00:00
|
|
|
)
|
|
|
|
|
2020-12-21 06:11:11 +00:00
|
|
|
timezoneName, _ := currentTime.Zone()
|
2017-03-31 05:56:42 +00:00
|
|
|
|
|
|
|
ch <- prometheus.MustNewConstMetric(
|
|
|
|
c.Timezone,
|
|
|
|
prometheus.GaugeValue,
|
|
|
|
1.0,
|
|
|
|
timezoneName,
|
|
|
|
)
|
|
|
|
|
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-09-19 07:02:05 +00:00
|
|
|
c.ProcessesLimit,
|
2016-08-26 08:23:41 +00:00
|
|
|
prometheus.GaugeValue,
|
|
|
|
float64(dst[0].MaxNumberOfProcesses),
|
|
|
|
)
|
|
|
|
|
|
|
|
ch <- prometheus.MustNewConstMetric(
|
2016-09-19 07:02:05 +00:00
|
|
|
c.ProcessMemoryLimitBytes,
|
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-09-19 07:02:05 +00:00
|
|
|
c.PagingLimitBytes,
|
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
|
|
|
|
}
|