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 ¶
- Variables
- func MemValue(entries []MemEntry, key string) (uint64, bool)
- type BoardPorts
- type Conntrack
- type FS
- func (f FS) BoardPorts() BoardPorts
- func (f FS) Conntrack() Conntrack
- func (f FS) DefaultRouteInterfaces() ([]string, error)
- func (f FS) Loadavg() (Load, error)
- func (f FS) Meminfo() ([]MemEntry, error)
- func (f FS) NetDev() ([]NetDev, error)
- func (f FS) Ports(o PortOptions) []Port
- func (f FS) Read(p string) ([]byte, error)
- func (f FS) ReadDir(p string) ([]os.DirEntry, error)
- func (f FS) ReadTrim(p string) (string, error)
- func (f FS) Snmp() (map[string]map[string]int64, error)
- type Load
- type MemEntry
- type NetDev
- type Port
- type PortOptions
- type PortReader
- type VirtualPolicy
Constants ¶
This section is empty.
Variables ¶
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 ¶
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 ¶
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) DefaultRouteInterfaces ¶
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) NetDev ¶
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).
type Load ¶
type Load struct{ Load1, Load5, Load15 float64 }
Load is /proc/loadavg's first three fields.
type MemEntry ¶
MemEntry is one /proc/meminfo line: its raw key ("MemTotal", "Active(anon)") and value, in bytes when the file says kB.
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" )