Skip to main content

Overview

Linux has the most complete and mature support in gopsutil, with nearly all features fully implemented. The library supports multiple architectures and provides extensive system monitoring capabilities.

Supported Architectures

i386

32-bit Intel/AMD processors

amd64

64-bit Intel/AMD processors (most common)

arm

ARM processors including Raspberry Pi

RISC-V

Partial support - host information only

Feature Support

Fully Supported Features

Linux provides complete support for the following features:

CPU Metrics

  • cpu.Times() - CPU time statistics (user, system, idle, etc.)
  • cpu.Count() - Number of logical and physical CPUs
  • cpu.Percent() - CPU utilization percentage
  • cpu.TimesPercent() - Per-CPU time percentages
  • cpu.Info() - Detailed CPU information including:
    • VendorID (e.g., “GenuineIntel”)
    • Family, Model, Stepping
    • PhysicalID and CoreID
    • ModelName (e.g., “Intel(R) Core(TM) i7-2640M CPU @ 2.80GHz”)
    • CPU frequency (MHz)
    • Cache size
    • CPU flags
    • Microcode version

Memory Metrics

  • mem.VirtualMemory() - Physical memory statistics
  • mem.SwapMemory() - Swap memory information
  • Platform-specific extended information via ExLinux (see below)

Disk Metrics

  • disk.Partitions() - List all disk partitions
  • disk.Usage() - Disk usage statistics per partition
  • disk.IOCounters() - I/O statistics with device mapper label support

Network Metrics

  • net.IOCounters() - Network interface statistics
  • net.Connections() - Active network connections
  • net.Interfaces() - Network interface information
  • net.ProtoCounters() - Protocol-level statistics (IP, TCP, UDP, etc.)
    • Sourced from /proc/net/snmp
    • Linux-only feature

System Information

  • host.Info() - Comprehensive host information:
    • Hostname
    • Uptime
    • Number of processes
    • OS (“linux”)
    • Platform (e.g., “ubuntu”, “arch”, “rhel”)
    • PlatformFamily (e.g., “debian”, “rhel”)
    • PlatformVersion
    • VirtualizationSystem (e.g., “kvm”, “docker”, “lxc”)
    • VirtualizationRole (“guest” or “host”)
  • host.BootTime() - System boot time (Unix timestamp)
  • host.Users() - Currently logged-in users
  • load.Avg() - System load averages (1, 5, 15 minutes)

Process Information

Linux has the most complete process monitoring support:
  • Basic: pid, ppid, name, cmdline, create_time, status, cwd, exe
  • User/Group: uids, gids, username
  • Resources: memory_info, memory_maps, memory_percent, cpu_times, cpu_percent
  • I/O: io_counters, open_files, num_fds
  • Context: terminal, nice, num_ctx_switches, rlimit
  • Threads: num_threads, threads
  • Relations: parent, children, connections
  • Signals: send_signal, suspend, resume, terminate, kill
  • Stats: page_faults

Linux-Exclusive Features

These features are only available on Linux:

Docker/Container Support

import "github.com/shirou/gopsutil/v4/docker"

// Get list of Docker container IDs
containers, err := docker.GetDockerIDList()

// Get cgroup CPU stats for a container
cpuStats, err := docker.CgroupCPU(containerID)

// Get cgroup memory stats for a container
memStats, err := docker.CgroupMem(containerID)

Netfilter Connection Tracking

import "github.com/shirou/gopsutil/v4/net"

// Get netfilter conntrack statistics
// Sourced from /proc/sys/net/netfilter/nf_conntrack_count
stats, err := net.FilterCounters()

Network Protocol Statistics

import "github.com/shirou/gopsutil/v4/net"

// Get system-wide network protocol stats
// Includes IP, TCP, UDP, ICMP, etc.
protos, err := net.ProtoCounters(nil)

Device Mapper Labels

import "github.com/shirou/gopsutil/v4/disk"

// IOCounters includes Label field for device mapper names
counters, err := disk.IOCounters()
for name, counter := range counters {
    fmt.Printf("%s: %s\n", name, counter.Label)
}

