67 lines
1.2 KiB
Go
67 lines
1.2 KiB
Go
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"github.com/prometheus-community/pro-bing"
|
|
"math"
|
|
"time"
|
|
)
|
|
|
|
const (
|
|
PingCount = 3
|
|
PingInterval = 5
|
|
)
|
|
|
|
func formatLine(stats *probing.Statistics) {
|
|
if stats.PacketLoss >= 100.0 {
|
|
// fontawesome/forkawesome doesn't have the fitting icon...
|
|
// so this is the utf-8 icon/emoji
|
|
fmt.Println("%{F#ff7070}🚫")
|
|
return
|
|
}
|
|
|
|
var rttColor string
|
|
var packetColor string
|
|
|
|
switch {
|
|
case stats.AvgRtt.Milliseconds() < 50:
|
|
rttColor = "%{F-}"
|
|
case stats.AvgRtt.Milliseconds() < 100:
|
|
rttColor = "%{F#e87205}"
|
|
default:
|
|
rttColor = "%{F#d60606}"
|
|
}
|
|
|
|
switch {
|
|
case stats.PacketLoss == 0:
|
|
packetColor = "%{F-}"
|
|
case stats.PacketLoss < 10:
|
|
packetColor = "%{F#f9dd04}"
|
|
case stats.PacketLoss < 25:
|
|
packetColor = "%{F#e87205}"
|
|
default:
|
|
packetColor = "%{F#d60606}"
|
|
}
|
|
|
|
fmt.Printf("%s%dms %%{F-}| %s%d%%\n", rttColor, stats.AvgRtt.Milliseconds(), packetColor, int(math.Round(stats.PacketLoss)))
|
|
}
|
|
|
|
func main() {
|
|
for {
|
|
time.Sleep(time.Duration(PingInterval) * time.Second)
|
|
|
|
pinger, err := probing.NewPinger("itsh.dev")
|
|
if err != nil {
|
|
fmt.Println(err)
|
|
continue
|
|
}
|
|
pinger.Count = PingCount
|
|
pinger.OnFinish = formatLine
|
|
|
|
if pinger.Run() != nil {
|
|
fmt.Println(err)
|
|
continue
|
|
}
|
|
}
|
|
}
|