Documentation
¶
Overview ¶
Package gomib loads and resolves SNMP MIB modules.
Call Load with one or more Source values to parse MIB files, resolve cross-module imports, build the OID tree, and return a read-only mib.Mib containing the merged result.
Index ¶
- Constants
- Variables
- func DefaultExtensions() []string
- func DiscoverSystemPaths() []string
- func Load(ctx context.Context, opts ...LoadOption) (*mib.Mib, error)
- type FindResult
- type LoadOption
- type Source
- func Dir(path string, opts ...SourceOption) (Source, error)
- func DirTree(root string, opts ...SourceOption) (Source, error)
- func DiscoverSystemSources() []Source
- func FS(name string, fsys fs.FS, opts ...SourceOption) Source
- func Multi(sources ...Source) Source
- func MustDir(path string, opts ...SourceOption) Source
- func MustDirTree(root string, opts ...SourceOption) Source
- type SourceOption
Constants ¶
const LevelTrace = types.LevelTrace
LevelTrace is a custom log level more verbose than Debug. Use for per-item iteration logging (tokens, OID nodes, imports). Enable with: &slog.HandlerOptions{Level: slog.Level(-8)}
Variables ¶
var ErrDiagnosticThreshold = errors.New("diagnostic threshold exceeded")
ErrDiagnosticThreshold is returned when diagnostics exceed the configured FailAt severity. The Mib is still returned with all resolved data.
var ErrMissingModules = errors.New("requested modules not found")
ErrMissingModules is returned when WithModules names are not found in any source. The Mib is still returned with whatever modules could be loaded.
var ErrNoSources = errors.New("no MIB sources provided")
ErrNoSources is returned when Load is called with no sources.
Functions ¶
func DefaultExtensions ¶
func DefaultExtensions() []string
DefaultExtensions returns the file extensions recognized as MIB files. Empty string matches files with no extension (e.g., "IF-MIB").
func DiscoverSystemPaths ¶ added in v0.2.1
func DiscoverSystemPaths() []string
DiscoverSystemPaths returns MIB directories from net-snmp and libsmi configuration, deduplicated and filtered to directories that exist.
Types ¶
type FindResult ¶
type FindResult struct {
Content []byte
// Path is used in diagnostic messages to identify the source.
Path string
}
FindResult holds the content and location of a found MIB file.
type LoadOption ¶
type LoadOption func(*loadConfig)
LoadOption configures Load.
func WithDiagnosticConfig ¶ added in v0.1.0
func WithDiagnosticConfig(cfg mib.DiagnosticConfig) LoadOption
WithDiagnosticConfig sets the diagnostic configuration for strictness control. If not set, defaults to Normal strictness (report Minor and above, fail on Severe).
func WithLogger ¶
func WithLogger(logger *slog.Logger) LoadOption
WithLogger sets the logger for debug/trace output. If not set, no logging occurs (zero overhead).
func WithModules ¶ added in v0.1.0
func WithModules(names ...string) LoadOption
WithModules restricts loading to the named modules and their dependencies. Omit to load all modules from the configured sources.
func WithSource ¶ added in v0.1.0
func WithSource(src ...Source) LoadOption
WithSource appends one or more MIB sources to the load configuration. Sources are searched in the order they are added.
func WithStrictness ¶ added in v0.1.0
func WithStrictness(level mib.StrictnessLevel) LoadOption
WithStrictness sets the strictness level using a preset configuration. Convenience wrapper for WithDiagnosticConfig with preset configs.
func WithSystemPaths ¶ added in v0.1.0
func WithSystemPaths() LoadOption
WithSystemPaths enables automatic discovery of MIB search paths from net-snmp and libsmi configuration (config files, env vars, defaults). Discovered paths are appended after any explicit source, serving as fallback. When source is nil and WithSystemPaths is set, system paths alone are sufficient.
type Source ¶
type Source interface {
// Find returns the MIB content for the named module,
// or fs.ErrNotExist if the module is not available.
Find(name string) (FindResult, error)
// ListModules returns all module names known to this source.
ListModules() ([]string, error)
}
Source provides access to MIB files for the loading pipeline. Implementations are passed to WithSource and searched in order during Load to locate module files by name. The standard implementations are Dir, DirTree, FS, and Multi.
func Dir ¶
func Dir(path string, opts ...SourceOption) (Source, error)
Dir creates a Source that searches a single directory (no recursion). Files are looked up lazily on each Find() call.
func DirTree ¶
func DirTree(root string, opts ...SourceOption) (Source, error)
DirTree creates a Source that recursively indexes a directory tree. It walks the tree once at construction and builds a name->path index. First match wins for duplicate names.
func DiscoverSystemSources ¶ added in v0.2.1
func DiscoverSystemSources() []Source
DiscoverSystemSources returns Sources for all discovered system MIB directories.
func FS ¶
func FS(name string, fsys fs.FS, opts ...SourceOption) Source
FS creates a Source backed by an fs.FS (e.g., embed.FS). The name is used in diagnostic paths. The filesystem is lazily indexed on first use.
Unlike Dir and DirTree, FS does not return an error at construction time. This is intentional: embed.FS cannot be walked until the program runs, so validation is deferred to the first Find or ListModules call.
func Multi ¶
Multi combines multiple sources into one. Find() tries each source in order, returning the first match.
func MustDir ¶
func MustDir(path string, opts ...SourceOption) Source
MustDir is like Dir but panics on error.
func MustDirTree ¶
func MustDirTree(root string, opts ...SourceOption) Source
MustDirTree is like DirTree but panics on error.
type SourceOption ¶
type SourceOption func(*sourceConfig)
SourceOption modifies source behavior.
func WithExtensions ¶
func WithExtensions(exts ...string) SourceOption
WithExtensions overrides the default file extensions used to match MIB files. Extensions are normalized to lowercase with a leading dot (e.g. "mib" becomes ".mib"). An empty string matches files with no extension.
Directories
¶
| Path | Synopsis |
|---|---|
|
cmd
|
|
|
gomib
command
Command gomib is a CLI tool for loading, querying, and dumping MIB modules.
|
Command gomib is a CLI tool for loading, querying, and dumping MIB modules. |
|
gomib-libsmi
command
Command gomib-libsmi compares gomib against libsmi for lexer/parser cross-validation.
|
Command gomib-libsmi compares gomib against libsmi for lexer/parser cross-validation. |
|
gomib-netsnmp
command
Command gomib-netsnmp compares gomib against net-snmp for cross-validation.
|
Command gomib-netsnmp compares gomib against net-snmp for cross-validation. |
|
internal/cliutil
Package cliutil provides shared CLI utilities for gomib command-line tools.
|
Package cliutil provides shared CLI utilities for gomib command-line tools. |
|
examples
|
|
|
basic
command
Load IF-MIB and print a module summary with selected objects.
|
Load IF-MIB and print a module summary with selected objects. |
|
diagnostics
command
Load MIBs at different strictness levels and show diagnostic output.
|
Load MIBs at different strictness levels and show diagnostic output. |
|
embed
command
Use embed.FS with gomib.FS() to load MIBs embedded in the binary.
|
Use embed.FS with gomib.FS() to load MIBs embedded in the binary. |
|
query
command
Show different query patterns: by name, module-scoped, OID, and prefix matching.
|
Show different query patterns: by name, module-scoped, OID, and prefix matching. |
|
tables
command
Load several MIBs and demonstrate the table API: structure, indexes, columns, AUGMENTS relationships, index encoding, and navigation.
|
Load several MIBs and demonstrate the table API: structure, indexes, columns, AUGMENTS relationships, index encoding, and navigation. |
|
types
command
Show type chain walking, textual conventions, and effective constraints.
|
Show type chain walking, textual conventions, and effective constraints. |
|
internal
|
|
|
ast
Package ast provides Abstract Syntax Tree types for parsed MIB modules.
|
Package ast provides Abstract Syntax Tree types for parsed MIB modules. |
|
graph
Package graph provides dependency graph construction and analysis for MIB resolution.
|
Package graph provides dependency graph construction and analysis for MIB resolution. |
|
lexer
Package lexer provides tokenization for SMIv1/SMIv2 MIB source text.
|
Package lexer provides tokenization for SMIv1/SMIv2 MIB source text. |
|
module
Package module provides a normalized representation of MIB modules.
|
Package module provides a normalized representation of MIB modules. |
|
parser
Package parser provides MIB parsing into an AST.
|
Package parser provides MIB parsing into an AST. |
|
testutil
Package testutil provides test assertion helpers.
|
Package testutil provides test assertion helpers. |
|
types
Package types provides internal types shared across gomib packages.
|
Package types provides internal types shared across gomib packages. |
|
Package mib provides the resolved, read-only model produced by loading SNMP MIB modules.
|
Package mib provides the resolved, read-only model produced by loading SNMP MIB modules. |
|
Package smiwrite emits resolved MIB models as canonical SMIv2 text.
|
Package smiwrite emits resolved MIB models as canonical SMIv2 text. |