forked from spaceapegames/go-wavefront
-
Notifications
You must be signed in to change notification settings - Fork 12
/
alerts_example_test.go
203 lines (173 loc) · 4.47 KB
/
alerts_example_test.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
package wavefront_test
import (
"fmt"
"github.com/WavefrontHQ/go-wavefront-management-api/v2"
"log"
"os"
)
func ExampleAlerts() {
config := &wavefront.Config{
Address: "test.wavefront.com",
Token: "xxxx-xxxx-xxxx-xxxx-xxxx",
}
client, err := wavefront.NewClient(config)
if err != nil {
log.Fatal(err)
}
alerts := client.Alerts()
targets := client.Targets()
// ######################
// EXAMPLE: Classic Alert
// ######################
a := &wavefront.Alert{
Name: "My First Alert",
Target: "[email protected]",
Condition: "ts(servers.cpu.usage, dc=dc2) > 10 * 10",
DisplayExpression: "ts(servers.cpu.usage, dc=dc2)",
Minutes: 2,
ResolveAfterMinutes: 2,
Severity: "WARN",
Tags: []string{"dc1", "synergy"},
}
// Create the alert on Wavefront
err = alerts.Create(a)
if err != nil {
log.Fatal(err)
}
// The ID field is now set, so we can update/delete the Alert
fmt.Println("alert ID is", *a.ID)
// Alternatively we could search for the Alert
err = alerts.Get(&wavefront.Alert{
ID: a.ID,
})
if err != nil {
log.Fatal(err)
}
// Update the Alert
a.Target = "[email protected],[email protected]"
err = alerts.Update(a)
if err != nil {
log.Fatal(err)
}
// Delete the Alert
err = alerts.Delete(a, true)
if err != nil {
log.Fatal(err)
}
fmt.Println("alert deleted")
// ########################
// EXAMPLE: Threshold Alert
// ########################
// Threshold Alerts only accept custom alert targets
// Create Alert Targets that can be used for the example
tmpl, _ := os.ReadFile("./target-template.tmpl")
targetA := wavefront.Target{
Title: "test target",
Description: "testing something A",
Method: "WEBHOOK",
Recipient: "https://hooks.slack.com/services/test/me",
ContentType: "application/json",
CustomHeaders: map[string]string{
"Testing": "true",
},
Triggers: []string{"ALERT_OPENED", "ALERT_RESOLVED"},
Template: string(tmpl),
}
targetB := wavefront.Target{
Title: "test target",
Description: "testing something B",
Method: "WEBHOOK",
Recipient: "https://hooks.slack.com/services/test/me",
ContentType: "application/json",
CustomHeaders: map[string]string{
"Testing": "true",
},
Triggers: []string{"ALERT_OPENED", "ALERT_RESOLVED"},
Template: string(tmpl),
}
targetC := wavefront.Target{
Title: "test target",
Description: "testing something C",
Method: "WEBHOOK",
Recipient: "https://hooks.slack.com/services/test/me",
ContentType: "application/json",
CustomHeaders: map[string]string{
"Testing": "true",
},
Triggers: []string{"ALERT_OPENED", "ALERT_RESOLVED"},
Template: string(tmpl),
}
// Create the targets on Wavefront
err = targets.Create(&targetA)
if err != nil {
log.Fatal(err)
}
err = targets.Create(&targetB)
if err != nil {
log.Fatal(err)
}
err = targets.Create(&targetC)
if err != nil {
log.Fatal(err)
}
fmt.Println("alert targets created")
strA := fmt.Sprintf("target:%s", *targetA.ID)
strB := fmt.Sprintf("target:%s", *targetB.ID)
strC := fmt.Sprintf("target:%s", *targetC.ID)
mta := &wavefront.Alert{
Name: "My First Threshold Alert",
AlertType: "THRESHOLD",
Targets: map[string]string{
"smoke": strA,
"warn": strB,
},
Conditions: map[string]string{
"smoke": "ts(servers.cpu.usage) > 70",
"warn": "ts(servers.cpu.usage) > 100",
},
DisplayExpression: "ts(servers.cpu.usage)",
Minutes: 2,
ResolveAfterMinutes: 2,
SeverityList: []string{"SMOKE", "WARN"},
Tags: []string{"dc1", "synergy"},
}
// Create the thresold alert on Wavefront
err = alerts.Create(mta)
if err != nil {
log.Fatal(err)
}
// The ID field is now set, so we can update/delete the Threshold Alert
fmt.Println("threshold alert ID is", *mta.ID)
// Alternatively we could search for the Threshold Alert
err = alerts.Get(&wavefront.Alert{
ID: mta.ID,
})
if err != nil {
log.Fatal(err)
}
// Update the Threshold Alert
mta.Targets["smoke"] = strC
err = alerts.Update(mta)
if err != nil {
log.Fatal(err)
}
// Delete the Threshold Alert
err = alerts.Delete(mta, true)
if err != nil {
log.Fatal(err)
}
fmt.Println("threshold alert deleted")
err = targets.Delete(&targetA)
if err != nil {
log.Fatal(err)
}
err = targets.Delete(&targetB)
if err != nil {
log.Fatal(err)
}
err = targets.Delete(&targetC)
if err != nil {
log.Fatal(err)
}
fmt.Println("alert targets deleted")
}