-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
84 lines (73 loc) · 2.44 KB
/
main.go
File metadata and controls
84 lines (73 loc) · 2.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
//go:generate bash ./g_version.sh
package main
import (
"fmt"
"log"
"log/syslog"
"os"
"os/exec"
"path"
"strings"
"github.com/sirupsen/logrus"
logSys "github.com/sirupsen/logrus/hooks/syslog"
"gopkg.in/alecthomas/kingpin.v2"
)
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 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()
eventTime = app.Flag("event-time", "if set, the event time will be used instead of current time (Use --no-event-time to set to false)").Default("true").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:]))
if *debug {
logrus.SetLevel(logrus.DebugLevel)
}
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)
}
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 {
logrus.Errorf("Got error running `%s`: `%s` : `%s`", *ipmiSel, err, outStr) // purposely do not quit
}
lines := strings.Split(strings.TrimSpace(outStr), "\n")
logrus.Debugf("%#v", lines)
if len(lines) >= 0 && lines[0] != "" {
for _, line := range lines {
line = strings.TrimSpace(line)
if len(line) == 0 {
logrus.Warnf("Got empty line from output of ipmi-sel.")
continue
}
logrus.Debugf("Line: `%s`", line)
ev, err := newIPMIEvent(line)
logrus.Debugf("Event: %#v", ev)
if err != nil {
logrus.Errorf("Could not parse line `%s`, err: `%s`", line, err)
continue
}
fmt.Println(ev.InfluxDB(*checkName, hostname, *eventTime))
}
} else if *deadman {
ev := newEmptyIPMIEvent()
fmt.Println(ev.InfluxDB(*checkName, hostname, *eventTime))
}
}