mi: replace all WMI calls with MI calls (#1700)

This commit is contained in:
Jan-Otto Kröpke
2024-11-03 01:03:34 +01:00
committed by GitHub
parent 582d8dd29c
commit c4f5d58a3e
82 changed files with 2767 additions and 738 deletions

View File

@@ -0,0 +1,28 @@
package testutils
import (
"unsafe"
"golang.org/x/sys/windows"
)
var (
modkernel32 = windows.NewLazySystemDLL("kernel32.dll")
procGetProcessHandleCount = modkernel32.NewProc("GetProcessHandleCount")
)
func GetProcessHandleCount(handle windows.Handle) (uint32, error) {
var count uint32
r1, _, err := procGetProcessHandleCount.Call(
uintptr(handle),
uintptr(unsafe.Pointer(&count)),
)
if r1 != 1 {
return 0, err
}
return count, nil
}

View File

@@ -10,10 +10,10 @@ import (
"time"
"github.com/alecthomas/kingpin/v2"
"github.com/prometheus-community/windows_exporter/internal/mi"
"github.com/prometheus-community/windows_exporter/pkg/collector"
"github.com/prometheus/client_golang/prometheus"
"github.com/stretchr/testify/require"
"github.com/yusufpapurcu/wmi"
)
func FuncBenchmarkCollector[C collector.Collector](b *testing.B, name string, collectFunc collector.BuilderWithFlags[C]) {
@@ -57,14 +57,16 @@ func TestCollector[C collector.Collector, V interface{}](t *testing.T, fn func(*
c := fn(conf)
ch := make(chan prometheus.Metric, 10000)
wmiClient := &wmi.Client{
AllowMissingFields: true,
}
wmiClient.SWbemServicesClient, err = wmi.InitializeSWbemServices(wmiClient)
miApp, err := mi.Application_Initialize()
require.NoError(t, err)
miSession, err := miApp.NewSession(nil)
require.NoError(t, err)
t.Cleanup(func() {
require.NoError(t, c.Close(logger))
require.NoError(t, miSession.Close())
require.NoError(t, miApp.Close())
})
wg := sync.WaitGroup{}
@@ -78,7 +80,7 @@ func TestCollector[C collector.Collector, V interface{}](t *testing.T, fn func(*
}
}()
require.NoError(t, c.Build(logger, wmiClient))
require.NoError(t, c.Build(logger, miSession))
time.Sleep(1 * time.Second)