$ go get go.jamescun.com/procfsThis package implements reading structured text-based information about a Linux (or other compatible) system through the procfs filesystem.
The source for this project is hosted on both GitHub and Codeberg.
This package is built with containers in mind, specifically the case where the procfs filesystem mounted at /proc is not the one you want to read, instead you want to read from the procfs of the host system bind mounted elsewhere inside a container.
Initializing to read from the normal /proc directory:
proc := procfs.New()Initializing to read from a different directory:
proc := procfs.From(os.DirFS("/mnt/host/proc"))It can also be used with anything implementing the fs.FS interface.
The procfs filesystem can be read directly as bytes or strings, additionally all the structures contained within this package implement the encoding.TextUnmarshaler interface, which can be used with anything using that interface.
Below are some examples of how to use the procfs package.
Note
Error handling is omitted in these examples for brevity.
package main
import (
"fmt"
"go.jamescun.com/procfs"
)
func main() {
// initialize a procfs reader for the root /proc directory.
proc := procfs.New()
// read the mounted filesystems from /proc/self/mountinfo.
mounts, _ := procfs.GetMounts(proc)
for _, mount := range mounts {
fmt.Printf("Root: %s, Path: %s, Type: %s\n", mount.Root, mount.Path, mount.Type)
}
}package main
import (
"fmt"
"go.jamescun.com/procfs"
)
func main() {
// initialize a procfs reader for the root /proc directory.
proc := procfs.New()
// get statistics from all the network links/interfaces on the system.
stats, _ := procfs.GetLinkStats(proc)
for _, link := range stats {
fmt.Printf("Interface: %s\n", link.Name)
fmt.Printf(" Receive: %d bytes, %d packets\n", link.Rx.Bytes, link.Rx.Packets)
fmt.Printf(" Transmit: %d bytes, %d packets\n", link.Tx.Bytes, link.Tx.Packets)
}
}$ go get go.jamescun.com/procfs/sysctlThis package also contains a wrapper for procfs for reading sysctl Linux Kernel parameters, which are exposed through the procfs filesystem, under the sys/ subdirectory.