hoststat

package
v0.2.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 22, 2026 License: MIT Imports: 13 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package hoststat reads the kernel's view of the host from /proc: load, memory, interface counters, connection tracking, SNMP counters and the default routes. Every reader goes through FS so tests (and the daemons' tests) can point it at a fixture tree instead of /.

Index

Constants

This section is empty.

Variables

View Source
var NetDevFields = [16]string{
	"receive_bytes", "receive_packets", "receive_errs", "receive_drop",
	"receive_fifo", "receive_frame", "receive_compressed", "receive_multicast",
	"transmit_bytes", "transmit_packets", "transmit_errs", "transmit_drop",
	"transmit_fifo", "transmit_colls", "transmit_carrier", "transmit_compressed",
}

NetDevFields names the sixteen counters of /proc/net/dev in file order.

Functions

func MemValue

func MemValue(entries []MemEntry, key string) (uint64, bool)

MemValue finds one key in Meminfo's result.

Types

type BoardPorts added in v0.2.0

type BoardPorts struct{ LAN, WAN []string }

BoardPorts is /etc/board.json's port roles, in board order.

type Conntrack

type Conntrack struct {
	Entries, Limit       uint64
	HasEntries, HasLimit bool
}

Conntrack is the connection-tracking table fill. Either half may be missing (no nf_conntrack module, or a container that hides the limit).

type FS

type FS struct{ Root string }

FS reads files under a root.

func (FS) BoardPorts added in v0.2.0

func (f FS) BoardPorts() BoardPorts

BoardPorts reads the port roles from /etc/board.json (OpenWrt): network.lan and network.wan name their ports as "device" (one name) and/or "ports" (a list), in the order of the case. A missing or malformed file, or a section of the wrong shape, yields no names for that role.

func (FS) Conntrack

func (f FS) Conntrack() Conntrack

Conntrack reads nf_conntrack_count and nf_conntrack_max.

func (FS) DefaultRouteInterfaces

func (f FS) DefaultRouteInterfaces() ([]string, error)

DefaultRouteInterfaces lists the interfaces that hold a default route in the main table: IPv4 rows of /proc/net/route with destination and mask 0, IPv6 rows of /proc/net/ipv6_route for ::/0. Routes that are down or reject routes (and everything on lo) do not count. Sorted and unique. An error only when neither file can be read.

func (FS) Loadavg

func (f FS) Loadavg() (Load, error)

Loadavg reads /proc/loadavg.

func (FS) Meminfo

func (f FS) Meminfo() ([]MemEntry, error)

Meminfo reads /proc/meminfo in file order. Lines that do not parse are skipped.

func (FS) NetDev

func (f FS) NetDev() ([]NetDev, error)

NetDev reads /proc/net/dev in file order. Lines with fewer than sixteen counters are skipped; a counter that does not parse reads as 0.

func (FS) Ports added in v0.2.0

func (f FS) Ports(o PortOptions) []Port

Ports is the one-shot form (CLI, tests).

func (FS) Read

func (f FS) Read(p string) ([]byte, error)

Read returns a file's contents.

func (FS) ReadDir

func (f FS) ReadDir(p string) ([]os.DirEntry, error)

ReadDir lists a directory.

func (FS) ReadTrim

func (f FS) ReadTrim(p string) (string, error)

ReadTrim returns a file's contents without surrounding whitespace.

func (FS) Snmp

func (f FS) Snmp() (map[string]map[string]int64, error)

Snmp reads /proc/net/snmp: per protocol ("Tcp", "Udp", …) a header line of names and a line of values, returned as protocol → name → value.

type Load

type Load struct{ Load1, Load5, Load15 float64 }

Load is /proc/loadavg's first three fields.

type MemEntry

type MemEntry struct {
	Key   string
	Value uint64
}

MemEntry is one /proc/meminfo line: its raw key ("MemTotal", "Active(anon)") and value, in bytes when the file says kB.

type NetDev

type NetDev struct {
	Name     string
	Counters [16]uint64
}

NetDev is one interface's /proc/net/dev line.

func (NetDev) RxBytes

func (d NetDev) RxBytes() uint64

RxBytes is the receive byte counter.

func (NetDev) TxBytes

func (d NetDev) TxBytes() uint64

TxBytes is the transmit byte counter.

type Port added in v0.2.0

type Port struct {
	Name           string  `json:"name"`
	Label          string  `json:"label,omitempty"`
	Role           string  `json:"role,omitempty"`   // "wan" | "lan"
	Medium         string  `json:"medium,omitempty"` // "copper" | "sfp" | "virtual" | "wireless"
	MAC            string  `json:"mac,omitempty"`
	AdminUp        *bool   `json:"adminUp,omitempty"`
	Carrier        *bool   `json:"carrier,omitempty"`
	Operstate      string  `json:"operstate,omitempty"`
	SpeedMbps      *int    `json:"speedMbps,omitempty"`
	Duplex         string  `json:"duplex,omitempty"` // "full" | "half"
	CarrierChanges *uint64 `json:"carrierChanges,omitempty"`
}

Port is one Ethernet port of the host as the kernel describes it (docs/infrastructure-view.md §2.1 in perch-controller). Fields the kernel does not answer for are left out rather than guessed.

type PortOptions added in v0.2.0

type PortOptions struct {
	// Virtual is the virtual NIC policy; any value other than VirtualNever
	// and VirtualAlways means VirtualAuto.
	Virtual VirtualPolicy
	// WAN marks extra interfaces as role "wan" (the collector passes the
	// interfaces its gateway stats count).
	WAN []string
	// Max caps the result (default 64).
	Max int
}

PortOptions tune what Read reports.

type PortReader added in v0.2.0

type PortReader struct {
	FS      FS
	Options PortOptions
	TTL     time.Duration
	// contains filtered or unexported fields
}

PortReader reads ports repeatedly, caching the facts that do not change (device type, label, medium, MAC) for TTL (default 5 min) per name+ifindex, and board.json for as long, so a 5 s push costs one ReadDir and ~7 small reads per port.

Read is safe for concurrent use. Options may be changed between reads (not during one): roles are worked out on every read from the cached board.json and the current Options, so a new WAN list applies at once.

func (*PortReader) Read added in v0.2.0

func (r *PortReader) Read() []Port

Read lists the host's ports in display order: role "wan" first, then board.json's LAN order, then the rest in natural order ("lan2" before "lan10"), capped at Options.Max. board.json roles apply to hardware ports only; Options.WAN to every port. It never fails: a file it cannot read leaves its field out. The result is nil only when /sys/class/net itself cannot be listed; a host without ports gets a non-nil, empty slice, so a caller can tell "could not look" from "looked and found none".

type VirtualPolicy added in v0.2.0

type VirtualPolicy string

VirtualPolicy decides whether NICs whose peer is in another network namespace count as ports.

const (
	// VirtualAuto reports them only when the host has no wired hardware
	// port (an LTE modem does not count): a router in a container has
	// nothing else (§2).
	VirtualAuto   VirtualPolicy = ""
	VirtualNever  VirtualPolicy = "never"
	VirtualAlways VirtualPolicy = "always"
)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL