Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Lx9FNLG2CqeBbVGGd9bFsP
132 lines
3.8 KiB
Go
132 lines
3.8 KiB
Go
// Package profiletest runs discovery and one poll of every interval group of
|
|
// a device's profile against a live agent (or any SNMP device), in a
|
|
// throw-away inventory, and reports what the profile collected. It backs
|
|
// `nmon profile test` and the fixture harness.
|
|
package profiletest
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"io"
|
|
"log/slog"
|
|
"net/netip"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
"time"
|
|
|
|
"github.com/v10networks/nmon/internal/clock"
|
|
"github.com/v10networks/nmon/internal/discovery"
|
|
"github.com/v10networks/nmon/internal/profile"
|
|
"github.com/v10networks/nmon/internal/secrets"
|
|
"github.com/v10networks/nmon/internal/snmp"
|
|
"github.com/v10networks/nmon/internal/store"
|
|
)
|
|
|
|
// Env is a throw-away inventory (a temp SQLite database) plus a profile set.
|
|
type Env struct {
|
|
Store store.Store
|
|
Profiles *profile.Set
|
|
// Timeout and Retries apply to every SNMP request; zero values take the SNMP client defaults.
|
|
Timeout time.Duration
|
|
Retries int
|
|
|
|
// Clock times the poll wait; nil means the wall clock.
|
|
Clock clock.Clock
|
|
|
|
dir string
|
|
}
|
|
|
|
func (e *Env) clock() clock.Clock {
|
|
if e.Clock == nil {
|
|
return clock.Real()
|
|
}
|
|
return e.Clock
|
|
}
|
|
|
|
// NewEnv creates an inventory in a temp directory. Close removes it.
|
|
func NewEnv(ctx context.Context, set *profile.Set) (*Env, error) {
|
|
dir, err := os.MkdirTemp("", "nmon-profiletest-")
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
e := &Env{Profiles: set, dir: dir}
|
|
key := filepath.Join(dir, "secret.key")
|
|
if err := secrets.GenerateKeyFile(key); err != nil {
|
|
e.Close()
|
|
return nil, err
|
|
}
|
|
kr, err := secrets.LoadKeyring(key)
|
|
if err != nil {
|
|
e.Close()
|
|
return nil, err
|
|
}
|
|
if e.Store, err = store.Open(ctx, store.DriverSQLite, filepath.Join(dir, "inventory.db"), kr, clock.Real()); err != nil {
|
|
e.Close()
|
|
return nil, err
|
|
}
|
|
if err := e.Store.Migrate(ctx); err != nil {
|
|
e.Close()
|
|
return nil, err
|
|
}
|
|
return e, nil
|
|
}
|
|
|
|
// Close closes the store and removes the temp directory.
|
|
func (e *Env) Close() {
|
|
if e.Store != nil {
|
|
_ = e.Store.Close() // throw-away database about to be deleted
|
|
}
|
|
_ = os.RemoveAll(e.dir) // best-effort cleanup of a temp dir
|
|
}
|
|
|
|
// AddDevice creates a network device at addr (the port is stored on the device).
|
|
func (e *Env) AddDevice(ctx context.Context, name string, addr netip.AddrPort, c snmp.Credential) (*store.Device, error) {
|
|
cred := &store.SNMPCredential{
|
|
Name: name + "-cred", Version: "v" + strings.TrimPrefix(c.Version, "v"), Community: c.Community,
|
|
V3User: c.V3User, V3AuthProto: c.V3AuthProto, V3AuthPass: c.V3AuthPass, V3PrivProto: c.V3PrivProto,
|
|
V3PrivPass: c.V3PrivPass, V3Context: c.V3Context,
|
|
}
|
|
if err := e.Store.CreateCredential(ctx, cred); err != nil {
|
|
return nil, err
|
|
}
|
|
dev := &store.Device{Name: name, Kind: store.KindNetwork, SNMPCredentialID: &cred.ID, Enabled: true,
|
|
Profile: "generic", SNMPPort: int(addr.Port())}
|
|
if err := e.Store.CreateDevice(ctx, dev); err != nil {
|
|
return nil, err
|
|
}
|
|
if err := e.Store.SetAddresses(ctx, dev.ID, []string{addr.Addr().Unmap().String()}); err != nil {
|
|
return nil, err
|
|
}
|
|
return e.Store.GetDeviceByID(ctx, dev.ID)
|
|
}
|
|
|
|
// Discover runs discovery for dev with the Env's timeout settings.
|
|
func (e *Env) Discover(ctx context.Context, dev *store.Device) (*discovery.Report, error) {
|
|
deps := discovery.Deps{
|
|
Store: e.Store, Profiles: e.Profiles,
|
|
Dial: func(ctx context.Context, t snmp.Target, path string) (*snmp.Session, error) {
|
|
if e.Timeout > 0 {
|
|
t.Timeout = e.Timeout
|
|
}
|
|
if e.Retries != 0 {
|
|
t.Retries = e.Retries
|
|
}
|
|
return snmp.Dial(ctx, t, path)
|
|
},
|
|
Logger: slog.New(slog.NewTextHandler(io.Discard, nil)),
|
|
}
|
|
return discovery.Discover(ctx, deps, dev)
|
|
}
|
|
|
|
// ErrNoProfiles is returned when no profile set is available.
|
|
var ErrNoProfiles = errors.New("profiletest: no profiles")
|
|
|
|
func ifaceByID(ifs []store.Interface) map[string]store.Interface {
|
|
m := make(map[string]store.Interface, len(ifs))
|
|
for _, i := range ifs {
|
|
m[i.ID] = i
|
|
}
|
|
return m
|
|
}
|