From 33b224974e786cb870cebcde1bf45e1468c7ea64 Mon Sep 17 00:00:00 2001 From: Herbert Fischer Date: Mon, 13 Nov 2017 13:59:30 +0100 Subject: [PATCH] Fix command execution and time parsing --- event.go | 12 ++++++------ main.go | 17 ++++++----------- 2 files changed, 12 insertions(+), 17 deletions(-) diff --git a/event.go b/event.go index 7209f4a..1d4eb5a 100644 --- a/event.go +++ b/event.go @@ -8,7 +8,7 @@ import ( var ( errCouldNotParseCmdOut = "Unknown command output: %s" - errCouldNotParseTime = "Could not parse time: %s" + errCouldNotParseTime = "Could not parse time: %s: %s" ) type ipmiEvent struct { @@ -22,19 +22,19 @@ type ipmiEvent struct { } const ( - timeFmt = "Jan-02-2006T03:04:05" + timeFmt = "Jan-02-2006T15:04:05" ) -func newIPMIEvent(stdout string) (*ipmiEvent, error) { - parts := strings.Split(stdout, ",") +func newIPMIEvent(stdOutLine string) (*ipmiEvent, error) { + parts := strings.Split(stdOutLine, ",") if len(parts) != 7 { - return nil, fmt.Errorf(errCouldNotParseCmdOut, stdout) + return nil, fmt.Errorf(errCouldNotParseCmdOut, stdOutLine) } inputTime := parts[1] + "T" + parts[2] eventTime, err := time.Parse(timeFmt, inputTime) if err != nil { - return nil, fmt.Errorf(errCouldNotParseTime, inputTime) + return nil, fmt.Errorf(errCouldNotParseTime, inputTime, err) } event := &ipmiEvent{ diff --git a/main.go b/main.go index b9635ad..4652f51 100644 --- a/main.go +++ b/main.go @@ -2,7 +2,6 @@ package main import ( - "bytes" "fmt" "io" "log/syslog" @@ -47,14 +46,10 @@ func main() { log.SetOutput(slog) } - cmd := exec.Command(*ipmiSel, "--debug", "--output-event-state", "--comma-separated-output", "--no-header-output") - var stdout, stderr bytes.Buffer - cmd.Stdout = &stdout - cmd.Stderr = &stderr - outStr, errStr := string(stdout.Bytes()), string(stderr.Bytes()) - log.Printf("%s: stdout `%s`, stderr `%s`", *ipmiSel, outStr, errStr) - if err := cmd.Run(); err != nil { - log.Printf("Got error running `%s`: `%s`\n", *ipmiSel, err) // purposely do not quit + out, err := exec.Command(*ipmiSel, "--debug", "--output-event-state", "--comma-separated-output", "--no-header-output").Output() + outStr := string(out) + if err != nil { + log.Printf("Got error running `%s`: `%s` : `%s`\n", *ipmiSel, err, outStr) // purposely do not quit } lines := strings.Split(strings.TrimSpace(outStr), "\n") @@ -71,9 +66,9 @@ func main() { ev, err := newIPMIEvent(line) log.Printf("Event: %#v\n", ev) if err != nil { - log.Printf("Could not parse `%s` stdout: `%s`", line, outStr) + log.Printf("Could not parse line `%s`, err: `%s`", line, err) + continue } - fmt.Println(ev.InfluxDB(*checkName, hostname)) } } else if *deadman { ev := newEmptyIPMIEvent()