Skip to content
Snippets Groups Projects
Commit b03d78d0 authored by Oleg Grytsynevych's avatar Oleg Grytsynevych Committed by Daniel Nelson
Browse files

win_perf_counters: Format errors reported by pdh.dll in human-readable format (#2338)

parent 748ca7d5
No related branches found
No related tags found
No related merge requests found
...@@ -33,8 +33,11 @@ ...@@ -33,8 +33,11 @@
package win_perf_counters package win_perf_counters
import ( import (
"fmt"
"syscall" "syscall"
"unsafe" "unsafe"
"golang.org/x/sys/windows"
) )
// Error codes // Error codes
...@@ -417,3 +420,13 @@ func UTF16PtrToString(s *uint16) string { ...@@ -417,3 +420,13 @@ func UTF16PtrToString(s *uint16) string {
} }
return syscall.UTF16ToString((*[1 << 29]uint16)(unsafe.Pointer(s))[0:]) return syscall.UTF16ToString((*[1 << 29]uint16)(unsafe.Pointer(s))[0:])
} }
func PdhFormatError(msgId uint32) string {
var flags uint32 = windows.FORMAT_MESSAGE_FROM_HMODULE | windows.FORMAT_MESSAGE_ARGUMENT_ARRAY | windows.FORMAT_MESSAGE_IGNORE_INSERTS
buf := make([]uint16, 300)
_, err := windows.FormatMessage(flags, uintptr(libpdhDll.Handle), msgId, 0, buf, nil)
if err == nil {
return fmt.Sprintf("%s", UTF16PtrToString(&buf[0]))
}
return fmt.Sprintf("(pdhErr=%d) %s", msgId, err.Error())
}
...@@ -12,7 +12,7 @@ import ( ...@@ -12,7 +12,7 @@ import (
"github.com/influxdata/telegraf/plugins/inputs" "github.com/influxdata/telegraf/plugins/inputs"
) )
var sampleConfig string = ` var sampleConfig = `
## By default this plugin returns basic CPU and Disk statistics. ## By default this plugin returns basic CPU and Disk statistics.
## See the README file for more examples. ## See the README file for more examples.
## Uncomment examples below or write your own as you see fit. If the system ## Uncomment examples below or write your own as you see fit. If the system
...@@ -124,8 +124,8 @@ func (m *Win_PerfCounters) AddItem(metrics *itemList, query string, objectName s ...@@ -124,8 +124,8 @@ func (m *Win_PerfCounters) AddItem(metrics *itemList, query string, objectName s
// Call PdhCollectQueryData one time to check existance of the counter // Call PdhCollectQueryData one time to check existance of the counter
ret = PdhCollectQueryData(handle) ret = PdhCollectQueryData(handle)
if ret != ERROR_SUCCESS { if ret != ERROR_SUCCESS {
ret = PdhCloseQuery(handle) PdhCloseQuery(handle)
return errors.New("Invalid query for Performance Counters") return errors.New(PdhFormatError(ret))
} }
temp := &item{query, objectName, counter, instance, measurement, temp := &item{query, objectName, counter, instance, measurement,
...@@ -174,7 +174,7 @@ func (m *Win_PerfCounters) ParseConfig(metrics *itemList) error { ...@@ -174,7 +174,7 @@ func (m *Win_PerfCounters) ParseConfig(metrics *itemList) error {
} }
} else { } else {
if PerfObject.FailOnMissing || PerfObject.WarnOnMissing { if PerfObject.FailOnMissing || PerfObject.WarnOnMissing {
fmt.Printf("Invalid query: %s\n", query) fmt.Printf("Invalid query: '%s'. Error: %s", query, err.Error())
} }
if PerfObject.FailOnMissing { if PerfObject.FailOnMissing {
return err return err
...@@ -298,7 +298,6 @@ func (m *Win_PerfCounters) Gather(acc telegraf.Accumulator) error { ...@@ -298,7 +298,6 @@ func (m *Win_PerfCounters) Gather(acc telegraf.Accumulator) error {
bufCount = 0 bufCount = 0
bufSize = 0 bufSize = 0
} }
} }
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment