diff --git a/plugins/inputs/win_perf_counters/win_perf_counters.go b/plugins/inputs/win_perf_counters/win_perf_counters.go index d2ace523161e2..9e705d620664a 100644 --- a/plugins/inputs/win_perf_counters/win_perf_counters.go +++ b/plugins/inputs/win_perf_counters/win_perf_counters.go @@ -6,7 +6,6 @@ import ( "errors" "fmt" "log" - "regexp" "strings" "time" @@ -119,28 +118,46 @@ type instanceGrouping struct { var sanitizedChars = strings.NewReplacer("/sec", "_persec", "/Sec", "_persec", " ", "_", "%", "Percent", `\`, "") -//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 - -var counterPathRE = regexp.MustCompile(`.*\\(.*)\\(.*)`) -var objectInstanceRE = regexp.MustCompile(`(.*)\((.*)\)`) - -//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) +// extractCounterInfoFromCounterPath gets object name, instance name (if available) and counter name from counter path +// 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 +func extractCounterInfoFromCounterPath(counterPath string) (object string, instance string, counter string, err error) { + //find instance + var leftParenthesisIndex, rightParenthesisIndex, rightObjectBorderIndex int + + leftParenthesisIndex = strings.Index(counterPath, "(") + if leftParenthesisIndex != -1 { + rightParenthesisIndex = strings.LastIndex(counterPath, ")") + if rightParenthesisIndex == -1 { + err = errors.New("cannot parse instance from: " + counterPath) + return + } else { + instance = counterPath[leftParenthesisIndex+1 : rightParenthesisIndex] + } + } else { + rightParenthesisIndex = -1 + } + //find object + rightObjectBorderIndex = leftParenthesisIndex + if rightObjectBorderIndex == -1 { + rightObjectBorderIndex = strings.LastIndex(counterPath, "\\") + if rightObjectBorderIndex == -1 { + err = errors.New("cannot parse object from: " + counterPath) + return + } + } + leftobjectBorderIndex := strings.LastIndex(counterPath[:rightObjectBorderIndex], "\\") + if leftobjectBorderIndex == -1 { + err = errors.New("cannot parse object from: " + counterPath) return } - 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] + object = counterPath[leftobjectBorderIndex+1 : rightObjectBorderIndex] + //get countername + leftCounterBorderIndex := rightParenthesisIndex + 1 + if leftCounterBorderIndex == 0 { + leftCounterBorderIndex = rightObjectBorderIndex } + counter = counterPath[leftCounterBorderIndex+1:] return } @@ -184,7 +201,7 @@ func (m *Win_PerfCounters) AddItem(counterPath string, objectName string, instan var err error counterHandle, err := m.query.AddCounterToQuery(counterPath) - objectName, instance, counterName, err = extractObjectInstanceCounterFromQuery(counterPath) + objectName, instance, counterName, err = extractCounterInfoFromCounterPath(counterPath) if err != nil { return err }