-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
tronitor.sh
executable file
·1765 lines (1576 loc) · 83.3 KB
/
tronitor.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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env bash
#
# Script to utilize the UptimeRobot, StatusCake, and HealthChecks.io APIs to
# retrieve information on and work with checks you've created.
# Tronyx
set -eo pipefail
IFS=$'\n\t'
# Edit these with your corresponding information to finish setting up the script
# or just run it and it will prompt you for it.
# If your provider is StatusCake, specify your username.
scUsername=''
# If your provider is Upptime, specify the following.
repoOwner=''
gitHubUsername=''
upptimeRepo='upptime'
# Specify API key(s).
urApiKey=''
scApiKey=''
hcApiKey=''
ghToken=''
# Specify the domain to use for Healthchecks as they allow you to self-host the
# application. If you're self-hosting it, replace the default domain with the
# domain you're hosting it on.
healthchecksDomain='healthchecks.io'
# Specify the Discord/Slack webhook URL to send notifications to.
webhookUrl=''
# Set notifyAll to true for notification to apply for all running state as well.
notifyAll='false'
# Set JQ to false to disable its use for displaying output. This works better
# for using the script with cronjobs, etc.
jq='true'
# Declare some variables.
# Temp dir and filenames.
# Make sure you set this to something your user has write access to.
tempDir="$HOME/tronitor/"
apiTestFullFile="${tempDir}api_test_full.txt"
badMonitorsFile="${tempDir}bad_monitors.txt"
convertedMonitorsFile="${tempDir}converted_monitors.txt"
friendlyListFile="${tempDir}friendly_list.txt"
pausedMonitorsFile="${tempDir}paused_monitors.txt"
specifiedMonitorsFile="${tempDir}specified_monitors.txt"
monitorsFile="${tempDir}monitors.txt"
monitorsFullFile="${tempDir}monitors_full.txt"
hcPingURLsFile="${tempDir}hc_ping_urls.txt"
validMonitorsFile="${tempDir}valid_monitors.txt"
validMonitorsTempFile="${tempDir}valid_monitors_temp.txt"
healthchecksLockFile="${tempDir}healthchecks.lock"
# UUID regex pattern.
uuidPattern='^\{?[A-Z0-9a-z]{8}-[A-Z0-9a-z]{4}-[A-Z0-9a-z]{4}-[A-Z0-9a-z]{4}-[A-Z0-9a-z]{12}\}?$'
# Set initial API key(s) status.
urApiKeyStatus='invalid'
scApiKeyStatus='invalid'
hcApiKeyStatus='invalid'
ghTokenStatus='invalid'
# Set initial provider status.
urProviderStatus='invalid'
scProviderStatus='invalid'
hcProviderStatus='invalid'
upProviderStatus='invalid'
# Set initial SC username status.
scUsernameStatus='invalid'
# Set initial GH repo owner and username status.
ghRepoOwnerStatus='invalid'
ghUsernameStatus='invalid'
# Set initial Upptime repo status.
upRepoStatus='invalid'
# Arguments.
readonly args=("$@")
# Text colors.
#readonly blu='\e[34m'
readonly lblu='\e[94m'
readonly grn='\e[32m'
readonly red='\e[31m'
readonly ylw='\e[33m'
readonly org='\e[38;5;202m'
readonly lorg='\e[38;5;130m'
readonly mgt='\e[35m'
#readonly bold='\e[1m'
readonly endColor='\e[0m'
# Function to define usage and script options.
usage() {
cat <<- EOF
Usage: $(echo -e "${lorg}$0${endColor}") $(echo -e "${grn}"-m"${endColor}" "${ylw}"\{MONITOR\}"${endColor}") $(echo -e "${grn}"-[OPTION]"${endColor}") $(echo -e "${ylw}"\{ARGUMENT\}"${endColor}"...)
$(echo -e "${grn}"-m/--monitor"${endColor}" "${ylw}"VALUE"${endColor}") Specify the monitoring provider you would like to work with.
A) "$(echo -e "${lorg}"./tronitor.sh"${endColor}" "${grn}"-m"${endColor}" "${ylw}"UptimeRobot"${endColor}" "${grn}"-\[OPTION\]"${endColor}" "${ylw}"\{ARGUMENT\}"${endColor}")"
B) "$(echo -e "${lorg}"./tronitor.sh"${endColor}" "${grn}"--monitor"${endColor}" "${ylw}"\'sc\'"${endColor}" "${grn}"-l"${endColor}")"
C) "$(echo -e "${lorg}"./tronitor.sh"${endColor}" "${grn}"-m"${endColor}" "${ylw}"\"healthchecks\""${endColor}" "${grn}"-p"${endColor}" "${ylw}"all"${endColor}")"
$(echo -e "${grn}"-s/--stats"${endColor}""${red}"*+"${endColor}") List account statistics.
$(echo -e "${grn}"-l/--list"${endColor}""${red}"+"${endColor}") List all monitors.
$(echo -e "${grn}"-f/--find"${endColor}") Find all paused monitors.
$(echo -e "${grn}"-n/--no-prompt"${endColor}") Find all paused monitors without an unpause prompt.
$(echo -e "${grn}"-w/--webhook"${endColor}""${red}"+"${endColor}") Find all paused monitors without an unpause prompt and
send an alert to the Discord webhook specified in the script.
$(echo -e "${grn}"-i/--info"${endColor}""${red}"+"${endColor}" "${ylw}"VALUE"${endColor}") List all information for the specified monitor.
A) "$(echo -e "${lorg}"./tronitor.sh"${endColor}" "${grn}"-i"${endColor}" "${ylw}"18095689"${endColor}")"
B) "$(echo -e "${lorg}"./tronitor.sh"${endColor}" "${grn}"--info"${endColor}" "${ylw}"\'Plex\'"${endColor}")"
C) "$(echo -e "${lorg}"./tronitor.sh"${endColor}" "${grn}"-i"${endColor}" "${ylw}"\"Tautulli\""${endColor}")"
$(echo -e "${grn}"-a/--alerts"${endColor}""${red}"+"${endColor}") List all alert contacts.
$(echo -e "${grn}"-p/--pause"${endColor}" "${ylw}"VALUE"${endColor}") Pause specified monitors.
Option accepts arguments in the form of "$(echo -e "${ylw}"all"${endColor}")" or a comma-separated list
of monitors by ID or Friendly Name. Friendly Name should be wrapped in
a set of single or double quotes, IE:
A) "$(echo -e "${lorg}"./tronitor.sh"${endColor}" "${grn}"-p"${endColor}" "${ylw}"all"${endColor}")"
B) "$(echo -e "${lorg}"./tronitor.sh"${endColor}" "${grn}"--pause"${endColor}" "${ylw}"18095687,18095688,18095689"${endColor}")"
C) "$(echo -e "${lorg}"./tronitor.sh"${endColor}" "${grn}"-p"${endColor}" "${ylw}"\'Plex\',\"Tautulli\",18095689"${endColor}")"
$(echo -e "${ylw}"NOTE:"${endColor}" For Upptime, the only possible option is "${ylw}"all"${endColor}".)
$(echo -e "${grn}"-u/--unpause"${endColor}" "${ylw}"VALUE"${endColor}") Unpause specified monitors.
Option accepts arguments in the form of "$(echo -e "${ylw}"all"${endColor}")" or a comma-separated list
of monitors by ID or Friendly Name. Friendly Name should be wrapped in
a set of single or double quotes, IE:
A) "$(echo -e "${lorg}"./tronitor.sh"${endColor}" "${grn}"-u"${endColor}" "${ylw}"all"${endColor}")"
B) "$(echo -e "${lorg}"./tronitor.sh"${endColor}" "${grn}"--unpause"${endColor}" "${ylw}"18095687,18095688,18095689"${endColor}")"
C) "$(echo -e "${lorg}"./tronitor.sh"${endColor}" "${grn}"-u"${endColor}" "${ylw}"\'Plex\',\"Tautulli\",18095689"${endColor}")"
$(echo -e "${ylw}"NOTE:"${endColor}" For Upptime, the only possible option is "${ylw}"all"${endColor}".)
$(echo -e "${grn}"-c/--create"${endColor}""${red}"+"${endColor}" "${ylw}"VALUE"${endColor}") Create a new monitor using the corresponding template file. Each type of test
(HTTP, Ping, Port, & Keyword) has a template file in the Templates directory.
Just edit the template file for the monitor type you wish to create and then run
the script with the corresponding monitor type, IE:
A) "$(echo -e "${lorg}"./tronitor.sh"${endColor}" "${grn}"-c"${endColor}" "${ylw}"http"${endColor}")"
B) "$(echo -e "${lorg}"./tronitor.sh"${endColor}" "${grn}"--create"${endColor}" "${ylw}"port"${endColor}")"
C) "$(echo -e "${lorg}"./tronitor.sh"${endColor}" "${grn}"-c"${endColor}" "${ylw}"keyword"${endColor}")"
$(echo -e "${grn}"-d/--delete"${endColor}""${red}"+"${endColor}" "${ylw}"VALUE"${endColor}") Delete the specified monitor, IE:
A) "$(echo -e "${lorg}"./tronitor.sh"${endColor}" "${grn}"-d"${endColor}" "${ylw}"\'Plex\'"${endColor}")"
B) "$(echo -e "${lorg}"./tronitor.sh"${endColor}" "${grn}"--delete"${endColor}" "${ylw}"\"Tautulli\""${endColor}")"
C) "$(echo -e "${lorg}"./tronitor.sh"${endColor}" "${grn}"-d"${endColor}" "${ylw}"18095688"${endColor}")"
$(echo -e "${grn}"-r/--reset"${endColor}""${red}"*+"${endColor}" "${ylw}"VALUE"${endColor}") Reset the specified monitor, IE:
A) "$(echo -e "${lorg}"./tronitor.sh"${endColor}" "${grn}"-r"${endColor}" "${ylw}"\'Plex\'"${endColor}")"
B) "$(echo -e "${lorg}"./tronitor.sh"${endColor}" "${grn}"--reset"${endColor}" "${ylw}"\"Tautulli\""${endColor}")"
C) "$(echo -e "${lorg}"./tronitor.sh"${endColor}" "${grn}"-r"${endColor}" "${ylw}"18095688"${endColor}")"
$(echo -e "${grn}"-h/--help"${endColor}") Display this usage dialog.
$(echo -e "${red}"\*"${endColor}""${ylw}" - Option is not compatible with StatusCake or HealthChecks.io."${endColor}")
$(echo -e "${red}"\+"${endColor}""${ylw}" - Option is not compatible with Upptime."${endColor}")
EOF
}
# Function to define script options.
cmdline() {
local arg=
local local_args
local OPTERR=0
for arg; do
local delim=""
case "${arg}" in
# Translate --gnu-long-options to -g (short options).
--monitor) local_args="${local_args}-m " ;;
--stats) local_args="${local_args}-s " ;;
--list) local_args="${local_args}-l " ;;
--find) local_args="${local_args}-f " ;;
--no-prompt) local_args="${local_args}-n " ;;
--webhook) local_args="${local_args}-w " ;;
--info) local_args="${local_args:-}-i " ;;
--alerts) local_args="${local_args}-a " ;;
--create) local_args="${local_args:-}-c " ;;
--pause) local_args="${local_args:-}-p " ;;
--unpause) local_args="${local_args:-}-u " ;;
--reset) local_args="${local_args:-}-r " ;;
--delete) local_args="${local_args:-}-d " ;;
--help) local_args="${local_args}-h " ;;
# Pass through anything else.
*)
[[ ${arg:0:1} == '-' ]] || delim='"'
local_args="${local_args:-}${delim}${arg}${delim} "
;;
esac
done
# Reset the positional parameters to the short options.
eval set -- "${local_args:-}"
while getopts "hm:slfnwai:c:r:d:p:u:" OPTION; do
case "$OPTION" in
m)
providerName="${OPTARG}"
monitorFlag=true
;;
s)
stats=true
;;
l)
list=true
;;
f)
find=true
prompt=true
;;
n)
find=true
prompt=false
;;
w)
find=true
prompt=false
webhook=true
;;
a)
alerts=true
;;
i)
info=true
infoType="${OPTARG}"
;;
c)
create=true
createType="${OPTARG}"
;;
r)
reset=true
resetType="${OPTARG}"
;;
d)
delete=true
deleteType="${OPTARG}"
;;
p)
pause=true
pauseType="${OPTARG}"
;;
u)
unpause=true
unpauseType="${OPTARG}"
;;
h)
usage
exit
;;
*)
if [[ ${arg} == '-m' || ${arg} == '-p' || ${arg} == '-u' || ${arg} == '-r' || ${arg} == '-d' || ${arg} == '-i' || ${arg} == '-c' ]] && [[ -z ${OPTARG} ]]; then
echo -e "${red}Option ${arg} requires an argument!${endColor}"
else
echo -e "${red}You are specifying a non-existent option!${endColor}"
fi
usage
exit
;;
esac
done
shift $((OPTIND - 1))
return 0
}
# Function to gather script information.
get_scriptname() {
local source
local dir
source="${BASH_SOURCE[0]}"
while [[ -L ${source} ]]; do
dir="$(cd -P "$(dirname "${source}")" > /dev/null && pwd)"
source="$(readlink "${source}")"
[[ ${source} != /* ]] && source="${dir}/${source}"
done
echo "${source}"
}
readonly scriptname="$(get_scriptname)"
# Function to create the directory to neatly store temp files, if it does
# not exist.
create_dir() {
mkdir -p "${tempDir}"
chmod 777 "${tempDir}"
}
# Function to cleanup temp files.
cleanup() {
rm -rf "${tempDir}"*.txt || true
}
trap 'cleanup' 0 1 3 6 14 15
# Function to exit the script if the user hits CTRL+C.
function control_c() {
cleanup
exit 0
}
trap 'control_c' 2
# Function to check that the monitor option has been provided.
check_monitor_opt() {
if [[ ${monitorFlag} != 'true' ]]; then
echo -e "${red}You must specify the monitor you wish to work with!${endColor}"
usage
exit
fi
}
# Function to check that two options were provided.
check_opt_num() {
if [[ ${OPTIND} -lt '4' || ${OPTIND} -gt '5' ]]; then
echo -e "${red}You specified an invalid number of options!${endColor}"
usage
exit
fi
}
# Function to check for an empty arg.
check_empty_arg() {
for arg in "${args[@]:-}"; do
if [[ -z ${arg} ]]; then
usage
exit
fi
done
}
# Function to check that only all is used for pause and unpause if the provider
# is Upptime
check_pauseType_upptime() {
if [[ ${providerName} == 'upptime' ]] && [[ ${pause} == 'true' || ${unpause} == 'true' ]] && [[ ${pauseType} != 'all' && ${unpauseType} != 'all' ]]; then
echo -e "${red}You can only specify all for Upptime!${endColor}"
usage
exit
fi
}
# Function to check if cURL is installed and, if not, inform the user and exit.
check_curl() {
whichCURL=$(which curl)
if [[ -z ${whichCURL} ]]; then
echo -e "${red}cURL is not currently installed on this system!${endColor}"
echo -e "${ylw}The script with NOT function without it. Install cURL and run the script again.${endColor}"
exit
fi
}
# Function to grab line numbers of the user-defined and status variables.
get_line_numbers() {
# Line numbers for user-defined variables.
scUsernameLineNum=$(head -68 "${scriptname}" | grep -En -A1 'specify your username' | tail -1 | awk -F- '{print $1}')
ghRepoOwnerLineNum=$(head -68 "${scriptname}" | grep -En 'repoOwner' | awk -F: '{print $1}')
ghUsernameLineNum=$(head -68 "${scriptname}" | grep -En 'gitHubUser' | awk -F: '{print $1}')
upRepoLineNum=$(head -68 "${scriptname}" | grep -En 'upptimeRepo' | awk -F: '{print $1}')
urApiKeyLineNum=$(head -68 "${scriptname}" | grep -En -A4 'Specify API key' | grep 'ur' | awk -F- '{print $1}')
scApiKeyLineNum=$(head -68 "${scriptname}" | grep -En -A4 'Specify API key' | grep 'sc' | awk -F- '{print $1}')
hcApiKeyLineNum=$(head -68 "${scriptname}" | grep -En -A4 'Specify API key' | grep 'hc' | awk -F- '{print $1}')
ghTokenLineNum=$(head -68 "${scriptname}" | grep -En -A4 'Specify API key' | grep 'gh' | awk -F- '{print $1}')
webhookUrlLineNum=$(head -68 "${scriptname}" | grep -En -A1 'Discord/Slack' | tail -1 | awk -F- '{print $1}')
# Line numbers for status variables.
urApiStatusLineNum=$(head -68 "${scriptname}" | grep -En -A4 'Set initial API key' | grep 'ur' | awk -F- '{print $1}')
scApiStatusLineNum=$(head -68 "${scriptname}" | grep -En -A4 'Set initial API key' | grep 'sc' | awk -F- '{print $1}')
hcApiStatusLineNum=$(head -68 "${scriptname}" | grep -En -A4 'Set initial API key' | grep 'hc' | awk -F- '{print $1}')
ghTokenStatusLineNum=$(head -68 "${scriptname}" | grep -En -A4 'Set initial API key' | grep 'gh' | awk -F- '{print $1}')
urProviderStatusLineNum=$(head -68 "${scriptname}" | grep -En -A4 'Set initial provider status' | grep 'ur' | awk -F- '{print $1}')
scProviderStatusLineNum=$(head -68 "${scriptname}" | grep -En -A4 'Set initial provider status' | grep 'sc' | awk -F- '{print $1}')
hcProviderStatusLineNum=$(head -68 "${scriptname}" | grep -En -A4 'Set initial provider status' | grep 'hc' | awk -F- '{print $1}')
upProviderStatusLineNum=$(head -68 "${scriptname}" | grep -En -A4 'Set initial provider status' | grep 'up' | awk -F- '{print $1}')
scUserStatusLineNum=$(head -68 "${scriptname}" | grep -En -A1 'Set initial SC username status' | tail -1 | awk -F- '{print $1}')
ghUserStatusLineNum=$(head -68 "${scriptname}" | grep -En -A2 'Set initial GH repo owner and username status' | grep ghUser | awk -F- '{print $1}')
ghRepoOwnerStatusLineNum=$(head -68 "${scriptname}" | grep -En -A2 'Set initial GH repo owner and username status' | grep RepoOwner | awk -F- '{print $1}')
upRepoStatusLineNum=$(head -68 "${scriptname}" | grep -En -A1 'Set initial Upptime repo status' | tail -1 | awk -F- '{print $1}')
}
# Function for catching when a curl or jq command fails to display a message and
# exit the script.
fatal() {
echo -e "${red}There seems to be an issue connecting to ${providerName^}. Please try again in a few minutes.${endColor}"
exit
}
# Function to convert shorthand provider names to their full names and to make
# sure the provider name is lowercase and, if not, convert it.
convert_provider_name() {
if [[ ${providerName} == 'ur' ]]; then
providerName='uptimerobot'
elif [[ ${providerName} == 'sc' ]]; then
providerName='statuscake'
elif [[ ${providerName} == 'hc' ]]; then
providerName='healthchecks'
elif [[ ${providerName} == 'up' ]]; then
providerName='upptime'
fi
if [[ ${providerName} =~ [[:upper:]] ]]; then
providerName=$(echo "${providerName}" | awk '{print tolower($0)}')
fi
}
# Function to check that provider is not empty and valid.
check_provider() {
if [[ ${providerName} == 'uptimerobot' ]]; then
providerStatus="${urProviderStatus}"
elif [[ ${providerName} == 'statuscake' ]]; then
providerStatus="${scProviderStatus}"
elif [[ ${providerName} == 'healthchecks' ]]; then
providerStatus="${hcProviderStatus}"
elif [[ ${providerName} == 'upptime' ]]; then
providerStatus="${upProviderStatus}"
else
providerStatus='invalid'
fi
while [[ ${providerStatus} == 'invalid' ]]; do
if [[ ${providerName} != 'uptimerobot' ]] && [[ ${providerName} != 'statuscake' ]] && [[ ${providerName} != 'healthchecks' ]] && [[ ${providerName} != 'upptime' ]]; then
echo -e "${red}You didn't specify a valid monitoring provider with the -m flag!${endColor}"
echo -e "${ylw}Please specify either uptimerobot, statuscake, healthchecks, or upptime.${endColor}"
exit
else
if [[ ${providerName} == 'uptimerobot' ]]; then
sed -i "${urProviderStatusLineNum} s|urProviderStatus='[^']*'|urProviderStatus='ok'|" "${scriptname}"
elif [[ ${providerName} == 'statuscake' ]]; then
sed -i "${scProviderStatusLineNum} s|scProviderStatus='[^']*'|scProviderStatus='ok'|" "${scriptname}"
elif [[ ${providerName} == 'healthchecks' ]]; then
sed -i "${hcProviderStatusLineNum} s|hcProviderStatus='[^']*'|hcProviderStatus='ok'|" "${scriptname}"
elif [[ ${providerName} == 'upptime' ]]; then
sed -i "${upProviderStatusLineNum} s|upProviderStatus='[^']*'|upProviderStatus='ok'|" "${scriptname}"
fi
convert_provider_name
if [[ ${providerName} == 'uptimerobot' ]]; then
urProviderStatus='ok'
elif [[ ${providerName} == 'statuscake' ]]; then
scProviderStatus='ok'
elif [[ ${providerName} == 'healthchecks' ]]; then
hcProviderStatus='ok'
elif [[ ${providerName} == 'upptime' ]]; then
upProviderStatus='ok'
fi
providerStatus='ok'
fi
done
if [[ ${providerName} == 'uptimerobot' ]]; then
readonly apiUrl='https://api.uptimerobot.com/v2/'
elif [[ ${providerName} == 'statuscake' ]]; then
readonly apiUrl='https://app.statuscake.com/API/'
elif [[ ${providerName} == 'healthchecks' ]]; then
readonly apiUrl="https://${healthchecksDomain}/api/v1/"
elif [[ ${providerName} == 'upptime' ]]; then
readonly apiUrl='https://api.github.com/'
upRawURL="https://raw.githubusercontent.com/${repoOwner}/${upptimeRepo}/"
fi
}
# Function to specifically check that the provided StatusCake username and API
# key are valid.
check_sc_creds() {
while [[ ${scUsernameStatus} == 'invalid' ]] || [[ ${scApiKeyStatus} == 'invalid' ]]; do
if [[ -z ${scApiKey} ]]; then
echo -e "${red}You didn't define your ${providerName^} API key in the script!${endColor}"
echo ''
echo "Enter your ${providerName^} API key:"
read -rs API
echo ''
echo ''
sed -i "${scApiKeyLineNum} s/scApiKey='[^']*'/scApiKey='${API}'/" "${scriptname}"
scApiKey="${API}"
elif [[ -z ${scUsername} ]]; then
echo -e "${red}You didn't specify your ${providerName^} username in the script!${endColor}"
echo ''
echo "Enter your ${providerName^} username:"
read -r username
echo ''
echo ''
sed -i "${scUsernameLineNum} s/scUsername='[^']*'/scUsername='${username}'/" "${scriptname}"
scUsername="${username}"
else
scStatus=$(curl -s -H "API: ${scApiKey}" -H "Username: ${scUsername}" -X GET "${apiUrl}"Tests/ | jq .ErrNo 2> /dev/null || echo '1')
if [[ ${scStatus} == '0' ]]; then
clear >&2
echo -e "${red}The API Key and/or username that you provided for ${providerName^} are not valid!${endColor}"
sed -i "${scApiKeyLineNum} s/scApiKey='[^']*'/scApiKey=''/" "${scriptname}"
scApiKey=''
sed -i "${scUsernameLineNum} s/scUsername='[^']*'/scUsername=''/" "${scriptname}"
scUsername=''
echo ''
echo "Enter your ${providerName^} username:"
read -r username
echo ''
echo ''
sed -i "${scUsernameLineNum} s/scUsername='[^']*'/scUsername='${username}'/" "${scriptname}"
scUsername="${username}"
elif [[ ${scStatus} == '1' ]]; then
echo "Validating that the provided ${providerName^} username and API key are functional..."
sed -i "${scApiStatusLineNum} s/scApiKeyStatus='[^']*'/scApiKeyStatus='ok'/" "${scriptname}"
scApiKeyStatus='ok'
sed -i "${scUserStatusLineNum} s/scUsernameStatus='[^']*'/scUsernameStatus='ok'/" "${scriptname}"
scUsernameStatus='ok'
echo -e "${grn}Success!${endColor}"
echo ''
fi
fi
done
}
# Function to check that the provided Upptime repo owner, GitHub username, and PAT are valid.
check_gh_creds() {
while [[ ${ghRepoOwnerStatus} == 'invalid' ]] || [[ ${ghUsernameStatus} == 'invalid' ]] || [[ ${ghTokenStatus} == 'invalid' ]]; do
if [[ -z ${ghToken} ]]; then
echo -e "${red}You didn't define your ${providerName^} PAT in the script!${endColor}"
echo ''
echo "Enter your ${providerName^} PAT:"
read -rs API
echo ''
echo ''
sed -i "${ghTokenLineNum} s/ghToken='[^']*'/ghToken='${API}'/" "${scriptname}"
ghToken="${API}"
elif [[ -z ${repoOwner} ]]; then
echo -e "${red}You didn't specify the owner of your Upptime repository in the script!${endColor}"
echo ''
echo "Enter your Upptime repo owner:"
read -r owner
echo ''
echo ''
sed -i "${ghRepoOwnerLineNum} s/repoOwner='[^']*'/repoOwner='${owner}'/" "${scriptname}"
repoOwner="${owner}"
upRawURL="https://raw.githubusercontent.com/${repoOwner}/${upptimeRepo}/"
elif [[ -z ${gitHubUsername} ]]; then
echo -e "${red}You didn't specify your GitHub username in the script!${endColor}"
echo ''
echo "Enter your GitHub username:"
read -r username
echo ''
echo ''
sed -i "${ghUsernameLineNum} s/gitHubUsername='[^']*'/gitHubUsername='${username}'/" "${scriptname}"
gitHubUsername="${username}"
else
ghStatus=$(curl -s -XGET -H "Authorization: bearer ${ghToken}" "${apiUrl}"user | jq -r .login)
ghStatus=$(echo "${ghStatus}" | awk '{print tolower($0)}')
if [[ ${ghStatus} != "${gitHubUsername}" ]]; then
clear >&2
echo -e "${red}The PAT and/or username that you provided for GitHub are not valid!${endColor}"
sed -i "${ghTokenLineNum} s/ghToken='[^']*'/ghToken=''/" "${scriptname}"
ghToken=''
sed -i "${ghUsernameLineNum} s/gitHubUsername='[^']*'/gitHubUsername=''/" "${scriptname}"
gitHubUsername=''
echo ''
echo "Enter your GitHub username:"
read -r username
echo ''
echo ''
sed -i "${ghUsernameLineNum} s/gitHubUsername='[^']*'/gitHubUsername='${username}'/" "${scriptname}"
gitHubUsername="${username}"
upRawURL="https://raw.githubusercontent.com/${repoOwner}/${upptimeRepo}/"
elif [[ ${ghStatus} == "${gitHubUsername}" ]]; then
echo 'Validating that the provided Upptime repo owner, GitHub username, and PAT are functional...'
sed -i "${ghRepoOwnerStatusLineNum} s/ghRepoOwnerStatus='[^']*'/ghRepoOwnerStatus='ok'/" "${scriptname}"
ghRepoOwnerStatus='ok'
sed -i "${ghTokenStatusLineNum} s/ghTokenStatus='[^']*'/ghTokenStatus='ok'/" "${scriptname}"
ghTokenStatus='ok'
sed -i "${ghUserStatusLineNum} s/ghUsernameStatus='[^']*'/ghUsernameStatus='ok'/" "${scriptname}"
ghUsernameStatus='ok'
echo -e "${grn}Success!${endColor}"
echo ''
fi
fi
done
}
# Function to check that the provided Upptime repository is valid.
check_up_repo() {
while [[ ${upRepoStatus} == 'invalid' ]]; do
if [[ -z ${upptimeRepo} ]]; then
echo -e "${red}You didn't define your ${providerName^} repository in the script!${endColor}"
echo ''
echo "Enter your ${providerName^} repository name:"
read -r repo
echo ''
echo ''
sed -i "${upRepoLineNum} s/upptimeRepo='[^']*'/upptimeRepo='${repo}'/" "${scriptname}"
upptimeRepo="${repo}"
upRawURL="https://raw.githubusercontent.com/${repoOwner}/${upptimeRepo}/"
else
echo 'Validating that the provided Upptime repository is functional...'
status=$(curl -w "%{http_code}\n" -sI -o /dev/null https://github.com/"${repoOwner}"/"${upptimeRepo}"/) || fatal
if [[ ${status} != '200' ]]; then
echo -e "${red}The Upptime repository that you provided does not appear to be valid!${endColor}"
echo -e "${ylw}Resetting it so that you can enter it again...${endColor}"
sed -i "${upRepoLineNum} s/upptimeRepo='[^']*'/upptimeRepo=''/" "${scriptname}"
upptimeRepo=''
echo ''
elif [[ ${status} == '200' ]]; then
sed -i "${upRepoStatusLineNum} s/upRepoStatus='[^']*'/upRepoStatus='ok'/" "${scriptname}"
upRepoStatus='ok'
upRawURL="https://raw.githubusercontent.com/${repoOwner}/${upptimeRepo}/"
echo -e "${grn}Success!${endColor}"
echo ''
fi
fi
done
}
# Function to check that the provided UptimeRobot or Healthchecks.io API Key
# is valid.
check_api_key() {
if [[ ${providerName} == 'uptimerobot' ]]; then
while [[ ${urApiKeyStatus} == 'invalid' ]]; do
if [[ -z ${urApiKey} ]]; then
echo -e "${red}You didn't define your ${providerName^} API key in the script!${endColor}"
echo ''
echo "Enter your ${providerName^} API key:"
read -rs API
echo ''
echo ''
sed -i "${urApiKeyLineNum} s/urApiKey='[^']*'/urApiKey='${API}'/" "${scriptname}"
urApiKey="${API}"
else
curl --fail -s -X POST "${apiUrl}"getAccountDetails -d "api_key=${urApiKey}" -d "format=json" > "${apiTestFullFile}" || fatal
status=$(jq -r .stat "${apiTestFullFile}" 2> /dev/null) || fatal
if [[ ${status} == 'fail' ]]; then
echo -e "${red}The API Key that you provided for ${providerName^} is not valid!${endColor}"
sed -i "${urApiKeyLineNum} s/urApiKey='[^']*'/urApiKey=''/" "${scriptname}"
urApiKey=''
elif [[ ${status} == 'ok' ]]; then
echo "Validating that the provided ${providerName^} API key is functional..."
sed -i "${urApiStatusLineNum} s/urApiKeyStatus='[^']*'/urApiKeyStatus='${status}'/" "${scriptname}"
urApiKeyStatus="${status}"
echo -e "${grn}Success!${endColor}"
echo ''
fi
fi
done
elif [[ ${providerName} == 'healthchecks' ]]; then
while [[ ${hcApiKeyStatus} == 'invalid' ]]; do
if [[ -z ${hcApiKey} ]]; then
echo -e "${red}You didn't define your ${providerName^} API key in the script!${endColor}"
echo ''
echo "Enter your ${providerName^} API key:"
read -rs API
echo ''
echo ''
sed -i "${hcApiKeyLineNum} s/hcApiKey='[^']*'/hcApiKey='${API}'/" "${scriptname}"
hcApiKey="${API}"
else
curl --fail -s -H "X-Api-Key: ${hcApiKey}" -X GET "${apiUrl}"checks/ > "${apiTestFullFile}"
status=$(jq -r .error "${apiTestFullFile}" 2> /dev/null) || fatal
if [[ ${status} != 'null' ]]; then
echo -e "${red}The API Key that you provided for ${providerName^} is not valid!${endColor}"
sed -i "${hcApiKeyLineNum} s/hcApiKey='[^']*'/hcApiKey=''/" "${scriptname}"
hcApiKey=''
elif [[ ${status} == 'null' ]]; then
echo "Validating that the provided ${providerName^} API key is functional..."
sed -i "${hcApiStatusLineNum} s/hcApiKeyStatus='[^']*'/hcApiKeyStatus='ok'/" "${scriptname}"
hcApiKeyStatus='ok'
echo -e "${grn}Success!${endColor}"
echo ''
fi
fi
done
fi
}
# Function to check that the webhook URL is defined if alert is set to true.
check_webhook_url() {
if [[ ${webhookUrl} == '' ]] && [[ ${webhook} == 'true' ]]; then
echo -e "${red}You didn't define your Discord webhook URL!${endColor}"
echo ''
echo 'Enter your webhook URL:'
read -r url
echo ''
echo ''
sed -i "${webhookUrlLineNum} s|webhookUrl='[^']*'|webhookUrl='${url}'|" "${scriptname}"
webhookUrl="${url}"
fi
}
# Function to wrap all other checks into one function.
checks() {
get_line_numbers
check_monitor_opt
check_opt_num
check_empty_arg
check_pauseType_upptime
check_curl
check_provider
if [[ ${providerName} == 'statuscake' ]]; then
check_sc_creds
elif [[ ${providerName} == 'upptime' ]]; then
check_gh_creds
check_up_repo
else
check_api_key
fi
check_webhook_url
}
# Function to set the API key variable to the API key for the specified monitor.
set_api_key() {
if [[ ${providerName} == 'uptimerobot' ]]; then
apiKey="${urApiKey}"
elif [[ ${providerName} == 'statuscake' ]]; then
apiKey="${scApiKey}"
elif [[ ${providerName} == 'healthchecks' ]]; then
apiKey="${hcApiKey}"
elif [[ ${providerName} == 'upptime' ]]; then
apiKey="${ghToken}"
fi
}
# Function to grab data for all monitors.
get_data() {
if [[ ${providerName} == 'uptimerobot' ]]; then
curl --fail -s -X POST -H "Content-Type: application/x-www-form-urlencoded" -H "Cache-Control: no-cache" "${apiUrl}"getMonitors -d "api_key=${apiKey}" -d "format=json" > "${monitorsFullFile}" || fatal
elif [[ ${providerName} == 'statuscake' ]]; then
curl --fail -s -H "API: ${apiKey}" -H "Username: ${scUsername}" -X GET "${apiUrl}"Tests/ > "${monitorsFullFile}" || fatal
elif [[ ${providerName} == 'healthchecks' ]]; then
curl --fail -s -H "X-Api-Key: ${apiKey}" -X GET "${apiUrl}"checks/ > "${monitorsFullFile}" || fatal
elif [[ ${providerName} == 'upptime' ]]; then
curl --fail -s -H "Authorization: bearer ${ghToken}" "${apiUrl}repos/${repoOwner}/${upptimeRepo}/git/trees/master?recursive=1" | grep -e 'api\/' | awk -F'/' 'NF==2' | awk -F'/' '{print $2}' | tr -d '",' > "${monitorsFullFile}" || fatal
fi
}
# Function to create a list of monitor IDs.
get_monitors() {
if [[ ${providerName} == 'uptimerobot' ]]; then
totalMonitors=$(jq -r .pagination.total "${monitorsFullFile}" 2> /dev/null) || fatal
elif [[ ${providerName} == 'statuscake' ]]; then
totalMonitors=$(jq -r .[].TestID "${monitorsFullFile}" | wc -l 2> /dev/null) || fatal
elif [[ ${providerName} == 'healthchecks' ]]; then
totalMonitors=$(jq -r .checks[].name "${monitorsFullFile}" | wc -l 2> /dev/null) || fatal
elif [[ ${providerName} == 'upptime' ]]; then
totalMonitors=$(wc -l "${monitorsFullFile}" | awk '{print $1}' 2> /dev/null) || fatal
fi
if [[ ${totalMonitors} == '0' ]]; then
echo "There are currently no monitors associated with your ${providerName^} account."
exit 0
else
if [[ ${providerName} == 'uptimerobot' ]]; then
jq -r .monitors[].id "${monitorsFullFile}" > "${monitorsFile}" 2> /dev/null || fatal
elif [[ ${providerName} == 'statuscake' ]]; then
jq -r .[].TestID "${monitorsFullFile}" > "${monitorsFile}" 2> /dev/null || fatal
elif [[ ${providerName} == 'healthchecks' ]]; then
jq -r .checks[].ping_url "${monitorsFullFile}" 2> /dev/null > "${hcPingURLsFile}" || fatal
rev "${hcPingURLsFile}" | cut -c1-36 | rev > "${monitorsFile}"
elif [[ ${providerName} == 'upptime' ]]; then
cat "${monitorsFullFile}" > "${monitorsFile}" 2> /dev/null || fatal
fi
fi
}
# Function to create individual monitor files.
create_monitor_files() {
while IFS= read -r monitor; do
if [[ ${providerName} == 'uptimerobot' ]]; then
jq -r '. | {stat: .stat, pagination: .pagination, monitors: [.monitors[] | select(.id=='"${monitor}"')]} | .pagination.total=1' "${monitorsFullFile}" > "${tempDir}${monitor}".txt
elif [[ ${providerName} == 'statuscake' ]]; then
curl --fail -s -H "API: ${apiKey}" -H "Username: ${scUsername}" -X GET "${apiUrl}Tests/Details/?TestID=${monitor}" > "${tempDir}${monitor}".txt || fatal
elif [[ ${providerName} == 'healthchecks' ]]; then
jq --arg monitor "${monitor}" '.checks[] | select(.ping_url | contains($monitor))' "${monitorsFullFile}" > "${tempDir}${monitor}".txt
elif [[ ${providerName} == 'upptime' ]]; then
curl --fail -s -H "Authorization: bearer ${ghToken}" "${upRawURL}master/history/${monitor}.yml" 2> /dev/null > "${tempDir}${monitor}".txt || fatal
fi
done < <(cat "${monitorsFile}")
}
# Function to create friendly output of all monitors.
create_friendly_list() {
true > "${friendlyListFile}"
while IFS= read -r monitor; do
if [[ ${providerName} == 'uptimerobot' ]]; then
friendlyName=$(jq -r .monitors[].friendly_name "${tempDir}${monitor}".txt 2> /dev/null) || fatal
status=$(jq .monitors[].status "${tempDir}${monitor}".txt 2> /dev/null) || fatal
if [[ ${status} == '0' ]]; then
friendlyStatus="${ylw}Paused${endColor}"
elif [[ ${status} == '1' ]]; then
friendlyStatus="${mgt}Not checked yet${endColor}"
elif [[ ${status} == '2' ]]; then
friendlyStatus="${grn}Up${endColor}"
elif [[ ${status} == '8' ]]; then
friendlyStatus="${org}Seems down${endColor}"
elif [[ ${status} == '9' ]]; then
friendlyStatus="${red}Down${endColor}"
fi
elif [[ ${providerName} == 'statuscake' ]]; then
friendlyName=$(jq -r .WebsiteName "${tempDir}${monitor}".txt 2> /dev/null) || fatal
status=$(jq -r .Status "${tempDir}${monitor}".txt 2> /dev/null) || fatal
paused=$(jq -r .Paused "${tempDir}${monitor}".txt 2> /dev/null) || fatal
if [[ ${status} == 'Up' ]] && [[ ${paused} == 'true' ]]; then
friendlyStatus="${ylw}Paused (Up)${endColor}"
elif [[ ${status} == 'Down' ]] && [[ ${paused} == 'true' ]]; then
friendlyStatus="${ylw}Paused (Down)${endColor}"
elif [[ ${status} == 'Up' ]] && [[ ${paused} == 'false' ]]; then
friendlyStatus="${grn}Up${endColor}"
elif [[ ${status} == 'Down' ]] && [[ ${paused} == 'false' ]]; then
friendlyStatus="${red}Down${endColor}"
fi
elif [[ ${providerName} == 'healthchecks' ]]; then
cp "${tempDir}${monitor}".txt "${tempDir}${monitor}"_short.txt
friendlyName=$(jq -r .name "${tempDir}${monitor}"_short.txt 2> /dev/null) || fatal
status=$(jq -r .status "${tempDir}${monitor}"_short.txt 2> /dev/null) || fatal
if [[ ${status} == 'up' ]]; then
friendlyStatus="${grn}Up${endColor}"
elif [[ ${status} == 'down' ]]; then
friendlyStatus="${red}Down${endColor}"
elif [[ ${status} == 'paused' ]]; then
friendlyStatus="${ylw}Paused${endColor}"
elif [[ ${status} == 'late' ]]; then
friendlyStatus="${org}Late${endColor}"
elif [[ ${status} == 'new' ]]; then
friendlyStatus="${mgt}New${endColor}"
fi
elif [[ ${providerName} == 'upptime' ]]; then
cp "${tempDir}${monitor}".txt "${tempDir}${monitor}"_short.txt
siteURL=$(grep url "${tempDir}${monitor}"_short.txt | awk '{print $2}')
friendlyName=$(curl --fail -s "${upRawURL}master/.upptimerc.yml" | grep -v href | grep -B1 "${siteURL}"$ | grep name | awk -F':' '{print $2}' | cut -c2- 2> /dev/null) || fatal
status=$(grep status "${tempDir}${monitor}"_short.txt | grep -v url | awk '{print $2}' 2> /dev/null) || fatal
workflowStatus=$(curl -s -H "Authorization: bearer ${ghToken}" -H "Accept: application/vnd.github.v3+json" "${apiUrl}repos/${repoOwner}/${upptimeRepo}/actions/workflows/uptime.yml" | jq -r .state)
if [[ ${workflowStatus} == 'disabled_manually' ]]; then
status='paused'
fi
if [[ ${status} == 'up' ]]; then
friendlyStatus="${grn}Up${endColor}"
elif [[ ${status} == 'down' ]]; then
friendlyStatus="${red}Down${endColor}"
elif [[ ${status} == 'paused' ]]; then
friendlyStatus="${ylw}Paused${endColor}"
fi
fi
if [[ ${providerName} == 'upptime' ]]; then
echo -e "${lorg}${friendlyName}${endColor} | URL: ${lblu}${siteURL}${endColor} | Status: ${friendlyStatus}" >> "${friendlyListFile}"
else
echo -e "${lorg}${friendlyName}${endColor} | ID: ${lblu}${monitor}${endColor} | Status: ${friendlyStatus}" >> "${friendlyListFile}"
fi
done < <(cat "${monitorsFile}")
}
# Function to display a friendly list of all monitors.
display_all_monitors() {
if [[ -s ${friendlyListFile} ]]; then
if [[ ${providerName} == 'upptime' ]]; then
echo "The following monitors were found in your ${providerName^} repository:"
echo ''
column -ts "|" "${friendlyListFile}"
echo ''
else
echo "The following monitors were found in your ${providerName^} account:"
echo ''
column -ts "|" "${friendlyListFile}"
echo ''
fi
fi
}
# Function to find all currently paused monitors.
get_paused_monitors() {
true > "${pausedMonitorsFile}"
if [[ ${providerName} == 'upptime' ]]; then
workflowStatus=$(curl -s -H "Authorization: bearer ${ghToken}" -H "Accept: application/vnd.github.v3+json" "${apiUrl}repos/${repoOwner}/${upptimeRepo}/actions/workflows/uptime.yml" | jq -r .state)
if [[ ${workflowStatus} == 'disabled_manually' ]]; then
while IFS= read -r monitor; do
cp "${tempDir}${monitor}".txt "${tempDir}${monitor}"_short.txt
siteURL=$(grep url "${tempDir}${monitor}"_short.txt | awk '{print $2}')
friendlyName=$(curl --fail -s "${upRawURL}master/.upptimerc.yml" | grep -v href | grep -B1 "${siteURL}"$ | grep name | awk -F':' '{print $2}' | cut -c2- 2> /dev/null) || fatal
echo -e "${lorg}${friendlyName}${endColor} | URL: ${lblu}${siteURL}${endColor}" >> "${pausedMonitorsFile}"
done < <(cat "${monitorsFile}")
fi
else
while IFS= read -r monitor; do
if [[ ${providerName} == 'uptimerobot' ]]; then
friendlyName=$(jq -r .monitors[].friendly_name "${tempDir}${monitor}".txt 2> /dev/null) || fatal
status=$(jq -r .monitors[].status "${tempDir}${monitor}".txt 2> /dev/null) || fatal
if [[ ${status} == '0' ]]; then
echo -e "${lorg}${friendlyName}${endColor} | ID: ${lblu}${monitor}${endColor}" >> "${pausedMonitorsFile}"
fi
elif [[ ${providerName} == 'statuscake' ]]; then
friendlyName=$(jq -r .WebsiteName "${tempDir}${monitor}".txt 2> /dev/null) || fatal
status=$(jq -r .Status "${tempDir}${monitor}".txt 2> /dev/null) || fatal
paused=$(jq -r .Paused "${tempDir}${monitor}".txt 2> /dev/null) || fatal
if [[ ${status} == 'Up' ]] && [[ ${paused} == 'true' ]]; then
echo -e "${lorg}${friendlyName}${endColor} | ID: ${lblu}${monitor}${endColor}" >> "${pausedMonitorsFile}"
fi
elif [[ ${providerName} == 'healthchecks' ]]; then
cp "${tempDir}${monitor}".txt "${tempDir}${monitor}"_short.txt
friendlyName=$(jq -r .name "${tempDir}${monitor}"_short.txt 2> /dev/null) || fatal
status=$(jq -r .status "${tempDir}${monitor}"_short.txt 2> /dev/null) || fatal
if [[ ${status} == 'paused' ]]; then
echo -e "${lorg}${friendlyName}${endColor} | ID: ${lblu}${monitor}${endColor}" >> "${pausedMonitorsFile}"
fi
fi
done < <(cat "${monitorsFile}")
fi
}
# Function to display a list of all paused monitors.
display_paused_monitors() {
if [[ -s ${pausedMonitorsFile} ]]; then
echo "The following ${providerName^} monitors are currently paused:"
echo ''
column -ts "|" "${pausedMonitorsFile}"
echo ''
else
echo "There are currently no paused ${providerName^} monitors."
echo ''
fi
}
# Function to prompt the user to unpause monitors after finding paused monitors.
unpause_prompt() {
echo ''
echo -e "Would you like to unpause the currently paused monitors? (${grn}[Y]${endColor}es or ${red}[N]${endColor}o): "
read -r unpausePrompt
echo ''
if ! [[ ${unpausePrompt} =~ ^(Yes|yes|Y|y|No|no|N|n)$ ]]; then
echo -e "${red}Please specify yes, y, no, or n.${endColor}"
read -r unpausePrompt
fi
}
# Function to prompt the user to continue actioning valid monitors after
# finding invalid ones.
invalid_prompt() {
echo 'Would you like to continue actioning the following valid monitors?'
echo ''
cat "${validMonitorsFile}"
echo ''
echo -e "${grn}[Y]${endColor}es or ${red}[N]${endColor}o):"
read -r invalidPrompt
echo ''
if ! [[ ${invalidPrompt} =~ ^(Yes|yes|Y|y|No|no|N|n)$ ]]; then
echo -e "${red}Please specify yes, y, no, or n.${endColor}"
read -r invalidPrompt
fi
}
# Function to check if any bad, IE: non-existent, monitors were provided.
check_bad_monitors() {
true > "${badMonitorsFile}"
while IFS= read -r monitor; do
if [[ $(sed 's/\x1B\[[0-9;]*[JKmsu]//g' "${friendlyListFile}" | grep -ic "${monitor} |") != "1" ]]; then
if [[ ${monitor} =~ ^[A-Za-z]+$ ]]; then
echo -e "${lorg}${monitor}${endColor}" >> "${badMonitorsFile}"
elif [[ ${monitor} != ^[A-Za-z]+$ ]]; then
echo -e "${lblu}${monitor}${endColor}" >> "${badMonitorsFile}"
fi
fi
done < <(sed 's/\x1B\[[0-9;]*[JKmsu]//g' "${specifiedMonitorsFile}")
if [[ -s ${badMonitorsFile} ]]; then
echo -e "${red}The following monitors you specified are not valid:${endColor}"
echo ''
cat "${badMonitorsFile}"
sed -i 's/\x1B\[[0-9;]*[JKmsu]//g' "${badMonitorsFile}"
set +e
grep -vxf "${badMonitorsFile}" "${specifiedMonitorsFile}" > "${validMonitorsTempFile}"
true > "${validMonitorsFile}"
if [[ -s ${validMonitorsTempFile} ]]; then
while IFS= read -r monitor; do
echo -e "${grn}${monitor}${endColor}" >> "${validMonitorsFile}"
done < <(cat "${validMonitorsTempFile}")
echo ''
invalid_prompt
elif [[ ! -s ${validMonitorsTempFile} ]]; then
echo ''
echo 'Please make sure you are specifying a valid monitor and try again.'
echo ''
exit
fi
set -e
fi
}
# Function to convert friendly names to IDs.