Skip to content

Commit

Permalink
add support ipv6 ping
Browse files Browse the repository at this point in the history
  • Loading branch information
Jaeyo committed Sep 15, 2018
1 parent 2f783d6 commit 19461ba
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 12 deletions.
21 changes: 15 additions & 6 deletions plugins/inputs/ping/ping.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import (
// HostPinger is a function that runs the "ping" function using a list of
// passed arguments. This can be easily switched with a mocked ping function
// for unit test purposes (see ping_test.go)
type HostPinger func(timeout float64, args ...string) (string, error)
type HostPinger func(timeout float64, isV6 bool, args ...string) (string, error)

type Ping struct {
// Interval at which to ping (ping -i <INTERVAL>)
Expand Down Expand Up @@ -89,15 +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)
p.pingToURL(url, wg, acc)
go p.pingToURL(url, false, wg, acc)
}
for _, url := range p.UrlsV6 {
wg.Add(1)
go p.pingToURL(url, true, wg, acc)
}

wg.wait()

return nil
}

func (p *Ping) pingToURL(u string, 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 All @@ -113,7 +117,7 @@ func (p *Ping) pingToURL(u string, wg sync.WaitGroup, acc telegraf.Accumulator)
args := p.args(u, runtime.GOOS)
totalTimeout := float64(p.Count)*p.Timeout + float64(p.Count-1)*p.PingInterval

out, err := p.pingHost(totalTimeout, args...)
out, err := p.pingHost(totalTimeout, isV6, args...)
if err != nil {
// Some implementations of ping return a 1 exit code on
// timeout, if this occurs we will not exit and try to parse
Expand Down Expand Up @@ -167,8 +171,13 @@ func (p *Ping) pingToURL(u string, wg sync.WaitGroup, acc telegraf.Accumulator)
acc.AddFields("ping", fields, tags)
}

func hostpinger(timeout float64, args ...string) (string, error) {
bin, err := exec.lookpath("ping")
func hostPinger(timeout float64, isV6 bool, args ...string) (string, error) {
pingCmd := "ping"
if isV6 == true {
pingCmd = "ping6"
}

bin, err := exec.lookpath(pingCmd)
if err != nil {
return "", err
}
Expand Down
21 changes: 15 additions & 6 deletions plugins/inputs/ping/ping_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import (
// HostPinger is a function that runs the "ping" function using a list of
// passed arguments. This can be easily switched with a mocked ping function
// for unit test purposes (see ping_test.go)
type HostPinger func(timeout float64, args ...string) (string, error)
type HostPinger func(timeout float64, isV6 bool, args ...string) (string, error)

type Ping struct {
// Number of pings to send (ping -c <COUNT>)
Expand Down Expand Up @@ -71,15 +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)
p.pingToURL(url, wg, acc)
go p.pingToURL(url, false, wg, acc)
}
for _, url := range p.UrlsV6 {
wg.Add(1)
go p.pingToURL(url, true, wg, acc)
}

wg.Wait()

return nil
}

func (p *Ping) pingToURL(u string, 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 All @@ -95,7 +99,7 @@ func (p *Ping) pingToURL(u string, wg sync.WaitGroup, acc telegraf.Accumulator)

args := p.args(u)
totalTimeout := p.timeout() * float64(p.Count)
out, err := p.pingHost(totalTimeout, args...)
out, err := p.pingHost(totalTimeout, isV6, args...)
// ping host return exitcode != 0 also when there was no response from host
// but command was execute successfully
if err != nil {
Expand Down Expand Up @@ -137,8 +141,13 @@ func (p *Ping) pingToURL(u string, wg sync.WaitGroup, acc telegraf.Accumulator)
acc.AddFields("ping", fields, tags)
}

func hostPinger(timeout float64, args ...string) (string, error) {
bin, err := exec.LookPath("ping")
func hostPinger(timeout float64, isV6 bool, args ...string) (string, error) {
pingCmd := "ping"
if isV6 == true {
pingCmd = "ping6"
}

bin, err := exec.LookPath(pingCmd)
if err != nil {
return "", err
}
Expand Down

0 comments on commit 19461ba

Please sign in to comment.