commit d2a368a176e6cf29eb40c11c2bea85b10e13b063 Author: Henrik Hautakoski Date: Thu Feb 9 09:01:32 2023 +0100 Initial commit diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..adb36c8 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +*.exe \ No newline at end of file diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..d5cd6a0 --- /dev/null +++ b/go.mod @@ -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 +) diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..9b14ceb --- /dev/null +++ b/go.sum @@ -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= diff --git a/main.go b/main.go new file mode 100644 index 0000000..fd1eeae --- /dev/null +++ b/main.go @@ -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 \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 + } +}