@@ -7,11 +7,14 @@ import (
77 "strconv"
88 "strings"
99 "time"
10+ "unsafe"
1011
1112 "github.com/consol-monitoring/snclient/pkg/pdh"
1213 "golang.org/x/sys/windows"
1314)
1415
16+ const querySleepDuration = 500 * time .Millisecond
17+
1518type PdhValue struct {
1619 CounterInstance string
1720 Value int64
@@ -29,12 +32,11 @@ func (c *CheckPDH) check(_ context.Context, _ *Agent, check *CheckData, args []A
2932 var possiblePaths []string
3033 var hQuery pdh.PDH_HQUERY
3134 // Open Query - Data Source = 0 => Real Time Datasource
32- ret := pdh .PdhOpenQuery (uintptr (0 ), uintptr (0 ), & hQuery )
33- defer pdh .PdhCloseQuery (hQuery )
34-
35+ ret := pdh .PdhOpenQuery (0 , 0 , & hQuery )
3536 if ret != pdh .ERROR_SUCCESS {
3637 return nil , fmt .Errorf ("could not open query, something is wrong with the countername" )
3738 }
39+ defer pdh .PdhCloseQuery (hQuery )
3840
3941 tmpPath := c .CounterPath
4042 if c .EnglishFallBackNames {
@@ -168,7 +170,7 @@ func collectQueryData(hQuery *pdh.PDH_HQUERY) uint32 {
168170 return ret
169171 }
170172 // PDH requires a double collection with a second wait between the calls See MSDN
171- time .Sleep (time . Duration ( 1 ) )
173+ time .Sleep (querySleepDuration )
172174 ret = pdh .PdhCollectQueryData (* hQuery )
173175
174176 return ret
@@ -180,26 +182,24 @@ func collectQueryData(hQuery *pdh.PDH_HQUERY) uint32 {
180182- if More Data -> Create Actual Array and fill
181183*/
182184func collectLargeValuesArray (hCounter pdh.PDH_HCOUNTER , hQuery pdh.PDH_HQUERY ) (values []PdhValue , apiResponseCode uint32 ) {
183- var resArr [1 ]pdh.PDH_FMT_COUNTERVALUE_ITEM_LARGE // Need at least one nil pointer
184- var ret uint32
185- var filledBuf []pdh.PDH_FMT_COUNTERVALUE_ITEM_LARGE
186- size := uint32 (0 )
187- bufferCount := uint32 (0 )
185+ var bufCount uint32
186+ var bufSize uint32
187+ size := uint32 (unsafe .Sizeof (pdh.PDH_FMT_COUNTERVALUE_ITEM_LARGE {}))
188+ var emptyBuf [1 ]pdh.PDH_FMT_COUNTERVALUE_ITEM_LARGE // need at least 1 addressable null ptr.
188189 if res := collectQueryData (& hQuery ); res != pdh .ERROR_SUCCESS {
189190 return nil , res
190191 }
191- if ret = pdh .PdhGetFormattedCounterArrayLarge (hCounter , & size , & bufferCount , & resArr [0 ]); ret == pdh .PDH_MORE_DATA {
192- // create array of size = bufferCount * sizeOf(pdh.PDH_FMT_COUNTERVALUE_ITEM_LARGE)
193- filledBuf = make ([]pdh.PDH_FMT_COUNTERVALUE_ITEM_LARGE , bufferCount )
194- ret = pdh .PdhGetFormattedCounterArrayLarge (hCounter , & size , & bufferCount , & filledBuf [0 ])
195- }
196- returnArray := make ([]PdhValue , 0 , bufferCount )
197- for _ , pdhVal := range filledBuf {
198- returnArray = append (returnArray , PdhValue {
199- CounterInstance : windows .UTF16PtrToString (pdhVal .SzName ),
200- Value : pdhVal .FmtValue .LargeValue ,
201- })
192+ returnArray := []PdhValue {}
193+ if ret := pdh .PdhGetFormattedCounterArrayLarge (hCounter , & bufSize , & bufCount , & emptyBuf [0 ]); ret == pdh .PDH_MORE_DATA {
194+ filledBuf := make ([]pdh.PDH_FMT_COUNTERVALUE_ITEM_LARGE , bufCount * size )
195+ pdh .PdhGetFormattedCounterArrayLarge (hCounter , & bufSize , & bufCount , & filledBuf [0 ])
196+ for i := range int (bufCount ) {
197+ returnArray = append (returnArray , PdhValue {
198+ CounterInstance : windows .UTF16PtrToString (filledBuf [i ].SzName ),
199+ Value : filledBuf [i ].FmtValue .LargeValue ,
200+ })
201+ }
202202 }
203203
204- return returnArray , ret
204+ return returnArray , pdh . ERROR_SUCCESS
205205}
0 commit comments