Skip to main content
Get up and running with gopsutil by building a simple system monitor.

Prerequisites

  • Go 1.18 or later installed
  • Basic familiarity with Go

Install gopsutil

1

Add gopsutil to your project

go get github.com/shirou/gopsutil/v4
2

Create a new Go file

Create a file named monitor.go:
monitor.go
package main

import (
    "fmt"
    "github.com/shirou/gopsutil/v4/mem"
)

func main() {
    v, _ := mem.VirtualMemory()

    // Almost every return value is a struct
    fmt.Printf("Total: %v, Free:%v, UsedPercent:%f%%\n", 
        v.Total, v.Free, v.UsedPercent)

    // Convert to JSON. String() is also implemented
    fmt.Println(v)
}
This example is from the gopsutil README.
3

Run your program

go run monitor.go
You’ll see output like:
Total: 3179569152, Free:284233728, UsedPercent:84.508194%
{"total":3179569152,"available":492572672,"used":2895335424,"usedPercent":84.50819439828305,...}

Monitor CPU Usage

Track CPU information and usage percentages:
package main

import (
    "fmt"
    "github.com/shirou/gopsutil/v4/cpu"
)

func main() {
    // Get CPU info
    info, _ := cpu.Info()
    fmt.Printf("CPU: %s\n", info[0].ModelName)
    fmt.Printf("Cores: %d\n", info[0].Cores)

    // Get CPU usage percentage
    percent, _ := cpu.Percent(0, false)
    fmt.Printf("CPU Usage: %.2f%%\n", percent[0])

    // Get per-CPU usage
    perCPU, _ := cpu.Percent(0, true)
    for i, p := range perCPU {
        fmt.Printf("CPU %d: %.2f%%\n", i, p)
    }
}
Use cpu.Percent(0, false) to get cached values for better performance. Non-zero intervals will block for that duration.

Monitor Memory

Monitor both virtual and swap memory:
package main

import (
    "fmt"
    "github.com/shirou/gopsutil/v4/mem"
)

func main() {
    // Virtual memory
    v, _ := mem.VirtualMemory()
    fmt.Printf("Total: %v GB\n", v.Total/1024/1024/1024)
    fmt.Printf("Used: %v GB\n", v.Used/1024/1024/1024)
    fmt.Printf("Usage: %.2f%%\n", v.UsedPercent)

    // Swap memory
    s, _ := mem.SwapMemory()
    fmt.Printf("Swap Total: %v GB\n", s.Total/1024/1024/1024)
    fmt.Printf("Swap Used: %v GB\n", s.Used/1024/1024/1024)
}
Example output:
Total: 16 GB
Used: 12 GB
Usage: 75.32%
Swap Total: 8 GB
Swap Used: 2 GB

Monitor Disk Usage

Check disk partitions and usage:
package main

import (
    "fmt"
    "github.com/shirou/gopsutil/v4/disk"
)

func main() {
    // Get all partitions
    partitions, _ := disk.Partitions(false)
    
    for _, partition := range partitions {
        fmt.Printf("\nDisk: %s\n", partition.Device)
        fmt.Printf("Mount: %s\n", partition.Mountpoint)
        fmt.Printf("Type: %s\n", partition.Fstype)
        
        // Get usage stats
        usage, _ := disk.Usage(partition.Mountpoint)
        fmt.Printf("Total: %v GB\n", usage.Total/1024/1024/1024)
        fmt.Printf("Used: %v GB (%.2f%%)\n", 
            usage.Used/1024/1024/1024, usage.UsedPercent)
        fmt.Printf("Free: %v GB\n", usage.Free/1024/1024/1024)
    }
}
Pass the mountpoint (not device) to disk.Usage(). For example, use "/" instead of "/dev/sda1".

Complete System Monitor

Here’s a complete example combining CPU, memory, and disk monitoring:
monitor.go
package main

import (
    "fmt"
    "github.com/shirou/gopsutil/v4/cpu"
    "github.com/shirou/gopsutil/v4/disk"
    "github.com/shirou/gopsutil/v4/host"
    "github.com/shirou/gopsutil/v4/mem"
)

func main() {
    // Host info
    hostInfo, _ := host.Info()
    fmt.Printf("=== System: %s ===\n", hostInfo.Hostname)
    fmt.Printf("OS: %s %s\n", hostInfo.Platform, hostInfo.PlatformVersion)
    fmt.Printf("Uptime: %d seconds\n\n", hostInfo.Uptime)

    // CPU
    cpuPercent, _ := cpu.Percent(0, false)
    fmt.Printf("=== CPU ===\n")
    fmt.Printf("Usage: %.2f%%\n\n", cpuPercent[0])

    // Memory
    v, _ := mem.VirtualMemory()
    fmt.Printf("=== Memory ===\n")
    fmt.Printf("Total: %v GB\n", v.Total/1024/1024/1024)
    fmt.Printf("Used: %v GB (%.2f%%)\n", 
        v.Used/1024/1024/1024, v.UsedPercent)
    fmt.Printf("Available: %v GB\n\n", v.Available/1024/1024/1024)

    // Disk
    usage, _ := disk.Usage("/")
    fmt.Printf("=== Disk (/) ===\n")
    fmt.Printf("Total: %v GB\n", usage.Total/1024/1024/1024)
    fmt.Printf("Used: %v GB (%.2f%%)\n", 
        usage.Used/1024/1024/1024, usage.UsedPercent)
    fmt.Printf("Free: %v GB\n", usage.Free/1024/1024/1024)
}
Example output:
=== System: my-computer ===
OS: ubuntu 22.04
Uptime: 123456 seconds

=== CPU ===
Usage: 23.50%

=== Memory ===
Total: 16 GB
Used: 12 GB (75.00%)
Available: 4 GB

=== Disk (/) ===
Total: 500 GB
Used: 350 GB (70.00%)
Free: 150 GB

Error Handling

Always handle errors in production code:
package main

import (
    "fmt"
    "log"
    "github.com/shirou/gopsutil/v4/mem"
)

func main() {
    v, err := mem.VirtualMemory()
    if err != nil {
        log.Fatalf("Failed to get memory info: %v", err)
    }

    fmt.Printf("Memory Usage: %.2f%%\n", v.UsedPercent)
}
Some functions may return ErrNotImplementedError on platforms where the feature is not supported. Always check errors.

Next Steps

Now that you understand the basics, explore more features:

Build docs developers (and LLMs) love