mirror of
https://github.com/prometheus-community/windows_exporter.git
synced 2026-02-16 09:46:35 +00:00
*: avoid using default wmi client. (#1590)
This commit is contained in:
@@ -5,43 +5,39 @@ package winversion
|
||||
import (
|
||||
"fmt"
|
||||
"strconv"
|
||||
"sync"
|
||||
|
||||
"golang.org/x/sys/windows/registry"
|
||||
)
|
||||
|
||||
var (
|
||||
WindowsVersion string
|
||||
WindowsVersionFloat float64
|
||||
)
|
||||
|
||||
//nolint:gochecknoinits
|
||||
func init() {
|
||||
var err error
|
||||
WindowsVersion, WindowsVersionFloat, err = GetWindowsVersion()
|
||||
var WindowsVersionFloat = sync.OnceValue[float64](func() float64 {
|
||||
version, err := getWindowsVersion()
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
|
||||
return version
|
||||
})
|
||||
|
||||
// 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() (string, float64, error) {
|
||||
func getWindowsVersion() (float64, error) {
|
||||
reg, err := registry.OpenKey(registry.LOCAL_MACHINE, `SOFTWARE\Microsoft\Windows NT\CurrentVersion`, registry.QUERY_VALUE)
|
||||
if err != nil {
|
||||
return "", 0, fmt.Errorf("couldn't open registry: %w", err)
|
||||
return 0, fmt.Errorf("couldn't open registry: %w", err)
|
||||
}
|
||||
|
||||
defer reg.Close()
|
||||
|
||||
windowsVersion, _, err := reg.GetStringValue("CurrentVersion")
|
||||
if err != nil {
|
||||
return "", 0, fmt.Errorf("couldn't open registry: %w", err)
|
||||
return 0, fmt.Errorf("couldn't open registry: %w", err)
|
||||
}
|
||||
|
||||
windowsVersionFloat, err := strconv.ParseFloat(windowsVersion, 64)
|
||||
if err != nil {
|
||||
return "", 0, fmt.Errorf("couldn't open registry: %w", err)
|
||||
return 0, fmt.Errorf("couldn't open registry: %w", err)
|
||||
}
|
||||
|
||||
return windowsVersion, windowsVersionFloat, nil
|
||||
return windowsVersionFloat, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user