2016-01-22 22:02:21 +00:00
|
|
|
// +build windows
|
|
|
|
|
|
|
|
package win_perf_counters
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
"unsafe"
|
|
|
|
|
|
|
|
"github.com/influxdata/telegraf"
|
|
|
|
"github.com/influxdata/telegraf/plugins/inputs"
|
|
|
|
)
|
|
|
|
|
2017-04-20 18:22:44 +00:00
|
|
|
var sampleConfig = `
|
2016-02-18 21:26:51 +00:00
|
|
|
## 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
|
|
|
|
## being polled for data does not have the Object at startup of the Telegraf
|
|
|
|
## agent, it will not be gathered.
|
|
|
|
## Settings:
|
2016-01-22 22:02:21 +00:00
|
|
|
# PrintValid = false # Print All matching performance counters
|
|
|
|
|
|
|
|
[[inputs.win_perf_counters.object]]
|
|
|
|
# Processor usage, alternative to native, reports on a per core.
|
|
|
|
ObjectName = "Processor"
|
|
|
|
Instances = ["*"]
|
|
|
|
Counters = [
|
2016-01-30 06:50:30 +00:00
|
|
|
"%% Idle Time", "%% Interrupt Time",
|
|
|
|
"%% Privileged Time", "%% User Time",
|
|
|
|
"%% Processor Time"
|
2016-01-22 22:02:21 +00:00
|
|
|
]
|
|
|
|
Measurement = "win_cpu"
|
|
|
|
# Set to true to include _Total instance when querying for all (*).
|
|
|
|
# IncludeTotal=false
|
|
|
|
# Print out when the performance counter is missing from object, counter or instance.
|
|
|
|
# WarnOnMissing = false
|
|
|
|
|
|
|
|
[[inputs.win_perf_counters.object]]
|
|
|
|
# Disk times and queues
|
|
|
|
ObjectName = "LogicalDisk"
|
|
|
|
Instances = ["*"]
|
|
|
|
Counters = [
|
2016-01-30 06:50:30 +00:00
|
|
|
"%% Idle Time", "%% Disk Time","%% Disk Read Time",
|
|
|
|
"%% Disk Write Time", "%% User Time", "Current Disk Queue Length"
|
2016-01-22 22:02:21 +00:00
|
|
|
]
|
|
|
|
Measurement = "win_disk"
|
|
|
|
|
|
|
|
[[inputs.win_perf_counters.object]]
|
|
|
|
ObjectName = "System"
|
|
|
|
Counters = ["Context Switches/sec","System Calls/sec"]
|
|
|
|
Instances = ["------"]
|
|
|
|
Measurement = "win_system"
|
|
|
|
|
|
|
|
[[inputs.win_perf_counters.object]]
|
|
|
|
# Example query where the Instance portion must be removed to get data back,
|
|
|
|
# such as from the Memory object.
|
|
|
|
ObjectName = "Memory"
|
|
|
|
Counters = [
|
|
|
|
"Available Bytes", "Cache Faults/sec", "Demand Zero Faults/sec",
|
|
|
|
"Page Faults/sec", "Pages/sec", "Transition Faults/sec",
|
|
|
|
"Pool Nonpaged Bytes", "Pool Paged Bytes"
|
|
|
|
]
|
|
|
|
Instances = ["------"] # Use 6 x - to remove the Instance bit from the query.
|
|
|
|
Measurement = "win_mem"
|
|
|
|
`
|
|
|
|
|
|
|
|
type Win_PerfCounters struct {
|
2016-01-31 16:40:50 +00:00
|
|
|
PrintValid bool
|
|
|
|
PreVistaSupport bool
|
|
|
|
Object []perfobject
|
2017-05-22 18:58:00 +00:00
|
|
|
|
|
|
|
configParsed bool
|
|
|
|
itemCache []*item
|
2016-01-22 22:02:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type perfobject struct {
|
|
|
|
ObjectName string
|
|
|
|
Counters []string
|
|
|
|
Instances []string
|
|
|
|
Measurement string
|
|
|
|
WarnOnMissing bool
|
|
|
|
FailOnMissing bool
|
|
|
|
IncludeTotal bool
|
|
|
|
}
|
|
|
|
|
|
|
|
type item struct {
|
|
|
|
query string
|
|
|
|
objectName string
|
|
|
|
counter string
|
|
|
|
instance string
|
|
|
|
measurement string
|
|
|
|
include_total bool
|
2016-11-09 18:43:39 +00:00
|
|
|
handle PDH_HQUERY
|
|
|
|
counterHandle PDH_HCOUNTER
|
2016-01-22 22:02:21 +00:00
|
|
|
}
|
|
|
|
|
2016-07-20 08:24:34 +00:00
|
|
|
var sanitizedChars = strings.NewReplacer("/sec", "_persec", "/Sec", "_persec",
|
|
|
|
" ", "_", "%", "Percent", `\`, "")
|
2016-04-28 17:12:04 +00:00
|
|
|
|
2017-05-22 18:58:00 +00:00
|
|
|
func (m *Win_PerfCounters) AddItem(query string, objectName string, counter string, instance string,
|
2017-01-24 20:46:06 +00:00
|
|
|
measurement string, include_total bool) error {
|
2016-01-22 22:02:21 +00:00
|
|
|
|
2016-11-09 18:43:39 +00:00
|
|
|
var handle PDH_HQUERY
|
|
|
|
var counterHandle PDH_HCOUNTER
|
|
|
|
ret := PdhOpenQuery(0, 0, &handle)
|
2017-01-24 20:46:06 +00:00
|
|
|
if m.PreVistaSupport {
|
|
|
|
ret = PdhAddCounter(handle, query, 0, &counterHandle)
|
|
|
|
} else {
|
|
|
|
ret = PdhAddEnglishCounter(handle, query, 0, &counterHandle)
|
|
|
|
}
|
|
|
|
|
2017-11-01 00:00:06 +00:00
|
|
|
// Call PdhCollectQueryData one time to check existence of the counter
|
2017-01-24 20:46:06 +00:00
|
|
|
ret = PdhCollectQueryData(handle)
|
|
|
|
if ret != ERROR_SUCCESS {
|
2017-04-20 18:22:44 +00:00
|
|
|
PdhCloseQuery(handle)
|
|
|
|
return errors.New(PdhFormatError(ret))
|
2017-01-24 20:46:06 +00:00
|
|
|
}
|
2016-01-22 22:02:21 +00:00
|
|
|
|
2017-05-22 18:58:00 +00:00
|
|
|
newItem := &item{query, objectName, counter, instance, measurement,
|
2016-01-22 22:02:21 +00:00
|
|
|
include_total, handle, counterHandle}
|
2017-05-22 18:58:00 +00:00
|
|
|
m.itemCache = append(m.itemCache, newItem)
|
2016-01-22 22:02:21 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *Win_PerfCounters) Description() string {
|
|
|
|
return "Input plugin to query Performance Counters on Windows operating systems"
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *Win_PerfCounters) SampleConfig() string {
|
|
|
|
return sampleConfig
|
|
|
|
}
|
|
|
|
|
2017-05-22 18:58:00 +00:00
|
|
|
func (m *Win_PerfCounters) ParseConfig() error {
|
2016-01-22 22:02:21 +00:00
|
|
|
var query string
|
|
|
|
|
|
|
|
if len(m.Object) > 0 {
|
|
|
|
for _, PerfObject := range m.Object {
|
|
|
|
for _, counter := range PerfObject.Counters {
|
|
|
|
for _, instance := range PerfObject.Instances {
|
|
|
|
objectname := PerfObject.ObjectName
|
|
|
|
|
|
|
|
if instance == "------" {
|
|
|
|
query = "\\" + objectname + "\\" + counter
|
|
|
|
} else {
|
|
|
|
query = "\\" + objectname + "(" + instance + ")\\" + counter
|
|
|
|
}
|
|
|
|
|
2017-05-22 18:58:00 +00:00
|
|
|
err := m.AddItem(query, objectname, counter, instance,
|
2017-01-24 20:46:06 +00:00
|
|
|
PerfObject.Measurement, PerfObject.IncludeTotal)
|
2016-01-22 22:02:21 +00:00
|
|
|
|
2017-01-24 20:46:06 +00:00
|
|
|
if err == nil {
|
2016-01-22 22:02:21 +00:00
|
|
|
if m.PrintValid {
|
|
|
|
fmt.Printf("Valid: %s\n", query)
|
|
|
|
}
|
|
|
|
} else {
|
2016-01-31 13:21:24 +00:00
|
|
|
if PerfObject.FailOnMissing || PerfObject.WarnOnMissing {
|
2017-04-20 18:22:44 +00:00
|
|
|
fmt.Printf("Invalid query: '%s'. Error: %s", query, err.Error())
|
2017-01-24 20:46:06 +00:00
|
|
|
}
|
|
|
|
if PerfObject.FailOnMissing {
|
2016-01-31 13:21:24 +00:00
|
|
|
return err
|
|
|
|
}
|
2016-01-22 22:02:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
} else {
|
|
|
|
err := errors.New("No performance objects configured!")
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-31 18:03:26 +00:00
|
|
|
func (m *Win_PerfCounters) GetParsedItemsForTesting() []*item {
|
|
|
|
return m.itemCache
|
|
|
|
}
|
|
|
|
|
2016-01-22 22:02:21 +00:00
|
|
|
func (m *Win_PerfCounters) Gather(acc telegraf.Accumulator) error {
|
2017-05-22 18:58:00 +00:00
|
|
|
// Parse the config once
|
|
|
|
if !m.configParsed {
|
|
|
|
err := m.ParseConfig()
|
|
|
|
m.configParsed = true
|
2016-01-22 22:02:21 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var bufSize uint32
|
|
|
|
var bufCount uint32
|
2016-11-09 18:43:39 +00:00
|
|
|
var size uint32 = uint32(unsafe.Sizeof(PDH_FMT_COUNTERVALUE_ITEM_DOUBLE{}))
|
|
|
|
var emptyBuf [1]PDH_FMT_COUNTERVALUE_ITEM_DOUBLE // need at least 1 addressable null ptr.
|
2016-01-22 22:02:21 +00:00
|
|
|
|
2018-05-01 00:48:45 +00:00
|
|
|
type InstanceGrouping struct {
|
|
|
|
name string
|
|
|
|
instance string
|
|
|
|
objectname string
|
|
|
|
}
|
|
|
|
|
|
|
|
var collectFields = make(map[InstanceGrouping]map[string]interface{})
|
|
|
|
|
2016-01-22 22:02:21 +00:00
|
|
|
// For iterate over the known metrics and get the samples.
|
2017-05-22 18:58:00 +00:00
|
|
|
for _, metric := range m.itemCache {
|
2016-01-22 22:02:21 +00:00
|
|
|
// collect
|
2016-11-09 18:43:39 +00:00
|
|
|
ret := PdhCollectQueryData(metric.handle)
|
|
|
|
if ret == ERROR_SUCCESS {
|
|
|
|
ret = PdhGetFormattedCounterArrayDouble(metric.counterHandle, &bufSize,
|
2016-01-22 22:02:21 +00:00
|
|
|
&bufCount, &emptyBuf[0]) // uses null ptr here according to MSDN.
|
2016-11-09 18:43:39 +00:00
|
|
|
if ret == PDH_MORE_DATA {
|
|
|
|
filledBuf := make([]PDH_FMT_COUNTERVALUE_ITEM_DOUBLE, bufCount*size)
|
2016-08-09 06:23:22 +00:00
|
|
|
if len(filledBuf) == 0 {
|
|
|
|
continue
|
|
|
|
}
|
2016-11-09 18:43:39 +00:00
|
|
|
ret = PdhGetFormattedCounterArrayDouble(metric.counterHandle,
|
2016-01-22 22:02:21 +00:00
|
|
|
&bufSize, &bufCount, &filledBuf[0])
|
|
|
|
for i := 0; i < int(bufCount); i++ {
|
|
|
|
c := filledBuf[i]
|
2016-11-09 18:43:39 +00:00
|
|
|
var s string = UTF16PtrToString(c.SzName)
|
2016-01-22 22:02:21 +00:00
|
|
|
|
|
|
|
var add bool
|
|
|
|
|
|
|
|
if metric.include_total {
|
|
|
|
// If IncludeTotal is set, include all.
|
|
|
|
add = true
|
|
|
|
} else if metric.instance == "*" && !strings.Contains(s, "_Total") {
|
|
|
|
// Catch if set to * and that it is not a '*_Total*' instance.
|
|
|
|
add = true
|
|
|
|
} else if metric.instance == s {
|
|
|
|
// Catch if we set it to total or some form of it
|
|
|
|
add = true
|
2017-03-22 19:04:58 +00:00
|
|
|
} else if strings.Contains(metric.instance, "#") && strings.HasPrefix(metric.instance, s) {
|
|
|
|
// If you are using a multiple instance identifier such as "w3wp#1"
|
|
|
|
// phd.dll returns only the first 2 characters of the identifier.
|
|
|
|
add = true
|
|
|
|
s = metric.instance
|
2016-01-22 22:02:21 +00:00
|
|
|
} else if metric.instance == "------" {
|
|
|
|
add = true
|
|
|
|
}
|
|
|
|
|
|
|
|
if add {
|
|
|
|
tags := make(map[string]string)
|
|
|
|
if s != "" {
|
|
|
|
tags["instance"] = s
|
|
|
|
}
|
|
|
|
tags["objectname"] = metric.objectName
|
|
|
|
|
2016-07-20 08:24:34 +00:00
|
|
|
measurement := sanitizedChars.Replace(metric.measurement)
|
|
|
|
if measurement == "" {
|
2016-01-22 22:02:21 +00:00
|
|
|
measurement = "win_perf_counters"
|
|
|
|
}
|
2018-05-01 00:48:45 +00:00
|
|
|
var instance = InstanceGrouping{measurement, s, metric.objectName}
|
|
|
|
|
|
|
|
if collectFields[instance] == nil {
|
|
|
|
collectFields[instance] = make(map[string]interface{})
|
|
|
|
}
|
|
|
|
collectFields[instance][sanitizedChars.Replace(metric.counter)] = float32(c.FmtValue.DoubleValue)
|
2016-01-22 22:02:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
filledBuf = nil
|
|
|
|
// Need to at least set bufSize to zero, because if not, the function will not
|
|
|
|
// return PDH_MORE_DATA and will not set the bufSize.
|
|
|
|
bufCount = 0
|
|
|
|
bufSize = 0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-01 00:48:45 +00:00
|
|
|
for instance, fields := range collectFields {
|
|
|
|
var tags = map[string]string{
|
|
|
|
"instance": instance.instance,
|
|
|
|
"objectname": instance.objectname,
|
|
|
|
}
|
|
|
|
acc.AddFields(instance.name, fields, tags)
|
|
|
|
}
|
|
|
|
|
2016-01-22 22:02:21 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
inputs.Add("win_perf_counters", func() telegraf.Input { return &Win_PerfCounters{} })
|
|
|
|
}
|