This repository has been archived by the owner on Feb 17, 2023. It is now read-only.
forked from tronyx/tronitor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
uptimerobot_monitor_utility.sh
309 lines (285 loc) · 10.9 KB
/
uptimerobot_monitor_utility.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
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
#!/usr/bin/env bash
#
# Script to utilize the UptimeRobot API to retrieve and work with monitors.
# Tronyx
# Specify UptimeRobot API key
apiKey=''
webhookUrl=''
# Set notifyAll to true for notification to apply for all running state as well
notifyAll='false'
# Define usage and script options
function usage {
cat <<EOM
Usage: $(basename "$0") -[OPTION] (ARGUMENT)...
-l List all UptimeRobot monitors.
-f Find all paused UptimeRobot monitors.
-n Find all paused UptimeRobot monitors
without an unpause prompt.
-a Find all paused UptimeRobot monitors
without an unpause prompt and send
an alert via Discord webhook.
-p VALUE Pause specified UptimeRobot monitors.
Option accepts arguments in the form of "all"
or a comma-separated list of monitors, IE:
"$(basename "$0") -p all"
"$(basename "$0") -p 18095687,18095688,18095689"
-u VALUE Unpause specified UptimeRobot monitors.
Option accepts arguments in the form of "all"
or a comma-separated list of monitors, IE:
"$(basename "$0") -u all"
"$(basename "$0") -u 18095687,18095688,18095689"
-h Display this usage dialog.
EOM
exit 2
}
while getopts "hlfnap:u:" OPTION
do
case "$OPTION" in
l)
list=true
;;
f)
find=true
prompt=true
;;
n)
find=true
prompt=false
;;
a)
find=true
prompt=false
alert=true
;;
p)
pause=true
pauseType="${OPTARG}"
;;
u)
unpause=true
unpauseType="${OPTARG}"
;;
:)
echo "Option -${OPTARG} requires an argument."
exit 1
;;
h|*)
usage
;;
esac
done
if [[ $1 == "" ]]; then
usage
exit 1
elif [ "${apiKey}" = "" ]; then
echo "You didn't define your API key!"
exit 1
elif [ "${webhookUrl}" = "" ] && [ "${alert}" = "true" ]; then
echo "You didn't define your Discord webhook URL!"
exit 1
else
:
fi
# Create directory to neatly store temp files
function create_dir {
mkdir -p /tmp/uptimerobot_monitor_utility
}
# Check that provided API Key is valid
function check_api_key {
curl -s -X POST https://api.uptimerobot.com/v2/getAccountDetails -d "api_key=${apiKey}" -d "format=json" > /tmp/uptimerobot_monitor_utility/api_test_full.txt
status=$(egrep -oi '"stat":"[a-z]*"' /tmp/uptimerobot_monitor_utility/api_test_full.txt |awk -F':' '{print $2}' |tr -d '"')
if [ "${status}" = "fail" ]; then
echo "The API Key that you provided is not valid!"
exit 1
elif [ "${status}" = "ok" ]; then
:
fi
}
# Grab data for all monitors
function get_data {
curl -s -X POST https://api.uptimerobot.com/v2/getMonitors -d "api_key=${apiKey}" -d "format=json" > /tmp/uptimerobot_monitor_utility/ur_monitors_full.txt
}
# Create list of monitor IDs
function get_monitors {
egrep -oi '"id":[!0-9]*' /tmp/uptimerobot_monitor_utility/ur_monitors_full.txt |tr -d '"id:' > /tmp/uptimerobot_monitor_utility/ur_monitors.txt
}
# Create individual monitor files
function create_monitor_files {
for monitor in $(cat /tmp/uptimerobot_monitor_utility/ur_monitors.txt); do curl -s -X POST https://api.uptimerobot.com/v2/getMonitors -d "api_key=${apiKey}" -d "monitors=${monitor}" -d "format=json" > /tmp/uptimerobot_monitor_utility/"${monitor}".txt; done
}
# Create friendly output of all monitors
function create_friendly_list {
> /tmp/uptimerobot_monitor_utility/friendly_list.txt
for monitor in $(cat /tmp/uptimerobot_monitor_utility/ur_monitors.txt); do
egrep -oi '"id":[!0-9]*|"friendly_name":["^][^"]*"' /tmp/uptimerobot_monitor_utility/"${monitor}".txt > /tmp/uptimerobot_monitor_utility/"${monitor}"_short.txt
friendlyName=$(grep friend /tmp/uptimerobot_monitor_utility/"${monitor}"_short.txt |awk -F':' '{print $2}' |tr -d '"')
echo "${friendlyName} (ID: ${monitor})" >> /tmp/uptimerobot_monitor_utility/friendly_list.txt
done
}
# Display friendly list of all monitors
function display_all_monitors {
if [ -s /tmp/uptimerobot_monitor_utility/friendly_list.txt ]; then
echo "The following UptimeRobot monitors were found in your UptimeRobot account:"
cat /tmp/uptimerobot_monitor_utility/friendly_list.txt
else
echo "There are currently no monitors associated with your UptimeRobot account."
fi
}
# Find all paused monitors
function get_paused_monitors {
> /tmp/uptimerobot_monitor_utility/paused_monitors.txt
for monitor in $(cat /tmp/uptimerobot_monitor_utility/ur_monitors.txt); do
egrep -oi '"id":[!0-9]*|"friendly_name":["^][^"]*"|"status":[!0-9]*' /tmp/uptimerobot_monitor_utility/"${monitor}".txt > /tmp/uptimerobot_monitor_utility/"${monitor}"_short.txt
friendlyName=$(grep friend /tmp/uptimerobot_monitor_utility/"${monitor}"_short.txt |awk -F':' '{print $2}' |tr -d '"')
status=$(grep status /tmp/uptimerobot_monitor_utility/"${monitor}"_short.txt |awk -F':' '{print $2}' |tr -d '"')
if [ "${status}" = '0' ]; then
echo "${friendlyName} (ID: ${monitor})" >> /tmp/uptimerobot_monitor_utility/paused_monitors.txt
else
:
fi
done
}
# Display list of all paused monitors
function display_paused_monitors {
if [ -s /tmp/uptimerobot_monitor_utility/paused_monitors.txt ]; then
echo "The following UptimeRobot monitors are currently paused:"
cat /tmp/uptimerobot_monitor_utility/paused_monitors.txt
else
echo "There are currently no paused UptimeRobot monitors."
fi
}
# Prompt user to unpause monitors after finding paused monitors
function unpause_prompt {
read -p 'Would you like to unpause the paused monitors? ([y]es or [n]o): ' unpausePrompt
if ! [[ "$unpausePrompt" =~ ^(yes|y|no|n)$ ]]; then
echo "Please specify yes, y, no, or n."
else
:
fi
}
# Pause all monitors
function pause_all_monitors {
for monitor in $(cat /tmp/uptimerobot_monitor_utility/ur_monitors.txt); do
egrep -oi '"id":[!0-9]*|"friendly_name":["^][^"]*"|"status":[!0-9]*' /tmp/uptimerobot_monitor_utility/"${monitor}".txt > /tmp/uptimerobot_monitor_utility/"${monitor}"_short.txt
friendlyName=$(grep friend /tmp/uptimerobot_monitor_utility/"${monitor}"_short.txt |awk -F':' '{print $2}' |tr -d '"')
echo "Pausing ${friendlyName}:"
curl -s -X POST https://api.uptimerobot.com/v2/editMonitor -d "api_key=${apiKey}" -d "id=${monitor}" -d "status=0" |jq
echo ""
done
}
# Pause specified monitors
function pause_specified_monitors {
echo "${pauseType}" |tr , '\n' > /tmp/uptimerobot_monitor_utility/specified_monitors.txt
for monitor in $(cat /tmp/uptimerobot_monitor_utility/specified_monitors.txt); do
egrep -oi '"id":[!0-9]*|"friendly_name":["^][^"]*"|"status":[!0-9]*' /tmp/uptimerobot_monitor_utility/"${monitor}".txt > /tmp/uptimerobot_monitor_utility/"${monitor}"_short.txt
friendlyName=$(grep friend /tmp/uptimerobot_monitor_utility/"${monitor}"_short.txt |awk -F':' '{print $2}' |tr -d '"')
echo "Pausing ${friendlyName}:"
curl -s -X POST https://api.uptimerobot.com/v2/editMonitor -d "api_key=${apiKey}" -d "id=${monitor}" -d "status=0" |jq
echo ""
done
}
# Unpause all monitors
function unpause_all_monitors {
for monitor in $(cat /tmp/uptimerobot_monitor_utility/ur_monitors.txt); do
egrep -oi '"id":[!0-9]*|"friendly_name":["^][^"]*"|"status":[!0-9]*' /tmp/uptimerobot_monitor_utility/"${monitor}".txt > /tmp/uptimerobot_monitor_utility/"${monitor}"_short.txt
friendlyName=$(grep friend /tmp/uptimerobot_monitor_utility/"${monitor}"_short.txt |awk -F':' '{print $2}' |tr -d '"')
echo "Unpausing ${friendlyName}:"
curl -s -X POST https://api.uptimerobot.com/v2/editMonitor -d "api_key=${apiKey}" -d "id=${monitor}" -d "status=1" |jq
echo ""
done
}
# Unpause specified monitors
function unpause_specified_monitors {
echo "${unpauseType}" |tr , '\n' > /tmp/uptimerobot_monitor_utility/specified_monitors.txt
for monitor in $(cat /tmp/uptimerobot_monitor_utility/specified_monitors.txt); do
egrep -oi '"id":[!0-9]*|"friendly_name":["^][^"]*"|"status":[!0-9]*' /tmp/uptimerobot_monitor_utility/"${monitor}".txt > /tmp/uptimerobot_monitor_utility/"${monitor}"_short.txt
friendlyName=$(grep friend /tmp/uptimerobot_monitor_utility/"${monitor}"_short.txt |awk -F':' '{print $2}' |tr -d '"')
echo "Unpausing ${friendlyName}:"
curl -s -X POST https://api.uptimerobot.com/v2/editMonitor -d "api_key=${apiKey}" -d "id=${monitor}" -d "status=1" |jq
echo ""
done
}
# Send Discord notification
function send_notification {
if [ "${webhookUrl}" = "" ]; then
echo "You didn't define your Discord webhook, skipping notification."
else
if [ -s /tmp/uptimerobot_monitor_utility/paused_monitors.txt ]; then
pausedTests=$(paste -s -d, /tmp/uptimerobot_monitor_utility/paused_monitors.txt)
curl -s -H "Content-Type: application/json" -X POST -d '{"content": "There are currently paused UptimeRobot monitors:\n\n'"${pausedTests}"'"}' ${webhookUrl}
elif [ "${notifyAll}" = "true" ]; then
curl -s -H "Content-Type: application/json" -X POST -d '{"content": "All UptimeRobot monitors are currently running."}' ${webhookUrl}
fi
fi
}
# Cleanup temp files
function cleanup {
rm -rf /tmp/uptimerobot_monitor_utility/*.txt
}
# Run functions
create_dir
check_api_key
if [ "${list}" = "true" ]; then
get_data
get_monitors
create_monitor_files
create_friendly_list
display_all_monitors
cleanup
elif [ "${find}" = "true" ]; then
get_data
get_monitors
create_monitor_files
get_paused_monitors
display_paused_monitors
if [ -s /tmp/uptimerobot_monitor_utility/paused_monitors.txt ]; then
if [ "${prompt}" = "false" ]; then
:
else
unpause_prompt
if [[ "$unpausePrompt" =~ ^(yes|y)$ ]]; then
for monitor in $(cat /tmp/uptimerobot_monitor_utility/paused_monitors.txt |awk -F: '{print $2}' |tr -d ')' |tr -d ' '); do
friendlyName=$(cat /tmp/uptimerobot_monitor_utility/paused_monitors.txt |awk '{print $1}')
echo "Unpausing ${friendlyName}:"
curl -s -X POST https://api.uptimerobot.com/v2/editMonitor -d "api_key=${apiKey}" -d "id=${monitor}" -d "status=1" |jq
echo ""
done
elif [[ "$unpausePrompt" =~ ^(no|n)$ ]]; then
exit 1
fi
fi
else
:
fi
if [ "${alert}" = "true" ]; then
send_notification
fi
elif [ "${pause}" = "true" ]; then
if [ "${pauseType}" = "all" ]; then
get_data
get_monitors
create_monitor_files
pause_all_monitors
cleanup
elif [ "${pauseType}" != "all" ]; then
get_data
get_monitors
create_monitor_files
pause_specified_monitors
cleanup
fi
elif [ "${unpause}" = "true" ]; then
if [ "${unpauseType}" = "all" ]; then
get_data
get_monitors
create_monitor_files
unpause_all_monitors
cleanup
elif [ "${unpauseType}" != "all" ]; then
get_data
get_monitors
create_monitor_files
unpause_specified_monitors
cleanup
fi
fi