diff --git a/.gitignore b/.gitignore index c3015a88..7cc56baf 100644 --- a/.gitignore +++ b/.gitignore @@ -3,3 +3,4 @@ VERSION *.swp *.un~ output/ +.idea diff --git a/collector/collector.go b/collector/collector.go index 11622632..3cdac9a7 100644 --- a/collector/collector.go +++ b/collector/collector.go @@ -1,8 +1,12 @@ package collector import ( + "strconv" + "github.com/leoluk/perflib_exporter/perflib" "github.com/prometheus/client_golang/prometheus" + "github.com/prometheus/common/log" + "golang.org/x/sys/windows/registry" ) // ... @@ -15,6 +19,34 @@ const ( windowsEpoch = 116444736000000000 ) +// getWindowsVersion reads the version number of the OS from the Registry +// See https://docs.microsoft.com/en-us/windows/desktop/sysinfo/operating-system-version +func getWindowsVersion() float64 { + k, err := registry.OpenKey(registry.LOCAL_MACHINE, `SOFTWARE\Microsoft\Windows NT\CurrentVersion`, registry.QUERY_VALUE) + if err != nil { + log.Warn("Couldn't open registry", err) + return 0 + } + defer func() { + err = k.Close() + if err != nil { + log.Warnf("Failed to close registry key: %v", err) + } + }() + + currentv, _, err := k.GetStringValue("CurrentVersion") + if err != nil { + log.Warn("Couldn't open registry to determine current Windows version:", err) + return 0 + } + + currentv_flt, err := strconv.ParseFloat(currentv, 64) + + log.Debugf("Detected Windows version %f\n", currentv_flt) + + return currentv_flt +} + // Factories ... var Factories = make(map[string]func() (Collector, error)) diff --git a/collector/cpu.go b/collector/cpu.go index abd4d6f0..8aa7fa15 100644 --- a/collector/cpu.go +++ b/collector/cpu.go @@ -3,46 +3,15 @@ package collector import ( - "strconv" "strings" - "golang.org/x/sys/windows/registry" - "github.com/prometheus/client_golang/prometheus" - "github.com/prometheus/common/log" ) func init() { Factories["cpu"] = newCPUCollector } -// A function to get Windows version from registry -func getWindowsVersion() float64 { - k, err := registry.OpenKey(registry.LOCAL_MACHINE, `SOFTWARE\Microsoft\Windows NT\CurrentVersion`, registry.QUERY_VALUE) - if err != nil { - log.Warn("Couldn't open registry", err) - return 0 - } - defer func() { - err = k.Close() - if err != nil { - log.Warnf("Failed to close registry key: %v", err) - } - }() - - currentv, _, err := k.GetStringValue("CurrentVersion") - if err != nil { - log.Warn("Couldn't open registry to determine current Windows version:", err) - return 0 - } - - currentv_flt, err := strconv.ParseFloat(currentv, 64) - - log.Debugf("Detected Windows version %f\n", currentv_flt) - - return currentv_flt -} - type cpuCollectorBasic struct { CStateSecondsTotal *prometheus.Desc TimeTotal *prometheus.Desc @@ -67,10 +36,12 @@ func newCPUCollector() (Collector, error) { const subsystem = "cpu" version := getWindowsVersion() - // Windows version by number https://docs.microsoft.com/en-us/windows/desktop/sysinfo/operating-system-version - // For Windows 2008 or earlier Windows version is 6.0 or lower, where we only have the older "Processor" counters - // For Windows 2008 R2 or later Windows version is 6.1 or higher, so we can use "ProcessorInformation" counters - // Value 6.05 was selected just to split between Windows versions + // For Windows 2008 (version 6.0) or earlier we only have the "Processor" + // class. As of Windows 2008 R2 (version 6.1) the more detailed + // "ProcessorInformation" set is available (although some of the counters + // are added in later versions, so we aren't guaranteed to get all of + // them). + // Value 6.05 was selected to split between Windows versions. if version < 6.05 { return &cpuCollectorBasic{ CStateSecondsTotal: prometheus.NewDesc( diff --git a/collector/perflib.go b/collector/perflib.go index 0cf6e454..8abd5185 100644 --- a/collector/perflib.go +++ b/collector/perflib.go @@ -64,7 +64,6 @@ func unmarshalObject(obj *perflib.PerfObject, vs interface{}) error { ctr, found := counters[tag] if !found { log.Debugf("missing counter %q, has %v", tag, counters) - return fmt.Errorf("could not find counter %q on instance", tag) } if !target.Field(i).CanSet() { return fmt.Errorf("tagged field %v cannot be written to", f)