How to monitor your CPU and Memory usage in MacOS with Go Programming Language

Kevin Jonathan Harnanta
Mac O’Clock
Published in
2 min readMay 18, 2020

Hi all, thanks for reading my previous article about monitoring ping latency with go programming language. If you are new to my article, welcome and read my another post too, to get more experience. Today I will share my experience with Go to monitoring CPU and memory usage.

source: https://twitter.com/mroth

Why?

The default monitoring tools in Mac OS is the Activity Monitor. But I must search in the Spotlight, then open it when, and if I want to know my CPU and memory usage I must switch my window to see it. There are so many tools that provide better user experience, but these don’t come for free.

So I wanted to create my own utility, so I can monitor my CPU usage and memory without any additional window-application open. It will appear in our system tray. Let’s GO!

Requirements

  • Mac OS
  • Go programming language installed

Coding section

We need to import some libraries:
1. Library to monitoring CPU and Memory usage
2. Library to use MacOS system tray

Create getCpu usage function to get CPU usage every single second.

Create getMemory usage function to get Memory usage.

As usual, we need to create onReady function to manage our system tray

In code above, i call function getData(). The function is to get CPU and memory usage, and pretty print the string. I show you the code below.

And the main function is the same as in my previous article.

That’s it !!! Build this GO code, with my script in the previous article.

Your system tray will show like this

Thank you for reading my article! For more information about this code see my Github repository. Clap this article if you want more information like this.

--

--