inital commit
This commit is contained in:
111
ping-package.go
111
ping-package.go
@@ -2,72 +2,67 @@ package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strconv"
|
||||
|
||||
"github.com/go-ping/ping"
|
||||
"math"
|
||||
"time"
|
||||
)
|
||||
|
||||
const (
|
||||
PingCount = 3
|
||||
PingInterval = 5
|
||||
)
|
||||
|
||||
func formatLine(stats *ping.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() < 33:
|
||||
rttColor = "%{F#3cb703}"
|
||||
case stats.AvgRtt.Milliseconds() < 66:
|
||||
rttColor = "%{F#f9dd04}"
|
||||
case stats.AvgRtt.Milliseconds() < 100:
|
||||
rttColor = "%{F#e87205}"
|
||||
default:
|
||||
rttColor = "%{F#d60606}"
|
||||
}
|
||||
|
||||
switch {
|
||||
case stats.PacketLoss == 0:
|
||||
packetColor = "%{F#3cb703}"
|
||||
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() {
|
||||
pinger, err := ping.NewPinger("itsh.dev")
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
for {
|
||||
time.Sleep(time.Duration(PingInterval) * time.Second)
|
||||
|
||||
pinger.Count = 3
|
||||
|
||||
var rtt_float float64
|
||||
|
||||
pinger.OnRecv = func(pkt *ping.Packet) {
|
||||
rtt := pkt.Rtt.String()
|
||||
rtt_len := len(rtt) - 2
|
||||
rtt_f, err := strconv.ParseFloat(rtt[0:rtt_len], 2)
|
||||
pinger, err := ping.NewPinger("itsh.dev")
|
||||
if err != nil {
|
||||
panic(err)
|
||||
fmt.Println(err)
|
||||
continue
|
||||
}
|
||||
rtt_float = rtt_f
|
||||
}
|
||||
pinger.Count = PingCount
|
||||
pinger.OnFinish = formatLine
|
||||
|
||||
pinger.OnFinish = func(stats *ping.Statistics) {
|
||||
packet := fmt.Sprintf("%v", stats.PacketLoss)
|
||||
packet_loss, err := strconv.Atoi(packet)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
if pinger.Run() != nil {
|
||||
fmt.Println(err)
|
||||
continue
|
||||
}
|
||||
|
||||
if packet_loss == 100 {
|
||||
fmt.Print("%{F#ff7070}ﲁ'")
|
||||
} else {
|
||||
var (
|
||||
rtt_color string
|
||||
packet_color string
|
||||
)
|
||||
|
||||
if rtt_float < 33 {
|
||||
rtt_color = "%{F#3cb703}"
|
||||
} else if rtt_float < 66 {
|
||||
rtt_color = "%{F#f9dd04}"
|
||||
} else if rtt_float < 100 {
|
||||
rtt_color = "%{F#e87205}"
|
||||
} else {
|
||||
rtt_color = "%{F#d60606}"
|
||||
}
|
||||
|
||||
if packet_loss == 0 {
|
||||
packet_color = "%{F#3cb703}"
|
||||
} else if packet_loss < 10 {
|
||||
packet_color = "%{F#f9dd04}"
|
||||
} else if packet_loss < 25 {
|
||||
packet_color = "%{F#e87205}"
|
||||
} else {
|
||||
packet_color = "%{F#d60606}"
|
||||
}
|
||||
|
||||
fmt.Printf("%s%0.2fms %%{F-}| %s%d%%", rtt_color, rtt_float, packet_color, packet_loss)
|
||||
}
|
||||
}
|
||||
err = pinger.Run()
|
||||
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user