-
Notifications
You must be signed in to change notification settings - Fork 0
/
up_svc_batch.sh
executable file
·172 lines (142 loc) · 3.79 KB
/
up_svc_batch.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
#!/bin/bash
##############################################################
# Check multiple services using up_svc.sh
##############################################################
##############################################################
# Usage:
# ./up_svc_batch.sh [-q] [-m [EMAIL_ADDRESSES]]
#
# Options:
# -q: quiet
# -m: send email notification of services with non-
# zero exit status. Followed by optional parameter
# EMAIL_ADDRESSES. Separate multiple addresses
# with commas. If addresses not supplied
# uses default parameter $email (see below).
##############################################################
# Set working directory
DIR="$(cd "$(dirname "$0")" && pwd)"
######################################################
# Local parameters
# Only parameters in this file you may need to change
######################################################
# Error notification email subject
header="BIEN service error notification"
# Error notification email body title
ti="The following BIEN services may be down:"
######################################################
# External parameters
# Set in shared parameters file (params.sh)
######################################################
# Load default parameters
source ${DIR}/params.sh
# List of service URLs to check (from params file)
doms=$SVCS
# Default notification email(s)
# Overridden if email(s) supplied with -m option
email_default=$EMAIL_DEF
########################
# Functions
########################
function status_msg(){
###############################
# Returns status message for a
# give integer status code
#
# Usage:
# status_msg "$status"
###############################
case $status in
0)
statusmsg="OK"
;;
1)
statusmsg="inaccessible"
;;
2)
statusmsg="timed out"
;;
3)
statusmsg="not found"
;;
4)
statusmsg="connection refused"
;;
5)
statusmsg="offline or bad url"
;;
*)
statusmsg="unknown error"
;;
esac
echo "$statusmsg"
}
########################
# Get options
########################
quiet=false
notify=false
url=""
email=""
while [ "$1" != "" ]; do
# Get options
case $1 in
-q | --quiet ) quiet=true
;;
-m | --mailto ) notify=true
shift
email=$1
;;
* ) echo "invalid option!"; exit 1
esac
shift
done
if [ "$email" == "" ]; then
email=$email_default
fi
# "quiet" option for up_svc command
q_opt=""
if $quiet; then q_opt="-q"; fi
########################
# Main
########################
if ! $quiet; then echo "Checking service status"; fi
domsdown=""
first_loop=1
# Remove leading and trailing newlines, if present
doms="${doms#$'\n'}"
doms="${doms%$'\n'}"
while IFS='' read dom; do
IFS='|' read -ra arr <<< "$dom"
svc="${arr[0]}"
svc_disp="${svc} ${arr[1]}"
url="${arr[2]}"
# Strip trailing command from url, if present
base_url="${url/:\/\//PROTOCOL_DELIM}"
base_url="${base_url%/*}"
base_url="${base_url/PROTOCOL_DELIM/:\/\/}"
if ! $quiet; then
echo "${svc_disp}:"
echo -n " "
fi
# Check status of current service
${DIR}/up_svc.sh $q_opt -u $base_url
status=$?
if [ ! "$status" == "0" ]; then
# Compile status messages for services with errors
status_msg=$(status_msg "$status")
domdown="${svc_disp} (${base_url}): ${status_msg} [exit status ${status}]"
(( $first_loop )) && # "((...))" is bash specific
domsdown="$domdown" || # First -> Set
domsdown="$domsdown\n$domdown" # After -> Append
unset first_loop
fi
done <<< $doms
if $notify && [ "$domsdown" != "" ]; then
# Send error message
if ! $quiet; then echo -n "Sending sending service error notification..."; fi
domsdown=$(echo -e "$domsdown")
body=$(echo -e "${ti}\n\n${domsdown}")
echo -e "${body}\n\n"`date` | mail -s "$header" $email;
if ! $quiet; then echo "done"; fi
fi