mirror of
https://github.com/prometheus-community/windows_exporter.git
synced 2026-02-08 05:56:37 +00:00
config: fix lists (#2124)
This commit is contained in:
@@ -3,6 +3,10 @@
|
|||||||
collectors:
|
collectors:
|
||||||
enabled: cpu,cpu_info,exchange,iis,logical_disk,memory,net,os,performancecounter,process,remote_fx,service,system,tcp,time,terminal_services,textfile
|
enabled: cpu,cpu_info,exchange,iis,logical_disk,memory,net,os,performancecounter,process,remote_fx,service,system,tcp,time,terminal_services,textfile
|
||||||
collector:
|
collector:
|
||||||
|
textfile:
|
||||||
|
directories:
|
||||||
|
- 'C:\MyDir1'
|
||||||
|
- 'C:\MyDir2'
|
||||||
service:
|
service:
|
||||||
include: "windows_exporter"
|
include: "windows_exporter"
|
||||||
performancecounter:
|
performancecounter:
|
||||||
|
|||||||
@@ -73,19 +73,10 @@ func NewWithFlags(app *kingpin.Application) *Collector {
|
|||||||
}
|
}
|
||||||
c.config.FilePatterns = make([]string, 0)
|
c.config.FilePatterns = make([]string, 0)
|
||||||
|
|
||||||
var filePatterns string
|
|
||||||
|
|
||||||
app.Flag(
|
app.Flag(
|
||||||
"collector.filetime.file-patterns",
|
"collector.filetime.file-patterns",
|
||||||
"Comma-separated list of file patterns. Each pattern is a glob pattern that can contain `*`, `?`, and `**` (recursive). See https://github.com/bmatcuk/doublestar#patterns",
|
"Comma-separated list of file patterns. Each pattern is a glob pattern that can contain `*`, `?`, and `**` (recursive). See https://github.com/bmatcuk/doublestar#patterns",
|
||||||
).Default(strings.Join(ConfigDefaults.FilePatterns, ",")).StringVar(&filePatterns)
|
).Default(strings.Join(ConfigDefaults.FilePatterns, ",")).StringsVar(&c.config.FilePatterns)
|
||||||
|
|
||||||
app.Action(func(*kingpin.ParseContext) error {
|
|
||||||
// doublestar.Glob() requires forward slashes
|
|
||||||
c.config.FilePatterns = strings.Split(filepath.ToSlash(filePatterns), ",")
|
|
||||||
|
|
||||||
return nil
|
|
||||||
})
|
|
||||||
|
|
||||||
return c
|
return c
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -19,72 +19,52 @@ package config
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"strconv"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
// flatten flattens the nested struct.
|
// convertMap converts a map with any comparable key type to a map with string keys.
|
||||||
//
|
func convertMap[K comparable, V any](originalMap map[K]V) map[string]V {
|
||||||
// All keys will be joined by dot
|
convertedMap := make(map[string]V, len(originalMap))
|
||||||
// e.g. {"a": {"b":"c"}} => {"a.b":"c"}
|
|
||||||
// or {"a": {"b":[1,2]}} => {"a.b.0":1, "a.b.1": 2}.
|
|
||||||
func flatten(data map[string]interface{}) map[string]string {
|
|
||||||
ret := make(map[string]string)
|
|
||||||
|
|
||||||
for k, v := range data {
|
|
||||||
switch typed := v.(type) {
|
|
||||||
case map[interface{}]interface{}:
|
|
||||||
for fk, fv := range flatten(convertMap(typed)) {
|
|
||||||
ret[fmt.Sprintf("%s.%s", k, fk)] = fv
|
|
||||||
}
|
|
||||||
case map[string]interface{}:
|
|
||||||
for fk, fv := range flatten(typed) {
|
|
||||||
ret[fmt.Sprintf("%s.%s", k, fk)] = fv
|
|
||||||
}
|
|
||||||
case []interface{}:
|
|
||||||
for fk, fv := range flattenSlice(typed) {
|
|
||||||
ret[fmt.Sprintf("%s.%s", k, fk)] = fv
|
|
||||||
}
|
|
||||||
default:
|
|
||||||
ret[k] = fmt.Sprint(typed)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return ret
|
|
||||||
}
|
|
||||||
|
|
||||||
func flattenSlice(data []interface{}) map[string]string {
|
|
||||||
ret := make(map[string]string)
|
|
||||||
|
|
||||||
for idx, v := range data {
|
|
||||||
switch typed := v.(type) {
|
|
||||||
case map[interface{}]interface{}:
|
|
||||||
for fk, fv := range flatten(convertMap(typed)) {
|
|
||||||
ret[fmt.Sprintf("%d,%s", idx, fk)] = fv
|
|
||||||
}
|
|
||||||
case map[string]interface{}:
|
|
||||||
for fk, fv := range flatten(typed) {
|
|
||||||
ret[fmt.Sprintf("%d,%s", idx, fk)] = fv
|
|
||||||
}
|
|
||||||
case []interface{}:
|
|
||||||
for fk, fv := range flattenSlice(typed) {
|
|
||||||
ret[fmt.Sprintf("%d,%s", idx, fk)] = fv
|
|
||||||
}
|
|
||||||
default:
|
|
||||||
ret[strconv.Itoa(idx)] = fmt.Sprint(typed)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return ret
|
|
||||||
}
|
|
||||||
|
|
||||||
func convertMap(originalMap map[interface{}]interface{}) map[string]interface{} {
|
|
||||||
convertedMap := map[string]interface{}{}
|
|
||||||
|
|
||||||
for key, value := range originalMap {
|
for key, value := range originalMap {
|
||||||
if keyString, ok := key.(string); ok {
|
if keyString, ok := any(key).(string); ok {
|
||||||
convertedMap[keyString] = value
|
convertedMap[keyString] = value
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return convertedMap
|
return convertedMap
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// flatten flattens a nested map, joining keys with dots.
|
||||||
|
// e.g. {"a": {"b":"c"}} => {"a.b":"c"}
|
||||||
|
func flatten(data map[string]any) map[string]string {
|
||||||
|
result := make(map[string]string)
|
||||||
|
|
||||||
|
flattenHelper("", data, result)
|
||||||
|
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
|
||||||
|
func flattenHelper(prefix string, data map[string]any, result map[string]string) {
|
||||||
|
for k, v := range data {
|
||||||
|
fullKey := k
|
||||||
|
if prefix != "" {
|
||||||
|
fullKey = prefix + "." + k
|
||||||
|
}
|
||||||
|
|
||||||
|
switch val := v.(type) {
|
||||||
|
case map[any]any:
|
||||||
|
flattenHelper(fullKey, convertMap(val), result)
|
||||||
|
case map[string]any:
|
||||||
|
flattenHelper(fullKey, val, result)
|
||||||
|
case []any:
|
||||||
|
strSlice := make([]string, len(val))
|
||||||
|
for i, elem := range val {
|
||||||
|
strSlice[i] = fmt.Sprint(elem)
|
||||||
|
}
|
||||||
|
|
||||||
|
result[fullKey] = strings.Join(strSlice, ",")
|
||||||
|
default:
|
||||||
|
result[fullKey] = fmt.Sprint(val)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user