-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpolling.sh
More file actions
74 lines (65 loc) · 1.18 KB
/
polling.sh
File metadata and controls
74 lines (65 loc) · 1.18 KB
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
#!/bin/bash
declare -i duration=1
declare -i iterator=0
declare -i max_iterations=60
declare hasUrl=""
declare endpoint
usage() {
cat <<END
polling.sh [-i] [-h] endpoint
Report the health status of the endpoint
-i: include Uri for the format
-h: help
END
}
while getopts "ih" opt; do
case $opt in
i)
hasUrl=true
;;
h)
usage
exit 0
;;
\?)
echo "Unknown option: -${OPTARG}" >&2
exit 1
;;
esac
done
shift $((OPTIND -1))
if [[ $1 ]]; then
endpoint=$1
else
echo "Please specify the endpoint."
usage
exit 1
fi
healthcheck() {
declare url=$1
result=$(curl -i $url 2>/dev/null | grep HTTP/2)
echo $result
}
while [[ true ]]; do
result=`healthcheck $endpoint`
declare status
if [[ -z $result ]]; then
status="N/A"
else
status=${result:7:3}
fi
timestamp=$(date "+%Y%m%d-%H%M%S")
if [[ -z $hasUrl ]]; then
echo "$timestamp | $status "
else
echo "$timestamp | $status | $endpoint "
fi
if [ "$status" == "200" ]; then
exit 0
fi
sleep $duration
iterator=$((iterator+1))
if [ "$iterator" == "$max_iterations" ]; then
exit 1
fi
done