snmp

package
v0.0.0-...-efbc44a Latest Latest
Warning

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

Go to latest
Published: Aug 2, 2026 License: GPL-2.0 Imports: 5 Imported by: 0

Documentation

Overview

Package snmp exports keepalived's MIBs through an AgentX subagent.

The shape of the problem

SNMP is a flat, ordered namespace. A manager asks for one name, or asks for the name after a name, and walks the whole tree that way. What this package has instead is a set of instances, each with a handful of attributes, and the job is to project one onto the other so that a walk visits every value once, in order, and terminates.

The projection is done by materialising the whole namespace as a sorted slice on each request rather than by clever traversal. That is not the fastest way and it is the right one here: a router has tens of instances, a walk happens when a monitoring system polls, and the alternative — an incremental traversal over a table whose rows can change between two GetNexts — is where walks that loop forever come from.

Index

Constants

This section is empty.

Variables

View Source
var (
	// VirtualServerTable is check(3).virtualServerTable(3).entry(1).
	VirtualServerTable = CheckRoot.Append(3, 1)
	// RealServerTable is check(3).realServerTable(4).entry(1).
	RealServerTable = CheckRoot.Append(4, 1)
)

The two checker tables.

View Source
var (
	// KeepalivedRoot is the enterprise subtree, 1.3.6.1.4.1.9586.100.5.
	KeepalivedRoot = agentx.OID{1, 3, 6, 1, 4, 1, 9586, 100, 5}
	// VRRPRoot is its VRRP branch.
	VRRPRoot = KeepalivedRoot.Append(2)
	// CheckRoot is its healthchecker branch.
	CheckRoot = KeepalivedRoot.Append(3)

	// SyncGroupTable and InstanceTable are the two tables this package
	// serves under VRRPRoot.
	SyncGroupTable = VRRPRoot.Append(1, 1)
	InstanceTable  = VRRPRoot.Append(3, 1)

	// VRRPv2Root is VRRP-MIB (RFC 2787), mib-2 68.
	VRRPv2Root = agentx.OID{1, 3, 6, 1, 2, 1, 68}
	// OperTable is its vrrpOperTable, vrrpOperations(1).vrrpOperTable(3).
	OperTable = VRRPv2Root.Append(1, 3, 1)

	// VRRPv3Root is VRRPV3-MIB (RFC 6527), mib-2 207.
	VRRPv3Root = agentx.OID{1, 3, 6, 1, 2, 1, 207}
	// OperationsTable is its vrrpv3OperationsTable.
	OperationsTable = VRRPv3Root.Append(1, 1, 1, 1)
)

The MIB roots (keepalived/include/snmp.h:52, vrrp_snmp.c:135,388,450).

View Source
var (
	// TrapVRRPInstance and TrapVRRPSyncGroup are keepalived's own
	// (vrrp_snmp.c:3073,3152).
	TrapVRRPSyncGroup = VRRPRoot.Append(10, 0, 1)
	TrapVRRPInstance  = VRRPRoot.Append(10, 0, 2)

	// TrapNewMaster and TrapAuthFailure are RFC 2787's
	// (vrrp_snmp.c:3757,3798).
	TrapNewMaster   = VRRPv2Root.Append(0, 1)
	TrapAuthFailure = VRRPv2Root.Append(0, 2)

	// TrapV3NewMaster is RFC 6527's.
	TrapV3NewMaster = VRRPv3Root.Append(0, 1)
)

Trap OIDs.

Functions

This section is empty.

Types

type Agent

type Agent struct {
	// contains filtered or unexported fields
}

Agent is the running subagent.

func New

func New(cfg Config) (*Agent, error)

New connects to the master agent and registers the enabled subtrees.

func (*Agent) Close

func (a *Agent) Close() error

Close ends the session.

func (*Agent) Describe

func (a *Agent) Describe() string

Describe reports what was registered, for the startup log.

func (*Agent) DroppedTraps

func (a *Agent) DroppedTraps() uint64

DroppedTraps reports how many notifications were discarded because the master agent was not keeping up.

func (*Agent) InstanceChanged

func (a *Agent) InstanceChanged(in Instance)

InstanceChanged sends the traps a state transition produces.

Which traps depends on which MIBs are exported, and they are not interchangeable: a manager configured for the standard VRRP-MIB is watching for vrrpTrapNewMaster and will not recognise keepalived's enterprise notification, and the reverse. Both go out when both are enabled, which is what C does.

func (*Agent) Serve

func (a *Agent) Serve() error

Serve answers the master until the connection ends.

func (*Agent) SyncGroupChanged

func (a *Agent) SyncGroupChanged(g SyncGroup)

SyncGroupChanged sends the sync group trap.

func (*Agent) Update

func (a *Agent) Update(instances []Instance, groups []SyncGroup)

Update replaces what the MIB reports.

func (*Agent) UpdateCheckers

func (a *Agent) UpdateCheckers(servers []VirtualServer, backends []RealServer)

UpdateCheckers replaces what the healthchecker tables report.

type Config

