inital commit
This commit is contained in:
7
go.mod
7
go.mod
@@ -2,10 +2,11 @@ module ping-package/mod
|
|||||||
|
|
||||||
go 1.19
|
go 1.19
|
||||||
|
|
||||||
|
require github.com/go-ping/ping v1.1.0
|
||||||
|
|
||||||
require (
|
require (
|
||||||
github.com/go-ping/ping v1.1.0 // indirect
|
|
||||||
github.com/google/uuid v1.3.0 // indirect
|
github.com/google/uuid v1.3.0 // indirect
|
||||||
golang.org/x/net v0.1.0 // indirect
|
golang.org/x/net v0.9.0 // indirect
|
||||||
golang.org/x/sync v0.1.0 // indirect
|
golang.org/x/sync v0.1.0 // indirect
|
||||||
golang.org/x/sys v0.1.0 // indirect
|
golang.org/x/sys v0.7.0 // indirect
|
||||||
)
|
)
|
||||||
|
8
go.sum
8
go.sum
@@ -4,15 +4,15 @@ github.com/google/uuid v1.2.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+
|
|||||||
github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I=
|
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/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
|
||||||
golang.org/x/net v0.0.0-20210316092652-d523dce5a7f4/go.mod h1:RBQZq4jEuRlivfhVLdyRGr576XBO4/greRjx4P4O3yc=
|
golang.org/x/net v0.0.0-20210316092652-d523dce5a7f4/go.mod h1:RBQZq4jEuRlivfhVLdyRGr576XBO4/greRjx4P4O3yc=
|
||||||
golang.org/x/net v0.1.0 h1:hZ/3BUoy5aId7sCpA/Tc5lt8DkFgdVS2onTpJsZ/fl0=
|
golang.org/x/net v0.9.0 h1:aWJ/m6xSmxWBx+V0XRHTlrYrPG56jKsLdTFmsSsCzOM=
|
||||||
golang.org/x/net v0.1.0/go.mod h1:Cx3nUiGt4eDBEyega/BKRp+/AlGL8hYe7U9odMt2Cco=
|
golang.org/x/net v0.9.0/go.mod h1:d48xBJpPfHeWQsugry2m+kC02ZBRGRgulfHnEXEuWns=
|
||||||
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||||
golang.org/x/sync v0.1.0 h1:wsuoTGHzEhffawBOhz5CYhcrV4IdKZbEyZjBMuTp12o=
|
golang.org/x/sync v0.1.0 h1:wsuoTGHzEhffawBOhz5CYhcrV4IdKZbEyZjBMuTp12o=
|
||||||
golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||||
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||||
golang.org/x/sys v0.0.0-20210315160823-c6e025ad8005/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
golang.org/x/sys v0.0.0-20210315160823-c6e025ad8005/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||||
golang.org/x/sys v0.1.0 h1:kunALQeHf1/185U1i0GOB/fy1IPRDDpuoOOqRReG57U=
|
golang.org/x/sys v0.7.0 h1:3jlCCIQZPdOYu1h8BkNvLz8Kgwtae2cagcG/VamtZRU=
|
||||||
golang.org/x/sys v0.1.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
golang.org/x/sys v0.7.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||||
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
|
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
|
||||||
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
|
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
|
||||||
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
|
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
|
||||||
|
111
ping-package.go
111
ping-package.go
@@ -2,72 +2,67 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"strconv"
|
|
||||||
|
|
||||||
"github.com/go-ping/ping"
|
"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() {
|
func main() {
|
||||||
pinger, err := ping.NewPinger("itsh.dev")
|
for {
|
||||||
if err != nil {
|
time.Sleep(time.Duration(PingInterval) * time.Second)
|
||||||
panic(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
pinger.Count = 3
|
pinger, err := ping.NewPinger("itsh.dev")
|
||||||
|
|
||||||
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)
|
|
||||||
if err != nil {
|
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) {
|
if pinger.Run() != nil {
|
||||||
packet := fmt.Sprintf("%v", stats.PacketLoss)
|
fmt.Println(err)
|
||||||
packet_loss, err := strconv.Atoi(packet)
|
continue
|
||||||
if err != nil {
|
|
||||||
panic(err)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
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