The Go code itself was never the real culprit –
libsmi (which gosmi wraps) will only assign a numeric OID to a node
if every ancestor identifier is known.
When you load just your proprietary MIB, all ancestors that live in
“SNMPv2-SMI”, “SNMPv2-TC”, “SNMPv2-MIB”, “RFC-1213-MIB”, … are missing, so
node.Oid is empty and you end up writing an empty string.
What you have to do is load every module the target MIB depends on
before you start iterating over the nodes. gosmi gives you two easy ways
to do that:
-
Point gosmi at a directory that already contains all the standard MIBs
(Ubuntu/Debian: /usr/share/snmp/mibs, macOS: /usr/local/share/snmp/mibs, …)
and call gosmi.ScanModules().
-
Or, explicitly load the well-known base modules one by one
(SNMPv2-SMI, SNMPv2-TC, …) before you load your own module.
Below is a working version that uses option #1 (scan a complete
directory).
Try it, then run something as simple as
go run . -path /usr/share/snmp/mibs -module IF-MIB
You will see the full numeric OIDs in the generated YAML.
package parsers
import (
"fmt"
"os"
"path/filepath"
"strings"
"github.com/abruneau/dd-snmp-profiler/models"
"github.com/sleepinggenius2/gosmi"
"gopkg.in/yaml.v3"
)
/* -------------------------------------------------------------------------- */
/* Helpers */
/* -------------------------------------------------------------------------- */
func symbolFromNode(n gosmi.SmiNode) models.Symbol {
return models.NewSymbol(n.Oid.String(), n.Name, n.Description)
}
/* -------------------------------------------------------------------------- */
/* Scalar handler */
/* -------------------------------------------------------------------------- */
func parseScalar(mibName string, node gosmi.SmiNode) models.BaseMetric {
return models.BaseMetric{
MIB: mibName,
Symbol: symbolFromNode(node),
}
}
/* -------------------------------------------------------------------------- */
/* Table handler */
/* -------------------------------------------------------------------------- */
func parseTable(mibName string, tableNode gosmi.SmiNode) (models.TableMetric, error) {
t := tableNode.AsTable()
metric := models.TableMetric{
MIB: mibName,
Table: symbolFromNode(tableNode),
}
for _, colNode := range t.Columns {
// Re-look-up the column so we certainly have its *full* OID
fullNode, err := gosmi.GetNode(colNode.Name)
if err != nil {
return metric, err
}
sym := symbolFromNode(fullNode)
if len(fullNode.Oid) > 0 && fullNode.Oid[len(fullNode.Oid)-1] == 1 {
metric.MetricTags = append(metric.MetricTags, models.MetricTag{Column: sym})
} else {
metric.Symbols = append(metric.Symbols, sym)
}
}
return metric, nil
}
/* -------------------------------------------------------------------------- */
/* Driver */
/* -------------------------------------------------------------------------- */
// ParseMIBModule parses a MIB module and writes the generated profile to `output`.
func ParseMIBModule(mibDir, module, output string, debug bool) error {
gosmi.Init()
defer gosmi.Exit()
/* 1. Tell gosmi where all the MIBs live (the directory
must include SNMPv2-SMI, SNMPv2-TC, …). */
if stat, err := os.Stat(mibDir); err != nil || !stat.IsDir() {
return fmt.Errorf("MIB directory %q does not exist", mibDir)
}
gosmi.AppendPath(mibDir)
/* 2. Load *all* modules that are present in that directory.
This resolves the parent chain all the way up to
1.3.6.1.4.1 … */
loaded := gosmi.ScanModules()
if debug {
fmt.Printf("Scanned %d modules from %s\n", len(loaded), mibDir)
}
/* 3. Now load the module we actually want to profile. */
loadedName, err := gosmi.LoadModule(module)
if err != nil {
return err
}
if debug {
fmt.Printf("Loaded target module %s\n", loadedName)
}
m, err := gosmi.GetModule(loadedName)
if err != nil {
return err
}
var profile models.Profile
for _, node := range m.GetNodes() {
if node.Status.String() != "Current" {
continue
}
switch node.Kind.String() {
case "Scalar":
profile.Metrics = append(profile.Metrics, parseScalar(m.Name, node))
case "Table":
tMetric, err := parseTable(m.Name, node)
if err != nil {
return err
}
profile.Metrics = append(profile.Metrics, tMetric)
}
}
yProfile, err := yaml.Marshal(&profile)
if err != nil {
return err
}
if err = WriteToFile(output, &yProfile); err != nil {
return err
}
if debug {
fmt.Printf("Wrote %s profile (%d metrics) to %s\n",
loadedName, len(profile.Metrics), output)
}
return nil
}
/* -------------------------------------------------------------------------- */
/* Utility: allow colon-separated “MIBDIRS” env var */
/* -------------------------------------------------------------------------- */
func AppendEnvPath() {
if v := os.Getenv("MIBDIRS"); v != "" {
for _, p := range strings.Split(v, ":") {
if _, err := os.Stat(p); err == nil {
gosmi.AppendPath(filepath.Clean(p))
}
}
}
}