diff --git a/main.go b/main.go index 6e62c01..e515af9 100644 --- a/main.go +++ b/main.go @@ -4,6 +4,7 @@ package main import ( "bytes" "fmt" + "io" "log/syslog" "os" "os/exec" @@ -19,6 +20,7 @@ var ( appName = path.Base(os.Args[0]) app = kingpin.New(appName, "A command-line checker for IPMI checks using ipmi-sel, by CrossEngage") checkName = app.Flag("name", "check name").Default(appName).String() + debug = app.Flag("debug", "if set, enables debug log on stderr").Default("false").Bool() ipmiSel = app.Flag("ipmi-sel", "Path of ipmi-sel").Default("/usr/sbin/ipmi-sel").String() ) @@ -35,7 +37,12 @@ func main() { if err != nil { log.Fatal(err) } - log.SetOutput(slog) + + if *debug { + log.SetOutput(io.MultiWriter(slog, os.Stderr)) + } else { + log.SetOutput(slog) + } cmd := exec.Command(*ipmiSel, "--output-event-state", "--comma-separated-output", "--no-header-output") var stdout, stderr bytes.Buffer @@ -46,15 +53,22 @@ func main() { } outStr, errStr := string(stdout.Bytes()), string(stderr.Bytes()) - slog.Debug(fmt.Sprintf("%s: stdout `%s`, stderr `%s`", *ipmiSel, outStr, errStr)) + log.Printf("%s: stdout `%s`, stderr `%s`", *ipmiSel, outStr, errStr) outStr = strings.TrimSpace(outStr) lines := strings.Split(outStr, "\n") if len(lines) >= 0 { for _, line := range lines { + line = strings.TrimSpace(line) + if len(line) == 0 { + log.Println("Got empty line from output of ipmi-sel.") + continue + } + log.Printf("Line: `%s`\n", line) ev, err := newIPMIEvent(line) + log.Printf("Event: %#v\n", ev) if err != nil { - slog.Err(fmt.Sprintf("Could not parse `%s` stdout: `%s`", line, outStr)) + log.Printf("Could not parse `%s` stdout: `%s`", line, outStr) } fmt.Println(ev.InfluxDB(*checkName, hostname)) }