Skip to content

Commit

Permalink
Fix command execution and time parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
Herbert Fischer committed Nov 13, 2017
1 parent f29b68b commit 33b2249
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 17 deletions.
12 changes: 6 additions & 6 deletions event.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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{
Expand Down
17 changes: 6 additions & 11 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
package main

import (
"bytes"
"fmt"
"io"
"log/syslog"
Expand Down Expand Up @@ -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")
Expand All @@ -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()
Expand Down

0 comments on commit 33b2249

Please sign in to comment.