From 2f8d0f4d4784f1ee217bce2c40c94b1dd78bd908 Mon Sep 17 00:00:00 2001 From: Windkit Li Date: Fri, 13 Oct 2017 09:26:14 +0900 Subject: [PATCH] Fix snmpwalk address format in leofs input (#3328) --- etc/telegraf.conf | 4 +- plugins/inputs/leofs/leofs.go | 40 ++++----- plugins/inputs/leofs/leofs_test.go | 136 ++++++++++++++--------------- 3 files changed, 86 insertions(+), 94 deletions(-) diff --git a/etc/telegraf.conf b/etc/telegraf.conf index 2c23864cf..f3128b8fd 100644 --- a/etc/telegraf.conf +++ b/etc/telegraf.conf @@ -1586,8 +1586,8 @@ # # Read metrics from a LeoFS Server via SNMP # [[inputs.leofs]] # ## An array of URLs of the form: -# ## "udp://" host [ ":" port] -# servers = ["udp://127.0.0.1:4020"] +# ## host [ ":" port] +# servers = ["127.0.0.1:4020"] # # Provides Linux sysctl fs metrics diff --git a/plugins/inputs/leofs/leofs.go b/plugins/inputs/leofs/leofs.go index 55a727ef8..9381eace8 100644 --- a/plugins/inputs/leofs/leofs.go +++ b/plugins/inputs/leofs/leofs.go @@ -3,8 +3,6 @@ package leofs import ( "bufio" "fmt" - "log" - "net/url" "os/exec" "strconv" "strings" @@ -19,7 +17,7 @@ import ( const oid = ".1.3.6.1.4.1.35450" // For Manager Master -const defaultEndpoint = "udp://127.0.0.1:4020" +const defaultEndpoint = "127.0.0.1:4020" type ServerType int @@ -137,8 +135,8 @@ var serverTypeMapping = map[string]ServerType{ var sampleConfig = ` ## An array of URLs of the form: - ## "udp://" host [ ":" port] - servers = ["udp://127.0.0.1:4020"] + ## host [ ":" port] + servers = ["127.0.0.1:4020"] ` func (l *LeoFS) SampleConfig() string { @@ -155,28 +153,22 @@ func (l *LeoFS) Gather(acc telegraf.Accumulator) error { return nil } var wg sync.WaitGroup - for i, endpoint := range l.Servers { - if !strings.HasPrefix(endpoint, "udp://") { - // Preserve backwards compatibility for hostnames without a - // scheme, broken in go 1.8. Remove in Telegraf 2.0 - endpoint = "udp://" + endpoint - log.Printf("W! [inputs.mongodb] Using %q as connection URL; please update your configuration to use an URL", endpoint) - l.Servers[i] = endpoint - } - u, err := url.Parse(endpoint) - if err != nil { - acc.AddError(fmt.Errorf("Unable to parse address %q: %s", endpoint, err)) - continue - } - if u.Host == "" { + for _, endpoint := range l.Servers { + results := strings.Split(endpoint, ":") + + port := "4020" + if len(results) > 2 { acc.AddError(fmt.Errorf("Unable to parse address %q", endpoint)) continue + } else if len(results) == 2 { + if _, err := strconv.Atoi(results[1]); err == nil { + port = results[1] + } else { + acc.AddError(fmt.Errorf("Unable to parse port from %q", endpoint)) + continue + } } - port := u.Port() - if port == "" { - port = "4020" - } st, ok := serverTypeMapping[port] if !ok { st = ServerTypeStorage @@ -196,7 +188,7 @@ func (l *LeoFS) gatherServer( serverType ServerType, acc telegraf.Accumulator, ) error { - cmd := exec.Command("snmpwalk", "-v2c", "-cpublic", endpoint, oid) + cmd := exec.Command("snmpwalk", "-v2c", "-cpublic", "-On", endpoint, oid) stdout, err := cmd.StdoutPipe() if err != nil { return err diff --git a/plugins/inputs/leofs/leofs_test.go b/plugins/inputs/leofs/leofs_test.go index a5ca30432..f3699b8d9 100644 --- a/plugins/inputs/leofs/leofs_test.go +++ b/plugins/inputs/leofs/leofs_test.go @@ -16,21 +16,21 @@ package main import "fmt" -const output = ` + "`" + `iso.3.6.1.4.1.35450.15.1.0 = STRING: "manager_888@127.0.0.1" -iso.3.6.1.4.1.35450.15.2.0 = Gauge32: 186 -iso.3.6.1.4.1.35450.15.3.0 = Gauge32: 46235519 -iso.3.6.1.4.1.35450.15.4.0 = Gauge32: 32168525 -iso.3.6.1.4.1.35450.15.5.0 = Gauge32: 14066068 -iso.3.6.1.4.1.35450.15.6.0 = Gauge32: 5512968 -iso.3.6.1.4.1.35450.15.7.0 = Gauge32: 186 -iso.3.6.1.4.1.35450.15.8.0 = Gauge32: 46269006 -iso.3.6.1.4.1.35450.15.9.0 = Gauge32: 32202867 -iso.3.6.1.4.1.35450.15.10.0 = Gauge32: 14064995 -iso.3.6.1.4.1.35450.15.11.0 = Gauge32: 5492634 -iso.3.6.1.4.1.35450.15.12.0 = Gauge32: 60 -iso.3.6.1.4.1.35450.15.13.0 = Gauge32: 43515904 -iso.3.6.1.4.1.35450.15.14.0 = Gauge32: 60 -iso.3.6.1.4.1.35450.15.15.0 = Gauge32: 43533983` + "`" + +const output = ` + "`" + `.1.3.6.1.4.1.35450.15.1.0 = STRING: "manager_888@127.0.0.1" +.1.3.6.1.4.1.35450.15.2.0 = Gauge32: 186 +.1.3.6.1.4.1.35450.15.3.0 = Gauge32: 46235519 +.1.3.6.1.4.1.35450.15.4.0 = Gauge32: 32168525 +.1.3.6.1.4.1.35450.15.5.0 = Gauge32: 14066068 +.1.3.6.1.4.1.35450.15.6.0 = Gauge32: 5512968 +.1.3.6.1.4.1.35450.15.7.0 = Gauge32: 186 +.1.3.6.1.4.1.35450.15.8.0 = Gauge32: 46269006 +.1.3.6.1.4.1.35450.15.9.0 = Gauge32: 32202867 +.1.3.6.1.4.1.35450.15.10.0 = Gauge32: 14064995 +.1.3.6.1.4.1.35450.15.11.0 = Gauge32: 5492634 +.1.3.6.1.4.1.35450.15.12.0 = Gauge32: 60 +.1.3.6.1.4.1.35450.15.13.0 = Gauge32: 43515904 +.1.3.6.1.4.1.35450.15.14.0 = Gauge32: 60 +.1.3.6.1.4.1.35450.15.15.0 = Gauge32: 43533983` + "`" + ` func main() { fmt.Println(output) @@ -42,34 +42,34 @@ package main import "fmt" -const output = ` + "`" + `iso.3.6.1.4.1.35450.34.1.0 = STRING: "storage_0@127.0.0.1" -iso.3.6.1.4.1.35450.34.2.0 = Gauge32: 512 -iso.3.6.1.4.1.35450.34.3.0 = Gauge32: 38126307 -iso.3.6.1.4.1.35450.34.4.0 = Gauge32: 22308716 -iso.3.6.1.4.1.35450.34.5.0 = Gauge32: 15816448 -iso.3.6.1.4.1.35450.34.6.0 = Gauge32: 5232008 -iso.3.6.1.4.1.35450.34.7.0 = Gauge32: 512 -iso.3.6.1.4.1.35450.34.8.0 = Gauge32: 38113176 -iso.3.6.1.4.1.35450.34.9.0 = Gauge32: 22313398 -iso.3.6.1.4.1.35450.34.10.0 = Gauge32: 15798779 -iso.3.6.1.4.1.35450.34.11.0 = Gauge32: 5237315 -iso.3.6.1.4.1.35450.34.12.0 = Gauge32: 191 -iso.3.6.1.4.1.35450.34.13.0 = Gauge32: 824 -iso.3.6.1.4.1.35450.34.14.0 = Gauge32: 0 -iso.3.6.1.4.1.35450.34.15.0 = Gauge32: 50105 -iso.3.6.1.4.1.35450.34.16.0 = Gauge32: 196654 -iso.3.6.1.4.1.35450.34.17.0 = Gauge32: 0 -iso.3.6.1.4.1.35450.34.18.0 = Gauge32: 2052 -iso.3.6.1.4.1.35450.34.19.0 = Gauge32: 50296 -iso.3.6.1.4.1.35450.34.20.0 = Gauge32: 35 -iso.3.6.1.4.1.35450.34.21.0 = Gauge32: 898 -iso.3.6.1.4.1.35450.34.22.0 = Gauge32: 0 -iso.3.6.1.4.1.35450.34.23.0 = Gauge32: 0 -iso.3.6.1.4.1.35450.34.24.0 = Gauge32: 0 -iso.3.6.1.4.1.35450.34.31.0 = Gauge32: 51 -iso.3.6.1.4.1.35450.34.32.0 = Gauge32: 53219328 -iso.3.6.1.4.1.35450.34.33.0 = Gauge32: 51 -iso.3.6.1.4.1.35450.34.34.0 = Gauge32: 53351083` + "`" + +const output = ` + "`" + `.1.3.6.1.4.1.35450.34.1.0 = STRING: "storage_0@127.0.0.1" +.1.3.6.1.4.1.35450.34.2.0 = Gauge32: 512 +.1.3.6.1.4.1.35450.34.3.0 = Gauge32: 38126307 +.1.3.6.1.4.1.35450.34.4.0 = Gauge32: 22308716 +.1.3.6.1.4.1.35450.34.5.0 = Gauge32: 15816448 +.1.3.6.1.4.1.35450.34.6.0 = Gauge32: 5232008 +.1.3.6.1.4.1.35450.34.7.0 = Gauge32: 512 +.1.3.6.1.4.1.35450.34.8.0 = Gauge32: 38113176 +.1.3.6.1.4.1.35450.34.9.0 = Gauge32: 22313398 +.1.3.6.1.4.1.35450.34.10.0 = Gauge32: 15798779 +.1.3.6.1.4.1.35450.34.11.0 = Gauge32: 5237315 +.1.3.6.1.4.1.35450.34.12.0 = Gauge32: 191 +.1.3.6.1.4.1.35450.34.13.0 = Gauge32: 824 +.1.3.6.1.4.1.35450.34.14.0 = Gauge32: 0 +.1.3.6.1.4.1.35450.34.15.0 = Gauge32: 50105 +.1.3.6.1.4.1.35450.34.16.0 = Gauge32: 196654 +.1.3.6.1.4.1.35450.34.17.0 = Gauge32: 0 +.1.3.6.1.4.1.35450.34.18.0 = Gauge32: 2052 +.1.3.6.1.4.1.35450.34.19.0 = Gauge32: 50296 +.1.3.6.1.4.1.35450.34.20.0 = Gauge32: 35 +.1.3.6.1.4.1.35450.34.21.0 = Gauge32: 898 +.1.3.6.1.4.1.35450.34.22.0 = Gauge32: 0 +.1.3.6.1.4.1.35450.34.23.0 = Gauge32: 0 +.1.3.6.1.4.1.35450.34.24.0 = Gauge32: 0 +.1.3.6.1.4.1.35450.34.31.0 = Gauge32: 51 +.1.3.6.1.4.1.35450.34.32.0 = Gauge32: 53219328 +.1.3.6.1.4.1.35450.34.33.0 = Gauge32: 51 +.1.3.6.1.4.1.35450.34.34.0 = Gauge32: 53351083` + "`" + ` func main() { fmt.Println(output) @@ -81,31 +81,31 @@ package main import "fmt" -const output = ` + "`" + `iso.3.6.1.4.1.35450.34.1.0 = STRING: "gateway_0@127.0.0.1" -iso.3.6.1.4.1.35450.34.2.0 = Gauge32: 465 -iso.3.6.1.4.1.35450.34.3.0 = Gauge32: 61676335 -iso.3.6.1.4.1.35450.34.4.0 = Gauge32: 46890415 -iso.3.6.1.4.1.35450.34.5.0 = Gauge32: 14785011 -iso.3.6.1.4.1.35450.34.6.0 = Gauge32: 5578855 -iso.3.6.1.4.1.35450.34.7.0 = Gauge32: 465 -iso.3.6.1.4.1.35450.34.8.0 = Gauge32: 61644426 -iso.3.6.1.4.1.35450.34.9.0 = Gauge32: 46880358 -iso.3.6.1.4.1.35450.34.10.0 = Gauge32: 14763002 -iso.3.6.1.4.1.35450.34.11.0 = Gauge32: 5582125 -iso.3.6.1.4.1.35450.34.12.0 = Gauge32: 191 -iso.3.6.1.4.1.35450.34.13.0 = Gauge32: 827 -iso.3.6.1.4.1.35450.34.14.0 = Gauge32: 0 -iso.3.6.1.4.1.35450.34.15.0 = Gauge32: 50105 -iso.3.6.1.4.1.35450.34.16.0 = Gauge32: 196650 -iso.3.6.1.4.1.35450.34.17.0 = Gauge32: 0 -iso.3.6.1.4.1.35450.34.18.0 = Gauge32: 30256 -iso.3.6.1.4.1.35450.34.19.0 = Gauge32: 532158 -iso.3.6.1.4.1.35450.34.20.0 = Gauge32: 34 -iso.3.6.1.4.1.35450.34.21.0 = Gauge32: 1 -iso.3.6.1.4.1.35450.34.31.0 = Gauge32: 53 -iso.3.6.1.4.1.35450.34.32.0 = Gauge32: 55050240 -iso.3.6.1.4.1.35450.34.33.0 = Gauge32: 53 -iso.3.6.1.4.1.35450.34.34.0 = Gauge32: 55186538` + "`" + +const output = ` + "`" + `.1.3.6.1.4.1.35450.34.1.0 = STRING: "gateway_0@127.0.0.1" +.1.3.6.1.4.1.35450.34.2.0 = Gauge32: 465 +.1.3.6.1.4.1.35450.34.3.0 = Gauge32: 61676335 +.1.3.6.1.4.1.35450.34.4.0 = Gauge32: 46890415 +.1.3.6.1.4.1.35450.34.5.0 = Gauge32: 14785011 +.1.3.6.1.4.1.35450.34.6.0 = Gauge32: 5578855 +.1.3.6.1.4.1.35450.34.7.0 = Gauge32: 465 +.1.3.6.1.4.1.35450.34.8.0 = Gauge32: 61644426 +.1.3.6.1.4.1.35450.34.9.0 = Gauge32: 46880358 +.1.3.6.1.4.1.35450.34.10.0 = Gauge32: 14763002 +.1.3.6.1.4.1.35450.34.11.0 = Gauge32: 5582125 +.1.3.6.1.4.1.35450.34.12.0 = Gauge32: 191 +.1.3.6.1.4.1.35450.34.13.0 = Gauge32: 827 +.1.3.6.1.4.1.35450.34.14.0 = Gauge32: 0 +.1.3.6.1.4.1.35450.34.15.0 = Gauge32: 50105 +.1.3.6.1.4.1.35450.34.16.0 = Gauge32: 196650 +.1.3.6.1.4.1.35450.34.17.0 = Gauge32: 0 +.1.3.6.1.4.1.35450.34.18.0 = Gauge32: 30256 +.1.3.6.1.4.1.35450.34.19.0 = Gauge32: 532158 +.1.3.6.1.4.1.35450.34.20.0 = Gauge32: 34 +.1.3.6.1.4.1.35450.34.21.0 = Gauge32: 1 +.1.3.6.1.4.1.35450.34.31.0 = Gauge32: 53 +.1.3.6.1.4.1.35450.34.32.0 = Gauge32: 55050240 +.1.3.6.1.4.1.35450.34.33.0 = Gauge32: 53 +.1.3.6.1.4.1.35450.34.34.0 = Gauge32: 55186538` + "`" + ` func main() { fmt.Println(output)