Skip to content

Commit

Permalink
fix typing error
Browse files Browse the repository at this point in the history
  • Loading branch information
Jaeyo committed Sep 15, 2018
1 parent 09bef98 commit 4c556c1
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 38 deletions.
58 changes: 29 additions & 29 deletions plugins/inputs/ping/ping.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ type Ping struct {
Urls []string

// URLs to ping ipv6 address
UrlsV6 []string `toml:urls_v6`
UrlsV6 []string `toml:"urls_v6"`

// host ping function
pingHost HostPinger
Expand Down Expand Up @@ -89,19 +89,19 @@ func (p *Ping) Gather(acc telegraf.Accumulator) error {
// Spin off a go routine for each url to ping
for _, url := range p.Urls {
wg.Add(1)
go p.pingToURL(url, false, wg, acc)
go p.pingToURL(url, false, &wg, acc)
}
for _, url := range p.UrlsV6 {
wg.Add(1)
go p.pingToURL(url, true, wg, acc)
go p.pingToURL(url, true, &wg, acc)
}

wg.wait()
wg.Wait()

return nil
}

func (p *Ping) pingToURL(u string, isV6 bool, wg sync.WaitGroup, acc telegraf.Accumulator) {
func (p *Ping) pingToURL(u string, isV6 bool, wg *sync.WaitGroup, acc telegraf.Accumulator) {
defer wg.Done()
tags := map[string]string{"url": u}
fields := map[string]interface{}{"result_code": 0}
Expand Down Expand Up @@ -177,54 +177,54 @@ func hostPinger(timeout float64, isV6 bool, args ...string) (string, error) {
pingCmd = "ping6"
}

bin, err := exec.lookpath(pingCmd)
bin, err := exec.LookPath(pingCmd)
if err != nil {
return "", err
}
c := exec.command(bin, args...)
out, err := internal.combinedoutputtimeout(c,
time.second*time.duration(timeout+5))
c := exec.Command(bin, args...)
out, err := internal.CombinedOutputTimeout(c,
time.Second*time.Duration(timeout+5))
return string(out), err
}

// args returns the arguments for the 'ping' executable
func (p *ping) args(url string, system string) []string {
// build the ping command args based on toml config
args := []string{"-c", strconv.itoa(p.count), "-n", "-s", "16"}
if p.pinginterval > 0 {
args = append(args, "-i", strconv.formatfloat(p.pinginterval, 'f', -1, 64))
func (p *Ping) args(url string, system string) []string {
// Build the ping command args based on toml config
args := []string{"-c", strconv.Itoa(p.Count), "-n", "-s", "16"}
if p.PingInterval > 0 {
args = append(args, "-i", strconv.FormatFloat(p.PingInterval, 'f', -1, 64))
}
if p.timeout > 0 {
if p.Timeout > 0 {
switch system {
case "darwin", "freebsd", "netbsd", "openbsd":
args = append(args, "-w", strconv.formatfloat(p.timeout*1000, 'f', -1, 64))
args = append(args, "-W", strconv.FormatFloat(p.Timeout*1000, 'f', -1, 64))
case "linux":
args = append(args, "-w", strconv.formatfloat(p.timeout, 'f', -1, 64))
args = append(args, "-W", strconv.FormatFloat(p.Timeout, 'f', -1, 64))
default:
// not sure the best option here, just assume gnu ping?
args = append(args, "-w", strconv.formatfloat(p.timeout, 'f', -1, 64))
// Not sure the best option here, just assume GNU ping?
args = append(args, "-W", strconv.FormatFloat(p.Timeout, 'f', -1, 64))
}
}
if p.deadline > 0 {
if p.Deadline > 0 {
switch system {
case "darwin", "freebsd", "netbsd", "openbsd":
args = append(args, "-t", strconv.itoa(p.deadline))
args = append(args, "-t", strconv.Itoa(p.Deadline))
case "linux":
args = append(args, "-w", strconv.itoa(p.deadline))
args = append(args, "-w", strconv.Itoa(p.Deadline))
default:
// not sure the best option here, just assume gnu ping?
args = append(args, "-w", strconv.itoa(p.deadline))
// Not sure the best option here, just assume GNU ping?
args = append(args, "-w", strconv.Itoa(p.Deadline))
}
}
if p.interface != "" {
if p.Interface != "" {
switch system {
case "darwin", "freebsd", "netbsd", "openbsd":
args = append(args, "-s", p.interface)
args = append(args, "-S", p.Interface)
case "linux":
args = append(args, "-i", p.interface)
args = append(args, "-I", p.Interface)
default:
// not sure the best option here, just assume gnu ping?
args = append(args, "-i", p.interface)
// Not sure the best option here, just assume GNU ping?
args = append(args, "-I", p.Interface)
}
}
args = append(args, url)
Expand Down
10 changes: 5 additions & 5 deletions plugins/inputs/ping/ping_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ func TestArgs(t *testing.T) {
}
}

func mockHostPinger(timeout float64, args ...string) (string, error) {
func mockHostPinger(timeout float64, isV6 bool, args ...string) (string, error) {
return linuxPingOutput, nil
}

Expand Down Expand Up @@ -165,7 +165,7 @@ PING www.google.com (216.58.218.164) 56(84) bytes of data.
rtt min/avg/max/mdev = 35.225/44.033/51.806/5.325 ms
`

func mockLossyHostPinger(timeout float64, args ...string) (string, error) {
func mockLossyHostPinger(timeout float64, isV6 bool, args ...string) (string, error) {
return lossyPingOutput, nil
}

Expand Down Expand Up @@ -200,7 +200,7 @@ Request timeout for icmp_seq 0
2 packets transmitted, 0 packets received, 100.0% packet loss
`

func mockErrorHostPinger(timeout float64, args ...string) (string, error) {
func mockErrorHostPinger(timeout float64, isV6 bool, args ...string) (string, error) {
// This error will not trigger correct error paths
return errorPingOutput, nil
}
Expand All @@ -225,7 +225,7 @@ func TestBadPingGather(t *testing.T) {
acc.AssertContainsTaggedFields(t, "ping", fields, tags)
}

func mockFatalHostPinger(timeout float64, args ...string) (string, error) {
func mockFatalHostPinger(timeout float64, isV6 bool, args ...string) (string, error) {
return fatalPingOutput, errors.New("So very bad")
}

Expand Down Expand Up @@ -265,7 +265,7 @@ func TestErrorWithHostNamePingGather(t *testing.T) {
var acc testutil.Accumulator
p := Ping{
Urls: []string{"www.amazon.com"},
pingHost: func(timeout float64, args ...string) (string, error) {
pingHost: func(timeout float64, isV6 bool, args ...string) (string, error) {
return param.out, errors.New("So very bad")
},
}
Expand Down
8 changes: 4 additions & 4 deletions plugins/inputs/ping/ping_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ type Ping struct {
Urls []string

// URLs to ping ipv6 address
UrlsV6 []string `toml:urls_v6`
UrlsV6 []string `toml:"urls_v6"`

// host ping function
pingHost HostPinger
Expand Down Expand Up @@ -71,19 +71,19 @@ func (p *Ping) Gather(acc telegraf.Accumulator) error {
// Spin off a go routine for each url to ping
for _, url := range p.Urls {
wg.Add(1)
go p.pingToURL(url, false, wg, acc)
go p.pingToURL(url, false, &wg, acc)
}
for _, url := range p.UrlsV6 {
wg.Add(1)
go p.pingToURL(url, true, wg, acc)
go p.pingToURL(url, true, &wg, acc)
}

wg.Wait()

return nil
}

func (p *Ping) pingToURL(u string, isV6 bool, wg sync.WaitGroup, acc telegraf.Accumulator) {
func (p *Ping) pingToURL(u string, isV6 bool, wg *sync.WaitGroup, acc telegraf.Accumulator) {
defer wg.Done()

tags := map[string]string{"url": u}
Expand Down

0 comments on commit 4c556c1

Please sign in to comment.