Skip to content

Commit

Permalink
Merge pull request prometheus-community#702 from benridley/dev_cs_col…
Browse files Browse the repository at this point in the history
…lector

Replace WMI in cs and os collectors
  • Loading branch information
carlpett authored Mar 30, 2021
2 parents 0188f1e + d5559d5 commit cdc543d
Show file tree
Hide file tree
Showing 7 changed files with 490 additions and 56 deletions.
51 changes: 23 additions & 28 deletions collector/cs.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,9 @@
package collector

import (
"errors"

"github.com/StackExchange/wmi"
"github.com/prometheus-community/windows_exporter/headers/sysinfoapi"
"github.com/prometheus-community/windows_exporter/log"

"github.com/prometheus/client_golang/prometheus"
)

Expand Down Expand Up @@ -60,51 +59,47 @@ func (c *CSCollector) Collect(ctx *ScrapeContext, ch chan<- prometheus.Metric) e
return nil
}

// Win32_ComputerSystem docs:
// - https://msdn.microsoft.com/en-us/library/aa394102
type Win32_ComputerSystem struct {
NumberOfLogicalProcessors uint32
TotalPhysicalMemory uint64
DNSHostname string
Domain string
Workgroup *string
}

func (c *CSCollector) collect(ch chan<- prometheus.Metric) (*prometheus.Desc, error) {
var dst []Win32_ComputerSystem
q := queryAll(&dst)
if err := wmi.Query(q, &dst); err != nil {
// Get systeminfo for number of processors
systemInfo := sysinfoapi.GetSystemInfo()

// Get memory status for physical memory
mem, err := sysinfoapi.GlobalMemoryStatusEx()
if err != nil {
return nil, err
}
if len(dst) == 0 {
return nil, errors.New("WMI query returned empty result set")
}

ch <- prometheus.MustNewConstMetric(
c.LogicalProcessors,
prometheus.GaugeValue,
float64(dst[0].NumberOfLogicalProcessors),
float64(systemInfo.NumberOfProcessors),
)

ch <- prometheus.MustNewConstMetric(
c.PhysicalMemoryBytes,
prometheus.GaugeValue,
float64(dst[0].TotalPhysicalMemory),
float64(mem.TotalPhys),
)

var fqdn string
if dst[0].Workgroup == nil || dst[0].Domain != *dst[0].Workgroup {
fqdn = dst[0].DNSHostname + "." + dst[0].Domain
} else {
fqdn = dst[0].DNSHostname
hostname, err := sysinfoapi.GetComputerName(sysinfoapi.ComputerNameDNSHostname)
if err != nil {
return nil, err
}
domain, err := sysinfoapi.GetComputerName(sysinfoapi.ComputerNameDNSDomain)
if err != nil {
return nil, err
}
fqdn, err := sysinfoapi.GetComputerName(sysinfoapi.ComputerNameDNSFullyQualified)
if err != nil {
return nil, err
}

ch <- prometheus.MustNewConstMetric(
c.Hostname,
prometheus.GaugeValue,
1.0,
dst[0].DNSHostname,
dst[0].Domain,
hostname,
domain,
fqdn,
)

Expand Down
23 changes: 23 additions & 0 deletions collector/cs_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package collector

import (
"testing"

"github.com/prometheus/client_golang/prometheus"
)

func BenchmarkCsCollect(b *testing.B) {
c, err := NewCSCollector()
if err != nil {
b.Error(err)
}
metrics := make(chan prometheus.Metric)
go func() {
for {
<-metrics
}
}()
for i := 0; i < b.N; i++ {
c.Collect(&ScrapeContext{}, metrics)
}
}
131 changes: 103 additions & 28 deletions collector/os.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,21 @@
package collector

import (
"errors"
"fmt"
"os"
"strings"
"time"

"github.com/StackExchange/wmi"
"github.com/prometheus-community/windows_exporter/log"
"github.com/prometheus-community/windows_exporter/headers/netapi32"
"github.com/prometheus-community/windows_exporter/headers/psapi"
"github.com/prometheus-community/windows_exporter/headers/sysinfoapi"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/common/log"
"golang.org/x/sys/windows/registry"
)

func init() {
registerCollector("os", NewOSCollector)
registerCollector("os", NewOSCollector, "Paging File")
}

// A OSCollector is a Prometheus collector for WMI metrics
Expand All @@ -32,6 +37,12 @@ type OSCollector struct {
Timezone *prometheus.Desc
}

type pagingFileCounter struct {
Name string
Usage float64 `perflib:"% Usage"`
UsagePeak float64 `perflib:"% Usage Peak"`
}

// NewOSCollector ...
func NewOSCollector() (Collector, error) {
const subsystem = "os"
Expand Down Expand Up @@ -121,7 +132,7 @@ func NewOSCollector() (Collector, error) {
// Collect sends the metric values for each metric
// to the provided prometheus Metric channel.
func (c *OSCollector) Collect(ctx *ScrapeContext, ch chan<- prometheus.Metric) error {
if desc, err := c.collect(ch); err != nil {
if desc, err := c.collect(ctx, ch); err != nil {
log.Error("failed collecting os metrics:", desc, err)
return err
}
Expand All @@ -146,41 +157,102 @@ type Win32_OperatingSystem struct {
Version string
}

func (c *OSCollector) collect(ch chan<- prometheus.Metric) (*prometheus.Desc, error) {
var dst []Win32_OperatingSystem
q := queryAll(&dst)
if err := wmi.Query(q, &dst); err != nil {
func (c *OSCollector) collect(ctx *ScrapeContext, ch chan<- prometheus.Metric) (*prometheus.Desc, error) {
nwgi, err := netapi32.GetWorkstationInfo()
if err != nil {
return nil, err
}

gmse, err := sysinfoapi.GlobalMemoryStatusEx()
if err != nil {
return nil, err
}

if len(dst) == 0 {
return nil, errors.New("WMI query returned empty result set")
currentTime := time.Now()
timezoneName, _ := currentTime.Zone()

// Get total allocation of paging files across all disks.
memManKey, err := registry.OpenKey(registry.LOCAL_MACHINE, `SYSTEM\CurrentControlSet\Control\Session Manager\Memory Management`, registry.QUERY_VALUE)
defer memManKey.Close()

if err != nil {
return nil, err
}
pagingFiles, _, err := memManKey.GetStringsValue("ExistingPageFiles")
if err != nil {
return nil, err
}

// Get build number and product name from registry
ntKey, err := registry.OpenKey(registry.LOCAL_MACHINE, `SOFTWARE\Microsoft\Windows NT\CurrentVersion`, registry.QUERY_VALUE)
defer ntKey.Close()

if err != nil {
return nil, err
}

pn, _, err := ntKey.GetStringValue("ProductName")
if err != nil {
return nil, err
}

bn, _, err := ntKey.GetStringValue("CurrentBuildNumber")
if err != nil {
return nil, err
}

var fsipf float64 = 0
for _, pagingFile := range pagingFiles {
fileString := strings.ReplaceAll(pagingFile, `\??\`, "")
file, err := os.Stat(fileString)
if err != nil {
return nil, err
}
fsipf += float64(file.Size())
}

gpi, err := psapi.GetPerformanceInfo()
if err != nil {
return nil, err
}

var pfc = make([]pagingFileCounter, 0)
if err := unmarshalObject(ctx.perfObjects["Paging File"], &pfc); err != nil {
return nil, err
}

// Get current page file usage.
var pfbRaw float64 = 0
for _, pageFile := range pfc {
if strings.Contains(strings.ToLower(pageFile.Name), "_total") {
continue
}
pfbRaw += pageFile.Usage
}

// Subtract from total page file allocation on disk.
pfb := fsipf - (pfbRaw * float64(gpi.PageSize))

ch <- prometheus.MustNewConstMetric(
c.OSInformation,
prometheus.GaugeValue,
1.0,
dst[0].Caption,
dst[0].Version,
fmt.Sprintf("Microsoft %s", pn), // Caption
fmt.Sprintf("%d.%d.%s", nwgi.VersionMajor, nwgi.VersionMinor, bn), // Version
)

ch <- prometheus.MustNewConstMetric(
c.PhysicalMemoryFreeBytes,
prometheus.GaugeValue,
float64(dst[0].FreePhysicalMemory*1024), // KiB -> bytes
float64(gmse.AvailPhys),
)

time := dst[0].LocalDateTime

ch <- prometheus.MustNewConstMetric(
c.Time,
prometheus.GaugeValue,
float64(time.Unix()),
float64(currentTime.Unix()),
)

timezoneName, _ := time.Zone()

ch <- prometheus.MustNewConstMetric(
c.Timezone,
prometheus.GaugeValue,
Expand All @@ -191,55 +263,58 @@ func (c *OSCollector) collect(ch chan<- prometheus.Metric) (*prometheus.Desc, er
ch <- prometheus.MustNewConstMetric(
c.PagingFreeBytes,
prometheus.GaugeValue,
float64(dst[0].FreeSpaceInPagingFiles*1024), // KiB -> bytes
pfb,
)

ch <- prometheus.MustNewConstMetric(
c.VirtualMemoryFreeBytes,
prometheus.GaugeValue,
float64(dst[0].FreeVirtualMemory*1024), // KiB -> bytes
float64(gmse.AvailPageFile),
)

// 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
ch <- prometheus.MustNewConstMetric(
c.ProcessesLimit,
prometheus.GaugeValue,
float64(dst[0].MaxNumberOfProcesses),
float64(4294967295),
)

ch <- prometheus.MustNewConstMetric(
c.ProcessMemoryLimitBytes,
prometheus.GaugeValue,
float64(dst[0].MaxProcessMemorySize*1024), // KiB -> bytes
float64(gmse.TotalVirtual),
)

ch <- prometheus.MustNewConstMetric(
c.Processes,
prometheus.GaugeValue,
float64(dst[0].NumberOfProcesses),
float64(gpi.ProcessCount),
)

ch <- prometheus.MustNewConstMetric(
c.Users,
prometheus.GaugeValue,
float64(dst[0].NumberOfUsers),
float64(nwgi.LoggedOnUsers),
)

ch <- prometheus.MustNewConstMetric(
c.PagingLimitBytes,
prometheus.GaugeValue,
float64(dst[0].SizeStoredInPagingFiles*1024), // KiB -> bytes
float64(fsipf),
)

ch <- prometheus.MustNewConstMetric(
c.VirtualMemoryBytes,
prometheus.GaugeValue,
float64(dst[0].TotalVirtualMemorySize*1024), // KiB -> bytes
float64(gmse.TotalPageFile),
)

ch <- prometheus.MustNewConstMetric(
c.VisibleMemoryBytes,
prometheus.GaugeValue,
float64(dst[0].TotalVisibleMemorySize*1024), // KiB -> bytes
float64(gmse.TotalPhys),
)

return nil, nil
Expand Down
24 changes: 24 additions & 0 deletions collector/os_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package collector

import (
"testing"

"github.com/prometheus/client_golang/prometheus"
)

func BenchmarkOsCollect(b *testing.B) {
o, err := NewOSCollector()
if err != nil {
b.Error(err)
}
metrics := make(chan prometheus.Metric)
go func() {
for {
<-metrics
}
}()
s, err := PrepareScrapeContext([]string{"os"})
for i := 0; i < b.N; i++ {
o.Collect(s, metrics)
}
}
Loading

0 comments on commit cdc543d

Please sign in to comment.