Documentation
¶
Overview ¶
Package snmp polls devices over SNMP v1, v2c and v3, receives traps and turns both into metrics and logs. OID sets come from YAML manifests (integrations/snmp/, embedded copies in manifests/), hand-written or compiled from vendor MIBs by package mibc (`wtctl mib compile`).
Everything that names a device or holds a credential (address, community, v3 users and passphrases) comes from the agent's own file. None of it is exported, logged or put in an error.
Index ¶
- Constants
- func EmitLinks(out sink.Sink, local Device, ns []Neighbor, states map[string]PortState, ...)
- func EmitNeighbors(out sink.Sink, local Device, ns []Neighbor, at time.Time)
- func Manifests(dir string) (map[string]Manifest, error)
- func PollNeighbors(ctx context.Context, t Target, out sink.Sink, at time.Time) error
- func PollTarget(ctx context.Context, t Target, ms []Manifest, rates *Rates, out sink.Sink, ...) error
- func Run(ctx context.Context, raw json.RawMessage, out sink.Sink) error
- func RunTopology(ctx context.Context, raw json.RawMessage, out sink.Sink) error
- func SubnetHosts(cidr string) ([]netip.Addr, error)
- func SysInfo(ctx context.Context, t Target) (objectID, name, descr string, err error)
- func TrapName(oid string) string
- func Walk(ctx context.Context, t Target, oids []string, max int) ([]gosnmp.SnmpPDU, error)
- type Config
- type Device
- type Extra
- type Found
- type Item
- type LinkRates
- type Listener
- type Manifest
- type Neighbor
- type PortState
- type Profile
- type Profiles
- type Rates
- type Table
- type Target
- type TrapConfig
- type TrapDef
- type TrapEntry
- type TrapProfileFile
- type TrapProfiles
- type TrapVarbind
- type V3
Constants ¶
const MaxSubnetHosts = 4096
MaxSubnetHosts bounds a discovery sweep.
const MaxVarbinds = 10000
MaxVarbinds caps the varbinds read from one device in one poll.
Variables ¶
This section is empty.
Functions ¶
func EmitLinks ¶
func EmitLinks(out sink.Sink, local Device, ns []Neighbor, states map[string]PortState, at time.Time)
EmitLinks is EmitNeighbors plus the local port's state (linkstatus.go): link.oper_status and link.utilization (0..1) when known.
func EmitNeighbors ¶
EmitNeighbors sends one `network.neighbor` log record per adjacency; the worker turns them into `entity_links` of kind lldp or cdp between `network_device` entities.
func Manifests ¶
Manifests returns the manifests by name: the embedded ones, then any *.yaml in dir (local config only) replacing those of the same name.
func PollNeighbors ¶
PollNeighbors walks one device and emits its adjacencies. Whatever Walk read before an error is used (a device without CDP still gives its LLDP neighbours); only an empty walk is an error.
func PollTarget ¶
func PollTarget(ctx context.Context, t Target, ms []Manifest, rates *Rates, out sink.Sink, at time.Time) error
PollTarget polls one device once and writes its metrics.
func RunTopology ¶
RunTopology is the `topology` agent module: every interval, walk each configured device, each poll under its own deadline.
func SubnetHosts ¶
SubnetHosts lists the host addresses of a CIDR (network and broadcast excluded for IPv4).
func SysInfo ¶
SysInfo reads sysObjectID.0, sysName.0 and sysDescr.0 of a device (discovery, INF-021).
func Walk ¶
Walk reads the subtrees under oids from the device, at most max varbinds in all (max <= 0 or above MaxVarbinds means MaxVarbinds). ctx bounds the whole walk. When a subtree fails the varbinds read so far are still returned together with the first error; error text never contains the address or a credential.
Types ¶
type Config ¶
type Config struct {
Enabled *bool `json:"enabled"`
Interval string `json:"interval"` // default 60s, at least 10s
PollTimeout string `json:"poll_timeout"` // deadline for one device's whole poll, default 30s
ManifestDir string `json:"manifest_dir"` // extra or replacement manifests
ProfileDir string `json:"profile_dir"` // extra or replacement vendor profiles
Targets []Target `json:"targets"`
Trap *TrapConfig `json:"trap"`
}
Config is the snmp module's config. Remote config may only set Enabled and Interval; targets, credentials, the manifest directory and the trap listener exist only in the agent's file.
type Device ¶
type Device struct {
ID, Name, Addr string
}
Device is one end of an adjacency. ID is the stable key (LLDP chassis id, CDP device id, or the operator's target name for the polled device); Addr is the management address a neighbour announced, if any. The polled device's own address is never part of it.
type Extra ¶
type Extra struct {
OID string `yaml:"oid,omitempty"` // the column OID, e.g. ifName 1.3.6.1.2.1.31.1.1.1.1
Attr string `yaml:"attr,omitempty"`
}
Extra is a descriptive attribute read from a column of another table with the same row index.
type Found ¶
Found is one discovered device.
func Discover ¶
func Discover(ctx context.Context, ps Profiles, hosts []netip.Addr, conc int, probe func(context.Context, netip.Addr) (string, error)) ([]Found, error)
Discover asks every host for its sysObjectID (probe) with at most conc probes at once and returns the hosts whose vendor has a profile, in address order.
type Item ¶
type Item struct {
OID string `yaml:"oid,omitempty"` // scalars: the full instance OID
Col string `yaml:"col,omitempty"` // table items: the column sub-identifier
MulCol string `yaml:"mul_col,omitempty"` // table items: multiply by this column of the same row
Metric string `yaml:"metric,omitempty"`
Unit string `yaml:"unit,omitempty"`
Kind string `yaml:"kind,omitempty"` // gauge (default) or counter (reported as a rate)
Scale float64 `yaml:"scale,omitempty"` // 0 means 1
Attrs map[string]string `yaml:"attrs,omitempty"`
// SpeedLimit marks a byte counter whose rate cannot exceed the interface speed (the table's
// SpeedCol); a bigger rate is a counter reset, not traffic.
SpeedLimit bool `yaml:"speed_limit,omitempty"`
}
Item is one metric: a scalar OID, or a column of a table.
type LinkRates ¶
type LinkRates struct {
// contains filtered or unexported fields
}
LinkRates keeps the previous octet counters per device and port, to turn them into rates.
type Listener ¶
type Listener struct {
// contains filtered or unexported fields
}
Listener receives traps over UDP and writes each as an OTLP log record.
func NewListener ¶
func NewListener(c TrapConfig, out sink.Sink) (*Listener, error)
NewListener validates the config. Call Listen, then Serve.
func (*Listener) Dropped ¶
Dropped counts the traps rejected for a reason: "community", "decode" (unknown v3 user, bad authentication or a malformed packet) or "size".
type Manifest ¶
type Manifest struct {
Name string `yaml:"name"`
Tables []Table `yaml:"tables,omitempty"`
Scalars []Item `yaml:"scalars,omitempty"`
Walk []string `yaml:"walk,omitempty"` // subtrees for other modules (LLDP); not polled here
}
Manifest is one MIB's worth of OIDs.
type Neighbor ¶
type Neighbor struct {
Kind string // lldp or cdp
LocalPort string
Remote Device
RemotePort string
}
Neighbor is one adjacency seen from the polled device.
func ParseNeighbors ¶
ParseNeighbors reads LLDP-MIB lldpRemTable and CISCO-CDP-MIB cdpCacheTable rows. A remote is keyed by its chassis id (LLDP) or device id (CDP), else by its system name. Rows come out sorted so a poll is reproducible.
type PortState ¶
type PortState struct {
Oper string // up, down, testing, dormant, notPresent, lowerLayerDown, unknown
Util float64
HasUtil bool
}
PortState is what the map shows for one local port.
type Profile ¶
type Profile struct {
Enterprise int `yaml:"enterprise"`
Vendor string `yaml:"vendor"`
Class string `yaml:"class"`
// VendorManifests lists the vendor's own manifests (CPU, memory, environment), polled on top of the
// class set.
VendorManifests []string `yaml:"manifests,omitempty"`
}
Profile maps a vendor (the enterprise number of sysObjectID) to a device class.
type Profiles ¶
Profiles is a set of vendor profiles keyed by enterprise number.
func LoadProfiles ¶
LoadProfiles reads the embedded vendor profiles, then *.yaml in dir (local config) which replace entries with the same enterprise number.
type Rates ¶
type Rates struct {
// contains filtered or unexported fields
}
Rates turns cumulative counters into per-second rates and keeps the other per-device state that lives across polls (sysUpTime, truncation counts).
shortcut: series are never pruned; a device is capped at 10,000 varbinds per poll, so growth is bounded per device. Add expiry if interfaces churn a lot.
func (*Rates) Forget ¶
Forget drops the counter baselines of one device, so its next samples only re-baseline.
func (*Rates) Truncated ¶
Truncated counts one cut-off walk of dev's table and returns the running total and whether it should be logged now (once per device and table per hour).
func (*Rates) Update ¶
Update records v for the series key and returns the rate since the last call. bits is 32 or 64. A 32-bit counter that went down is taken to have wrapped once (emit checks the result against the interface speed and sysUpTime, which catch resets); a 64-bit one that went down was reset, so that interval yields no rate.
type Table ¶
type Table struct {
Name string `yaml:"name"`
OID string `yaml:"oid,omitempty"`
LabelCol string `yaml:"label_col,omitempty"` // column holding the row's name; the row index otherwise
LabelAttr string `yaml:"label_attr,omitempty"` // attribute that carries the label
Items []Item `yaml:"items,omitempty"`
Sensor bool `yaml:"sensor,omitempty"` // an ENTITY-SENSOR-MIB table (type, scale, precision, value)
NameOID string `yaml:"name_oid,omitempty"` // sensor rows are named from this column
// Priority orders the walks of one poll, highest first, so that when the varbind cap is hit
// the low-value tables are the ones cut.
Priority int `yaml:"priority,omitempty"`
Extra []Extra `yaml:"extra,omitempty"`
SpeedCol string `yaml:"speed_col,omitempty"` // column with the interface speed
SpeedScale float64 `yaml:"speed_scale,omitempty"` // multiplier to bit/s (ifHighSpeed is in Mbit/s); 0 means 1
}
Table is a conceptual table walked from OID.
type Target ¶
type Target struct {
Name string `json:"name"` // shown as snmp.device; the address is never exported
Address string `json:"address"` // host or host:port (default port 161)
Version string `json:"version"` // "1", "2c" (default) or "3"
Community string `json:"community"` // v1 and v2c
V3 *V3 `json:"v3"`
Timeout string `json:"timeout"` // per request, default 3s
Retries int `json:"retries"` // default 1
Manifests []string `json:"manifests"`
}
Target is one polled device. It is read from the agent's own file only.
type TrapConfig ¶
type TrapConfig struct {
Listen string `json:"listen"` // default 0.0.0.0:162
Communities []string `json:"communities"` // accepted for v1 and v2c; others are dropped
V3 []V3 `json:"v3"` // accepted v3 users; others are dropped
Sources map[string]string `json:"sources"` // sender IP -> device name (snmp.device)
MaxPacket int `json:"max_packet"` // bytes; larger datagrams are dropped (default 4096)
// RatePerSource is the most traps per second accepted from one sender IP (default 100);
// the excess is dropped and counted as "rate".
RatePerSource int `json:"rate_per_source"`
// ProfileDir holds extra per-vendor trap profiles (*.yaml, e.g. from `wtctl mib compile`).
ProfileDir string `json:"profile_dir"`
}
TrapConfig configures the trap receiver. Communities and users are local-file only.
type TrapDef ¶
type TrapDef struct {
OID string `yaml:"oid"`
Name string `yaml:"name"`
Severity string `yaml:"severity,omitempty"` // INFO, WARN, ERROR; default WARN
Description string `yaml:"description,omitempty"`
Varbinds []TrapVarbind `yaml:"varbinds,omitempty"`
}
TrapDef is one notification.
type TrapProfileFile ¶
TrapProfileFile is one vendor's (or one MIB's) file.
type TrapProfiles ¶
TrapProfiles maps a trap OID (no leading dot) to its definition.
func LoadTrapProfiles ¶
func LoadTrapProfiles(dir string) (TrapProfiles, error)
LoadTrapProfiles reads the embedded profiles, then *.yaml in dir (local config only), whose entries replace built-in ones with the same OID.
type TrapVarbind ¶
type TrapVarbind struct {
OID string `yaml:"oid"`
Name string `yaml:"name"`
Enum map[string]string `yaml:"enum,omitempty"` // integer value -> label
}
TrapVarbind names one varbind column (the instance suffix is ignored when matching).
func (TrapVarbind) Label ¶
func (v TrapVarbind) Label(val string) string
Label renders a varbind value, with its enum label when the profile has one.
type V3 ¶
type V3 struct {
// EngineID is the sender's snmpEngineID in hex. Trap users need it (the sender is the
// authoritative engine, so keys are localized with it); polling finds it by discovery.
EngineID string `json:"engine_id"`
User string `json:"user"`
AuthProto string `json:"auth_protocol"` // sha, sha224, sha256, sha384, sha512
AuthPass string `json:"auth_passphrase"`
PrivProto string `json:"priv_protocol"` // aes, aes192, aes256, aes192c, aes256c
PrivPass string `json:"priv_passphrase"`
}
V3 is one SNMPv3 user. Used for polling a device and, in the trap config, for the users whose traps are accepted.
Source Files
¶
Directories
¶
| Path | Synopsis |
|---|---|
|
Package mibc compiles SMIv1/SMIv2 MIB modules into the agent's SNMP manifests and trap profiles (INF-014, INF-015, gap G-20), so a vendor MIB can be polled and its traps named without hand-written YAML.
|
Package mibc compiles SMIv1/SMIv2 MIB modules into the agent's SNMP manifests and trap profiles (INF-014, INF-015, gap G-20), so a vendor MIB can be polled and its traps named without hand-written YAML. |