smi

package
v0.0.0-...-ec9027f Latest Latest
Warning

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

Go to latest
Published: Sep 26, 2022 License: MIT Imports: 12 Imported by: 0

Documentation

Overview

Package smi implements a parser for SNMP MIBs.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func ModuleName

func ModuleName(filename string) (string, error)

ModuleName returns the module name for the given file. If the file is not a module file then a NotAModuleError error is returned.

Types

type Import

type Import struct {
	From    string
	Symbols []string
}

An Import represents all of the symbols imported from a single module.

type Lexer

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

Lexer holds the current state of the lexer

func NewLexer

func NewLexer(r io.Reader) *Lexer

NewLexer creates a new lexer instance

func (*Lexer) Error

func (lex *Lexer) Error(e string)

func (*Lexer) Lex

func (lex *Lexer) Lex(lval *smiSymType) int

Lex returns the next token on the input stream

type MIB

type MIB struct {
	Modules map[string]*Module
	Root    *Symbol
	Symbols map[string]*Symbol
	Debug   bool
	// contains filtered or unexported fields
}

A MIB is a collection of SNMP modules. The MIB provides a high-level API for loading and accessing the contents of parsed MIBs.

func NewMIB

func NewMIB(dirs ...string) *MIB

NewMIB creates a MIB object for the modules contained in the dirs directories. Creating a MIB does not load any modules from the directories. You need to call LoadModules() on the resulting MIB object.

func (*MIB) LoadModules

func (mib *MIB) LoadModules(modNames ...string) error

LoadModules scans the MIB directories and loads the modules listed by modNames. The imported modules are also loaded. A module's name is the one specified on the first line of the MIB file. The file names do not have to exactly match the module names.

func (*MIB) OID

func (mib *MIB) OID(name string) (OID, error)

OID parses the name string in the format provided by the SymbolString function (e.g. Module::Symbol.1.2.3) and returns an OID object. The module and index parts of the string are optional.

Example
package main

import (
	"fmt"
	"log"

	"github.com/Atop-NMS-team/mibtool/smi"
)

func main() {
	mib := smi.NewMIB("testdata")
	err := mib.LoadModules("IF-MIB")
	if err != nil {
		log.Fatal(err)
	}

	examples := []string{
		"ifTable", "IF-MIB::ifIndex",
		"ifType.3", "IF-MIB::ifOperStatus.4",
	}
	for _, example := range examples {
		oid, err := mib.OID(example)
		if err != nil {
			log.Fatal(err)
		}
		fmt.Println(oid.String())
	}
}
Output:

1.3.6.1.2.1.2.2
1.3.6.1.2.1.2.2.1.1
1.3.6.1.2.1.2.2.1.3.3
1.3.6.1.2.1.2.2.1.8.4

func (*MIB) Symbol

func (mib *MIB) Symbol(oid OID) (*Symbol, OID)

Symbol returns the Symbol and an OID index for the specified OID.

func (*MIB) SymbolString

func (mib *MIB) SymbolString(oid OID) string

SymbolString returns a string representation of the information provided by the Symbol function.

func (*MIB) VisitSymbols

func (mib *MIB) VisitSymbols(action func(sym *Symbol, oid OID))

VisitSymbols walks all symbols defined in the MIB in order by OID. The action function is called once for each symbol.

Example
package main

import (
	"fmt"
	"log"

	"github.com/Atop-NMS-team/mibtool/smi"
)

func main() {
	mib := smi.NewMIB("testdata")
	mib.Debug = true
	err := mib.LoadModules("SNMPv2-SMI")
	if err != nil {
		log.Fatal(err)
	}
	mib.VisitSymbols(func(sym *smi.Symbol, oid smi.OID) {
		fmt.Printf("%-40s %s\n", sym, oid)
	})
}
Output:

SNMPv2-SMI::org                          1.3
SNMPv2-SMI::dod                          1.3.6
SNMPv2-SMI::internet                     1.3.6.1
SNMPv2-SMI::directory                    1.3.6.1.1
SNMPv2-SMI::mgmt                         1.3.6.1.2
SNMPv2-SMI::mib-2                        1.3.6.1.2.1
SNMPv2-SMI::transmission                 1.3.6.1.2.1.10
SNMPv2-SMI::experimental                 1.3.6.1.3
SNMPv2-SMI::private                      1.3.6.1.4
SNMPv2-SMI::enterprises                  1.3.6.1.4.1
SNMPv2-SMI::security                     1.3.6.1.5
SNMPv2-SMI::snmpV2                       1.3.6.1.6
SNMPv2-SMI::snmpDomains                  1.3.6.1.6.1
SNMPv2-SMI::snmpProxys                   1.3.6.1.6.2
SNMPv2-SMI::snmpModules                  1.3.6.1.6.3

type Module

type Module struct {
	Name     string
	File     string
	Imports  []Import
	Nodes    []Node
	IsLoaded bool
	Symbols  map[string]*Symbol
}

A Module contains all of the parse results for a single module file. Only the Name and File fields are valid if the IsLoaded flag is false.

func ParseModule

func ParseModule(filename string) (*Module, error)

ParseModule attempts to parse a MIB module from the given file

type Node

type Node struct {
	Label string

	Syntax      string //---support more properties of mib object
	Access      string
	Status      string
	Description string

	Type NodeType
	IDs  []SubID
}

A Node represents a parse node in an SMI document

func SymbolNode

func SymbolNode(sym *Symbol) (*Node, error)

Get MIB object by a Symboil

type NodeType

type NodeType int

NodeType distinguishes the different types of objects that make up the Nodes in a Module.

const (
	NodeNotSupported NodeType = iota
	NodeModuleID
	NodeObjectID
	NodeObjectType
	NodeNotification
)

NodeType values for the supported node types

type NotAModuleError

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

NotAModuleError is returned when a parsed file is not a valid module file. type NotAModuleError string

func (NotAModuleError) Error

func (f NotAModuleError) Error() string

func (NotAModuleError) Filename

func (f NotAModuleError) Filename() string

Filename returns the name of the file that is not a valid module.

type OID

type OID []int

The OID type represents a dot-formated MIB object ID

func (OID) Equal

func (oid OID) Equal(other OID) bool

Equal returns true if the two object IDs have the same value

func (OID) String

func (oid OID) String() string

type SubID

type SubID struct {
	ID    int
	Label string
}

SubID is a label and/or ID associated with a Node

func (SubID) String

func (s SubID) String() string

type Symbol

type Symbol struct {
	Name   string
	ID     int
	Module *Module

	NodeIndex int //---index to mod.Nodes[]

	Parent       *Symbol
	ChildByLabel map[string]*Symbol
	ChildByID    map[int]*Symbol
}

A Symbol represents a single symbol in the tree of identifiers. The tree can be traversed by label or by ID. The collection of IDs in the path from the root of the tree to the symbol is the object identifier (OID) of the symbol.

func (*Symbol) String

func (s *Symbol) String() string

Jump to

Keyboard shortcuts

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