-
Notifications
You must be signed in to change notification settings - Fork 0
/
monitorsites.sh
executable file
·455 lines (376 loc) · 12.4 KB
/
monitorsites.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
#!/bin/sh
SELF="$0"
BASENAME="$(basename $0)"
USR="$(whoami)"
USAGE="Monitor sites. This script is supposed to run from cron.
USAGE
-statsdir path
Directory where to store stats. If ommited, /tmp will be used.
-tasks
Directory where tasks are stored. If ommited, will use /etc/monitorsites.
-pagegen [page_file_path]
Generate HTML page with stats. If no path specified, will store as 'index.html' in stats directory.
-onalert command
Invoke specified command on alert. Alert message will be piped to command's stdin.
-help
Print this help.
EXAMPLE
$BASENAME -statsdir /var/log/sitesmonitor -tasks /etc/monitorsites/tasks -pagegen /var/www/stats/index.html
https://github.com/rekcuFniarB/ZabbixSucks#readme"
PATH='/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin'
## print messages to stderr
errlog () {
echo "$@" 1>&2
}
## sendmail wrapper to add some headers
fn_sendmail() {
local MAILNAME MESSAGE X_HEADERS HEADERS_DONE LINE
if [ -f /etc/mailname ]; then
MAILNAME="$(cat /etc/mailname | head -n 1)"
fi
if [ -z "$MAILNAME" ]; then
MAILNAME='localhost'
fi
X_HEADERS="To: $USR@$MAILNAME
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-transfer-encoding: 8bit
"
while read LINE; do
if [ -z "$HEADERS_DONE" ]; then
if [ -z "$LINE" ]; then
MESSAGE="$MESSAGE$X_HEADERS"
HEADERS_DONE=1
fi
fi
MESSAGE="$MESSAGE$LINE
"
done # while
echo "$MESSAGE" | sendmail "$USR"
} # fn_sendmail()
## send email or just print
## @param file
fn_notify () {
if [ -n "$(which sendmail)" ]; then
## send email directly
{
echo "Subject: $MAIL_SUBJ\n\n"
cat "$1"
} | fn_sendmail
else
## Just print to stderr
cat "$1" 1>&2
fi
if [ -n "$_arg_onalert" -a -x "$_arg_onalert" ]; then
cat "$1" | "$_arg_onalert"
fi
} ## fn_notify()
getfilemodified () {
stat "$1" | grep 'Modify:' | sed 's/Modify://'
}
## Args parser
## Supported arg formats:
## -arg --arg (both are identical)
## -arg value
## -arg=value
parse_args() {
local ARG _PREV_ARG _ARG _VAR _VAL _ARGN SKIP
for ARG in "$@"; do
case "$ARG" in
--) _PREV_ARG=''
SKIP=1 ;;
-*) if [ -z "$SKIP" ]; then
_ARG="$(echo -n "$ARG" | sed 's/^-\+//' | sed 's/-/_/g' | tr -d '\r' | tr '\t\v\n' ' ')"
case "$_ARG" in
*=*) _PREV_ARG=''
_VAR="$(echo -n "$_ARG" | sed 's/=.*//')"
_VAL="$(echo -n "$_ARG" | sed 's/.\+=//')"
if [ -z "$_VAL" ]; then
_VAL=0
fi
eval "_arg_$_VAR=\$_VAL" ;;
*) _PREV_ARG="_arg_$_ARG"
eval "_arg_$_ARG=1" ;;
esac
fi ;;
*) if [ -n "$_PREV_ARG" ]; then
eval "$_PREV_ARG=\$ARG"
_PREV_ARG=''
else
_ARGN=$((_ARGN+1))
eval "_arg$_ARGN=\$ARG"
fi ;;
esac
done
} # parse_args()
## Standard site availability test
## Accepts no args. Invoked from tasks.
fn_test_site() {
if [ -z "$URL" ]; then
errlog "URL required."
exit 1
fi
if [ -z "$USERAGENT" ]; then
USERAGENT='Sites monitor +https://github.com/rekcuFniarB/ZabbixSucks#readme'
fi
START=$(date '+%s%N')
RESPONSE="$(wget -T 2 -t 3 -o /dev/null -U "$USERAGENT" -O - "$URL")"
ST=$?
END=$(date '+%s%N')
DIFF=$(( (END - START)/1000000 ))
errlog "Query time: ${DIFF}ms"
if [ -z "$THRESHOLD" ]; then
THRESHOLD=500
fi
if [ $DIFF -gt $THRESHOLD ]; then
echo "Query time took more than ${THRESHOLD}ms (${DIFF}ms)"
STATUS=$(( 200 + ST ))
fi
if [ -z "$RESPONSE" ]; then
echo "Got empty response."
STATUS=$(( 500 + ST ))
fi
if [ -z $STATUS ]; then
STATUS=$ST
fi
## If above request failed, test domain to print in logs
if [ $STATUS -gt 0 ] && [ -n "$DOMAIN" ]; then
timeout 10 ping -c 3 $DOMAIN 1>&2
errlog
timeout 10 traceroute $DOMAIN 1>&2
errlog
#traceroute6 $DOMAIN 1>&2
#errlog
timeout 10 dig @8.8.8.8 $DOMAIN 1>&2
#errlog
#dig AAAA $DOMAIN 1>&2
#errlog
#dig @8.8.8.8 AAAA $DOMAIN 1>&2
#errlog
#/sbin/ip -6 route show 1>&2
#errlog
#/sbin/route -A inet6 1>&2
fi
errlog
#errlog "PATH: $PATH"
exit $STATUS
}
if [ _"$BASENAME" = _'monitorsites' -o _"$BASENAME" = _'monitorsites.sh' ]; then
for util in sed grep wget ping traceroute dig; do
if [ -z "$(which "$util")" ]; then
errlog "ERROR: required '$util' not found."
exit 1
fi
done
parse_args "$@"
if [ -n "$_arg_help" ] || [ -n "$_arg_h" ]; then
errlog "$USAGE"
exit 0
fi
export SELF USR BASENAME
if [ -n "$_arg_tasks" ] && [ "$_arg_tasks" != 1 ]; then
TASKS="$_arg_tasks"
else
TASKS='/etc/monitorsites'
fi
if [ ! -d "$TASKS" ]; then
errlog "No such directory $TASKS"
exit 1
fi
if [ -n "$_arg_statsdir" ] && [ "$_arg_tasks" != 1 ]; then
STATSDIR="$_arg_statsdir"
else
#ID=$(id -u)
#TMPDIR="/run/user/$ID"
#
#if [ ! -d "$TMPDIR" ]; then
# TMPDIR="/dev/shm"
#fi
TMPDIR='/tmp'
STATSDIR="$TMPDIR/monitorsites"
if [ ! -d "$STATSDIR" ]; then
mkdir "$STATSDIR"
fi
if [ ! -d "$STATSDIR" ]; then
errlog "Could not create directory '$STATSDIR'"
exit 1
fi
fi
## Executing every task
for TASK in "$TASKS/"*.task; do
TIME="$(date)"
TASKNAME="$(basename "$TASK" | sed 's/\.task$//' )"
if [ _"$TASKNAME" = _'*' ]; then
errlog "SITES MONITOR\n=============\n\nNo tasks."
exit
fi
if [ ! -x "$TASK" ]; then
# errlog "Monitor Sites: $TASK non executable. Skipping..."
continue
fi
if [ ! -f "$STATSDIR/TASK__$TASKNAME.msg" ]; then
touch "$STATSDIR/TASK__$TASKNAME.msg"
fi
if [ ! -f "$STATSDIR/TASK__$TASKNAME.err" ]; then
touch "$STATSDIR/TASK__$TASKNAME.err"
fi
if [ ! -f "$STATSDIR/TASK__$TASKNAME.status" ]; then
touch "$STATSDIR/TASK__$TASKNAME.status"
fi
START=$(date +%s%N)
timeout 2m "$TASK" > "$STATSDIR/TASK.MSG" 2> "$STATSDIR/TASK.ERR"
STATUS=$?
END=$(date +%s%N)
DIFF=$(( (END - START)/1000000 ))
CHANGED=''
## timed out command returns exits code 124 if command not completed
if [ $STATUS -eq 124 ]; then
ERR='Task timed out.'
fi
_STATUS="$(cat "$STATSDIR/TASK__$TASKNAME.status")"
if [ _"$STATUS" != _"$_STATUS" ]; then
CHANGED="${CHANGED}STATUS "
STATUS_CHANGED=1
fi
OUT="$(cat "$STATSDIR/TASK.MSG")"
_OUT="$(cat "$STATSDIR/TASK__$TASKNAME.msg")"
if [ _"$OUT" != _"$_OUT" ]; then
CHANGED="${CHANGED}OUTPUT"
fi
if [ -n "$CHANGED" ]; then
ERR="$ERR\n$(cat "$STATSDIR/TASK.ERR")"
## store status
echo -n "$STATUS" > "$STATSDIR/TASK__$TASKNAME.status"
mv "$STATSDIR/TASK.MSG" "$STATSDIR/TASK__$TASKNAME.msg"
mv "$STATSDIR/TASK.ERR" "$STATSDIR/TASK__$TASKNAME.err"
REPORT="$REPORT
## TASK $TASKNAME ##
Date: $TIME
Time: ${DIFF}ms
Changed: $CHANGED
New status: $STATUS
Prev. status: $_STATUS
### Output:
$OUT
### Prev. output:
$_OUT
### StdErr:
$ERR
"
MAIL_SUBJ="$MAIL_SUBJ$TASKNAME: $STATUS; "
fi ## if $CHANGED
done ## for tasks
if [ -n "$REPORT" ]; then
REPORT="
SITES MONITOR REPORT
====================
$REPORT
"
echo -n "$REPORT" > "$STATSDIR/REPORT.txt"
## send notification if status changed
if [ -n "$STATUS_CHANGED" ]; then
fn_notify "$STATSDIR/REPORT.txt"
fi
fi
### Generate HTML page
if [ -n "$_arg_pagegen" ]; then
if [ "$_arg_pagegen" = 1 ]; then
PAGE="$STATSDIR/index.html"
else
PAGE="$_arg_pagegen"
fi
export LANG=C
echo '<!DOCTYPE html>
<html>
<head>
<title>Sites monitoring status</title>
<meta charset="UTF-8">
<style>
h2 {
margin: 1em;
}
div.type {
padding-left: 1em;
}
div.infoblock {
border: 1px solid black;
margin-bottom: 1em;
}
div.tbl {
display: table;
width: 100%;
background-color: #c0c0c0;
}
div.tr {
display: table-row;
}
div.td {
display: table-cell;
}
div.time{
text-align: right;
}
pre {
background-color: #eeeeee;
padding: 1em;
margin: 0;
}
</style>
</head>
<body>' > "$PAGE"
for FILE in "$STATSDIR/TASK__"*
do
TASKNAME="$(basename "$FILE" | sed 's/TASK__//')"
TIME="$(getfilemodified "$FILE")"
echo '<div class="infoblock">' >> "$PAGE"
echo "<h2>$TASKNAME" | sed 's#\.err#</h2>\n<div class="tbl"><div class="tr"><div class="type td">StdErr</div>%#' | \
sed 's#\.msg#</h2>\n<div class="tbl"><div class="tr"><div class="type td">Message</div>%#' | \
sed 's#\.status#</h2>\n<div class="tbl"><div class="tr"><div class="type td">Status</div>%#' | \
sed "s#%#<div class=\"time td\">$TIME</div></div></div>#" >> "$PAGE"
echo "<pre>" >> "$PAGE"
cat "$FILE" >> "$PAGE"
echo "</pre>" >> "$PAGE"
echo '</div>' >> "$PAGE"
echo '' >> "$PAGE"
done
if [ -f "$STATSDIR/REPORT.txt" ]; then
echo '<div class="infoblock">' >> "$PAGE"
echo '<h2>Last report</h2>' >> "$PAGE"
echo '<div class="tbl"><div class="tr"><div class="type td">Summary</div><div class="time td">' >> "$PAGE"
getfilemodified "$STATSDIR/REPORT.txt" >> "$PAGE"
echo '</div></div></div>' >> "$PAGE"
echo '<pre>' >> "$PAGE"
cat "$STATSDIR/REPORT.txt" >> "$PAGE"
echo '</pre>' >> "$PAGE"
echo '</div>' >> "$PAGE"
echo '' >> "$PAGE"
fi
if [ -f "$STATSDIR/TASK.MSG" ]; then
echo '<div class="infoblock">' >> "$PAGE"
echo '<h2>Last stdout</h2>' >> "$PAGE"
echo '<div class="tbl"><div class="tr"><div class="type td">Message</div><div class="time td">' >> "$PAGE"
getfilemodified "$STATSDIR/TASK.MSG" >> "$PAGE"
echo '</div></div></div>' >> "$PAGE"
echo '<pre>' >> "$PAGE"
cat "$STATSDIR/TASK.MSG" >> "$PAGE"
echo '</pre>' >> "$PAGE"
echo '</div>' >> "$PAGE"
echo '' >> "$PAGE"
fi
if [ -f "$STATSDIR/TASK.ERR" ]; then
echo '<div class="infoblock">' >> "$PAGE"
echo '<h2>Last stderr</h2>' >> "$PAGE"
echo '<div class="tbl"><div class="tr"><div class="type td">StdErr</div><div class="time td">' >> "$PAGE"
getfilemodified "$STATSDIR/TASK.ERR" >> "$PAGE"
echo '</div></div></div>' >> "$PAGE"
echo '<pre>' >> "$PAGE"
cat "$STATSDIR/TASK.ERR" >> "$PAGE"
echo '</pre>' >> "$PAGE"
echo '</div>' >> "$PAGE"
echo '' >> "$PAGE"
fi
echo '</body>
</html>
' >> "$PAGE"
fi # if --pagegen
fi ## if self