From f29b68b567f6f902bb23a60543532f389aade427 Mon Sep 17 00:00:00 2001 From: Herbert Date: Thu, 29 Jun 2017 11:15:55 +0200 Subject: [PATCH] Add deadman flag and fix value of `state` now that the meaning is actually (0 for OK, 1 for error) --- Makefile | 7 +++++-- event.go | 4 ++-- main.go | 13 +++++++------ 3 files changed, 14 insertions(+), 10 deletions(-) diff --git a/Makefile b/Makefile index 8aca33e..a7dc7ed 100644 --- a/Makefile +++ b/Makefile @@ -11,11 +11,14 @@ build: go build -v -dist: $(PLATFORMS) +cleanup_dist: + rm -rfv $(DIST_DIR) + mkdir -p $(DIST_DIR) + +dist: cleanup_dist $(PLATFORMS) $(PLATFORMS): $(eval GOOS := $(firstword $(subst -, ,$@))) $(eval GOARCH := $(lastword $(subst -, ,$@))) - mkdir -p $(DIST_DIR) env GOOS=$(GOOS) GOARCH=$(GOARCH) go build -o $(DIST_DIR)/$(APPNAME).$(RELEASE).$@ md5: dist diff --git a/event.go b/event.go index c19ad12..7209f4a 100644 --- a/event.go +++ b/event.go @@ -52,7 +52,7 @@ func newIPMIEvent(stdout string) (*ipmiEvent, error) { func (ev ipmiEvent) InfluxDB(checkName, hostname string) string { return fmt.Sprintf( - `%s,host=%s,event_id=%s,error_level=%s,event_type=%s event_date="%s",event_time="%s",error_level="%s",sensor_name="%s",event_type="%s",error_message="%s",state=%d\n`, + `%s,host=%s,event_id=%s,error_level=%s,event_type=%s event_date="%s",event_time="%s",error_level="%s",sensor_name="%s",event_type="%s",error_message="%s",state=%d`, checkName, hostname, ev.ID, ev.Level, ev.Type, ev.Time.Format("Jan-02-2006"), ev.Time.Format("03:04:05"), ev.Level, ev.Sensor, ev.Type, ev.Message, ev.State, ) } @@ -65,6 +65,6 @@ func newEmptyIPMIEvent() *ipmiEvent { Type: "OK", Level: "OK", Message: "No errors", - State: 1, + State: 0, } } diff --git a/main.go b/main.go index ce8bacf..b9635ad 100644 --- a/main.go +++ b/main.go @@ -22,6 +22,7 @@ var ( 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() + deadman = app.Flag("deadman", "if set, this program will always print something").Default("false").Bool() ipmiSel = app.Flag("ipmi-sel", "Path of ipmi-sel").Default("/usr/sbin/ipmi-sel").String() ) @@ -53,13 +54,13 @@ func main() { 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) - os.Exit(0) + log.Printf("Got error running `%s`: `%s`\n", *ipmiSel, err) // purposely do not quit } - outStr = strings.TrimSpace(outStr) - lines := strings.Split(outStr, "\n") - if len(lines) >= 0 { + lines := strings.Split(strings.TrimSpace(outStr), "\n") + log.Printf("%#v\n", lines) + + if len(lines) >= 0 && lines[0] != "" { for _, line := range lines { line = strings.TrimSpace(line) if len(line) == 0 { @@ -74,7 +75,7 @@ func main() { } fmt.Println(ev.InfluxDB(*checkName, hostname)) } - } else { + } else if *deadman { ev := newEmptyIPMIEvent() fmt.Println(ev.InfluxDB(*checkName, hostname)) }