Skip to main content
The gopsutil library provides cross-platform APIs for retrieving system information and statistics. This documentation covers all available packages and their functions.

Available Packages

CPU

Retrieve CPU times, information, core counts, and utilization percentages

Memory

Access system memory and swap statistics

Disk

Get disk usage, partitions, and I/O statistics

Process

Monitor and manage system processes

Network

Retrieve network interface and connection information

Host

Get host system information, uptime, and boot time

Key Features

  • Cross-Platform: Works on Linux, Windows, macOS, FreeBSD, OpenBSD, and more
  • Context Support: All functions have context-aware variants for cancellation and timeouts
  • JSON Serialization: All structs support JSON marshaling for easy integration
  • Pure Go: No CGO dependencies for most platforms

Installation

go get github.com/shirou/gopsutil/v4

Basic Usage

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

func main() {
    // Get CPU usage percentage
    percent, _ := cpu.Percent(0, false)
    fmt.Printf("CPU Usage: %.2f%%\n", percent[0])
    
    // Get memory statistics
    vmStat, _ := mem.VirtualMemory()
    fmt.Printf("Memory Usage: %.2f%%\n", vmStat.UsedPercent)
}

Context Support

All major functions support context for better control:
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()

info, err := cpu.InfoWithContext(ctx)
if err != nil {
    // Handle timeout or cancellation
}

Build docs developers (and LLMs) love