1
0
Fork 0
mirror of https://github.com/pnx/pinger.git synced 2026-06-16 03:24:54 +02:00

Initial commit

This commit is contained in:
Henrik Hautakoski 2023-02-09 09:01:32 +01:00
commit d2a368a176
4 changed files with 84 additions and 0 deletions

1
.gitignore vendored Normal file
View file

@ -0,0 +1 @@
*.exe

12
go.mod Normal file
View file

@ -0,0 +1,12 @@
module pinger
go 1.19
require github.com/prometheus-community/pro-bing v0.1.0
require (
github.com/google/uuid v1.3.0 // indirect
golang.org/x/net v0.5.0 // indirect
golang.org/x/sync v0.0.0-20220601150217-0de741cfad7f // indirect
golang.org/x/sys v0.4.0 // indirect
)

10
go.sum Normal file
View file

@ -0,0 +1,10 @@
github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I=
github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/prometheus-community/pro-bing v0.1.0 h1:zjzLGhfNPP0bP1OlzGB+SJcguOViw7df12LPg2vUJh8=
github.com/prometheus-community/pro-bing v0.1.0/go.mod h1:BpWlHurD9flHtzq8wrh8QGWYz9ka9z9ZJAyOel8ej58=
golang.org/x/net v0.5.0 h1:GyT4nK/YDHSqa1c4753ouYCDajOYKTja9Xb/OHtgvSw=
golang.org/x/net v0.5.0/go.mod h1:DivGGAXEgPSlEBzxGzZI+ZLohi+xUj054jfeKui00ws=
golang.org/x/sync v0.0.0-20220601150217-0de741cfad7f h1:Ax0t5p6N38Ga0dThY21weqDEyz2oklo4IvDkpigvkD8=
golang.org/x/sync v0.0.0-20220601150217-0de741cfad7f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.4.0 h1:Zr2JFtRQNX3BCZ8YtxRE9hNJYC8J6I1MVbMg6owUp18=
golang.org/x/sys v0.4.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=

61
main.go Normal file
View file

@ -0,0 +1,61 @@
package main
import (
"fmt"
"os"
"os/signal"
"path/filepath"
"syscall"
"time"
"github.com/prometheus-community/pro-bing"
)
func printStats(pinger *probing.Pinger) {
stats := pinger.Statistics()
fmt.Printf("%d packets transmitted, %d packets received, %.2f%% 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)
}
func eventLoop(pinger *probing.Pinger) {
ticker := time.NewTicker(time.Second * 4)
sig := make(chan os.Signal, 1)
signal.Notify(sig, syscall.SIGINT, syscall.SIGTERM)
for {
select {
case <-sig:
pinger.Stop()
return
case <-ticker.C:
printStats(pinger)
}
}
}
func main() {
if len(os.Args) < 2 {
fmt.Printf("Usage: %s <ip>\n", filepath.Base(os.Args[0]))
os.Exit(1)
}
pinger, err := probing.NewPinger(os.Args[1])
if err != nil {
fmt.Println("Error:", err)
return
}
pinger.SetPrivileged(true)
pinger.Interval = time.Second * 2
go eventLoop(pinger)
fmt.Println("PING", pinger.Addr())
if err = pinger.Run(); err != nil {
fmt.Println("Error:", err)
return
}
}