Skip to content

Commit

Permalink
Fix influxdb line-protocol output
Browse files Browse the repository at this point in the history
  • Loading branch information
Herbert Fischer committed Nov 13, 2017
1 parent 33b2249 commit 4e32285
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 30 deletions.
5 changes: 2 additions & 3 deletions event.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,8 @@ func newIPMIEvent(stdOutLine 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`,
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,
)
`%s,host="%s",event_id=%s,error_level="%s",event_type="%s",sensor_name="%s" error_message="%s",state=%d %d`,
checkName, hostname, ev.ID, ev.Level, ev.Type, ev.Sensor, ev.Message, ev.State, ev.Time.UnixNano())
}

func newEmptyIPMIEvent() *ipmiEvent {
Expand Down
60 changes: 33 additions & 27 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,72 +3,78 @@ package main

import (
"fmt"
"io"
"log"
"log/syslog"
"os"
"os/exec"
"path"
"strings"
"syscall"

"log"

"github.com/sirupsen/logrus"
logSys "github.com/sirupsen/logrus/hooks/syslog"
"gopkg.in/alecthomas/kingpin.v1"
)

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()
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()
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 logging").Default("false").Bool()
syslogHook = app.Flag("syslog", "if set, enables logging to syslog").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()
)

func main() {
app.Version(version)
kingpin.MustParse(app.Parse(os.Args[1:]))

hostname, err := os.Hostname()
if err != nil {
log.Fatal(err)
if *debug {
logrus.SetLevel(logrus.DebugLevel)
}

slog, err := syslog.New(syslog.LOG_NOTICE|syslog.LOG_DAEMON, appName)
if err != nil {
log.Fatal(err)
log.SetOutput(os.Stderr)
logrus.SetFormatter(&logrus.TextFormatter{})

if *syslogHook {
hook, err := logSys.NewSyslogHook("", "", syslog.LOG_INFO, appName)
if err != nil {
logrus.Error("Unable to connect to local syslog daemon")
} else {
logrus.AddHook(hook)
}
log.SetFlags(log.Lshortfile)
}

if *debug {
log.SetOutput(io.MultiWriter(slog, os.Stderr))
log.Printf("uid=%d euid=%d gid=%d egid=%d\n", syscall.Getuid(), syscall.Geteuid(), syscall.Getgid(), syscall.Getegid())
} else {
log.SetOutput(slog)
hostname, err := os.Hostname()
if err != nil {
logrus.Fatal(err)
}

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
logrus.Errorf("Got error running `%s`: `%s` : `%s`", *ipmiSel, err, outStr) // purposely do not quit
}

lines := strings.Split(strings.TrimSpace(outStr), "\n")
log.Printf("%#v\n", lines)
logrus.Debugf("%#v", lines)

if len(lines) >= 0 && lines[0] != "" {
for _, line := range lines {
line = strings.TrimSpace(line)
if len(line) == 0 {
log.Println("Got empty line from output of ipmi-sel.")
logrus.Warnf("Got empty line from output of ipmi-sel.")
continue
}
log.Printf("Line: `%s`\n", line)
logrus.Debugf("Line: `%s`", line)
ev, err := newIPMIEvent(line)
log.Printf("Event: %#v\n", ev)
logrus.Debugf("Event: %#v", ev)
if err != nil {
log.Printf("Could not parse line `%s`, err: `%s`", line, err)
logrus.Errorf("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 4e32285

Please sign in to comment.