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