-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuptime.sh
executable file
·62 lines (51 loc) · 1.63 KB
/
uptime.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
#!/bin/env bash
set -uo pipefail;
err() {
echo "[$(date +'%Y-%m-%dT%H:%M:%S%z')]: $*" >&2
}
case "$(curl -s --max-time 2 -I https://www.google.com | sed 's/^[^ ]* *\([0-9]\).*/\1/; 1q')" in
[23]) echo "HTTP connectivity is up";;
5) echo "The web proxy won't let us through";;
*) err "The network is down or very slow"; exit 1 ;;
esac
aflag='';
fflag='';
while getopts 'af' flag; do
case "${flag}" in
a) aflag='true' ;;
f) fflag='true' ;;
*) err "Unexpected flag"; exit 1 ;;
esac
done
date=$(date);
if [[ -n $aflag ]]; then
filename="reports/$date-report.txt"
else
# Clear the report.txt file
filename="reports/temp-report.txt"
cp /dev/null reports/temp-report.txt;
fi
printf "Report generated on: %s \n" "$date" | tee -a "$filename";
success=( );
failure=( );
while IFS= read -r line; do
curl=$(curl -Is --max-time 15 "$line");
res=$?
if test "$res" != "0"; then
printf "the curl command failed for %s with: %s \n" "$line" "$res" | tee -a "$filename";
failure+=("$line");
else
printf "The curl command was successful for %s. It exited with a status code of:" "$line" | tee -a "$filename";
printf "%s" "$curl" | head -n 1 | tee -a "$filename";
printf "\n";
success+=("$line");
fi
done < urls.txt
if [[ -n $fflag ]]; then
cp /dev/null reports/formatted-report.txt;
printf "SUCCESS:\n"
printf '%s\n' "${success[@]}" | tee -a "reports/formatted-report.txt";
printf "\n" | tee -a "reports/formatted-report.txt";
printf "FAILURE:\n" | tee -a "reports/formatted-report.txt";
printf '%s\n' "${failure[@]}" | tee -a "reports/formatted-report.txt";
fi