-
Notifications
You must be signed in to change notification settings - Fork 0
/
up_svc.sh
executable file
·139 lines (119 loc) · 3.44 KB
/
up_svc.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
#!/bin/bash
#################################################
# Check if base service is up and accepting requests
#################################################
##############################################################
# Usage:
# ./up_svc.sh [-q] [-v] -u $URL
#
# Options:
# -q: quiet
# -v: verbose
# -u: service URL, takes parameter $URL
##############################################################
#############################################
# Exit status codes:
# 0: service up, status OK
# 1: service inaccessible
# 2: service timed out
# 3: service not found
# 4: service connection refused
# 5: server down or bad server name
#############################################
#################
# Parameters
#################
quiet=false
verbose=false
########################
# Get options
########################
url=""
while [ "$1" != "" ]; do
# Get options
case $1 in
-q | --quiet ) quiet=true
;;
-v | --verbose ) quiet=false
verbose=true
;;
-u | --url ) shift
url=$1
;;
* ) echo "invalid option!"; exit 1
esac
shift
done
if [ "$url" == "" ]; then
if ! $quiet; then echo "ERROR: server parameter not defined"; fi
exit 5
fi
########################
# Extract server name
########################
server=$url
server=${server#"http://"}
server=${server#"https://"}
server=${server#"www."}
IFS=':' read -r -a arr <<< "$server"
server=${arr[0]}
IFS='/' read -r -a arr <<< "$server"
server=${arr[0]}
########################
# Check service status
# (port open on base url)
########################
if ! $quiet; then
echo -n "Checking service: $url...";
fi
# Note the following wget options:
# --spider: return 0 if resource found but do not download
# --server-response: Print HTTP headers and FTP responses
# Also: 2>&1: include error messages in STDOUT
response="$(wget --spider --server-response --timeout=5 --tries=1 $url 2>&1)"
if [ $(echo "$response" | grep -q "timed out"; echo $?) == 0 ]; then
svc_status="timeout"
elif [ $(echo "$response" | grep -q "200 OK"; echo $?) == 0 ]; then
svc_status="OK"
elif [ $(echo "$response" | grep -q "Name or service not known"; echo $?) == 0 ]; then
svc_status="not found"
elif [ $(echo "$response" | grep -q "onnection refused"; echo $?) == 0 ]; then
svc_status="Connection refused"
fi
if [[ $svc_status == "OK" ]] ; then
if ! $quiet; then echo "OK ✓"; fi
exitcode=0
if $verbose; then echo "exit status = ${exitcode}"; fi
exit 0
elif [[ $svc_status == "timeout" ]] ; then
if ! $quiet; then echo "timeout ✗"; fi
exitcode=2
elif [[ $svc_status == "not found" ]] ; then
if ! $quiet; then echo "not found ✗"; fi
exitcode=3
elif [[ $svc_status == "Connection refused" ]] ; then
if ! $quiet; then echo "Connection refused ✗"; fi
exitcode=4
else
if ! $quiet; then echo "service inaccessible ✗"; fi
exitcode=1
fi
########################
# Check server status
########################
if ! $quiet; then
echo -n " Checking server: $server..."
fi
# Server down or not responding
# nmap works even if IMCP/ping disabled
nmap -PN -T4 -p 80 $server 2>&1 | grep -q "Host is up"
# grep exit status=1 if host is down or bad server name
status=$( echo $? )
if [[ $status == 0 ]] ; then
if ! $quiet; then echo "online ✓"; fi
else
if ! $quiet; then echo "offline or bad url ✗"; fi
exitcode=5
fi
if $verbose; then echo "exit status = "$exitcode; fi
exit $exitcode