From 18bc6857cd1fb9c1850fcdfa53a6938e60d85f8f Mon Sep 17 00:00:00 2001 From: Ildar Svetlov Date: Fri, 3 Nov 2017 15:03:38 +0400 Subject: [PATCH 1/4] Add Bond Interface input plugin --- plugins/inputs/all/all.go | 1 + plugins/inputs/bond/README.md | 81 +++++++++++++++ plugins/inputs/bond/bond.go | 172 +++++++++++++++++++++++++++++++ plugins/inputs/bond/bond_test.go | 77 ++++++++++++++ 4 files changed, 331 insertions(+) create mode 100644 plugins/inputs/bond/README.md create mode 100644 plugins/inputs/bond/bond.go create mode 100644 plugins/inputs/bond/bond_test.go diff --git a/plugins/inputs/all/all.go b/plugins/inputs/all/all.go index 235169c8685d1..e9eb72f313e67 100644 --- a/plugins/inputs/all/all.go +++ b/plugins/inputs/all/all.go @@ -5,6 +5,7 @@ import ( _ "github.com/influxdata/telegraf/plugins/inputs/amqp_consumer" _ "github.com/influxdata/telegraf/plugins/inputs/apache" _ "github.com/influxdata/telegraf/plugins/inputs/bcache" + _ "github.com/influxdata/telegraf/plugins/inputs/bond" _ "github.com/influxdata/telegraf/plugins/inputs/cassandra" _ "github.com/influxdata/telegraf/plugins/inputs/ceph" _ "github.com/influxdata/telegraf/plugins/inputs/cgroup" diff --git a/plugins/inputs/bond/README.md b/plugins/inputs/bond/README.md new file mode 100644 index 0000000000000..aff5efd288c0f --- /dev/null +++ b/plugins/inputs/bond/README.md @@ -0,0 +1,81 @@ +# Bond Input Plugin + +The Bond Input plugin collects bond interface status, bond's slaves interfaces +status and failures count of bond's slaves interfaces. +The plugin collects these metrics from `/proc/net/bonding/*` files. + +### Configuration: + +```toml +[[inputs.bond]] + ## Sets bonding directory path + ## If not specified, then default is: + bond_path = "/proc/net/bonding" + + ## By default, telegraf gather stats for all bond interfaces + ## Setting interfaces will restrict the stats to the specified + ## bond interfaces. + bond_interfaces = ["bond0"] +``` + +### Measurements & Fields: + +- bond + - status + +- bond_slave + - failures + - status + +### Description: + +``` +status + Status of bond interface or bonds's slave interface (down = 0, up = 1). + +failures + Amount of failures for bond's slave interface. +``` + +### Tags: + +- bond + - bond + +- bond_slave + - bond + - interface + +### Example output: + +Configuration: + +``` +[[inputs.bond]] + ## Sets bonding directory path + ## If not specified, then default is: + bond_path = "/proc/net/bonding" + + ## By default, telegraf gather stats for all bond interfaces + ## Setting interfaces will restrict the stats to the specified + ## bond interfaces. + bond_interfaces = ["bond0", "bond1"] +``` + +Run: + +``` +telegraf --config telegraf.conf --input-filter bond --test +``` + +Output: + +``` +* Plugin: inputs.bond, Collection 1 +> bond,bond=bond1,host=local status=1i 1509704525000000000 +> bond_slave,bond=bond1,interface=eth0,host=local status=1i,failures=0i 1509704525000000000 +> bond_slave,host=local,bond=bond1,interface=eth1 status=1i,failures=0i 1509704525000000000 +> bond,bond=bond0,host=isvetlov-mac.local status=1i 1509704525000000000 +> bond_slave,bond=bond0,interface=eth1,host=local status=1i,failures=0i 1509704525000000000 +> bond_slave,bond=bond0,interface=eth2,host=local status=1i,failures=0i 1509704525000000000 +``` diff --git a/plugins/inputs/bond/bond.go b/plugins/inputs/bond/bond.go new file mode 100644 index 0000000000000..3c0d9ec9e2ddd --- /dev/null +++ b/plugins/inputs/bond/bond.go @@ -0,0 +1,172 @@ +package bond + +import ( + "fmt" + "io/ioutil" + "path/filepath" + "strconv" + "strings" + + "github.com/influxdata/telegraf" + "github.com/influxdata/telegraf/plugins/inputs" +) + +// default bond directory path +const ( + BOND_PATH = "/proc/net/bonding" +) + +type Bond struct { + BondPath string `toml:"bond_path"` + BondInterfaces []string `toml:"bond_interfaces"` +} + +var sampleConfig = ` + ## Sets bonding directory path + ## If not specified, then default is: + bond_path = "/proc/net/bonding" + + ## By default, telegraf gather stats for all bond interfaces + ## Setting interfaces will restrict the stats to the specified + ## bond interfaces. + bond_interfaces = ["bond0"] +` + +func (bond *Bond) Description() string { + return "Collect bond interface status, slaves statuses and failures count" +} + +func (bond *Bond) SampleConfig() string { + return sampleConfig +} + +func (bond *Bond) Gather(acc telegraf.Accumulator) error { + // load path, get default value if config value and env variables are empty; + // list bond interfaces from bonding directory or gather all interfaces. + err := bond.listInterfaces() + if err != nil { + return err + } + for _, bondName := range bond.BondInterfaces { + file, err := ioutil.ReadFile(bond.BondPath + "/" + bondName) + if err != nil { + acc.AddError(fmt.Errorf("E! error due inspecting '%s' interface: %v", bondName, err)) + continue + } + rawFile := strings.TrimSpace(string(file)) + err = bond.gatherBondInterface(bondName, rawFile, acc) + if err != nil { + acc.AddError(fmt.Errorf("E! error due inspecting '%s' interface: %v", bondName, err)) + } + } + return nil +} + +func (bond *Bond) gatherBondInterface(bondName string, rawFile string, acc telegraf.Accumulator) error { + splitIndex := strings.Index(rawFile, "Slave Interface:") + if splitIndex == -1 { + splitIndex = len(rawFile) + } + bondPart := rawFile[:splitIndex] + slavePart := rawFile[splitIndex:] + + err := bond.gatherBondPart(bondName, bondPart, acc) + if err != nil { + return err + } + err = bond.gatherSlavePart(bondName, slavePart, acc) + if err != nil { + return err + } + return nil +} + +func (bond *Bond) gatherBondPart(bondName string, rawFile string, acc telegraf.Accumulator) error { + fields := make(map[string]interface{}) + tags := map[string]string{ + "bond": bondName, + } + + lines := strings.Split(rawFile, "\n") + for _, line := range lines { + stats := strings.Split(line, ":") + if len(stats) < 2 { + continue + } + name := strings.ToLower(strings.Replace(strings.TrimSpace(stats[0]), " ", "_", -1)) + value := strings.TrimSpace(stats[1]) + if strings.Contains(name, "mii_status") { + fields["status"] = 0 + if value == "up" { + fields["status"] = 1 + } + acc.AddFields("bond", fields, tags) + return nil + } + } + return fmt.Errorf("E! Couldn't find status info for '%s' ", bondName) +} + +func (bond *Bond) gatherSlavePart(bondName string, rawFile string, acc telegraf.Accumulator) error { + var slave string + var status int + + lines := strings.Split(rawFile, "\n") + for _, line := range lines { + stats := strings.Split(line, ":") + if len(stats) < 2 { + continue + } + name := strings.ToLower(strings.Replace(strings.TrimSpace(stats[0]), " ", "_", -1)) + value := strings.TrimSpace(stats[1]) + if strings.Contains(name, "slave_interface") { + slave = value + } + if strings.Contains(name, "mii_status") { + status = 0 + if value == "up" { + status = 1 + } + } + if strings.Contains(name, "link_failure_count") { + count, err := strconv.Atoi(value) + if err != nil { + return err + } + fields := map[string]interface{}{ + "status": status, + "failures": count, + } + tags := map[string]string{ + "bond": bondName, + "interface": slave, + } + acc.AddFields("bond_slave", fields, tags) + } + } + return nil +} + +func (bond *Bond) listInterfaces() error { + if bond.BondPath == "" { + bond.BondPath = BOND_PATH + } + if len(bond.BondInterfaces) == 0 { + paths, err := filepath.Glob(bond.BondPath + "/*") + if err != nil { + return err + } + var interfaces []string + for _, p := range paths { + interfaces = append(interfaces, filepath.Base(p)) + } + bond.BondInterfaces = interfaces + } + return nil +} + +func init() { + inputs.Add("bond", func() telegraf.Input { + return &Bond{} + }) +} diff --git a/plugins/inputs/bond/bond_test.go b/plugins/inputs/bond/bond_test.go new file mode 100644 index 0000000000000..7d1ddf28beb7d --- /dev/null +++ b/plugins/inputs/bond/bond_test.go @@ -0,0 +1,77 @@ +package bond + +import ( + "testing" + + "github.com/influxdata/telegraf/testutil" +) + +var sampleTest802 = ` +Ethernet Channel Bonding Driver: v3.5.0 (November 4, 2008) + +Bonding Mode: IEEE 802.3ad Dynamic link aggregation +Transmit Hash Policy: layer2 (0) +MII Status: up +MII Polling Interval (ms): 100 +Up Delay (ms): 0 +Down Delay (ms): 0 + +802.3ad info +LACP rate: fast +Aggregator selection policy (ad_select): stable +bond bond0 has no active aggregator + +Slave Interface: eth1 +MII Status: up +Link Failure Count: 0 +Permanent HW addr: 00:0c:29:f5:b7:11 +Aggregator ID: N/A + +Slave Interface: eth2 +MII Status: up +Link Failure Count: 3 +Permanent HW addr: 00:0c:29:f5:b7:1b +Aggregator ID: N/A +` + +var sampleTestAB = ` +Ethernet Channel Bonding Driver: v3.6.0 (September 26, 2009) + +Bonding Mode: fault-tolerance (active-backup) +Primary Slave: eth2 (primary_reselect always) +Currently Active Slave: eth2 +MII Status: up +MII Polling Interval (ms): 100 +Up Delay (ms): 0 +Down Delay (ms): 0 + +Slave Interface: eth3 +MII Status: down +Speed: 1000 Mbps +Duplex: full +Link Failure Count: 2 +Permanent HW addr: +Slave queue ID: 0 + +Slave Interface: eth2 +MII Status: up +Speed: 100 Mbps +Duplex: full +Link Failure Count: 0 +Permanent HW addr: +` + +func TestGatherBondInterface(t *testing.T) { + var acc testutil.Accumulator + bond := &Bond{} + + bond.gatherBondInterface("bond802", sampleTest802, &acc) + acc.AssertContainsTaggedFields(t, "bond", map[string]interface{}{"status": 1}, map[string]string{"bond": "bond802"}) + acc.AssertContainsTaggedFields(t, "bond_slave", map[string]interface{}{"failures": 0, "status": 1}, map[string]string{"bond": "bond802", "interface": "eth1"}) + acc.AssertContainsTaggedFields(t, "bond_slave", map[string]interface{}{"failures": 3, "status": 1}, map[string]string{"bond": "bond802", "interface": "eth2"}) + + bond.gatherBondInterface("bondAB", sampleTestAB, &acc) + acc.AssertContainsTaggedFields(t, "bond", map[string]interface{}{"status": 1}, map[string]string{"bond": "bondAB"}) + acc.AssertContainsTaggedFields(t, "bond_slave", map[string]interface{}{"failures": 2, "status": 0}, map[string]string{"bond": "bondAB", "interface": "eth3"}) + acc.AssertContainsTaggedFields(t, "bond_slave", map[string]interface{}{"failures": 0, "status": 1}, map[string]string{"bond": "bondAB", "interface": "eth2"}) +} From a943169a5bacbc1dbc5346adbd17902738acda8b Mon Sep 17 00:00:00 2001 From: Ildar Svetlov Date: Sun, 19 Nov 2017 23:17:15 +0400 Subject: [PATCH 2/4] Made some fixes and improvements - align README.md - made host_proc configurable, add loading from env variables - removed log levels from error messages - used bufio.Scanner instead of strings.Split - removed cache of bond interfaces - gathered active slave for active-backuo mode --- plugins/inputs/bond/README.md | 28 +++++--- plugins/inputs/bond/bond.go | 108 ++++++++++++++++++++++--------- plugins/inputs/bond/bond_test.go | 2 +- 3 files changed, 98 insertions(+), 40 deletions(-) diff --git a/plugins/inputs/bond/README.md b/plugins/inputs/bond/README.md index aff5efd288c0f..bebb88e8eeeb0 100644 --- a/plugins/inputs/bond/README.md +++ b/plugins/inputs/bond/README.md @@ -8,19 +8,24 @@ The plugin collects these metrics from `/proc/net/bonding/*` files. ```toml [[inputs.bond]] - ## Sets bonding directory path - ## If not specified, then default is: - bond_path = "/proc/net/bonding" + ## Sets 'proc' directory path + ## If not specified, then default is /proc + # host_proc = "/proc" + + ## Sets 'bonding' directory relative path + ## If not specified, then default is /net/bonding + # bond_path = "/net/bonding" ## By default, telegraf gather stats for all bond interfaces ## Setting interfaces will restrict the stats to the specified ## bond interfaces. - bond_interfaces = ["bond0"] + # bond_interfaces = ["bond0"] ``` ### Measurements & Fields: - bond + - active_slave (for active-backup mode) - status - bond_slave @@ -30,6 +35,9 @@ The plugin collects these metrics from `/proc/net/bonding/*` files. ### Description: ``` +active_slave + Currently active slave interface for active-backup mode. + status Status of bond interface or bonds's slave interface (down = 0, up = 1). @@ -52,9 +60,13 @@ Configuration: ``` [[inputs.bond]] - ## Sets bonding directory path - ## If not specified, then default is: - bond_path = "/proc/net/bonding" + ## Sets 'proc' directory path + ## If not specified, then default is /proc + host_proc = "/proc" + + ## Sets 'bonding' directory relative path + ## If not specified, then default is /net/bonding + bond_path = "/net/bonding" ## By default, telegraf gather stats for all bond interfaces ## Setting interfaces will restrict the stats to the specified @@ -72,7 +84,7 @@ Output: ``` * Plugin: inputs.bond, Collection 1 -> bond,bond=bond1,host=local status=1i 1509704525000000000 +> bond,bond=bond1,host=local active_slave="eth0",status=1i 1509704525000000000 > bond_slave,bond=bond1,interface=eth0,host=local status=1i,failures=0i 1509704525000000000 > bond_slave,host=local,bond=bond1,interface=eth1 status=1i,failures=0i 1509704525000000000 > bond,bond=bond0,host=isvetlov-mac.local status=1i 1509704525000000000 diff --git a/plugins/inputs/bond/bond.go b/plugins/inputs/bond/bond.go index 3c0d9ec9e2ddd..a26feddb34395 100644 --- a/plugins/inputs/bond/bond.go +++ b/plugins/inputs/bond/bond.go @@ -1,8 +1,10 @@ package bond import ( + "bufio" "fmt" "io/ioutil" + "os" "path/filepath" "strconv" "strings" @@ -11,25 +13,37 @@ import ( "github.com/influxdata/telegraf/plugins/inputs" ) -// default bond directory path +// default file paths const ( - BOND_PATH = "/proc/net/bonding" + defaultBondPath = "/net/bonding" + defaultHostProc = "/proc" +) + +// env variable names +const ( + envBonding = "PROC_NET_BONDING" + envProc = "HOST_PROC" ) type Bond struct { + HostProc string `toml:"host_proc"` BondPath string `toml:"bond_path"` BondInterfaces []string `toml:"bond_interfaces"` } var sampleConfig = ` - ## Sets bonding directory path - ## If not specified, then default is: - bond_path = "/proc/net/bonding" + ## Sets 'proc' directory path + ## If not specified, then default is /proc + # host_proc = "/proc" + + ## Sets 'bonding' directory relative path + ## If not specified, then default is /net/bonding + # bond_path = "/net/bonding" ## By default, telegraf gather stats for all bond interfaces ## Setting interfaces will restrict the stats to the specified ## bond interfaces. - bond_interfaces = ["bond0"] + # bond_interfaces = ["bond0"] ` func (bond *Bond) Description() string { @@ -41,22 +55,24 @@ func (bond *Bond) SampleConfig() string { } func (bond *Bond) Gather(acc telegraf.Accumulator) error { - // load path, get default value if config value and env variables are empty; + // load paths, get default value if config value and env variables are empty + bond.loadPaths() // list bond interfaces from bonding directory or gather all interfaces. - err := bond.listInterfaces() + bondNames, err := bond.listInterfaces() if err != nil { return err } - for _, bondName := range bond.BondInterfaces { - file, err := ioutil.ReadFile(bond.BondPath + "/" + bondName) + for _, bondName := range bondNames { + bondAbsPath := bond.HostProc + bond.BondPath + "/" + bondName + file, err := ioutil.ReadFile(bondAbsPath) if err != nil { - acc.AddError(fmt.Errorf("E! error due inspecting '%s' interface: %v", bondName, err)) + acc.AddError(fmt.Errorf("error inspecting '%s' interface: %v", bondAbsPath, err)) continue } rawFile := strings.TrimSpace(string(file)) err = bond.gatherBondInterface(bondName, rawFile, acc) if err != nil { - acc.AddError(fmt.Errorf("E! error due inspecting '%s' interface: %v", bondName, err)) + acc.AddError(fmt.Errorf("error inspecting '%s' interface: %v", bondName, err)) } } return nil @@ -87,15 +103,19 @@ func (bond *Bond) gatherBondPart(bondName string, rawFile string, acc telegraf.A "bond": bondName, } - lines := strings.Split(rawFile, "\n") - for _, line := range lines { + scanner := bufio.NewScanner(strings.NewReader(rawFile)) + for scanner.Scan() { + line := scanner.Text() stats := strings.Split(line, ":") if len(stats) < 2 { continue } - name := strings.ToLower(strings.Replace(strings.TrimSpace(stats[0]), " ", "_", -1)) + name := strings.TrimSpace(stats[0]) value := strings.TrimSpace(stats[1]) - if strings.Contains(name, "mii_status") { + if strings.Contains(name, "Currently Active Slave") { + fields["active_slave"] = value + } + if strings.Contains(name, "MII Status") { fields["status"] = 0 if value == "up" { fields["status"] = 1 @@ -104,31 +124,35 @@ func (bond *Bond) gatherBondPart(bondName string, rawFile string, acc telegraf.A return nil } } - return fmt.Errorf("E! Couldn't find status info for '%s' ", bondName) + if err := scanner.Err(); err != nil { + return err + } + return fmt.Errorf("Couldn't find status info for '%s' ", bondName) } func (bond *Bond) gatherSlavePart(bondName string, rawFile string, acc telegraf.Accumulator) error { var slave string var status int - lines := strings.Split(rawFile, "\n") - for _, line := range lines { + scanner := bufio.NewScanner(strings.NewReader(rawFile)) + for scanner.Scan() { + line := scanner.Text() stats := strings.Split(line, ":") if len(stats) < 2 { continue } - name := strings.ToLower(strings.Replace(strings.TrimSpace(stats[0]), " ", "_", -1)) + name := strings.TrimSpace(stats[0]) value := strings.TrimSpace(stats[1]) - if strings.Contains(name, "slave_interface") { + if strings.Contains(name, "Slave Interface") { slave = value } - if strings.Contains(name, "mii_status") { + if strings.Contains(name, "MII Status") { status = 0 if value == "up" { status = 1 } } - if strings.Contains(name, "link_failure_count") { + if strings.Contains(name, "Link Failure Count") { count, err := strconv.Atoi(value) if err != nil { return err @@ -144,25 +168,47 @@ func (bond *Bond) gatherSlavePart(bondName string, rawFile string, acc telegraf. acc.AddFields("bond_slave", fields, tags) } } + if err := scanner.Err(); err != nil { + return err + } return nil } -func (bond *Bond) listInterfaces() error { +// loadPaths can be used to read paths firstly from config +// if it is empty then try read from env variables +func (bond *Bond) loadPaths() { + if bond.HostProc == "" { + bond.HostProc = proc(envProc, defaultHostProc) + } if bond.BondPath == "" { - bond.BondPath = BOND_PATH + bond.BondPath = proc(envBonding, defaultBondPath) + } +} + +// proc can be used to read file paths from env +func proc(env, path string) string { + // try to read full file path + if p := os.Getenv(env); p != "" { + return p } - if len(bond.BondInterfaces) == 0 { - paths, err := filepath.Glob(bond.BondPath + "/*") + // return default path + return path +} + +func (bond *Bond) listInterfaces() ([]string, error) { + var interfaces []string + if len(bond.BondInterfaces) > 0 { + interfaces = bond.BondInterfaces + } else { + paths, err := filepath.Glob(bond.HostProc + bond.BondPath + "/*") if err != nil { - return err + return nil, err } - var interfaces []string for _, p := range paths { interfaces = append(interfaces, filepath.Base(p)) } - bond.BondInterfaces = interfaces } - return nil + return interfaces, nil } func init() { diff --git a/plugins/inputs/bond/bond_test.go b/plugins/inputs/bond/bond_test.go index 7d1ddf28beb7d..c07224350352c 100644 --- a/plugins/inputs/bond/bond_test.go +++ b/plugins/inputs/bond/bond_test.go @@ -71,7 +71,7 @@ func TestGatherBondInterface(t *testing.T) { acc.AssertContainsTaggedFields(t, "bond_slave", map[string]interface{}{"failures": 3, "status": 1}, map[string]string{"bond": "bond802", "interface": "eth2"}) bond.gatherBondInterface("bondAB", sampleTestAB, &acc) - acc.AssertContainsTaggedFields(t, "bond", map[string]interface{}{"status": 1}, map[string]string{"bond": "bondAB"}) + acc.AssertContainsTaggedFields(t, "bond", map[string]interface{}{"active_slave": "eth2", "status": 1}, map[string]string{"bond": "bondAB"}) acc.AssertContainsTaggedFields(t, "bond_slave", map[string]interface{}{"failures": 2, "status": 0}, map[string]string{"bond": "bondAB", "interface": "eth3"}) acc.AssertContainsTaggedFields(t, "bond_slave", map[string]interface{}{"failures": 0, "status": 1}, map[string]string{"bond": "bondAB", "interface": "eth2"}) } From 435d1f3a26290e775220cdda5752125f7b15129a Mon Sep 17 00:00:00 2001 From: Ildar Svetlov Date: Wed, 29 Nov 2017 01:06:01 +0400 Subject: [PATCH 3/4] Hardcode 'bonding' directory relative path --- plugins/inputs/bond/README.md | 8 -------- plugins/inputs/bond/bond.go | 36 +++++++++++------------------------ 2 files changed, 11 insertions(+), 33 deletions(-) diff --git a/plugins/inputs/bond/README.md b/plugins/inputs/bond/README.md index bebb88e8eeeb0..0a581418f589f 100644 --- a/plugins/inputs/bond/README.md +++ b/plugins/inputs/bond/README.md @@ -12,10 +12,6 @@ The plugin collects these metrics from `/proc/net/bonding/*` files. ## If not specified, then default is /proc # host_proc = "/proc" - ## Sets 'bonding' directory relative path - ## If not specified, then default is /net/bonding - # bond_path = "/net/bonding" - ## By default, telegraf gather stats for all bond interfaces ## Setting interfaces will restrict the stats to the specified ## bond interfaces. @@ -64,10 +60,6 @@ Configuration: ## If not specified, then default is /proc host_proc = "/proc" - ## Sets 'bonding' directory relative path - ## If not specified, then default is /net/bonding - bond_path = "/net/bonding" - ## By default, telegraf gather stats for all bond interfaces ## Setting interfaces will restrict the stats to the specified ## bond interfaces. diff --git a/plugins/inputs/bond/bond.go b/plugins/inputs/bond/bond.go index a26feddb34395..4825c0e564a83 100644 --- a/plugins/inputs/bond/bond.go +++ b/plugins/inputs/bond/bond.go @@ -13,21 +13,14 @@ import ( "github.com/influxdata/telegraf/plugins/inputs" ) -// default file paths -const ( - defaultBondPath = "/net/bonding" - defaultHostProc = "/proc" -) +// default host proc path +const defaultHostProc = "/proc" -// env variable names -const ( - envBonding = "PROC_NET_BONDING" - envProc = "HOST_PROC" -) +// env host proc variable name +const envProc = "HOST_PROC" type Bond struct { HostProc string `toml:"host_proc"` - BondPath string `toml:"bond_path"` BondInterfaces []string `toml:"bond_interfaces"` } @@ -36,10 +29,6 @@ var sampleConfig = ` ## If not specified, then default is /proc # host_proc = "/proc" - ## Sets 'bonding' directory relative path - ## If not specified, then default is /net/bonding - # bond_path = "/net/bonding" - ## By default, telegraf gather stats for all bond interfaces ## Setting interfaces will restrict the stats to the specified ## bond interfaces. @@ -55,15 +44,15 @@ func (bond *Bond) SampleConfig() string { } func (bond *Bond) Gather(acc telegraf.Accumulator) error { - // load paths, get default value if config value and env variables are empty - bond.loadPaths() + // load proc path, get default value if config value and env variable are empty + bond.loadPath() // list bond interfaces from bonding directory or gather all interfaces. bondNames, err := bond.listInterfaces() if err != nil { return err } for _, bondName := range bondNames { - bondAbsPath := bond.HostProc + bond.BondPath + "/" + bondName + bondAbsPath := bond.HostProc + "/net/bonding/" + bondName file, err := ioutil.ReadFile(bondAbsPath) if err != nil { acc.AddError(fmt.Errorf("error inspecting '%s' interface: %v", bondAbsPath, err)) @@ -174,15 +163,12 @@ func (bond *Bond) gatherSlavePart(bondName string, rawFile string, acc telegraf. return nil } -// loadPaths can be used to read paths firstly from config -// if it is empty then try read from env variables -func (bond *Bond) loadPaths() { +// loadPath can be used to read path firstly from config +// if it is empty then try read from env variable +func (bond *Bond) loadPath() { if bond.HostProc == "" { bond.HostProc = proc(envProc, defaultHostProc) } - if bond.BondPath == "" { - bond.BondPath = proc(envBonding, defaultBondPath) - } } // proc can be used to read file paths from env @@ -200,7 +186,7 @@ func (bond *Bond) listInterfaces() ([]string, error) { if len(bond.BondInterfaces) > 0 { interfaces = bond.BondInterfaces } else { - paths, err := filepath.Glob(bond.HostProc + bond.BondPath + "/*") + paths, err := filepath.Glob(bond.HostProc + "/net/bonding/*") if err != nil { return nil, err } From 925f2d358bc4df3ae0d2b4553d176e9d085f6f25 Mon Sep 17 00:00:00 2001 From: Ildar Svetlov Date: Wed, 29 Nov 2017 01:07:59 +0400 Subject: [PATCH 4/4] Align indentions --- plugins/inputs/bond/bond.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/plugins/inputs/bond/bond.go b/plugins/inputs/bond/bond.go index 4825c0e564a83..01f6f251be776 100644 --- a/plugins/inputs/bond/bond.go +++ b/plugins/inputs/bond/bond.go @@ -25,9 +25,9 @@ type Bond struct { } var sampleConfig = ` - ## Sets 'proc' directory path - ## If not specified, then default is /proc - # host_proc = "/proc" + ## Sets 'proc' directory path + ## If not specified, then default is /proc + # host_proc = "/proc" ## By default, telegraf gather stats for all bond interfaces ## Setting interfaces will restrict the stats to the specified