Added feature to allow metrics collection every X iterations in specified objects.

This commit is contained in:
Rune Darrud 2016-01-31 15:06:57 +01:00
parent 042cf517b2
commit 64ada49377
2 changed files with 92 additions and 66 deletions

View File

@ -59,6 +59,13 @@ Example: `Counters = ["% Idle Time", "% Disk Read Time", "% Disk Write Time"]`
This must be specified for every counter you want the results of, This must be specified for every counter you want the results of,
it is not possible to ask for all counters in the ObjectName. it is not possible to ask for all counters in the ObjectName.
### GetMetricEvery
*Optional*
This parameter can be used where you only want to read an object on every X iteration, say every 10 iterations of whatever the interval is set for in Telegraf. If Telegraf fetches its data every 10s, it translates to only getting the data for this metric every 100s. Useful if the metric you are obtaining is expensive to get back.
Example `GetMetricEvery = 10`
### Measurement ### Measurement
*Optional* *Optional*

View File

@ -87,6 +87,7 @@ type perfobject struct {
ObjectName string ObjectName string
Counters []string Counters []string
Instances []string Instances []string
GetMetricEvery int
Measurement string Measurement string
WarnOnMissing bool WarnOnMissing bool
FailOnMissing bool FailOnMissing bool
@ -104,15 +105,20 @@ type item struct {
objectName string objectName string
counter string counter string
instance string instance string
getmetricevery int
metriccounter int
measurement string measurement string
include_total bool include_total bool
handle win.PDH_HQUERY handle win.PDH_HQUERY
counterHandle win.PDH_HCOUNTER counterHandle win.PDH_HCOUNTER
} }
func (m *Win_PerfCounters) AddItem(metrics *itemList, query string, objectName string, counter string, instance string, func (m *Win_PerfCounters) AddItem(metrics *itemList, query string,
objectName string, counter string, instance string, getmetricevery int,
measurement string, include_total bool) { measurement string, include_total bool) {
var metriccounter int = 1
var handle win.PDH_HQUERY var handle win.PDH_HQUERY
var counterHandle win.PDH_HCOUNTER var counterHandle win.PDH_HCOUNTER
ret := win.PdhOpenQuery(0, 0, &handle) ret := win.PdhOpenQuery(0, 0, &handle)
@ -120,8 +126,9 @@ func (m *Win_PerfCounters) AddItem(metrics *itemList, query string, objectName s
_ = ret _ = ret
temp := &item{query, objectName, counter, instance, measurement, temp := &item{query, objectName, counter, instance, getmetricevery,
include_total, handle, counterHandle} metriccounter, measurement, include_total, handle, counterHandle}
index := len(gItemList) index := len(gItemList)
gItemList[index] = temp gItemList[index] = temp
@ -190,6 +197,13 @@ func (m *Win_PerfCounters) ParseConfig(metrics *itemList) error {
query = "\\" + objectname + "(" + instance + ")\\" + counter query = "\\" + objectname + "(" + instance + ")\\" + counter
} }
var getmetricevery int
if PerfObject.GetMetricEvery == 0 {
getmetricevery = 1
} else {
getmetricevery = PerfObject.GetMetricEvery
}
var exists uint32 = win.PdhValidatePath(query) var exists uint32 = win.PdhValidatePath(query)
if exists == win.ERROR_SUCCESS { if exists == win.ERROR_SUCCESS {
@ -197,7 +211,8 @@ func (m *Win_PerfCounters) ParseConfig(metrics *itemList) error {
fmt.Printf("Valid: %s\n", query) fmt.Printf("Valid: %s\n", query)
} }
m.AddItem(metrics, query, objectname, counter, instance, m.AddItem(metrics, query, objectname, counter, instance,
PerfObject.Measurement, PerfObject.IncludeTotal) getmetricevery, PerfObject.Measurement,
PerfObject.IncludeTotal)
} else { } else {
err := m.InvalidObject(exists, query, PerfObject, instance, counter) err := m.InvalidObject(exists, query, PerfObject, instance, counter)
return err return err
@ -271,6 +286,8 @@ func (m *Win_PerfCounters) Gather(acc telegraf.Accumulator) error {
// For iterate over the known metrics and get the samples. // For iterate over the known metrics and get the samples.
for _, metric := range gItemList { for _, metric := range gItemList {
// collect // collect
if metric.getmetricevery == metric.metriccounter {
metric.metriccounter = 1
ret := win.PdhCollectQueryData(metric.handle) ret := win.PdhCollectQueryData(metric.handle)
if ret == win.ERROR_SUCCESS { if ret == win.ERROR_SUCCESS {
ret = win.PdhGetFormattedCounterArrayDouble(metric.counterHandle, &bufSize, ret = win.PdhGetFormattedCounterArrayDouble(metric.counterHandle, &bufSize,
@ -323,7 +340,9 @@ func (m *Win_PerfCounters) Gather(acc telegraf.Accumulator) error {
bufCount = 0 bufCount = 0
bufSize = 0 bufSize = 0
} }
}
} else {
metric.metriccounter++
} }
} }