Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
Patap
bash-http-monitoring
Commits
5dbc74af
Commit
5dbc74af
authored
Dec 27, 2020
by
R. van Elst
Browse files
Shellcheck fixes
parent
3374cdd8
Changes
2
Hide whitespace changes
Inline
Side-by-side
screenshots/fail.png
View replaced file @
3374cdd8
View file @
5dbc74af
63.1 KB
|
W:
|
H:
67.9 KB
|
W:
|
H:
2-up
Swipe
Onion skin
srvmon.sh
View file @
5dbc74af
...
...
@@ -28,33 +28,46 @@ statuscode[gist.github.com]=302
# How many curl checks to run at the same time
maxConcurrentCurls
=
12
#
Start of script. Do not edit below
#
Max timeout of a check in seconds
defaultTimeOut
=
10
#
We're writing the status code to a file, reading that later on. Why?
#
Because an array cannot be filled via a subprocess (curl ... &)
.
#
Start of script. Do not edit below
#
Exit immediately if a command exits with a non-zero status
.
set
-e
# patterns which match no files expand to a null string, otherwise later on we'd have *.status as a file...
shopt
-s
nullglob
# start of the function definitions
# This function allows the script to execute all the curl calls in paralell.
# Otherwise, if one would timeout or take long, the rest after that would be
# slower.
# slower. We're writing the status code to a file, reading that later on. Why?
# Because an array cannot be filled via a subprocess (curl ... &)
doRequest
()
{
name
=
"
${
1
}
"
url
=
"
${
2
}
"
checkStartTimeMs
=
$(
date
+%s%3N
)
# epoch in microseconds, but stripped so it's milliseconds
checkStatusCode
=
$(
curl
--max-time
10
--silent
--show-error
--insecure
--output
/dev/null
--write-out
"%{http_code}"
"
$url
"
2>
${
tempfolder
}
/FAIL/
${
name
}
.error
)
checkEndTimeMs
=
$(
date
+%s%3N
)
timeCheckTook
=
$((
checkEndTimeMs-checkStartTimeMs
))
if
[[
!
-z
${
statuscode
[
${
name
}
]
}
]]
;
then
defaultExpectedStatusCode
=
${
statuscode
[
${
name
}
]
}
checkStartTimeMs
=
"
$(
date
+%s%3N
)
"
# epoch in microseconds, but last chars stripped so it's milliseconds
set
+e
# curl errors don't count for early exit, turn off e
checkStatusCode
=
"
$(
curl
--max-time
"
${
defaultTimeOut
}
"
--silent
--show-error
--insecure
--output
/dev/null
--write-out
"%{http_code}"
"
$url
"
--stderr
"
${
tempfolder
}
/FAIL/
${
name
}
.error"
)
"
set
-e
# turn e back on
checkEndTimeMs
=
"
$(
date
+%s%3N
)
"
timeCheckTook
=
"
$((
checkEndTimeMs-checkStartTimeMs
))
"
expectedStatusCode
=
${
defaultExpectedStatusCode
}
# check if there is a specific status code for this domain
if
[[
-n
"
${
statuscode
[
${
name
}
]
}
"
]]
;
then
expectedStatusCode
=
"
${
statuscode
[
${
name
}
]
}
"
fi
if
[[
${
defaultE
xpectedStatusCode
}
-eq
${
checkStatusCode
}
]]
;
then
echo
${
timeCheckTook
}
>
${
tempfolder
}
/OK/
${
name
}
.duration
if
[[
${
e
xpectedStatusCode
}
-eq
${
checkStatusCode
}
]]
;
then
echo
"
${
timeCheckTook
}
"
>
"
${
tempfolder
}
/OK/
${
name
}
.duration
"
else
echo
${
checkStatusCode
}
>
${
tempfolder
}
/FAIL/
${
name
}
.status
echo
"
${
checkStatusCode
}
"
>
"
${
tempfolder
}
/FAIL/
${
name
}
.status"
fi
# if the error file is empty, remove it.
if
[[
!
-s
"
${
tempfolder
}
/FAIL/
${
name
}
.error"
]]
;
then
rm
"
${
tempfolder
}
/FAIL/
${
name
}
.error"
fi
}
...
...
@@ -62,7 +75,7 @@ writeOkayChecks() {
echo
"</div></div>"
echo
"<div class=row><div class=col>"
echo
"<h2>Checks</h2>"
pushd
"
${
tempfolder
}
/OK"
2>&1
>
/dev/null
pushd
"
${
tempfolder
}
/OK"
>
/dev/null
2>&1
okFiles
=(
*
.duration
)
okCount
=
${#
okFiles
[@]
}
if
[[
okCount
-gt
0
]]
;
then
...
...
@@ -70,20 +83,20 @@ writeOkayChecks() {
if
[[
-r
$filename
]]
;
then
value
=
"
$(
cat
$filename
)
"
filenameWithoutExt
=
${
filename
%.*
}
echo
-n
'<a href="#" class="btn btn-success disabled" tabindex="-1" role="button" aria-disabled="true" style="margin-top: 10px; padding: 10px;">'
echo
-n
"
${
filenameWithoutExt
}
"
echo
-n
"<font color=LightGray> ("
echo
-n
${
value
}
echo
-n
" ms)</font>"
echo
-n
"</a> "
echo
'<a href="#" class="btn btn-success disabled" tabindex="-1" role="button" aria-disabled="true" style="margin-top: 10px; padding: 10px;">'
echo
"
${
filenameWithoutExt
}
"
echo
"<font color=LightGray> ("
echo
${
value
}
echo
" ms)</font>"
echo
"</a> "
fi
done
fi
popd
2>&1
>
/dev/null
popd
>
/dev/null
2>&1
}
writeFailedChecks
()
{
pushd
"
${
tempfolder
}
/FAIL"
2>&1
>
/dev/null
pushd
"
${
tempfolder
}
/FAIL"
>
/dev/null
2>&1
failFiles
=(
*
.status
)
failCount
=
${#
failFiles
[@]
}
if
[[
failCount
-gt
0
]]
;
then
...
...
@@ -99,19 +112,23 @@ writeFailedChecks() {
for
filename
in
"
${
failFiles
[@]
}
"
;
do
if
[[
-r
$filename
]]
;
then
filenameWithoutExt
=
${
filename
%.*
}
error
=
"
$(
cat
${
filenameWithoutExt
}
.error
)
"
if
[[
-r
"
${
filenameWithoutExt
}
.error"
]]
;
then
curlError
=
"
$(
cat
"
${
filenameWithoutExt
}
.error"
2>/dev/null
)
"
else
curlError
=
"Status code does not match expected code"
fi
status
=
"
$(
cat
${
filename
}
)
"
echo
-n
"<tr class='table-danger'><td>"
echo
-n
"
${
filenameWithoutExt
}
"
echo
-n
"</td><td>"
echo
-n
"
${
status
}
/ "
if
[[
-z
${
statuscode
[
${
filenameWithoutExt
}
]
}
]]
;
then
echo
-n
"200
"
echo
"<tr class='table-danger'><td>"
echo
"
${
filenameWithoutExt
}
"
echo
"</td><td>"
echo
"
${
status
}
/ "
if
[[
-z
"
${
statuscode
[
${
filenameWithoutExt
}
]
}
"
]]
;
then
echo
"
${
defaultExpectedStatusCode
}
"
else
echo
-n
"
${
statuscode
[
${
filenameWithoutExt
}
]
}
"
echo
"
${
statuscode
[
${
filenameWithoutExt
}
]
}
"
fi
echo
-n
"</td><td>"
echo
-n
"
${
e
rror
}
"
echo
"</td><td>"
echo
"
${
curlE
rror
}
"
echo
"</td></tr>"
fi
done
...
...
@@ -121,11 +138,11 @@ writeFailedChecks() {
echo
"All is well, all services are up."
echo
"</div>"
fi
popd
2>&1
>
/dev/null
popd
>
/dev/null
2>&1
}
cleanupFailedCheckFiles
()
{
pushd
"
${
tempfolder
}
/FAIL"
2>&1
>
/dev/null
pushd
"
${
tempfolder
}
/FAIL"
>
/dev/null
2>&1
errorFiles
=(
*
.error
)
errorCount
=
${#
errorFiles
[@]
}
for
filename
in
"
${
errorFiles
[@]
}
"
;
do
...
...
@@ -141,12 +158,12 @@ cleanupFailedCheckFiles() {
rm
"
${
filename
}
"
fi
done
popd
2>&1
>
/dev/null
popd
>
/dev/null
2>&1
rmdir
"
${
tempfolder
}
/FAIL"
}
cleanupOKCheckFiles
()
{
pushd
"
${
tempfolder
}
/OK"
2>&1
>
/dev/null
pushd
"
${
tempfolder
}
/OK"
>
/dev/null
2>&1
okFiles
=(
*
.duration
)
okCount
=
${#
okFiles
[@]
}
for
filename
in
"
${
okFiles
[@]
}
"
;
do
...
...
@@ -154,10 +171,40 @@ cleanupOKCheckFiles() {
rm
"
${
filename
}
"
fi
done
popd
2>&1
>
/dev/null
popd
>
/dev/null
2>&1
rmdir
"
${
tempfolder
}
/OK"
}
writeHeader
()
{
echo
'<!doctype html><html lang="en"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">'
echo
'<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4.5.3/dist/css/bootstrap.min.css" integrity="sha384-TX8t27EcRE3e/ihU7zmQxVncDAy5uIKz4rEkgIXeMed4M0jlfIDPvg6uqKI2xXr2" crossorigin="anonymous">'
echo
"<title>Monitoring</title>"
echo
"</head><body>"
echo
"<div class=container><div class=row><div class=col>"
echo
"<h1>Monitoring</h1>"
echo
"</div></div>"
echo
"<div class=row><div class=col>"
}
writeFooter
()
{
echo
"</div></div>"
echo
"<div class=row><div class=col>"
echo
"<h2>Info</h2>"
echo
"<p>Last check: "
date
echo
"<br>Total duration:
${
runtime
}
ms"
echo
"<br>Monitoring script by <a href='https://raymii.org/'>Remy van Elst</a>. License: GNU AGPLv3. "
echo
"<a href='https://github.com/raymiiorg/bash-http-monitoring'>Source code</a>"
echo
"</p>"
echo
"</div></div></div>"
echo
"</body></html>"
}
# script start
# Total script duration timer
start
=
$(
date
+%s%3N
)
...
...
@@ -168,7 +215,7 @@ tempfolder=${tmpdir:-/tmp/statusmon/}
mkdir
-p
"
${
tempfolder
}
/OK"
||
exit
1
mkdir
-p
"
${
tempfolder
}
/FAIL"
||
exit
1
pushd
"
${
tempfolder
}
"
2>&1
>
/dev/null
pushd
"
${
tempfolder
}
"
>
/dev/null
2>&1
# Do the checks parallel
for
key
in
"
${
!urls[@]
}
"
...
...
@@ -181,20 +228,10 @@ do
done
wait
# Header
echo
'<!doctype html><html lang="en"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">'
echo
'<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4.5.3/dist/css/bootstrap.min.css" integrity="sha384-TX8t27EcRE3e/ihU7zmQxVncDAy5uIKz4rEkgIXeMed4M0jlfIDPvg6uqKI2xXr2" crossorigin="anonymous">'
echo
"<title>Monitoring</title>"
echo
"</head><body>"
echo
"<div class=container><div class=row><div class=col>"
echo
"<h1>Monitoring</h1>"
echo
"</div></div>"
echo
"<div class=row><div class=col>"
writeHeader
# Failed checks, if any, go on top
writeFailedChecks
;
writeFailedChecks
# Okay checks go below the failed checks
writeOkayChecks
...
...
@@ -203,25 +240,12 @@ writeOkayChecks
cleanupFailedCheckFiles
cleanupOKCheckFiles
# Footer
echo
"</div></div>"
echo
"<div class=row><div class=col>"
echo
"<h2>Info</h2>"
echo
"<p>Last check: "
date
# stop the total timer
end
=
$(
date
+%s%3N
)
runtime
=
$((
end-start
))
echo
"<br>Total duration:
${
runtime
}
ms"
echo
"<br>Monitoring script by <a href='https://raymii.org/'>Remy van Elst</a>. License: GNU AGPLv3. "
echo
"<a href='https://github.com/raymiiorg/bash-http-monitoring'>Source code</a>"
echo
"</p>"
echo
"</div></div></div>"
echo
"</body></html>"
writeFooter
popd
2>&1
>
/dev/null
popd
>
/dev/null
2>&1
rmdir
"
${
tempfolder
}
"
\ No newline at end of file
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment