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 (
|
2021-01-13 00:23:40 +00:00
|
|
|
"fmt"
|
2017-03-31 05:56:42 +00:00
|
|
|
"time"
|
2018-04-05 05:11:36 +00:00
|
|
|
|
2021-01-13 00:23:40 +00:00
|
|
|
"github.com/prometheus-community/windows_exporter/headers/custom"
|
|
|
|
"github.com/prometheus-community/windows_exporter/headers/netapi32"
|
|
|
|
"github.com/prometheus-community/windows_exporter/headers/psapi"
|
|
|
|
"github.com/prometheus-community/windows_exporter/headers/sysinfoapi"
|
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) {
|
2021-01-13 02:54:51 +00:00
|
|
|
/*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")
|
2021-01-13 02:54:51 +00:00
|
|
|
}*/
|
2018-08-07 19:42:16 +00:00
|
|
|
|
2021-01-13 00:23:40 +00:00
|
|
|
product, buildNum := custom.GetProductDetails()
|
|
|
|
|
|
|
|
nwgi, _, err := netapi32.NetWkstaGetInfo()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2019-10-01 16:54:50 +00:00
|
|
|
ch <- prometheus.MustNewConstMetric(
|
|
|
|
c.OSInformation,
|
|
|
|
prometheus.GaugeValue,
|
|
|
|
1.0,
|
2021-01-13 03:31:26 +00:00
|
|
|
fmt.Sprintf("Microsoft %s", product), // Caption
|
|
|
|
fmt.Sprintf("%d.%d.%s", nwgi.Wki102_ver_major, nwgi.Wki102_ver_minor, buildNum), // Version
|
2019-10-01 16:54:50 +00:00
|
|
|
)
|
|
|
|
|
2021-01-13 00:23:40 +00:00
|
|
|
gmse, err := sysinfoapi.GlobalMemoryStatusEx()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
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,
|
2021-01-13 00:23:40 +00:00
|
|
|
float64(gmse.UllAvailPhys),
|
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,
|
2021-01-13 02:54:51 +00:00
|
|
|
float64(1234567),
|
2021-01-13 00:23:40 +00:00
|
|
|
// Cannot find a way to get this without WMI.
|
|
|
|
// Can get from CIM_OperatingSystem which is where WMI gets it from, but I can't figure out how to access this from cimwin32.dll
|
|
|
|
// https://docs.microsoft.com/en-us/windows/win32/cimwin32prov/cim-operatingsystem#properties
|
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,
|
2021-01-13 00:23:40 +00:00
|
|
|
float64(gmse.UllAvailPageFile),
|
2016-08-26 06:59:27 +00:00
|
|
|
)
|
|
|
|
|
2021-01-13 00:23:40 +00:00
|
|
|
// Windows has no defined limit, and is based off available resources. This currently isn't calculated by WMI and is set to default value.
|
|
|
|
// https://techcommunity.microsoft.com/t5/windows-blog-archive/pushing-the-limits-of-windows-processes-and-threads/ba-p/723824
|
|
|
|
// https://docs.microsoft.com/en-us/windows/win32/cimwin32prov/win32-operatingsystem
|
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,
|
2021-01-13 00:23:40 +00:00
|
|
|
float64(4294967295),
|
2016-08-26 08:23:41 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
ch <- prometheus.MustNewConstMetric(
|
2016-09-19 07:02:05 +00:00
|
|
|
c.ProcessMemoryLimitBytes,
|
2016-08-26 08:23:41 +00:00
|
|
|
prometheus.GaugeValue,
|
2021-01-13 00:23:40 +00:00
|
|
|
float64(gmse.UllTotalVirtual),
|
2016-08-26 08:23:41 +00:00
|
|
|
)
|
|
|
|
|
2021-01-13 00:23:40 +00:00
|
|
|
gpi, err := psapi.GetLPPerformanceInfo()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
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,
|
2021-01-13 00:23:40 +00:00
|
|
|
float64(gpi.ProcessCount),
|
2016-08-26 06:59:27 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
ch <- prometheus.MustNewConstMetric(
|
2016-08-26 09:35:31 +00:00
|
|
|
c.Users,
|
2016-08-26 06:59:27 +00:00
|
|
|
prometheus.GaugeValue,
|
2021-01-13 00:23:40 +00:00
|
|
|
float64(nwgi.Wki102_logged_on_users),
|
2016-08-26 06:59:27 +00:00
|
|
|
)
|
|
|
|
|
2021-01-13 00:23:40 +00:00
|
|
|
fsipf, err := custom.GetSizeStoredInPagingFiles()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
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,
|
2021-01-13 00:23:40 +00:00
|
|
|
float64(fsipf),
|
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,
|
2021-01-13 00:23:40 +00:00
|
|
|
float64(gmse.UllTotalPageFile),
|
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,
|
2021-01-13 00:23:40 +00:00
|
|
|
float64(gmse.UllTotalPhys),
|
2016-08-26 07:29:03 +00:00
|
|
|
)
|
|
|
|
|
2016-08-26 06:59:27 +00:00
|
|
|
return nil, nil
|
|
|
|
}
|