-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
62 lines (51 loc) · 1.71 KB
/
main.go
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
package main
import (
"fmt"
"log"
"log/syslog"
"os"
"os/exec"
"strings"
)
func main() {
if len(os.Args) < 2 {
fmt.Println("ERROR incorrect usage: onerror cmd [args...]")
os.Exit(2)
}
// command to run and its args if any
cmd := os.Args[1]
cmdArgs := os.Args[2:]
// Configure logger to write to the syslog NOTICE
sysLog, err := syslog.New(syslog.LOG_NOTICE|syslog.LOG_CRON, cmd)
if err != nil {
log.Println("onerror: Error connecting to syslog: ", err)
log.Println("onerror: Continuing executing, sending output to stderr")
}
stdOutStdErr, err := exec.Command(cmd, cmdArgs...).CombinedOutput()
if err == nil {
// Command was successful, output to syslog/stderr and exit
if sysLog != nil {
sysLog.Notice(fmt.Sprintf("onerror: SUCCESS runing %s %+v\n", cmd, cmdArgs))
sysLog.Notice("onerror: Command output:\n")
for _, line := range strings.Split(string(stdOutStdErr), "\n") {
sysLog.Notice(fmt.Sprintf("onerror: %s\n", string(line)))
}
} else {
log.Printf("onerror: SUCCESS running %s %+v\n", cmd, cmdArgs)
log.Printf("onerror: Command output:\n%s\n", stdOutStdErr)
}
os.Exit(0)
}
// Command failed, print to all output to syslog if available, and stderr
if sysLog != nil {
sysLog.Err(fmt.Sprintf("onerror: ERROR running %s %+v: %s\n", cmd, cmdArgs, err.Error()))
sysLog.Err("onerror: Command output:\n")
for _, line := range strings.Split(string(stdOutStdErr), "\n") {
sysLog.Err(fmt.Sprintf("onerror: %s\n", line))
}
}
log.Printf("onerror: ERROR running %s %+v %s\n", cmd, cmdArgs, err.Error())
log.Printf("onerror: Command output:\n%s\n", stdOutStdErr)
// TODO: return exit value of cmd
os.Exit(1)
}