From 8831651799974038c272c10e20fbafe541ac3345 Mon Sep 17 00:00:00 2001 From: Daniel Nelson Date: Thu, 2 Jan 2020 16:15:48 -0800 Subject: [PATCH] Show platform not supported warning only on plugin creation (#6801) --- plugins/inputs/ethtool/ethtool.go | 4 + ...thtool_nonlinux.go => ethtool_notlinux.go} | 8 +- plugins/inputs/synproxy/synproxy.go | 85 +----------------- plugins/inputs/synproxy/synproxy_linux.go | 90 +++++++++++++++++++ plugins/inputs/synproxy/synproxy_notlinux.go | 16 +--- plugins/inputs/wireless/wireless.go | 3 +- ...eless_nonlinux.go => wireless_notlinux.go} | 8 +- 7 files changed, 112 insertions(+), 102 deletions(-) rename plugins/inputs/ethtool/{ethtool_nonlinux.go => ethtool_notlinux.go} (75%) create mode 100644 plugins/inputs/synproxy/synproxy_linux.go rename plugins/inputs/wireless/{wireless_nonlinux.go => wireless_notlinux.go} (75%) diff --git a/plugins/inputs/ethtool/ethtool.go b/plugins/inputs/ethtool/ethtool.go index e8f6bfed4..3f8f8e156 100644 --- a/plugins/inputs/ethtool/ethtool.go +++ b/plugins/inputs/ethtool/ethtool.go @@ -2,6 +2,8 @@ package ethtool import ( "net" + + "github.com/influxdata/telegraf" ) type Command interface { @@ -18,6 +20,8 @@ type Ethtool struct { // This is the list of interface names to ignore InterfaceExclude []string `toml:"interface_exclude"` + Log telegraf.Logger `toml:"-"` + // the ethtool command command Command } diff --git a/plugins/inputs/ethtool/ethtool_nonlinux.go b/plugins/inputs/ethtool/ethtool_notlinux.go similarity index 75% rename from plugins/inputs/ethtool/ethtool_nonlinux.go rename to plugins/inputs/ethtool/ethtool_notlinux.go index 62a0de3c1..b022e0a46 100644 --- a/plugins/inputs/ethtool/ethtool_nonlinux.go +++ b/plugins/inputs/ethtool/ethtool_notlinux.go @@ -3,19 +3,21 @@ package ethtool import ( - "log" - "github.com/influxdata/telegraf" "github.com/influxdata/telegraf/plugins/inputs" ) +func (e *Ethtool) Init() error { + e.Log.Warn("Current platform is not supported") + return nil +} + func (e *Ethtool) Gather(acc telegraf.Accumulator) error { return nil } func init() { inputs.Add(pluginName, func() telegraf.Input { - log.Print("W! [inputs.ethtool] Current platform is not supported") return &Ethtool{} }) } diff --git a/plugins/inputs/synproxy/synproxy.go b/plugins/inputs/synproxy/synproxy.go index 510f5584d..6a5b2b323 100644 --- a/plugins/inputs/synproxy/synproxy.go +++ b/plugins/inputs/synproxy/synproxy.go @@ -1,20 +1,16 @@ -// +build linux - package synproxy import ( - "bufio" - "fmt" "os" "path" - "strconv" - "strings" "github.com/influxdata/telegraf" "github.com/influxdata/telegraf/plugins/inputs" ) type Synproxy struct { + Log telegraf.Logger `toml:"-"` + // Synproxy stats filename (proc filesystem) statFile string } @@ -27,83 +23,6 @@ func (k *Synproxy) SampleConfig() string { return "" } -func (k *Synproxy) Gather(acc telegraf.Accumulator) error { - data, err := k.getSynproxyStat() - if err != nil { - return err - } - - acc.AddCounter("synproxy", data, map[string]string{}) - return nil -} - -func inSlice(haystack []string, needle string) bool { - for _, val := range haystack { - if needle == val { - return true - } - } - return false -} - -func (k *Synproxy) getSynproxyStat() (map[string]interface{}, error) { - var hname []string - counters := []string{"entries", "syn_received", "cookie_invalid", "cookie_valid", "cookie_retrans", "conn_reopened"} - fields := make(map[string]interface{}) - - // Open synproxy file in proc filesystem - file, err := os.Open(k.statFile) - if err != nil { - return nil, err - } - defer file.Close() - - // Initialise expected fields - for _, val := range counters { - fields[val] = uint32(0) - } - - scanner := bufio.NewScanner(file) - // Read header row - if scanner.Scan() { - line := scanner.Text() - // Parse fields separated by whitespace - dataFields := strings.Fields(line) - for _, val := range dataFields { - if !inSlice(counters, val) { - val = "" - } - hname = append(hname, val) - } - } - if len(hname) == 0 { - return nil, fmt.Errorf("invalid data") - } - // Read data rows - for scanner.Scan() { - line := scanner.Text() - // Parse fields separated by whitespace - dataFields := strings.Fields(line) - // If number of data fields do not match number of header fields - if len(dataFields) != len(hname) { - return nil, fmt.Errorf("invalid number of columns in data, expected %d found %d", len(hname), - len(dataFields)) - } - for i, val := range dataFields { - // Convert from hexstring to int32 - x, err := strconv.ParseUint(val, 16, 32) - // If field is not a valid hexstring - if err != nil { - return nil, fmt.Errorf("invalid value '%s' found", val) - } - if hname[i] != "" { - fields[hname[i]] = fields[hname[i]].(uint32) + uint32(x) - } - } - } - return fields, nil -} - func getHostProc() string { procPath := "/proc" if os.Getenv("HOST_PROC") != "" { diff --git a/plugins/inputs/synproxy/synproxy_linux.go b/plugins/inputs/synproxy/synproxy_linux.go new file mode 100644 index 000000000..bcc972938 --- /dev/null +++ b/plugins/inputs/synproxy/synproxy_linux.go @@ -0,0 +1,90 @@ +// +build linux + +package synproxy + +import ( + "bufio" + "fmt" + "os" + "strconv" + "strings" + + "github.com/influxdata/telegraf" +) + +func (k *Synproxy) Gather(acc telegraf.Accumulator) error { + data, err := k.getSynproxyStat() + if err != nil { + return err + } + + acc.AddCounter("synproxy", data, map[string]string{}) + return nil +} + +func inSlice(haystack []string, needle string) bool { + for _, val := range haystack { + if needle == val { + return true + } + } + return false +} + +func (k *Synproxy) getSynproxyStat() (map[string]interface{}, error) { + var hname []string + counters := []string{"entries", "syn_received", "cookie_invalid", "cookie_valid", "cookie_retrans", "conn_reopened"} + fields := make(map[string]interface{}) + + // Open synproxy file in proc filesystem + file, err := os.Open(k.statFile) + if err != nil { + return nil, err + } + defer file.Close() + + // Initialise expected fields + for _, val := range counters { + fields[val] = uint32(0) + } + + scanner := bufio.NewScanner(file) + // Read header row + if scanner.Scan() { + line := scanner.Text() + // Parse fields separated by whitespace + dataFields := strings.Fields(line) + for _, val := range dataFields { + if !inSlice(counters, val) { + val = "" + } + hname = append(hname, val) + } + } + if len(hname) == 0 { + return nil, fmt.Errorf("invalid data") + } + // Read data rows + for scanner.Scan() { + line := scanner.Text() + // Parse fields separated by whitespace + dataFields := strings.Fields(line) + // If number of data fields do not match number of header fields + if len(dataFields) != len(hname) { + return nil, fmt.Errorf("invalid number of columns in data, expected %d found %d", len(hname), + len(dataFields)) + } + for i, val := range dataFields { + // Convert from hexstring to int32 + x, err := strconv.ParseUint(val, 16, 32) + // If field is not a valid hexstring + if err != nil { + return nil, fmt.Errorf("invalid value '%s' found", val) + } + if hname[i] != "" { + fields[hname[i]] = fields[hname[i]].(uint32) + uint32(x) + } + } + } + return fields, nil +} diff --git a/plugins/inputs/synproxy/synproxy_notlinux.go b/plugins/inputs/synproxy/synproxy_notlinux.go index e77f06903..71a223644 100644 --- a/plugins/inputs/synproxy/synproxy_notlinux.go +++ b/plugins/inputs/synproxy/synproxy_notlinux.go @@ -3,29 +3,21 @@ package synproxy import ( - "log" - "github.com/influxdata/telegraf" "github.com/influxdata/telegraf/plugins/inputs" ) -type Synproxy struct{} +func (k *Synproxy) Init() error { + k.Log.Warn("Current platform is not supported") + return nil +} func (k *Synproxy) Gather(acc telegraf.Accumulator) error { return nil } -func (k *Synproxy) Description() string { - return "" -} - -func (k *Synproxy) SampleConfig() string { - return "" -} - func init() { inputs.Add("synproxy", func() telegraf.Input { - log.Print("W! [inputs.synproxy] Current platform is not supported") return &Synproxy{} }) } diff --git a/plugins/inputs/wireless/wireless.go b/plugins/inputs/wireless/wireless.go index eb488ef59..911d7fb09 100644 --- a/plugins/inputs/wireless/wireless.go +++ b/plugins/inputs/wireless/wireless.go @@ -7,7 +7,8 @@ import ( // Wireless is used to store configuration values. type Wireless struct { - HostProc string `toml:"host_proc"` + HostProc string `toml:"host_proc"` + Log telegraf.Logger `toml:"-"` } var sampleConfig = ` diff --git a/plugins/inputs/wireless/wireless_nonlinux.go b/plugins/inputs/wireless/wireless_notlinux.go similarity index 75% rename from plugins/inputs/wireless/wireless_nonlinux.go rename to plugins/inputs/wireless/wireless_notlinux.go index 0fbe5eb06..4769acc97 100644 --- a/plugins/inputs/wireless/wireless_nonlinux.go +++ b/plugins/inputs/wireless/wireless_notlinux.go @@ -3,19 +3,21 @@ package wireless import ( - "log" - "github.com/influxdata/telegraf" "github.com/influxdata/telegraf/plugins/inputs" ) +func (w *Wireless) Init() error { + w.Log.Warn("Current platform is not supported") + return nil +} + func (w *Wireless) Gather(acc telegraf.Accumulator) error { return nil } func init() { inputs.Add("wireless", func() telegraf.Input { - log.Print("W! [inputs.wireless] Current platform is not supported") return &Wireless{} }) }