add a tag for the used ip-version

config item to force ipv4 or ipv6 for the ping
This commit is contained in:
Christian Loew 2016-10-15 18:57:05 +02:00
parent a65447d22e
commit 35e7a4de53
2 changed files with 36 additions and 9 deletions

View File

@ -13,6 +13,9 @@ This input plugin will measures the round-trip
## Ping timeout, in seconds. 0 means default timeout (ping -w <TIMEOUT>) ## Ping timeout, in seconds. 0 means default timeout (ping -w <TIMEOUT>)
Timeout = 0 Timeout = 0
## force ipv4 or ipv6 ('ping -6' or 'ping -4')
IPVersion = 6
``` ```
### Measurements & Fields: ### Measurements & Fields:
- packets_transmitted ( from ping output ) - packets_transmitted ( from ping output )
@ -28,9 +31,10 @@ This input plugin will measures the round-trip
### Tags: ### Tags:
- server - server
- ipversion
### Example Output: ### Example Output:
``` ```
* Plugin: ping, Collection 1 * Plugin: ping, Collection 1
ping,host=WIN-PBAPLP511R7,url=www.google.com average_response_ms=7i,maximum_response_ms=9i,minimum_response_ms=7i,packets_received=4i,packets_transmitted=4i,percent_packet_loss=0,percent_reply_loss=0,reply_received=4i 1469879119000000000 ping,host=WIN-PBAPLP511R7,url=www.google.com average_response_ms=7i,maximum_response_ms=9i,minimum_response_ms=7i,packets_received=4i,packets_transmitted=4i,percent_packet_loss=0,percent_reply_loss=0,reply_received=4i 1469879119000000000
``` ```

View File

@ -10,6 +10,7 @@ import (
"strings" "strings"
"sync" "sync"
"time" "time"
"regexp"
"github.com/influxdata/telegraf" "github.com/influxdata/telegraf"
"github.com/influxdata/telegraf/internal" "github.com/influxdata/telegraf/internal"
@ -39,6 +40,8 @@ type Ping struct {
// host ping function // host ping function
pingHost HostPinger pingHost HostPinger
IPVersion int
} }
func (_ *Ping) Description() string { func (_ *Ping) Description() string {
@ -59,6 +62,8 @@ const sampleConfig = `
timeout = 1.0 timeout = 1.0
## interface to send ping from (ping -I <INTERFACE>) ## interface to send ping from (ping -I <INTERFACE>)
interface = "" interface = ""
## force which IP version to use. (ping -6 or ping -4)
IPVersion = 4
` `
func (_ *Ping) SampleConfig() string { func (_ *Ping) SampleConfig() string {
@ -84,12 +89,14 @@ func (p *Ping) Gather(acc telegraf.Accumulator) error {
strings.TrimSpace(out) + ", " + err.Error()) strings.TrimSpace(out) + ", " + err.Error())
} }
tags := map[string]string{"url": u} tags := map[string]string{"url": u}
trans, rec, avg, err := processPingOutput(out)
trans, rec, avg, ipversion, err := processPingOutput(out)
if err != nil { if err != nil {
// fatal error // fatal error
errorChannel <- err errorChannel <- err
return return
} }
tags["ipversion"] = strconv.Itoa(ipversion)
// Calculate packet loss percentage // Calculate packet loss percentage
loss := float64(trans-rec) / float64(trans) * 100.0 loss := float64(trans-rec) / float64(trans) * 100.0
fields := map[string]interface{}{ fields := map[string]interface{}{
@ -152,6 +159,11 @@ func (p *Ping) args(url string) []string {
args = append(args, "-I", p.Interface) args = append(args, "-I", p.Interface)
} }
args = append(args, url) args = append(args, url)
switch p.IPVersion {
case 4: args = append(args, "-4")
case 6: args = append(args, "-6")
}
return args return args
} }
@ -166,36 +178,47 @@ func (p *Ping) args(url string) []string {
// round-trip min/avg/max/stddev = 34.843/43.508/52.172/8.664 ms // round-trip min/avg/max/stddev = 34.843/43.508/52.172/8.664 ms
// //
// It returns (<transmitted packets>, <received packets>, <average response>) // It returns (<transmitted packets>, <received packets>, <average response>)
func processPingOutput(out string) (int, int, float64, error) {
var trans, recv int // for IPv6 address the ping looks like
// PING google.com(fra16s08-in-x0e.1e100.net (2a00:1450:4001:817::200e)) 56 data bytes
func processPingOutput(out string) (int, int, float64, int, error) {
var trans, recv, ipversion int
var avg float64 var avg float64
// Set this error to nil if we find a 'transmitted' line // Set this error to nil if we find a 'transmitted' line
err := errors.New("Fatal error processing ping output") err := errors.New("Fatal error processing ping output")
re := regexp.MustCompile(`(?i)^ping.+\(([a-f0-9\.:]+)\)`) // re[1] is the IP
lines := strings.Split(out, "\n") lines := strings.Split(out, "\n")
for _, line := range lines { for _, line := range lines {
if strings.Contains(line, "transmitted") && str := re.FindStringSubmatch(line)
if len(str) != 0 {
if strings.Contains(str[1],":") {
ipversion = 6
} else {
ipversion = 4
}
} else if strings.Contains(line, "transmitted") &&
strings.Contains(line, "received") { strings.Contains(line, "received") {
err = nil err = nil
stats := strings.Split(line, ", ") stats := strings.Split(line, ", ")
// Transmitted packets // Transmitted packets
trans, err = strconv.Atoi(strings.Split(stats[0], " ")[0]) trans, err = strconv.Atoi(strings.Split(stats[0], " ")[0])
if err != nil { if err != nil {
return trans, recv, avg, err return trans, recv, avg, ipversion, err
} }
// Received packets // Received packets
recv, err = strconv.Atoi(strings.Split(stats[1], " ")[0]) recv, err = strconv.Atoi(strings.Split(stats[1], " ")[0])
if err != nil { if err != nil {
return trans, recv, avg, err return trans, recv, avg, ipversion, err
} }
} else if strings.Contains(line, "min/avg/max") { } else if strings.Contains(line, "min/avg/max") {
stats := strings.Split(line, " = ")[1] stats := strings.Split(line, " = ")[1]
avg, err = strconv.ParseFloat(strings.Split(stats, "/")[1], 64) avg, err = strconv.ParseFloat(strings.Split(stats, "/")[1], 64)
if err != nil { if err != nil {
return trans, recv, avg, err return trans, recv, avg, ipversion, err
} }
} }
} }
return trans, recv, avg, err return trans, recv, avg, ipversion, err
} }
func init() { func init() {