Platform-Specific Information (ExLinux)

Linux provides extended memory information through the ExLinux struct:
import "github.com/shirou/gopsutil/v4/mem"

ex := mem.NewExLinux()
v, err := ex.VirtualMemory()
if err != nil {
    panic(err)
}

// Access Linux-specific fields
fmt.Println("Buffers:", v.Buffers)
fmt.Println("Cached:", v.Cached)
fmt.Println("Active:", v.Active)
fmt.Println("Inactive:", v.Inactive)
See the Platform-Specific Information page for more details on Ex structs.

Environment Variables

Linux support includes flexible path configuration via environment variables:
VariableDefaultDescription
HOST_PROC/procProcess information filesystem
HOST_SYS/sysSystem information filesystem
HOST_ETC/etcConfiguration files
HOST_VAR/varVariable data
HOST_RUN/runRuntime data
HOST_DEV/devDevice files
HOST_ROOT/Root filesystem
HOST_PROC_MOUNTINFO/proc/N/mountinfoMount information

Using Environment Variables

# Monitor a different system's proc filesystem
export HOST_PROC=/mnt/remote/proc
export HOST_SYS=/mnt/remote/sys
./myapp

Using Context (v3.23.6+)

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

ctx := context.WithValue(context.Background(),
    common.EnvKey, common.EnvMap{
        common.HostProcEnvKey: "/custom/proc",
        common.HostSysEnvKey:  "/custom/sys",
    },
)

v, err := mem.VirtualMemoryWithContext(ctx)
Priority order: Context values → Environment variables → Default paths

Known Limitations

Temperature Sensors

import "github.com/shirou/gopsutil/v4/sensors"

temps, err := sensors.TemperaturesWithContext(ctx)
Temperature monitoring requires:
  • Appropriate kernel modules loaded (e.g., coretemp, k10temp)
  • Read permissions for /sys/class/hwmon/
  • May not work in containers without proper device access

Container Environments

When running inside containers:
  • Some /proc metrics may be virtualized or unavailable
  • Host system information may not be accessible
  • Disk partition information may be limited
  • Consider mounting host paths if full system monitoring is needed

RISC-V Support

RISC-V architecture support is currently limited to:
  • Host information via host.Info()
  • Other features are not yet implemented

Performance Considerations

Reading from /proc and /sys can be I/O intensive. Consider:
  • Caching values when appropriate
  • Using WithContext functions with timeouts
  • Implementing rate limiting for frequent polling

Boot Time Caching

import "github.com/shirou/gopsutil/v4/host"

// Enable boot time caching (use with caution)
host.EnableBootTimeCache = true
Boot time caching may cause issues if the system time is adjusted by NTP after boot. Only enable if you understand the implications.

Best Practices

Error Handling

import "github.com/shirou/gopsutil/v4/cpu"

percent, err := cpu.Percent(time.Second, false)
if err != nil {
    // Handle error appropriately
    log.Printf("Failed to get CPU percent: %v", err)
    return
}

Context Usage

ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()

v, err := mem.VirtualMemoryWithContext(ctx)

Monitoring Multiple Metrics

type SystemMetrics struct {
    CPU    float64
    Memory *mem.VirtualMemoryStat
    Disk   *disk.UsageStat
}

func collectMetrics(ctx context.Context) (*SystemMetrics, error) {
    metrics := &SystemMetrics{}
    
    cpu, err := cpu.PercentWithContext(ctx, time.Second, false)
    if err != nil {
        return nil, err
    }
    metrics.CPU = cpu[0]
    
    metrics.Memory, err = mem.VirtualMemoryWithContext(ctx)
    if err != nil {
        return nil, err
    }
    
    metrics.Disk, err = disk.UsageWithContext(ctx, "/")
    if err != nil {
        return nil, err
    }
    
    return metrics, nil
}

Platform Overview

Compare Linux support with other platforms

Platform-Specific

Learn about ExLinux structs

Build docs developers (and LLMs) love