Skip to content

Commit

Permalink
Fix multiple plugin loading in win_perf_counters (influxdata#2800)
Browse files Browse the repository at this point in the history
  • Loading branch information
skburgart authored and jeichorn committed Jul 24, 2017
1 parent cd0f613 commit 6cff0c2
Showing 1 changed file with 13 additions and 61 deletions.
74 changes: 13 additions & 61 deletions plugins/inputs/win_perf_counters/win_perf_counters.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,18 +65,13 @@ var sampleConfig = `
Measurement = "win_mem"
`

// Valid queries end up in this map.
var gItemList = make(map[int]*item)

var configParsed bool
var testConfigParsed bool
var testObject string

type Win_PerfCounters struct {
PrintValid bool
TestName string
PreVistaSupport bool
Object []perfobject

configParsed bool
itemCache []*item
}

type perfobject struct {
Expand All @@ -89,12 +84,6 @@ type perfobject struct {
IncludeTotal bool
}

// Parsed configuration ends up here after it has been validated for valid
// Performance Counter paths
type itemList struct {
items map[int]*item
}

type item struct {
query string
objectName string
Expand All @@ -109,7 +98,7 @@ type item struct {
var sanitizedChars = strings.NewReplacer("/sec", "_persec", "/Sec", "_persec",
" ", "_", "%", "Percent", `\`, "")

func (m *Win_PerfCounters) AddItem(metrics *itemList, query string, objectName string, counter string, instance string,
func (m *Win_PerfCounters) AddItem(query string, objectName string, counter string, instance string,
measurement string, include_total bool) error {

var handle PDH_HQUERY
Expand All @@ -128,15 +117,10 @@ func (m *Win_PerfCounters) AddItem(metrics *itemList, query string, objectName s
return errors.New(PdhFormatError(ret))
}

temp := &item{query, objectName, counter, instance, measurement,
newItem := &item{query, objectName, counter, instance, measurement,
include_total, handle, counterHandle}
index := len(gItemList)
gItemList[index] = temp
m.itemCache = append(m.itemCache, newItem)

if metrics.items == nil {
metrics.items = make(map[int]*item)
}
metrics.items[index] = temp
return nil
}

Expand All @@ -148,11 +132,9 @@ func (m *Win_PerfCounters) SampleConfig() string {
return sampleConfig
}

func (m *Win_PerfCounters) ParseConfig(metrics *itemList) error {
func (m *Win_PerfCounters) ParseConfig() error {
var query string

configParsed = true

if len(m.Object) > 0 {
for _, PerfObject := range m.Object {
for _, counter := range PerfObject.Counters {
Expand All @@ -165,7 +147,7 @@ func (m *Win_PerfCounters) ParseConfig(metrics *itemList) error {
query = "\\" + objectname + "(" + instance + ")\\" + counter
}

err := m.AddItem(metrics, query, objectname, counter, instance,
err := m.AddItem(query, objectname, counter, instance,
PerfObject.Measurement, PerfObject.IncludeTotal)

if err == nil {
Expand All @@ -191,41 +173,11 @@ func (m *Win_PerfCounters) ParseConfig(metrics *itemList) error {
}
}

func (m *Win_PerfCounters) Cleanup(metrics *itemList) {
// Cleanup

for _, metric := range metrics.items {
ret := PdhCloseQuery(metric.handle)
_ = ret
}
}

func (m *Win_PerfCounters) CleanupTestMode() {
// Cleanup for the testmode.

for _, metric := range gItemList {
ret := PdhCloseQuery(metric.handle)
_ = ret
}
}

func (m *Win_PerfCounters) Gather(acc telegraf.Accumulator) error {
metrics := itemList{}

// Both values are empty in normal use.
if m.TestName != testObject {
// Cleanup any handles before emptying the global variable containing valid queries.
m.CleanupTestMode()
gItemList = make(map[int]*item)
testObject = m.TestName
testConfigParsed = true
configParsed = false
}

// We only need to parse the config during the init, it uses the global variable after.
if configParsed == false {

err := m.ParseConfig(&metrics)
// Parse the config once
if !m.configParsed {
err := m.ParseConfig()
m.configParsed = true
if err != nil {
return err
}
Expand All @@ -237,7 +189,7 @@ func (m *Win_PerfCounters) Gather(acc telegraf.Accumulator) error {
var emptyBuf [1]PDH_FMT_COUNTERVALUE_ITEM_DOUBLE // need at least 1 addressable null ptr.

// For iterate over the known metrics and get the samples.
for _, metric := range gItemList {
for _, metric := range m.itemCache {
// collect
ret := PdhCollectQueryData(metric.handle)
if ret == ERROR_SUCCESS {
Expand Down

0 comments on commit 6cff0c2

Please sign in to comment.