| 1 | # pro-bing |
| 2 | [](https://pkg.go.dev/github.com/prometheus-community/pro-bing) |
| 3 | [](https://github.com/prometheus-community/pro-bing/actions/workflows/ci.yml) |
| 4 | |
| 5 | A simple but powerful ICMP echo (ping) library for Go, inspired by |
| 6 | [go-ping](https://github.com/go-ping/ping) & [go-fastping](https://github.com/tatsushid/go-fastping). |
| 7 | |
| 8 | Here is a very simple example that sends and receives three packets: |
| 9 | |
| 10 | ```go |
| 11 | pinger, err := probing.NewPinger("www.google.com") |
| 12 | if err != nil { |
| 13 | panic(err) |
| 14 | } |
| 15 | pinger.Count = 3 |
| 16 | err = pinger.Run() // Blocks until finished. |
| 17 | if err != nil { |
| 18 | panic(err) |
| 19 | } |
| 20 | stats := pinger.Statistics() // get send/receive/duplicate/rtt stats |
| 21 | ``` |
| 22 | |
| 23 | Here is an example that emulates the traditional UNIX ping command: |
| 24 | |
| 25 | ```go |
| 26 | pinger, err := probing.NewPinger("www.google.com") |
| 27 | if err != nil { |
| 28 | panic(err) |
| 29 | } |
| 30 | |
| 31 | // Listen for Ctrl-C. |
| 32 | c := make(chan os.Signal, 1) |
| 33 | signal.Notify(c, os.Interrupt) |
| 34 | go func() { |
| 35 | for _ = range c { |
| 36 | pinger.Stop() |
| 37 | } |
| 38 | }() |
| 39 | |
| 40 | pinger.OnRecv = func(pkt *probing.Packet) { |
| 41 | fmt.Printf("%d bytes from %s: icmp_seq=%d time=%v\n", |
| 42 | pkt.Nbytes, pkt.IPAddr, pkt.Seq, pkt.Rtt) |
| 43 | } |
| 44 | |
| 45 | pinger.OnDuplicateRecv = func(pkt *probing.Packet) { |
| 46 | fmt.Printf("%d bytes from %s: icmp_seq=%d time=%v ttl=%v (DUP!)\n", |
| 47 | pkt.Nbytes, pkt.IPAddr, pkt.Seq, pkt.Rtt, pkt.TTL) |
| 48 | } |
| 49 | |
| 50 | pinger.OnFinish = func(stats *probing.Statistics) { |
| 51 | fmt.Printf("\n--- %s ping statistics ---\n", stats.Addr) |
| 52 | fmt.Printf("%d packets transmitted, %d packets received, %v%% packet loss\n", |
| 53 | stats.PacketsSent, stats.PacketsRecv, stats.PacketLoss) |
| 54 | fmt.Printf("round-trip min/avg/max/stddev = %v/%v/%v/%v\n", |
| 55 | stats.MinRtt, stats.AvgRtt, stats.MaxRtt, stats.StdDevRtt) |
| 56 | } |
| 57 | |
| 58 | fmt.Printf("PING %s (%s):\n", pinger.Addr(), pinger.IPAddr()) |
| 59 | err = pinger.Run() |
| 60 | if err != nil { |
| 61 | panic(err) |
| 62 | } |
| 63 | ``` |
| 64 | |
| 65 | It sends ICMP Echo Request packet(s) and waits for an Echo Reply in |
| 66 | response. If it receives a response, it calls the `OnRecv` callback |
| 67 | unless a packet with that sequence number has already been received, |
| 68 | in which case it calls the `OnDuplicateRecv` callback. When it's |
| 69 | finished, it calls the `OnFinish` callback. |
| 70 | |
| 71 | For a full ping example, see |
| 72 | [cmd/ping/ping.go](https://github.com/prometheus-community/pro-bing/blob/master/cmd/ping/ping.go). |
| 73 | |
| 74 | ## Installation |
| 75 | |
| 76 | ``` |
| 77 | go get -u github.com/prometheus-community/pro-bing |
| 78 | ``` |
| 79 | |
| 80 | To install the native Go ping executable: |
| 81 | |
| 82 | ```bash |
| 83 | go get -u github.com/prometheus-community/pro-bing/... |
| 84 | $GOPATH/bin/ping |
| 85 | ``` |
| 86 | |
| 87 | ## Supported Operating Systems |
| 88 | |
| 89 | ### Linux |
| 90 | This library attempts to send an "unprivileged" ping via UDP. On Linux, |
| 91 | this must be enabled with the following sysctl command: |
| 92 | |
| 93 | ``` |
| 94 | sudo sysctl -w net.ipv4.ping_group_range="0 2147483647" |
| 95 | ``` |
| 96 | |
| 97 | If you do not wish to do this, you can call `pinger.SetPrivileged(true)` |
| 98 | in your code and then use setcap on your binary to allow it to bind to |
| 99 | raw sockets (or just run it as root): |
| 100 | |
| 101 | ``` |
| 102 | setcap cap_net_raw=+ep /path/to/your/compiled/binary |
| 103 | ``` |
| 104 | |
| 105 | See [this blog](https://sturmflut.github.io/linux/ubuntu/2015/01/17/unprivileged-icmp-sockets-on-linux/) |
| 106 | and the Go [x/net/icmp](https://godoc.org/golang.org/x/net/icmp) package |
| 107 | for more details. |
| 108 | |
| 109 | This library supports setting the `SO_MARK` socket option which is equivalent to the `-m mark` |
| 110 | flag in standard ping binaries on linux. Setting this option requires the `CAP_NET_ADMIN` capability |
| 111 | (via `setcap` or elevated privileges). You can set a mark (ex: 100) with `pinger.SetMark(100)` in your code. |
| 112 | |
| 113 | Setting the "Don't Fragment" bit is supported under Linux which is equivalent to `ping -Mdo`. |
| 114 | You can enable this with `pinger.SetDoNotFragment(true)`. |
| 115 | |
| 116 | ### Windows |
| 117 | |
| 118 | You must use `pinger.SetPrivileged(true)`, otherwise you will receive |
| 119 | the following error: |
| 120 | |
| 121 | ``` |
| 122 | socket: The requested protocol has not been configured into the system, or no implementation for it exists. |
| 123 | ``` |
| 124 | |
| 125 | Despite the method name, this should work without the need to elevate |
| 126 | privileges and has been tested on Windows 10. Please note that accessing |
| 127 | packet TTL values is not supported due to limitations in the Go |
| 128 | x/net/ipv4 and x/net/ipv6 packages. |
| 129 | |
| 130 | ### Plan 9 from Bell Labs |
| 131 | |
| 132 | There is no support for Plan 9. This is because the entire `x/net/ipv4` |
| 133 | and `x/net/ipv6` packages are not implemented by the Go programming |
| 134 | language. |
| 135 | |
| 136 | ## HTTP |
| 137 | |
| 138 | This library also provides support for HTTP probing. |
| 139 | Here is a trivial example: |
| 140 | |
| 141 | ```go |
| 142 | httpCaller := probing.NewHTTPCaller("https://www.google.com", |
| 143 | probing.WithHTTPCallerCallFrequency(time.Second), |
| 144 | probing.WithHTTPCallerOnResp(func(suite *probing.TraceSuite, info *probing.HTTPCallInfo) { |
| 145 | fmt.Printf("got resp, status code: %d, latency: %s\n", |
| 146 | info.StatusCode, |
| 147 | suite.GetGeneralEnd().Sub(suite.GetGeneralStart()), |
| 148 | ) |
| 149 | }), |
| 150 | ) |
| 151 | |
| 152 | // Listen for Ctrl-C. |
| 153 | c := make(chan os.Signal, 1) |
| 154 | signal.Notify(c, os.Interrupt) |
| 155 | go func() { |
| 156 | <-c |
| 157 | httpCaller.Stop() |
| 158 | }() |
| 159 | httpCaller.Run() |
| 160 | ``` |
| 161 | |
| 162 | Library provides a rich list of options available for a probing. You can check the full list of available |
| 163 | options in a generated doc. |
| 164 | |
| 165 | ### Callbacks |
| 166 | |
| 167 | HTTPCaller uses `net/http/httptrace` pkg to provide an API to track specific request event, e.g. tls handshake start. |
| 168 | It is highly recommended to check the httptrace library [doc](https://pkg.go.dev/net/http/httptrace) to understand |
| 169 | the purpose of provided callbacks. Nevertheless, httptrace callbacks are concurrent-unsafe, our implementation provides |
| 170 | a concurrent-safe API. In addition to that, each callback contains a TraceSuite object which provides an Extra field |
| 171 | which you can use to propagate your data across them and a number of timer fields, which are set prior to the execution of a |
| 172 | corresponding callback. |
| 173 | |
| 174 | ### Target RPS & performance |
| 175 | |
| 176 | Library provides two options, allowing to manipulate your call load: `callFrequency` & `maxConcurrentCalls`. |
| 177 | In case you set `callFrequency` to a value X, but it can't be achieved during the execution - you will need to |
| 178 | try increasing a number of `maxConcurrentCalls`. Moreover, your callbacks might directly influence an execution |
| 179 | performance. |
| 180 | |
| 181 | For a full documentation, please refer to the generated [doc](https://pkg.go.dev/github.com/prometheus-community/pro-bing). |
| 182 | |
| 183 | ## Maintainers and Getting Help: |
| 184 | |
| 185 | This repo was originally in the personal account of |
| 186 | [sparrc](https://github.com/sparrc), but is now maintained by the |
| 187 | [Prometheus Community](https://prometheus.io/community). |
| 188 | |
| 189 | ## Contributing |
| 190 | |
| 191 | Refer to [CONTRIBUTING.md](https://github.com/prometheus-community/pro-bing/blob/master/CONTRIBUTING.md) |