Skip to content
Snippets Groups Projects

check_http first lines

Merged Hahn Axel (hahn) requested to merge add-systemdunit into master
check_http 0 → 100755
+ 144
0
 
#!/bin/bash
 
# ================================================================================
 
#
 
# CHECK HTTP
 
#
 
#
 
# -------------------------------------------------------------------------------
 
# 2023-09-05 v0.1 <axel.hahn@unibe.ch>
 
# ================================================================================
 
 
. $( dirname $0 )/inc_pluginfunctions
 
 
export self_APPVERSION=1.0
 
 
# ----------------------------------------------------------------------
 
# FUNCTIONS
 
# ----------------------------------------------------------------------
 
 
# show help text
 
function showHelp(){
 
local _self; _self=$(basename $0)
 
cat <<EOF
 
$( ph.showImlHelpHeader )
 
 
TODO
 
 
SYNTAX:
 
$_self [-h] ...
 
 
OPTIONS:
 
 
-h this help
 
 
-u URL Set url to fetch
 
-s STATUSCODE Statuscode to ceck
 
-r REGEX String to check in http response header
 
-b REGEX String to check in response body
 
 
EXAMPLES:
 
 
$_self -u
 
 
EOF
 
}
 
 
function getHeader(){
 
set -vx
 
echo "$1" | sed -n "1,${2}p"
 
}
 
 
function getHeader(){
 
set -vx
 
echo "$1" | sed -n "1,${2}p"
 
}
 
 
# ----------------------------------------------------------------------
 
# MAIN
 
# ----------------------------------------------------------------------
 
 
ph.hasParamoption "h" "$@"; bOptHelp=$?
 
 
if [ $bOptHelp -eq 0 -o $# -eq 0 ]; then
 
showHelp
 
exit 0
 
fi
 
 
ph.require "curl"
 
 
sUrl=$( ph.getValueWithParam '' u "$@")
 
iStatus=$( ph.getValueWithParam '200' s "$@")
 
sHeader=$( ph.getValueWithParam '' r "$@")
 
sBody=$( ph.getValueWithParam '' b "$@")
 
 
curlParams="-si"
 
sProblems=
 
sOK=
 
 
if [ -z "$sUrl" ]; then
 
ph.setStatus unknown
 
ph.status "Wrong parameters - no url was given."
 
ph.exit
 
fi
 
 
test -z "$sBody" && curlParams+=" -I"
 
 
 
 
out=$( curl $curlParams "$sUrl" )
 
 
 
iHeaderEnd=$( echo "$out" | grep -n ^$'\r' | cut -f 1 -d ':')
 
 
_header=$(echo "$out" | sed -n "1,${iHeaderEnd}p")
 
_body=$( echo "$out" | sed -n "${iHeaderEnd},\$p")
 
 
# echo "HEADER"; echo "$_header"
 
# echo "BODY"; echo "$_body"
 
 
 
# --- test status
 
if [ -n "$iStatus" ]; then
 
if ! grep -i "^HTTP/[0-9\.]* ${iStatus}" <<< "${_header}" >/dev/null; then
 
ph.setStatus critical
 
sProblems+="- Statuscode is not [${iStatus}];\n"
 
else
 
sOK+="- Statuscode is [${iStatus}];\n"
 
fi
 
fi
 
 
 
if [ -n "$sHeader" ]; then
 
if ! grep -i "$sHeader" <<< "${_header}" >/dev/null; then
 
ph.setStatus critical
 
sProblems+="- Header does not contain [${sHeader}];\n"
 
else
 
sOK+="- [${sHeader}] was found in header;\n"
 
fi
 
 
fi
 
 
if [ -n "$sBody" ]; then
 
if ! grep -i "$sBody" <<< "${_body}" >/dev/null; then
 
ph.setStatus critical
 
sProblems+="- Body does not contain [${sBody}];\n"
 
else
 
sOK+="- [${sBody}] was found in body;\n"
 
fi
 
 
fi
 
 
 
test -n "$sProblems" && sProblems="Problems:\n$sProblems\n"
 
test -n "$sOK" && sOK="Found:\n$sOK"
 
 
ph.status "Url ${sUrl}"
 
echo
 
echo -e "${sProblems}${sOK}"
 
echo
 
 
# echo "HEADER:"; echo "$_header"
 
 
ph.exit
 
 
# ----------------------------------------------------------------------
Loading