-
Notifications
You must be signed in to change notification settings - Fork 32
/
command.go
100 lines (92 loc) · 3.32 KB
/
command.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
package wrap
import (
"fmt"
"os"
"github.com/mackerelio/mackerel-agent/config"
"github.com/mackerelio/mkr/logger"
"github.com/urfave/cli"
)
// Command is definition of mkr wrap
var Command = cli.Command{
Name: "wrap",
Usage: "Wrap and monitor batch jobs to run with cron etc",
ArgsUsage: "[--name|-n <name>] [OPTIONS] -- /path/to/batch",
Description: `
Wrap a batch command with specifying it as arguments. If the command failed
with non-zero exit code, it sends a report to Mackerel and raises an alert.
It is useful for cron jobs etc.
`,
Action: doWrap,
Flags: []cli.Flag{
cli.StringFlag{Name: "name, n", Value: "", Usage: "The `check-name` which must be unique on a host. If it is empty it will be automatically derived."},
cli.BoolFlag{Name: "detail, d", Usage: "send a detailed report contains command output"},
cli.StringFlag{Name: "note, N", Value: "", Usage: "`note` of the job"},
cli.StringFlag{Name: "host, H", Value: "", Usage: "`hostID`"},
cli.BoolFlag{Name: "warning, w", Usage: "alerts as warning"},
cli.BoolFlag{Name: "auto-close, a", Usage: "automatically close an existing alert when the command success"},
cli.DurationFlag{Name: "notification-interval, I", Usage: "The notification re-sending `interval`. If it is zero, never re-send. (minimum 10 minutes)"},
// XXX Implementation of maxCheckAttempts is difficult because the
// execution interval of cron or batches are not always one-minute.
// This is due to the server-side logic of the Mackerel.
},
}
func doWrap(c *cli.Context) error {
confFile := c.GlobalString("conf")
var conf *config.Config
if _, err := os.Stat(confFile); err == nil {
conf, err = config.LoadConfig(confFile)
if err != nil {
logger.Logf("error", "[mkr wrap] failed to load the config %q: %s", confFile, err)
}
} else {
logger.Logf("info", "[mkr wrap] configuraion file %q not found", confFile)
}
if conf == nil {
// fallback default config
conf = config.DefaultConfig
}
apibase := c.GlobalString("apibase")
if apibase == "" {
apibase = conf.Apibase
}
apikey := os.Getenv("MACKEREL_APIKEY")
if apikey == "" {
apikey = conf.Apikey
}
if apikey == "" {
logger.Log("error", "[mkr wrap] failed to detect Mackerel APIKey. Try to specify in mackerel-agent.conf or export MACKEREL_APIKEY='<Your apikey>'")
}
var hostID string
if id := c.String("host"); id != "" {
hostID = id
} else {
hostID, _ = conf.LoadHostID()
}
if hostID == "" {
logger.Log("error", "[mkr wrap] failed to load hostID. Try to specify -host option explicitly")
}
// Since command execution has the highest priority, even when the config
// loading is failed, or apikey or hostID is empty, we don't return errors
// and only output the log here.
cmd := c.Args()
if len(cmd) > 0 && cmd[0] == "--" {
cmd = cmd[1:]
}
if len(cmd) < 1 {
return fmt.Errorf("no commands specified")
}
return (&wrap{
apibase: apibase,
name: c.String("name"),
detail: c.Bool("detail"),
note: c.String("note"),
warning: c.Bool("warning"),
autoClose: c.Bool("auto-close"),
notificationInterval: c.Duration("notification-interval"),
hostID: hostID,
apikey: apikey,
cmd: cmd,
outStream: os.Stdout,
errStream: os.Stderr,
}).run()
}