...
1
2
3
4
5
6
7
8
9
10
11
12
13
14 package cli
15
16 import (
17 "context"
18 "fmt"
19 "time"
20
21 "github.com/go-openapi/strfmt"
22 "gopkg.in/alecthomas/kingpin.v2"
23
24 "github.com/prometheus/alertmanager/api/v2/client/alert"
25 "github.com/prometheus/alertmanager/api/v2/models"
26 )
27
28 type alertAddCmd struct {
29 annotations []string
30 generatorURL string
31 labels []string
32 start string
33 end string
34 }
35
36 const alertAddHelp = `Add a new alert.
37
38 This command is used to add a new alert to Alertmanager.
39
40 To add a new alert with labels:
41
42 amtool alert add alertname=foo node=bar
43
44 If alertname is omitted and the first argument does not contain a '=' then it will
45 be assumed to be the value of the alertname pair.
46
47 amtool alert add foo node=bar
48
49 One or more annotations can be added using the --annotation flag:
50
51 amtool alert add foo node=bar \
52 --annotation=runbook='http://runbook.biz' \
53 --annotation=summary='summary of the alert' \
54 --annotation=description='description of the alert'
55
56 Additional flags such as --generator-url, --start, and --end are also supported.
57 `
58
59 func configureAddAlertCmd(cc *kingpin.CmdClause) {
60 var (
61 a = &alertAddCmd{}
62 addCmd = cc.Command("add", alertAddHelp)
63 )
64 addCmd.Arg("labels", "List of labels to be included with the alert").StringsVar(&a.labels)
65 addCmd.Flag("generator-url", "Set the URL of the source that generated the alert").StringVar(&a.generatorURL)
66 addCmd.Flag("start", "Set when the alert should start. RFC3339 format 2006-01-02T15:04:05-07:00").StringVar(&a.start)
67 addCmd.Flag("end", "Set when the alert should should end. RFC3339 format 2006-01-02T15:04:05-07:00").StringVar(&a.end)
68 addCmd.Flag("annotation", "Set an annotation to be included with the alert").StringsVar(&a.annotations)
69 addCmd.Action(execWithTimeout(a.addAlert))
70 }
71
72 func (a *alertAddCmd) addAlert(ctx context.Context, _ *kingpin.ParseContext) error {
73 if len(a.labels) > 0 {
74
75
76 if _, err := parseLabels([]string{a.labels[0]}); err != nil {
77 a.labels[0] = fmt.Sprintf("alertname=%s", a.labels[0])
78 }
79 }
80
81 labels, err := parseLabels(a.labels)
82 if err != nil {
83 return err
84 }
85
86 annotations, err := parseLabels(a.annotations)
87 if err != nil {
88 return err
89 }
90
91 var startsAt, endsAt time.Time
92 if a.start != "" {
93 startsAt, err = time.Parse(time.RFC3339, a.start)
94 if err != nil {
95 return err
96 }
97 }
98 if a.end != "" {
99 endsAt, err = time.Parse(time.RFC3339, a.end)
100 if err != nil {
101 return err
102 }
103 }
104
105 pa := &models.PostableAlert{
106 Alert: models.Alert{
107 GeneratorURL: strfmt.URI(a.generatorURL),
108 Labels: labels,
109 },
110 Annotations: annotations,
111 StartsAt: strfmt.DateTime(startsAt),
112 EndsAt: strfmt.DateTime(endsAt),
113 }
114 alertParams := alert.NewPostAlertsParams().WithContext(ctx).
115 WithAlerts(models.PostableAlerts{pa})
116
117 amclient := NewAlertmanagerClient(alertmanagerURL)
118
119 _, err = amclient.Alert.PostAlerts(alertParams)
120 return err
121 }
122
View as plain text