2016-01-22 22:02:21 +00:00
|
|
|
// +build windows
|
|
|
|
|
|
|
|
package win_perf_counters
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
2018-05-25 01:25:06 +00:00
|
|
|
"log"
|
|
|
|
"regexp"
|
|
|
|
"strings"
|
|
|
|
"time"
|
2018-06-12 13:20:00 +00:00
|
|
|
|
|
|
|
"github.com/influxdata/telegraf"
|
|
|
|
"github.com/influxdata/telegraf/internal"
|
|
|
|
"github.com/influxdata/telegraf/plugins/inputs"
|
2016-01-22 22:02:21 +00:00
|
|
|
)
|
|
|
|
|
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
|
2018-06-12 13:20:00 +00:00
|
|
|
# If UseWildcardsExpansion params is set to true, wildcards (partial wildcards in instance names and wildcards in counters names) in configured counter paths will be expanded
|
2018-06-11 18:10:53 +00:00
|
|
|
# and in case of localized Windows, counter paths will be also localized. It also returns instance indexes in instance names.
|
2018-06-12 13:20:00 +00:00
|
|
|
# If false, wildcards (not partial) in instance names will still be expanded, but instance indexes will not be returned in instance names.
|
2018-06-11 18:10:53 +00:00
|
|
|
#UseWildcardsExpansion = false
|
2018-05-25 01:25:06 +00:00
|
|
|
# Period after which counters will be reread from configuration and wildcards in counter paths expanded
|
|
|
|
CountersRefreshInterval="1m"
|
2016-01-22 22:02:21 +00:00
|
|
|
|
|
|
|
[[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]]
|
2018-05-25 01:25:06 +00:00
|
|
|
# Example counterPath where the Instance portion must be removed to get data back,
|
2016-01-22 22:02:21 +00:00
|
|
|
# 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"
|
|
|
|
]
|
2018-05-25 01:25:06 +00:00
|
|
|
Instances = ["------"] # Use 6 x - to remove the Instance bit from the counterPath.
|
2016-01-22 22:02:21 +00:00
|
|
|
Measurement = "win_mem"
|
|
|
|
`
|
|
|
|
|
|
|
|
type Win_PerfCounters struct {
|
2018-05-25 01:25:06 +00:00
|
|
|
PrintValid bool
|
|
|
|
//deprecated: determined dynamically
|
|
|
|
PreVistaSupport bool
|
|
|
|
Object []perfobject
|
|
|
|
CountersRefreshInterval internal.Duration
|
2018-06-11 18:10:53 +00:00
|
|
|
UseWildcardsExpansion bool
|
2018-05-25 01:25:06 +00:00
|
|
|
|
|
|
|
lastRefreshed time.Time
|
|
|
|
counters []*counter
|
|
|
|
query PerformanceQuery
|
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
|
|
|
|
}
|
|
|
|
|
2018-05-25 01:25:06 +00:00
|
|
|
type counter struct {
|
|
|
|
counterPath string
|
2016-01-22 22:02:21 +00:00
|
|
|
objectName string
|
|
|
|
counter string
|
|
|
|
instance string
|
|
|
|
measurement string
|
2018-05-25 01:25:06 +00:00
|
|
|
includeTotal bool
|
2016-11-09 18:43:39 +00:00
|
|
|
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
|
|
|
|
2018-05-25 01:25:06 +00:00
|
|
|
//General Counter path pattern is: \\computer\object(parent/instance#index)\counter
|
|
|
|
//parent/instance#index part is skipped in single instance objects (e.g. Memory): \\computer\object\counter
|
2016-01-22 22:02:21 +00:00
|
|
|
|
2018-05-25 01:25:06 +00:00
|
|
|
var counterPathRE = regexp.MustCompile(`.*\\(.*)\\(.*)`)
|
|
|
|
var objectInstanceRE = regexp.MustCompile(`(.*)\((.*)\)`)
|
2017-01-24 20:46:06 +00:00
|
|
|
|
2018-05-25 01:25:06 +00:00
|
|
|
//extractObjectInstanceCounterFromQuery gets object name, instance name (if available) and counter name from counter path
|
|
|
|
func extractObjectInstanceCounterFromQuery(query string) (object string, instance string, counter string, err error) {
|
|
|
|
pathParts := counterPathRE.FindAllStringSubmatch(query, -1)
|
|
|
|
if pathParts == nil || len(pathParts[0]) != 3 {
|
|
|
|
err = errors.New("Could not extract counter info from: " + query)
|
|
|
|
return
|
2017-01-24 20:46:06 +00:00
|
|
|
}
|
2018-05-25 01:25:06 +00:00
|
|
|
counter = pathParts[0][2]
|
|
|
|
//try to get instance name
|
|
|
|
objectInstanceParts := objectInstanceRE.FindAllStringSubmatch(pathParts[0][1], -1)
|
|
|
|
if objectInstanceParts == nil || len(objectInstanceParts[0]) != 3 {
|
|
|
|
object = pathParts[0][1]
|
|
|
|
} else {
|
|
|
|
object = objectInstanceParts[0][1]
|
|
|
|
instance = objectInstanceParts[0][2]
|
|
|
|
}
|
|
|
|
return
|
2016-01-22 22:02:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (m *Win_PerfCounters) Description() string {
|
2018-05-25 01:25:06 +00:00
|
|
|
return "Input plugin to counterPath Performance Counters on Windows operating systems"
|
2016-01-22 22:02:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (m *Win_PerfCounters) SampleConfig() string {
|
|
|
|
return sampleConfig
|
|
|
|
}
|
|
|
|
|
2018-06-11 18:10:53 +00:00
|
|
|
//objectName string, counter string, instance string, measurement string, include_total bool
|
|
|
|
func (m *Win_PerfCounters) AddItem(counterPath string, objectName string, instance string, counterName string, measurement string, includeTotal bool) error {
|
|
|
|
var err error
|
|
|
|
var counterHandle PDH_HCOUNTER
|
2018-05-25 01:25:06 +00:00
|
|
|
if !m.query.AddEnglishCounterSupported() {
|
2018-06-11 18:10:53 +00:00
|
|
|
counterHandle, err = m.query.AddCounterToQuery(counterPath)
|
2018-05-25 01:25:06 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
} else {
|
2018-06-11 18:10:53 +00:00
|
|
|
counterHandle, err = m.query.AddEnglishCounterToQuery(counterPath)
|
2018-05-25 01:25:06 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-06-11 18:10:53 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if m.UseWildcardsExpansion {
|
|
|
|
origInstance := instance
|
2018-05-25 01:25:06 +00:00
|
|
|
counterPath, err = m.query.GetCounterPath(counterHandle)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-06-11 18:10:53 +00:00
|
|
|
counters, err := m.query.ExpandWildCardPath(counterPath)
|
2018-05-25 01:25:06 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-06-11 18:10:53 +00:00
|
|
|
for _, counterPath := range counters {
|
|
|
|
var err error
|
|
|
|
counterHandle, err := m.query.AddCounterToQuery(counterPath)
|
|
|
|
|
|
|
|
objectName, instance, counterName, err = extractObjectInstanceCounterFromQuery(counterPath)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if instance == "_Total" && origInstance == "*" && !includeTotal {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
newItem := &counter{counterPath, objectName, counterName, instance, measurement,
|
|
|
|
includeTotal, counterHandle}
|
|
|
|
m.counters = append(m.counters, newItem)
|
2018-05-25 01:25:06 +00:00
|
|
|
|
2018-06-11 18:10:53 +00:00
|
|
|
if m.PrintValid {
|
|
|
|
log.Printf("Valid: %s\n", counterPath)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
newItem := &counter{counterPath, objectName, counterName, instance, measurement,
|
2018-05-25 01:25:06 +00:00
|
|
|
includeTotal, counterHandle}
|
|
|
|
m.counters = append(m.counters, newItem)
|
|
|
|
if m.PrintValid {
|
|
|
|
log.Printf("Valid: %s\n", counterPath)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-05-22 18:58:00 +00:00
|
|
|
func (m *Win_PerfCounters) ParseConfig() error {
|
2018-05-25 01:25:06 +00:00
|
|
|
var counterPath string
|
2016-01-22 22:02:21 +00:00
|
|
|
|
|
|
|
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 == "------" {
|
2018-05-25 01:25:06 +00:00
|
|
|
counterPath = "\\" + objectname + "\\" + counter
|
2016-01-22 22:02:21 +00:00
|
|
|
} else {
|
2018-05-25 01:25:06 +00:00
|
|
|
counterPath = "\\" + objectname + "(" + instance + ")\\" + counter
|
2016-01-22 22:02:21 +00:00
|
|
|
}
|
|
|
|
|
2018-06-11 18:10:53 +00:00
|
|
|
err := m.AddItem(counterPath, objectname, instance, counter, PerfObject.Measurement, PerfObject.IncludeTotal)
|
2016-01-22 22:02:21 +00:00
|
|
|
|
2018-05-25 01:25:06 +00:00
|
|
|
if err != nil {
|
2016-01-31 13:21:24 +00:00
|
|
|
if PerfObject.FailOnMissing || PerfObject.WarnOnMissing {
|
2018-05-25 01:25:06 +00:00
|
|
|
log.Printf("Invalid counterPath: '%s'. Error: %s\n", counterPath, 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 {
|
2018-05-25 01:25:06 +00:00
|
|
|
err := errors.New("no performance objects configured")
|
2016-01-22 22:02:21 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-07-31 18:03:26 +00:00
|
|
|
}
|
|
|
|
|
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
|
2018-05-25 01:25:06 +00:00
|
|
|
var err error
|
|
|
|
|
|
|
|
if m.lastRefreshed.IsZero() || (m.CountersRefreshInterval.Duration.Nanoseconds() > 0 && m.lastRefreshed.Add(m.CountersRefreshInterval.Duration).Before(time.Now())) {
|
2018-06-11 18:10:53 +00:00
|
|
|
if m.counters != nil {
|
|
|
|
m.counters = m.counters[:0]
|
|
|
|
}
|
2018-05-25 01:25:06 +00:00
|
|
|
|
|
|
|
err = m.query.Open()
|
2016-01-22 22:02:21 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-05-25 01:25:06 +00:00
|
|
|
err = m.ParseConfig()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
//some counters need two data samples before computing a value
|
|
|
|
err = m.query.CollectData()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
m.lastRefreshed = time.Now()
|
|
|
|
|
|
|
|
time.Sleep(time.Second)
|
|
|
|
}
|
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{})
|
|
|
|
|
2018-05-25 01:25:06 +00:00
|
|
|
err = m.query.CollectData()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-01-22 22:02:21 +00:00
|
|
|
// For iterate over the known metrics and get the samples.
|
2018-05-25 01:25:06 +00:00
|
|
|
for _, metric := range m.counters {
|
2016-01-22 22:02:21 +00:00
|
|
|
// collect
|
2018-06-11 18:10:53 +00:00
|
|
|
if m.UseWildcardsExpansion {
|
|
|
|
value, err := m.query.GetFormattedCounterValueDouble(metric.counterHandle)
|
|
|
|
if err == nil {
|
|
|
|
measurement := sanitizedChars.Replace(metric.measurement)
|
|
|
|
if measurement == "" {
|
|
|
|
measurement = "win_perf_counters"
|
|
|
|
}
|
2016-01-22 22:02:21 +00:00
|
|
|
|
2018-06-11 18:10:53 +00:00
|
|
|
var instance = InstanceGrouping{measurement, metric.instance, metric.objectName}
|
|
|
|
if collectFields[instance] == nil {
|
|
|
|
collectFields[instance] = make(map[string]interface{})
|
|
|
|
}
|
|
|
|
collectFields[instance][sanitizedChars.Replace(metric.counter)] = float32(value)
|
|
|
|
} else {
|
|
|
|
//ignore invalid data from as some counters from process instances returns this sometimes
|
|
|
|
if phderr, ok := err.(*PdhError); ok && phderr.ErrorCode != PDH_INVALID_DATA && phderr.ErrorCode != PDH_CALC_NEGATIVE_VALUE {
|
|
|
|
return fmt.Errorf("error while getting value for counter %s: %v", metric.counterPath, err)
|
|
|
|
}
|
2018-05-25 01:25:06 +00:00
|
|
|
}
|
|
|
|
} else {
|
2018-06-11 18:10:53 +00:00
|
|
|
counterValues, err := m.query.GetFormattedCounterArrayDouble(metric.counterHandle)
|
|
|
|
if err == nil {
|
|
|
|
for _, cValue := range counterValues {
|
|
|
|
var add bool
|
|
|
|
if metric.includeTotal {
|
|
|
|
// If IncludeTotal is set, include all.
|
|
|
|
add = true
|
|
|
|
} else if metric.instance == "*" && !strings.Contains(cValue.InstanceName, "_Total") {
|
|
|
|
// Catch if set to * and that it is not a '*_Total*' instance.
|
|
|
|
add = true
|
|
|
|
} else if metric.instance == cValue.InstanceName {
|
|
|
|
// Catch if we set it to total or some form of it
|
|
|
|
add = true
|
|
|
|
} else if strings.Contains(metric.instance, "#") && strings.HasPrefix(metric.instance, cValue.InstanceName) {
|
|
|
|
// 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
|
|
|
|
cValue.InstanceName = metric.instance
|
|
|
|
} else if metric.instance == "------" {
|
|
|
|
add = true
|
|
|
|
}
|
|
|
|
|
|
|
|
if add {
|
|
|
|
measurement := sanitizedChars.Replace(metric.measurement)
|
|
|
|
if measurement == "" {
|
|
|
|
measurement = "win_perf_counters"
|
|
|
|
}
|
|
|
|
var instance = InstanceGrouping{measurement, cValue.InstanceName, metric.objectName}
|
|
|
|
|
|
|
|
if collectFields[instance] == nil {
|
|
|
|
collectFields[instance] = make(map[string]interface{})
|
|
|
|
}
|
|
|
|
collectFields[instance][sanitizedChars.Replace(metric.counter)] = float32(cValue.Value)
|
|
|
|
}
|
|
|
|
}
|
2016-01-22 22:02:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-01 00:48:45 +00:00
|
|
|
for instance, fields := range collectFields {
|
|
|
|
var tags = map[string]string{
|
|
|
|
"objectname": instance.objectname,
|
|
|
|
}
|
2018-05-25 01:25:06 +00:00
|
|
|
if len(instance.instance) > 0 {
|
|
|
|
tags["instance"] = instance.instance
|
|
|
|
}
|
2018-05-01 00:48:45 +00:00
|
|
|
acc.AddFields(instance.name, fields, tags)
|
|
|
|
}
|
|
|
|
|
2016-01-22 22:02:21 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
2018-05-25 01:25:06 +00:00
|
|
|
inputs.Add("win_perf_counters", func() telegraf.Input {
|
|
|
|
return &Win_PerfCounters{query: &PerformanceQueryImpl{}, CountersRefreshInterval: internal.Duration{Duration: time.Second * 60}}
|
|
|
|
})
|
2016-01-22 22:02:21 +00:00
|
|
|
}
|