|
| 1 | +package ping |
| 2 | + |
| 3 | +import ( |
| 4 | + "errors" |
| 5 | + "os/exec" |
| 6 | + "strconv" |
| 7 | + "strings" |
| 8 | + "sync" |
| 9 | + |
| 10 | + "github.com/influxdb/telegraf/plugins" |
| 11 | +) |
| 12 | + |
| 13 | +// HostPinger is a function that runs the "ping" function using a list of |
| 14 | +// passed arguments. This can be easily switched with a mocked ping function |
| 15 | +// for unit test purposes (see ping_test.go) |
| 16 | +type HostPinger func(args ...string) (string, error) |
| 17 | + |
| 18 | +type Ping struct { |
| 19 | + // Interval at which to ping (ping -i <INTERVAL>) |
| 20 | + PingInterval float64 `toml:"ping_interval"` |
| 21 | + |
| 22 | + // Number of pings to send (ping -c <COUNT>) |
| 23 | + Count int |
| 24 | + |
| 25 | + // Ping timeout, in seconds. 0 means no timeout (ping -t <TIMEOUT>) |
| 26 | + Timeout float64 |
| 27 | + |
| 28 | + // Interface to send ping from (ping -I <INTERFACE>) |
| 29 | + Interface string |
| 30 | + |
| 31 | + // URLs to ping |
| 32 | + Urls []string |
| 33 | + |
| 34 | + // host ping function |
| 35 | + pingHost HostPinger |
| 36 | +} |
| 37 | + |
| 38 | +func (_ *Ping) Description() string { |
| 39 | + return "Ping given url(s) and return statistics" |
| 40 | +} |
| 41 | + |
| 42 | +var sampleConfig = ` |
| 43 | + # urls to ping |
| 44 | + urls = ["www.google.com"] # required |
| 45 | + # number of pings to send (ping -c <COUNT>) |
| 46 | + count = 1 # required |
| 47 | + # interval, in s, at which to ping. 0 == default (ping -i <PING_INTERVAL>) |
| 48 | + ping_interval = 0.0 |
| 49 | + # ping timeout, in s. 0 == no timeout (ping -t <TIMEOUT>) |
| 50 | + timeout = 0.0 |
| 51 | + # interface to send ping from (ping -I <INTERFACE>) |
| 52 | + interface = "" |
| 53 | +` |
| 54 | + |
| 55 | +func (_ *Ping) SampleConfig() string { |
| 56 | + return sampleConfig |
| 57 | +} |
| 58 | + |
| 59 | +func (p *Ping) Gather(acc plugins.Accumulator) error { |
| 60 | + |
| 61 | + var wg sync.WaitGroup |
| 62 | + errorChannel := make(chan error, len(p.Urls)*2) |
| 63 | + |
| 64 | + // Spin off a go routine for each url to ping |
| 65 | + for _, url := range p.Urls { |
| 66 | + wg.Add(1) |
| 67 | + go func(url string, acc plugins.Accumulator) { |
| 68 | + defer wg.Done() |
| 69 | + args := p.args(url) |
| 70 | + out, err := p.pingHost(args...) |
| 71 | + if err != nil { |
| 72 | + // Combine go err + stderr output |
| 73 | + errorChannel <- errors.New( |
| 74 | + strings.TrimSpace(out) + ", " + err.Error()) |
| 75 | + } |
| 76 | + tags := map[string]string{"url": url} |
| 77 | + trans, rec, avg, err := processPingOutput(out) |
| 78 | + if err != nil { |
| 79 | + // fatal error |
| 80 | + errorChannel <- err |
| 81 | + return |
| 82 | + } |
| 83 | + // Calculate packet loss percentage |
| 84 | + loss := float64(trans-rec) / float64(trans) * 100.0 |
| 85 | + acc.Add("packets_transmitted", trans, tags) |
| 86 | + acc.Add("packets_received", rec, tags) |
| 87 | + acc.Add("percent_packet_loss", loss, tags) |
| 88 | + acc.Add("average_response_ms", avg, tags) |
| 89 | + }(url, acc) |
| 90 | + } |
| 91 | + |
| 92 | + wg.Wait() |
| 93 | + close(errorChannel) |
| 94 | + |
| 95 | + // Get all errors and return them as one giant error |
| 96 | + errorStrings := []string{} |
| 97 | + for err := range errorChannel { |
| 98 | + errorStrings = append(errorStrings, err.Error()) |
| 99 | + } |
| 100 | + |
| 101 | + if len(errorStrings) == 0 { |
| 102 | + return nil |
| 103 | + } |
| 104 | + return errors.New(strings.Join(errorStrings, "\n")) |
| 105 | +} |
| 106 | + |
| 107 | +func hostPinger(args ...string) (string, error) { |
| 108 | + c := exec.Command("ping", args...) |
| 109 | + out, err := c.CombinedOutput() |
| 110 | + return string(out), err |
| 111 | +} |
| 112 | + |
| 113 | +// args returns the arguments for the 'ping' executable |
| 114 | +func (p *Ping) args(url string) []string { |
| 115 | + // Build the ping command args based on toml config |
| 116 | + args := []string{"-c", strconv.Itoa(p.Count)} |
| 117 | + if p.PingInterval > 0 { |
| 118 | + args = append(args, "-i", strconv.FormatFloat(p.PingInterval, 'f', 1, 64)) |
| 119 | + } |
| 120 | + if p.Timeout > 0 { |
| 121 | + args = append(args, "-t", strconv.FormatFloat(p.Timeout, 'f', 1, 64)) |
| 122 | + } |
| 123 | + if p.Interface != "" { |
| 124 | + args = append(args, "-I", p.Interface) |
| 125 | + } |
| 126 | + args = append(args, url) |
| 127 | + return args |
| 128 | +} |
| 129 | + |
| 130 | +// processPingOutput takes in a string output from the ping command, like: |
| 131 | +// |
| 132 | +// PING www.google.com (173.194.115.84): 56 data bytes |
| 133 | +// 64 bytes from 173.194.115.84: icmp_seq=0 ttl=54 time=52.172 ms |
| 134 | +// 64 bytes from 173.194.115.84: icmp_seq=1 ttl=54 time=34.843 ms |
| 135 | +// |
| 136 | +// --- www.google.com ping statistics --- |
| 137 | +// 2 packets transmitted, 2 packets received, 0.0% packet loss |
| 138 | +// round-trip min/avg/max/stddev = 34.843/43.508/52.172/8.664 ms |
| 139 | +// |
| 140 | +// It returns (<transmitted packets>, <received packets>, <average response>) |
| 141 | +func processPingOutput(out string) (int, int, float64, error) { |
| 142 | + var trans, recv int |
| 143 | + var avg float64 |
| 144 | + // Set this error to nil if we find a 'transmitted' line |
| 145 | + err := errors.New("Fatal error processing ping output") |
| 146 | + lines := strings.Split(out, "\n") |
| 147 | + for _, line := range lines { |
| 148 | + if strings.Contains(line, "transmitted") && |
| 149 | + strings.Contains(line, "received") { |
| 150 | + err = nil |
| 151 | + stats := strings.Split(line, ", ") |
| 152 | + // Transmitted packets |
| 153 | + trans, err = strconv.Atoi(strings.Split(stats[0], " ")[0]) |
| 154 | + if err != nil { |
| 155 | + return trans, recv, avg, err |
| 156 | + } |
| 157 | + // Received packets |
| 158 | + recv, err = strconv.Atoi(strings.Split(stats[1], " ")[0]) |
| 159 | + if err != nil { |
| 160 | + return trans, recv, avg, err |
| 161 | + } |
| 162 | + } else if strings.Contains(line, "min/avg/max") { |
| 163 | + stats := strings.Split(line, " = ")[1] |
| 164 | + avg, err = strconv.ParseFloat(strings.Split(stats, "/")[1], 64) |
| 165 | + if err != nil { |
| 166 | + return trans, recv, avg, err |
| 167 | + } |
| 168 | + } |
| 169 | + } |
| 170 | + return trans, recv, avg, err |
| 171 | +} |
| 172 | + |
| 173 | +func init() { |
| 174 | + plugins.Add("ping", func() plugins.Plugin { |
| 175 | + return &Ping{pingHost: hostPinger} |
| 176 | + }) |
| 177 | +} |
0 commit comments