Table of Contents
- Introduction
- Prerequisites
- Setup
- Creating the CLI Tool
- Monitoring CPU Usage
- Monitoring Memory Usage
- Conclusion
Introduction
In this tutorial, we will be creating a command-line interface (CLI) tool using the Go programming language. The purpose of this tool is to monitor system resources such as CPU and memory usage. By the end of this tutorial, you will have a basic understanding of Go and be able to create your own CLI tools for system monitoring.
Prerequisites
Before starting this tutorial, you should have:
- Basic knowledge of the Go programming language
- Go installed on your machine (version 1.16 or above)
Setup
To begin, let’s set up our project folder and initialize a Go module. Open a terminal and perform the following steps:
- Create a new directory for your project:
mkdir system-monitor
-
Navigate into the project directory:
cd system-monitor
- Initialize a new Go module:
go mod init github.com/your-username/system-monitor
Creating the CLI Tool
First, let’s create the basic structure of our CLI tool. Create a new file main.go
in the project directory and open it in your preferred text editor.
package main
import (
"fmt"
"os"
)
func main() {
fmt.Println("System Monitor")
}
Save the file and return to the terminal. Build and run the program using the command go run main.go
. You should see the output System Monitor
printed to the console.
Monitoring CPU Usage
Next, let’s add functionality to monitor CPU usage. We will use the runtime
package in Go to retrieve information about the current CPU usage.
Add the following code to the main
function in main.go
:
package main
import (
"fmt"
"os"
"runtime"
)
func main() {
fmt.Println("System Monitor")
cpuUsage := getCurrentCPUUsage()
fmt.Printf("Current CPU Usage: %f%%\n", cpuUsage)
}
func getCurrentCPUUsage() float64 {
cpuUsage := new(runtime.MemStats)
runtime.ReadMemStats(cpuUsage)
return cpuUsage.CurCPU / 1000000
}
In this code, we import the runtime
package and define a new function getCurrentCPUUsage
which retrieves the current CPU usage using the ReadMemStats
function from the runtime
package.
Save the file and run the program again using go run main.go
. You should now see the CPU usage percentage printed to the console.
Monitoring Memory Usage
To monitor memory usage, we will again use the runtime
package in Go. Modify the main
function in main.go
as follows:
package main
import (
"fmt"
"os"
"runtime"
)
func main() {
fmt.Println("System Monitor")
cpuUsage := getCurrentCPUUsage()
fmt.Printf("Current CPU Usage: %f%%\n", cpuUsage)
memUsage := getCurrentMemoryUsage()
fmt.Printf("Current Memory Usage: %.2f MB\n", memUsage)
}
func getCurrentCPUUsage() float64 {
cpuUsage := new(runtime.MemStats)
runtime.ReadMemStats(cpuUsage)
return cpuUsage.CurCPU / 1000000
}
func getCurrentMemoryUsage() float64 {
memUsage := new(runtime.MemStats)
runtime.ReadMemStats(memUsage)
return float64(memUsage.Alloc) / 1024 / 1024
}
Here, we added a new function getCurrentMemoryUsage
which retrieves the current memory usage using the Alloc
field from the MemStats
struct in the runtime
package.
Save the file and run the program again. You should now see both the CPU usage percentage and the memory usage in megabytes printed to the console.
Conclusion
In this tutorial, we have learned how to create a CLI tool for monitoring system resources in Go. We covered the basics of Go programming and demonstrated how to retrieve and display CPU and memory usage. You can further enhance this tool by adding functionality to monitor other system resources.
Feel free to explore the Go standard library and additional packages to expand the capabilities of your tool. Happy coding!
With this tutorial, you have learned:
- How to create a CLI tool using Go
- How to retrieve and display CPU usage in a Go program
- How to retrieve and display memory usage in a Go program
By combining these concepts, you can now monitor system resources and create more advanced CLI tools with Go.