Contents

How to monitor your ping latency in macOS with Go Programming Language

Create you own ping latency monitoring on your mac during the pandemic.

How to monitor your ping latency in macOS with Go Programming Language

Background Story

During this COVID-19 period, we are encouraged to work from home. With the limitations of the existing internet, we often want to find out how much our ping latency. In this case, we can just execute a ping command from the terminal to get our ping latency. But that’s bad, we must switch the window to the terminal if we want to watch our latency. In this article, we will learn how to create your own tools for monitoring ping latency, and it will appear in the system tray.

Requirements

  • Mac OS
  • Go programming language installed

Coding section

We need to import some library

  1. Library to ping
  2. Library to use MacOS system tray

Create the ping function to return the latencies

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
func pingGoogle() (string,error) {
	pinger,err := ping.NewPinger("www.google.com")
	if err != nil {
		return "", errors.New("Network Error")
	}
	pinger.Count = 1
	pinger.Timeout=2 * time.Second
	pinger.Debug=true
	pinger.Run()
	if pinger.Statistics().Rtts==nil{
		return "",errors.New("Network Error")
	}
	return 	strconv.Itoa(int(pinger.Statistics().Rtts[0].Milliseconds())) +" ms",nil
}

In code above, I use google.com as a parameter for measuring latency. After that we need onReady, and onExit function to make your app running in the system tray:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
func onReady(){
	go func() {
		var result string
		var err error
		for {
			result,err=pingGoogle()
			if err!=nil{
				systray.SetTitle(err.Error())
			}else{
				systray.SetTitle(result)
				time.Sleep(2 * time.Second)
			}
		}
	}()
	systray.AddSeparator()
	mQuit := systray.AddMenuItem("Quit", "Quits this app")
	go func() {
		for {
			select {
			case <-mQuit.ClickedCh:
				systray.Quit()
				return
			}
		}
	}()
}

In onReady function above we use Go routine, to ping google.com every two second. After we get the result, we will set it to system tray title. After that, we need to create our main function like the code below:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
package main

import (
	"errors"
	"github.com/getlantern/systray"
	"github.com/sparrc/go-ping"
	"strconv"
	"time"
)
func main(){
	systray.Run(onReady, onExit)
}

Actually, onExit function is not really needed in this tool. That’s a function if you want to gracefully close the tools which is not used in this case.

Building section

If you compile the Golang code, you will get an executable binary not .app extensions like macOS common applications. So I already created a shell script to create macOS application of this tool. Here we go:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#!/bin/bash
echo "`date` : Build starting . . . "
mkdir -p ~/Applications/PingService.app/Contents/MacOS
go mod download
GOMAXPROCS=1 go build -o PingService 2>err.log
if [ -s "err.log" ];then
  echo -e "`date` : Build failed, check err.log "
  exit 2
fi
cp PingService ~/Applications/PingService.app/Contents/MacOS
cat << EOF > ~/Applications/PingService.app/Contents/Info.plist
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
        <key>NSHighResolutionCapable</key>
        <string>True</string>
        <!-- avoid showing the app on the Dock -->
        <key>LSUIElement</key>
        <string>1</string>
</dict>
</plist>
EOF
echo -e "`date` : Build Success . . . Find your app, in your home Applications "

After your build success, you will find your application in your home directory applications. You can use login items in system preferences to use auto startup application after your mac boot.

https://miro.medium.com/max/253/1*laUHtq2h0Pz9ACu1bJad5w.png
System tray macOS

That’s it thank you for reading my article. If you want to see my Github, visit the link below. Hope you guys enjoy and stay productive during this pandemic.