-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathalerts.sh
156 lines (140 loc) · 3.46 KB
/
alerts.sh
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
#!/bin/bash
requiresJsonnet() {
if ! type "jsonnet" > /dev/null; then
echo "you need you install jsonnet to run this script"
echo "follow the instructions on https://github.com/google/jsonnet"
exit 1
fi
}
setup() {
STATUS=$(curl -s -o /dev/null -w '%{http_code}' http://admin:[email protected]/api/alert-notifications/1)
if [ $STATUS -eq 200 ]; then
echo "Email already exists, skipping..."
else
curl -H "Content-Type: application/json" \
-d '{
"name": "Email",
"type": "email",
"isDefault": false,
"sendReminder": false,
"uploadImage": true,
"settings": {
"addresses": "[email protected]"
}
}' \
http://admin:[email protected]/api/alert-notifications
fi
STATUS=$(curl -s -o /dev/null -w '%{http_code}' http://admin:[email protected]/api/alert-notifications/2)
if [ $STATUS -eq 200 ]; then
echo "Slack already exists, skipping..."
else
curl -H "Content-Type: application/json" \
-d '{
"name": "Slack",
"type": "slack",
"isDefault": false,
"sendReminder": false,
"uploadImage": true
}' \
http://admin:[email protected]/api/alert-notifications
fi
}
slack() {
enabled=true
url=''
remind=false
remindEvery='10m'
while getopts ":e:u:dr" o; do
case "${o}" in
e)
remindEvery=${OPTARG}
;;
u)
url=${OPTARG}
;;
d)
enabled=false
;;
r)
remind=true
;;
esac
done
shift $((OPTIND-1))
curl -X PUT \
-H "Content-Type: application/json" \
-d '{
"id": 2,
"name": "Slack",
"type": "slack",
"isDefault": '$enabled',
"sendReminder": '$remind',
"frequency": "'$remindEvery'",
"uploadImage": true,
"settings": {
"url": "'$url'"
}
}' \
http://admin:[email protected]/api/alert-notifications/2
}
provision() {
alerts=1
condition=65
while getopts ":a:c:" o; do
case "${o}" in
a)
alerts=${OPTARG}
;;
c)
condition=${OPTARG}
;;
esac
done
shift $((OPTIND-1))
requiresJsonnet
find grafana/provisioning/dashboards/alerts -maxdepth 1 -name 'alert*.json' -delete
jsonnet -m grafana/provisioning/dashboards/alerts grafana/provisioning/alerts.jsonnet --ext-code alerts=$alerts --ext-code condition=$condition
}
pause() {
curl -H "Content-Type: application/json" \
-d '{"paused":true}' \
http://admin:[email protected]/api/admin/pause-all-alerts
}
unpause() {
curl -H "Content-Type: application/json" \
-d '{"paused":false}' \
http://admin:[email protected]/api/admin/pause-all-alerts
}
usage() {
echo -e "Usage: ./alerts.sh COMMAND [OPTIONS]\n"
echo -e "Commands"
echo -e " setup\t\t creates default alert notification channels"
echo -e " slack\t\t configure slack notification channel"
echo -e " [-d]\t\t\t disable notifier, default enabled"
echo -e " [-u]\t\t\t url"
echo -e " [-r]\t\t\t send reminders"
echo -e " [-e <remind every>]\t\t default 10m\n"
echo -e " provision\t provision alerts"
echo -e " [-a <alert rule count>]\t default 1"
echo -e " [-c <condition value>]\t default 65\n"
echo -e " pause\t\t pause all alerts"
echo -e " unpause\t unpause all alerts"
}
main() {
local cmd=$1
if [[ $cmd == "setup" ]]; then
setup
elif [[ $cmd == "slack" ]]; then
slack "${@:2}"
elif [[ $cmd == "provision" ]]; then
provision "${@:2}"
elif [[ $cmd == "pause" ]]; then
pause
elif [[ $cmd == "unpause" ]]; then
unpause
fi
if [[ -z "$cmd" ]]; then
usage
fi
}
main "$@"