Skip to content

Commit

Permalink
Improve generator parse error handling
Browse files Browse the repository at this point in the history
Improve the handling of parse errors from net-snmp.
* Fix the line split when there are no errors.
* Default to failing on parse errors.
* Add extra help messages to help when there are errors.
* Improve `parse_errors` output.

Signed-off-by: SuperQ <superq@gmail.com>
  • Loading branch information
SuperQ committed May 1, 2024
1 parent 01f11ea commit b401d52
Showing 1 changed file with 24 additions and 12 deletions.
36 changes: 24 additions & 12 deletions generator/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ func generateConfig(nodes *Node, nameToNode map[string]*Node, logger log.Logger)
}

var (
failOnParseErrors = kingpin.Flag("fail-on-parse-errors", "Exit with a non-zero status if there are MIB parsing errors").Default("false").Bool()
failOnParseErrors = kingpin.Flag("fail-on-parse-errors", "Exit with a non-zero status if there are MIB parsing errors").Default("true").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 = kingpin.Flag("mibs-dir", "Paths to mibs directory").Default("").Short('m').Strings()
Expand All @@ -112,30 +112,42 @@ func main() {
command := kingpin.Parse()
logger := promlog.New(promlogConfig)

parseOutput, err := initSNMP(logger)
output, err := initSNMP(logger)
if err != nil {
level.Error(logger).Log("msg", "Error initializing netsnmp", "err", err)
os.Exit(1)
}

parseOutput = strings.TrimSpace(parseOutput)
parseErrors := len(parseOutput) != 0
if parseErrors {
level.Warn(logger).Log("msg", "NetSNMP reported parse error(s)", "errors", len(strings.Split(parseOutput, "\n")))
output = strings.TrimSpace(strings.ToValidUTF8(output, "�"))
var parseOutput []string
if len(output) > 0 {
parseOutput = strings.Split(output, "\n")
}
parseErrors := len(parseOutput)
if parseErrors > 0 {
level.Warn(logger).Log("msg", "NetSNMP reported parse error(s)", "errors", parseErrors)
}

nodes := getMIBTree()
nameToNode := prepareTree(nodes, logger)

switch command {
case generateCommand.FullCommand():
err := generateConfig(nodes, nameToNode, logger)
if err != nil {
level.Error(logger).Log("msg", "Error generating config netsnmp", "err", err)
os.Exit(1)
if *failOnParseErrors && parseErrors > 0 {
level.Error(logger).Log("msg", "Failing on reported parse error(s)", "help", "Use 'generator parse_errors' command to see errors, --no-fail-on-parse-errors to ignore")
} else {
err := generateConfig(nodes, nameToNode, logger)
if err != nil {
level.Error(logger).Log("msg", "Error generating config netsnmp", "err", err)
os.Exit(1)
}
}
case parseErrorsCommand.FullCommand():
fmt.Println(parseOutput)
if parseErrors > 0 {
fmt.Printf("%s\n", strings.Join(parseOutput, "\n"))
} else {
level.Info(logger).Log("msg", "No parse errors")
}
case dumpCommand.FullCommand():
walkNode(nodes, func(n *Node) {
t := n.Type
Expand All @@ -150,7 +162,7 @@ func main() {
n.Oid, n.Label, t, n.TextualConvention, n.Hint, n.Indexes, implied, n.EnumValues, n.Description)
})
}
if *failOnParseErrors && parseErrors {
if *failOnParseErrors && parseErrors > 0 {
os.Exit(1)
}
}

0 comments on commit b401d52

Please # to comment.