This repository has been archived by the owner on Jun 14, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 73
/
command.go
333 lines (296 loc) · 8.56 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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
package main
import (
"fmt"
"github.com/prometheus/alertmanager/template"
"log"
"os"
"os/exec"
"sort"
"strconv"
"strings"
"sync"
"syscall"
"unicode"
)
const (
// Enum mask for kinds of results
CmdOk Result = 1 << iota
CmdFail Result = 1 << iota
CmdSigOk Result = 1 << iota
CmdSigFail Result = 1 << iota
CmdSkipSig Result = 1 << iota
)
var (
ResultStrings = map[Result]string{
CmdOk: "Ok",
CmdFail: "Fail",
CmdSigOk: "SigOk",
CmdSigFail: "SigFail",
CmdSkipSig: "SkipSig",
}
signals = map[string]syscall.Signal{
"SIGABRT": syscall.SIGABRT,
"SIGALRM": syscall.SIGALRM,
"SIGBUS": syscall.SIGBUS,
"SIGCHLD": syscall.SIGCHLD,
"SIGCONT": syscall.SIGCONT,
"SIGFPE": syscall.SIGFPE,
"SIGHUP": syscall.SIGHUP,
"SIGILL": syscall.SIGILL,
"SIGINT": syscall.SIGINT,
"SIGIO": syscall.SIGIO,
"SIGIOT": syscall.SIGIOT,
"SIGKILL": syscall.SIGKILL,
"SIGPIPE": syscall.SIGPIPE,
"SIGPROF": syscall.SIGPROF,
"SIGQUIT": syscall.SIGQUIT,
"SIGSEGV": syscall.SIGSEGV,
"SIGSTOP": syscall.SIGSTOP,
"SIGSYS": syscall.SIGSYS,
"SIGTERM": syscall.SIGTERM,
"SIGTRAP": syscall.SIGTRAP,
"SIGTSTP": syscall.SIGTSTP,
"SIGTTIN": syscall.SIGTTIN,
"SIGTTOU": syscall.SIGTTOU,
"SIGURG": syscall.SIGURG,
"SIGUSR1": syscall.SIGUSR1,
"SIGUSR2": syscall.SIGUSR2,
"SIGVTALRM": syscall.SIGVTALRM,
"SIGWINCH": syscall.SIGWINCH,
"SIGXCPU": syscall.SIGXCPU,
"SIGXFSZ": syscall.SIGXFSZ,
}
)
type Result int
type CommandResult struct {
Kind Result
Err error
}
// Command represents a command that could be run based on what labels match
type Command struct {
Cmd string `yaml:"cmd"`
Args []string `yaml:"args"`
// Only execute this command when all of the given labels match.
// The CommonLabels field of prometheus alert data is used for comparison.
MatchLabels map[string]string `yaml:"match_labels"`
// How many instances of this command can run at the same time.
// A zero or negative value is interpreted as 'no limit'.
Max int `yaml:"max"`
// Whether we should let the caller know if a command failed.
// Defaults to true.
// The value is a pointer to bool with the 'omitempty' tag,
// so we can tell when the value was not defined,
// meaning we'll provide the default value.
NotifyOnFailure *bool `yaml:"notify_on_failure,omitempty"`
// Whether command will ignore a 'resolved' notification for a matching command,
// and continue running to completion.
// Defaults to false.
IgnoreResolved *bool `yaml:"ignore_resolved,omitempty"`
ResolvedSig string `yaml:"resolved_signal"`
}
// Return a string representing the result state
func (r Result) String() string {
var has = make([]string, 0)
// To keep the string's content consistent, we'll sort the flags by the enum value, lowest to highest.
var index = make(map[string]Result)
for f, n := range ResultStrings {
index[n] = f
}
less := func(i, j int) bool {
iKey := has[i]
jKey := has[j]
if index[iKey] < index[jKey] {
return true
} else {
return false
}
}
for f, n := range ResultStrings {
if r.Has(f) {
has = append(has, n)
}
}
sort.Slice(has, less)
return strings.Join(has, "|")
}
// Has returns true if the result has the given flag set
func (r Result) Has(flag Result) bool {
return r&flag != 0
}
// Equal returns true if the Command is identical to another Command
func (c Command) Equal(other *Command) bool {
if c.Cmd != other.Cmd {
return false
}
if len(c.Args) != len(other.Args) {
return false
}
if len(c.MatchLabels) != len(other.MatchLabels) {
return false
}
for i, arg := range c.Args {
if arg != other.Args[i] {
return false
}
}
for k, v := range c.MatchLabels {
otherValue, ok := other.MatchLabels[k]
if !ok {
return false
}
if v != otherValue {
return false
}
}
return true
}
// Fingerprint returns the fingerprint of the first alarm that matches the command's labels.
// The first fingerprint found is returned if we have no MatchLabels defined.
func (c Command) Fingerprint(msg *template.Data) (string, bool) {
for _, alert := range msg.Alerts {
matched := 0
for k, v := range c.MatchLabels {
other, ok := alert.Labels[k]
if ok && v == other {
matched += 1
}
}
if matched == len(c.MatchLabels) {
return alert.Fingerprint, true
}
}
return "", false
}
// Matches returns true if all of its labels match against the given prometheus alert message.
// If we have no MatchLabels defined, we also return true.
func (c Command) Matches(msg *template.Data) bool {
if len(c.MatchLabels) == 0 {
return true
}
for k, v := range c.MatchLabels {
other, ok := msg.CommonLabels[k]
if !ok || v != other {
return false
}
}
return true
}
// Run executes the command, potentially signalling it if alarm that triggered command resolves.
// out channel is used to indicate the result of running or killing the program. May indicate errors.
// quit channel is used to determine if execution should quit early
// done channel is used to indicate to caller when execution has completed
func (c Command) Run(out chan<- CommandResult, quit chan struct{}, done chan struct{}, env ...string) {
defer close(out)
defer close(done)
var wg sync.WaitGroup
cmd := c.WithEnv(env...)
// We use a buffer of one, so that if the command is killed before it finishes,
// we will still be able to close the channel and end the Command.Run method;
// There won't be a channel reader left, because the select statement ended when quit was read from.
cmdOut := make(chan CommandResult, 1)
wg.Add(1)
go func() {
defer close(cmdOut)
defer wg.Done()
err := cmd.Run()
if err == nil {
cmdOut <- CommandResult{Kind: CmdOk, Err: nil}
} else {
cmdOut <- CommandResult{Kind: CmdFail, Err: err}
}
}()
select {
case r := <-cmdOut:
out <- r
case <-quit:
if c.ShouldIgnoreResolved() {
out <- CommandResult{Kind: CmdSkipSig, Err: nil}
} else {
sig, err := c.ParseSignal()
if err != nil {
errMsg := fmt.Errorf("Can't use signal %s to notify pid %d for command %s: %w", c.ResolvedSig, cmd.Process.Pid, c, err)
out <- CommandResult{Kind: CmdSigFail, Err: errMsg}
}
err = cmd.Process.Signal(sig)
if err == nil {
out <- CommandResult{Kind: CmdSigOk, Err: nil}
} else {
errMsg := fmt.Errorf("Failed sending %s to pid %d for command %s: %w", sig, cmd.Process.Pid, c, err)
out <- CommandResult{Kind: CmdSigFail, Err: errMsg}
}
}
}
wg.Wait()
}
// ShouldIgnoreResolved returns the interpreted value of c.IgnoreResolved.
// This method is used to work around ambiguity of unmarshalling yaml boolean values,
// due to the default value of a bool being false.
func (c Command) ShouldIgnoreResolved() bool {
if c.IgnoreResolved == nil {
// Default to false when value is not defined
return false
}
return *c.IgnoreResolved
}
// ShouldNotify returns the interpreted value of c.NotifyOnFailure.
// This method is used to work around ambiguity of unmarshalling yaml boolean values,
// due to the default value of a bool being false.
func (c Command) ShouldNotify() bool {
if c.NotifyOnFailure == nil {
// Default to true when value is not defined
return true
}
return *c.NotifyOnFailure
}
// ParseSignal returns the signal that is meant to be used for notifying the command that its triggering condition has resolved,
// and any error encountered while parsing.
func (c Command) ParseSignal() (os.Signal, error) {
if len(c.ResolvedSig) == 0 {
return os.Kill, nil
}
var notFound = os.Signal(syscall.Signal(-1))
if IsDigit(c.ResolvedSig) {
n, err := strconv.Atoi(c.ResolvedSig)
if err != nil {
return notFound, err
}
return os.Signal(syscall.Signal(n)), nil
}
want := strings.ToUpper(c.ResolvedSig)
sig, ok := signals[strings.ToUpper(c.ResolvedSig)]
if !ok {
return notFound, fmt.Errorf("Unknown signal %s", want)
}
return sig, nil
}
// String returns a string representation of the command
func (c Command) String() string {
if len(c.Args) == 0 {
return c.Cmd
}
return fmt.Sprintf("%s %s", c.Cmd, strings.Join(c.Args, " "))
}
// WithEnv returns a runnable command with the given environment variables added.
// Command STDOUT and STDERR is attached to the logger.
func (c Command) WithEnv(env ...string) *exec.Cmd {
lw := log.Writer()
cmd := exec.Command(c.Cmd, c.Args...)
cmd.Env = append(os.Environ(), env...)
cmd.Stdout = lw
cmd.Stderr = lw
return cmd
}
// IsDigit returns true if all of the string consists of digits
func IsDigit(s string) bool {
if len(s) == 0 {
return false
}
val := []rune(s)
var count = 0
for _, r := range val {
if unicode.IsDigit(r) {
count++
}
}
return count == len(val)
}