Skip to content

Commit

Permalink
Use kingpin Strings in the generator cli for the mibs-dir
Browse files Browse the repository at this point in the history
Signed-off-by: dnck <cookdj0128@gmail.com>
  • Loading branch information
dnck committed Nov 6, 2023
1 parent 8f18cd9 commit 3f8f84f
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 20 deletions.
7 changes: 4 additions & 3 deletions generator/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,15 @@ by the snmp_exporter executable to collect data from the snmp enabled devices.

Additional command are available for debugging, use the `help` command to see them.

After building, you can pass a directory of mibs, a path to the `generator.yml`
After building, you can pass a directories of mibs, a path to the `generator.yml`
file and the intended path of your output file e.g. `snmp.yml` to the `generate`
command like so,
```bash
./generator generate \
-m /tmp
-m /tmp/deviceFamilyMibs \
-m /tmp/sharedMibs \
-g /tmp/generator.yml \
-o /tmp/snmp.yml \
-o /tmp/snmp.yml
```

### MIB Parsing options
Expand Down
6 changes: 3 additions & 3 deletions generator/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,9 +107,9 @@ var (
failOnParseErrors = kingpin.Flag("fail-on-parse-errors", "Exit with a non-zero status if there are MIB parsing errors").Default("false").Bool()
snmpMIBOpts = kingpin.Flag("snmp.mibopts", "Toggle various defaults controlling MIB parsing, see snmpwalk --help").String()
generateCommand = kingpin.Command("generate", "Generate snmp.yml from generator.yml")
userMibsDir = generateCommand.Flag("mibs-dir", "Path to mibs directory").Default("").Short('m').String()
generatorYmlPath = generateCommand.Flag("generator-path", "Path to the generator.yml file").Default("generator.yml").Short('g').String()
outputPath = generateCommand.Flag("output-path", "Path to to write resulting config file").Default("snmp.yml").Short('o').String()
userMibsDir = generateCommand.Flag("mibs-dir", "Paths to mibs directory").Default("").Short('m').Strings()
generatorYmlPath = generateCommand.Flag("generator-path", "Path to the input generator.yml file").Default("generator.yml").Short('g').String()
outputPath = generateCommand.Flag("output-path", "Path to write the snmp_exporter's config file").Default("snmp.yml").Short('o').String()
parseErrorsCommand = kingpin.Command("parse_errors", "Debug: Print the parse errors output by NetSNMP")
dumpCommand = kingpin.Command("dump", "Debug: Dump the parsed and prepared MIBs")
)
Expand Down
27 changes: 13 additions & 14 deletions generator/net_snmp.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ import (
"io"
"os"
"sort"
"strings"

"github.com/go-kit/log"
"github.com/go-kit/log/level"
Expand Down Expand Up @@ -152,6 +153,15 @@ var (
}
)

// getMibsDir joins the user-specified MIB directories into a single string; if the user didn't pass any,
// the default netsnmp mibs directory is returned.
func getMibsDir(paths []string) string {
if len(paths) == 1 && paths[0] == "" {
return C.GoString(C.netsnmp_get_mib_directory())
}
return strings.Join(paths, ":")
}

// Initialize NetSNMP. Returns MIB parse errors.
//
// Warning: This function plays with the stderr file descriptor.
Expand All @@ -161,20 +171,9 @@ func initSNMP(logger log.Logger) (string, error) {
if err != nil {
return "", err
}
if *userMibsDir != "" {
// The user told us where to load the mibs
err = level.Info(logger).Log("msg", "Loading MIBs", "from", *userMibsDir)
if err != nil {
return "", err
}
C.netsnmp_set_mib_directory(C.CString(*userMibsDir))
} else {
// Help the user find their MIB directories.
err = level.Info(logger).Log("msg", "Loading MIBs", "from", C.GoString(C.netsnmp_get_mib_directory()))
if err != nil {
return "", err
}
}
mibsDir := getMibsDir(*userMibsDir)
level.Info(logger).Log("msg", "Loading MIBs", "from", mibsDir)
C.netsnmp_set_mib_directory(C.CString(mibsDir))
if *snmpMIBOpts != "" {
C.snmp_mib_toggle_options(C.CString(*snmpMIBOpts))
}
Expand Down

0 comments on commit 3f8f84f

Please sign in to comment.