diff --git a/main.go b/main.go index d9f742b..3a2ad37 100644 --- a/main.go +++ b/main.go @@ -9,6 +9,7 @@ import ( "os/exec" "path" "strings" + "syscall" "github.com/sirupsen/logrus" logSys "github.com/sirupsen/logrus/hooks/syslog" @@ -22,6 +23,7 @@ var ( 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() + useSudo = app.Flag("sudo", "if set, this program will use sudo to run ipmi-sel").Default("true").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() ) @@ -47,12 +49,23 @@ func main() { log.SetFlags(log.Lshortfile) } + if uid := syscall.Getuid(); !*useSudo && uid != 0 { + logrus.Fatal("Needs to be executed with UID 0, but it is running with UID ", uid) + } + 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() + args := []string{"--debug", "--output-event-state", "--comma-separated-output", "--no-header-output"} + cmd := exec.Command(*ipmiSel, args...) + if *useSudo { + args = append([]string{*ipmiSel}, args...) + cmd = exec.Command("sudo", args...) + } + + out, err := cmd.Output() outStr := string(out) if err != nil { logrus.Errorf("Got error running `%s`: `%s` : `%s`", *ipmiSel, err, outStr) // purposely do not quit