1#!/bin/bash
2
3requiresJsonnet() {
4		if ! type "jsonnet" > /dev/null; then
5				echo "you need you install jsonnet to run this script"
6				echo "follow the instructions on https://github.com/google/jsonnet"
7				exit 1
8		fi
9}
10
11setup() {
12	STATUS=$(curl -s -o /dev/null -w '%{http_code}' http://admin:admin@grafana.loc/api/alert-notifications/1)
13  if [ $STATUS -eq 200 ]; then
14    echo "Email already exists, skipping..."
15  else
16		curl -H "Content-Type: application/json" \
17		-d '{
18			"name": "Email",
19			"type":  "email",
20			"isDefault": false,
21			"sendReminder": false,
22			"uploadImage": true,
23			"settings": {
24				"addresses": "user@test.com"
25			}
26		}' \
27		http://admin:admin@grafana.loc/api/alert-notifications
28  fi
29
30	STATUS=$(curl -s -o /dev/null -w '%{http_code}' http://admin:admin@grafana.loc/api/alert-notifications/2)
31  if [ $STATUS -eq 200 ]; then
32    echo "Slack already exists, skipping..."
33  else
34		curl -H "Content-Type: application/json" \
35		-d '{
36			"name": "Slack",
37			"type":  "slack",
38			"isDefault": false,
39			"sendReminder": false,
40			"uploadImage": true
41		}' \
42		http://admin:admin@grafana.loc/api/alert-notifications
43  fi
44}
45
46slack() {
47	enabled=true
48	url=''
49	remind=false
50	remindEvery='10m'
51
52	while getopts ":e:u:dr" o; do
53    case "${o}" in
54				e)
55            remindEvery=${OPTARG}
56            ;;
57				u)
58            url=${OPTARG}
59            ;;
60				d)
61            enabled=false
62            ;;
63				r)
64            remind=true
65            ;;
66    esac
67	done
68	shift $((OPTIND-1))
69
70	curl -X PUT \
71		-H "Content-Type: application/json" \
72		-d '{
73			"id": 2,
74			"name": "Slack",
75			"type":  "slack",
76			"isDefault": '$enabled',
77			"sendReminder": '$remind',
78			"frequency": "'$remindEvery'",
79			"uploadImage": true,
80			"settings": {
81				"url": "'$url'"
82			}
83		}' \
84		http://admin:admin@grafana.loc/api/alert-notifications/2
85}
86
87provision() {
88	alerts=1
89	condition=65
90	while getopts ":a:c:" o; do
91    case "${o}" in
92        a)
93            alerts=${OPTARG}
94            ;;
95				c)
96            condition=${OPTARG}
97            ;;
98    esac
99	done
100	shift $((OPTIND-1))
101
102	requiresJsonnet
103
104	find grafana/provisioning/dashboards/alerts -maxdepth 1 -name 'alert*.json' -delete
105	jsonnet -m grafana/provisioning/dashboards/alerts grafana/provisioning/alerts.jsonnet --ext-code alerts=$alerts --ext-code condition=$condition
106}
107
108pause() {
109	curl -H "Content-Type: application/json" \
110  -d '{"paused":true}' \
111  http://admin:admin@grafana.loc/api/admin/pause-all-alerts
112}
113
114unpause() {
115	curl -H "Content-Type: application/json" \
116  -d '{"paused":false}' \
117  http://admin:admin@grafana.loc/api/admin/pause-all-alerts
118}
119
120usage() {
121	echo -e "Usage: ./alerts.sh COMMAND [OPTIONS]\n"
122	echo -e "Commands"
123	echo -e "  setup\t\t creates default alert notification channels"
124	echo -e "  slack\t\t configure slack notification channel"
125	echo -e "    [-d]\t\t\t disable notifier, default enabled"
126	echo -e "    [-u]\t\t\t url"
127	echo -e "    [-r]\t\t\t send reminders"
128	echo -e "    [-e <remind every>]\t\t default 10m\n"
129	echo -e "  provision\t provision alerts"
130	echo -e "    [-a <alert rule count>]\t default 1"
131	echo -e "    [-c <condition value>]\t default 65\n"
132	echo -e "  pause\t\t pause all alerts"
133	echo -e "  unpause\t unpause all alerts"
134}
135
136main() {
137	local cmd=$1
138
139	if [[ $cmd == "setup" ]]; then
140		setup
141	elif [[ $cmd == "slack" ]]; then
142		slack "${@:2}"
143	elif [[ $cmd == "provision" ]]; then
144		provision "${@:2}"
145	elif [[ $cmd == "pause" ]]; then
146		pause
147	elif [[ $cmd == "unpause" ]]; then
148		unpause
149	fi
150
151  if [[ -z "$cmd" ]]; then
152		usage
153	fi
154}
155
156main "$@"
157