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

This commit is contained in:
Oleg Grytsynevych 2017-04-20 20:22:44 +02:00 committed by Daniel Nelson
parent 748ca7d503
commit b03d78d00f
2 changed files with 17 additions and 5 deletions

View File

@ -33,8 +33,11 @@
package win_perf_counters
import (
"fmt"
"syscall"
"unsafe"
"golang.org/x/sys/windows"
)
// Error codes
@ -417,3 +420,13 @@ func UTF16PtrToString(s *uint16) string {
}
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())
}

View File

@ -12,7 +12,7 @@ import (
"github.com/influxdata/telegraf/plugins/inputs"
)
var sampleConfig string = `
var sampleConfig = `
## By default this plugin returns basic CPU and Disk statistics.
## See the README file for more examples.
## 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
// Call PdhCollectQueryData one time to check existance of the counter
ret = PdhCollectQueryData(handle)
if ret != ERROR_SUCCESS {
ret = PdhCloseQuery(handle)
return errors.New("Invalid query for Performance Counters")
PdhCloseQuery(handle)
return errors.New(PdhFormatError(ret))
}
temp := &item{query, objectName, counter, instance, measurement,
@ -174,7 +174,7 @@ func (m *Win_PerfCounters) ParseConfig(metrics *itemList) error {
}
} else {
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 {
return err
@ -298,7 +298,6 @@ func (m *Win_PerfCounters) Gather(acc telegraf.Accumulator) error {
bufCount = 0
bufSize = 0
}
}
}