type Config struct {
	// Socket is snmp_socket; empty means the master agent's default path.
	Socket string
	// The four enable_snmp_* switches. enable_snmp_rfc sets both RFC ones
	// and enable_snmp_keepalived is a spelling of VRRP; the parser resolves
	// that before this sees it.
	VRRP    bool
	RFCv2   bool
	RFCv3   bool
	Checker bool
	// Traps is enable_traps.
	Traps bool

	// Name and ID identify the subagent to the master.
	Name string
	Logf func(string, ...any)
}

Config is what global_defs asked for.

func (Config) Enabled

func (c Config) Enabled() bool

Enabled reports whether any branch was asked for.

type Instance

type Instance struct {
	// Index is the row index in KEEPALIVED-MIB's vrrpInstanceTable, which
	// is an arbitrary dense numbering rather than anything from the
	// configuration.
	Index uint32

	Name       string
	VRID       uint8
	State      State
	InitState  State
	WantState  State
	BasePrio   uint8
	EffectPrio uint8

	// Interface and IfIndex identify the row in the RFC tables, whose index
	// is (ifIndex, vrid) rather than a dense counter.
	Interface string
	IfIndex   uint32

	// IPv6 selects the address type in RFC 6527's index.
	IPv6 bool

	Preempt      bool
	PreemptDelay uint32
	AdvertInt    uint32 // centiseconds, which is what RFC 2787 uses
	AdvertIntMs  uint32 // milliseconds, which is what RFC 6527 uses
	AuthType     int32
	VIPCount     uint32
	SyncGroup    string
	Accept       bool
	Version      uint8

	// MasterIP is the address of the master this instance last heard from,
	// and PrimaryIP the address it sends from.
	MasterIP  []byte
	PrimaryIP []byte

	// UpTime is the value of sysUpTime when the instance last changed
	// state, in hundredths of a second, which is what a TimeStamp is.
	UpTime uint32
}

Instance is one VRRP instance as the MIBs see it.

It is a snapshot rather than a pointer into the daemon's state, taken under the lock when the daemon reports a change. A walk that read live state would see a table change under it between two GetNexts and could repeat or skip a row.

type MIB

type MIB struct {
	// Which branches are exported, from the enable_snmp_* keywords. A
	// branch that is off is not registered and not walked; a manager
	// walking into it gets whatever else owns that subtree.
	VRRP    bool
	RFCv2   bool
	RFCv3   bool
	Checker bool
	// contains filtered or unexported fields
}

MIB holds the current snapshot and answers requests against it.

func NewMIB

func NewMIB(vrrp, rfcv2, rfcv3, checker bool) *MIB

NewMIB returns an empty MIB.

func (*MIB) Get

func (m *MIB) Get(name agentx.OID) agentx.Value

Get implements agentx.Handler.

func (*MIB) Next

func (m *MIB) Next(from agentx.OID, inclusive bool) (agentx.VarBind, bool)

Next implements agentx.Handler.

func (*MIB) Subtrees

func (m *MIB) Subtrees() []agentx.OID

Subtrees returns the roots to register with the master agent.

func (*MIB) Update

func (m *MIB) Update(instances []Instance, groups []SyncGroup)

Update replaces the VRRP part of the snapshot.

func (*MIB) UpdateCheckers

func (m *MIB) UpdateCheckers(servers []VirtualServer, backends []RealServer)

UpdateCheckers replaces the healthchecker part of the snapshot.

Separate from Update because the two come from different daemons: the VRRP daemon knows its instances and the checker daemon its servers, and neither has the other's. A MIB serving both is only ever built by a process that runs both.

type RealServer

type RealServer struct {
	// VSIndex is the virtual server this backend belongs to, and Index its
	// position within it: the table is indexed by both, so a backend is
	// named by the pair rather than by a global counter.
	VSIndex uint32
	Index   uint32

	SNMPName string
	Address  []byte
	Port     uint16
	IPv6     bool

	Alive   bool
	Weight  int32
	UThresh uint32
	LThresh uint32
	// Sorry marks the sorry server, which the MIB distinguishes as
	// sorry(2) against regular(1).
	Sorry bool
}

RealServer is one row of realServerTable.

type State

type State uint8

State is a VRRP state as the MIBs number it.

The two numberings differ and both are exported: KEEPALIVED-MIB uses init(0)/backup(1)/master(2)/fault(3), the RFC MIBs initialize(1)/backup(2)/ master(3). Mapping one onto the other is the single easiest thing to get wrong here, because every value is valid in both and a monitoring system would simply report the wrong state.

const (
	StateInit State = iota
	StateBackup
	StateMaster
	StateFault
)

type SyncGroup

type SyncGroup struct {
	Index uint32
	Name  string
	State State
}

SyncGroup is one sync group.

type VirtualServer

type VirtualServer struct {
	// Index is the row index, which is the position in the configuration.
	Index uint32
	// SNMPName is snmp_name: a name the operator controls, stable across
	// the renumbering that removing an earlier server causes.
	SNMPName string

	// Address and Port identify the service; FwMark replaces them for a
	// firewall-mark server.
	Address []byte
	Port    uint16
	IPv6    bool
	FwMark  uint32
	// Protocol is tcp(1), udp(2), sctp(3).
	Protocol int32

	Alive     bool
	DelayLoop uint32
	HaSuspend bool
	Omega     bool
	Quorum    uint32
	RealTotal uint32
	RealUp    uint32
}

VirtualServer is one row of virtualServerTable.

Jump to

Keyboard shortcuts

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