Skip to content
Snippets Groups Projects
Commit 6648b4bc authored by Hahn Axel (hahn)'s avatar Hahn Axel (hahn)
Browse files

Merge branch '5127-detect-5xx-server-error' into 'master'

abort on http 5xx error

See merge request !4
parents 6770ec69 f58801dd
No related branches found
No related tags found
1 merge request!4abort on http 5xx error
...@@ -16,6 +16,7 @@ ...@@ -16,6 +16,7 @@
# ---------------------------------------------------------------------- # ----------------------------------------------------------------------
# ah = axel.hahn@iml.unibe.ch # ah = axel.hahn@iml.unibe.ch
# 2022-02-16 v0.2 ah add --cfg param # 2022-02-16 v0.2 ah add --cfg param
# 2022-03-04 v0.3 ah abort on http 5xx error
# ====================================================================== # ======================================================================
tmpfile=/tmp/outcurl.tmp tmpfile=/tmp/outcurl.tmp
...@@ -365,6 +366,10 @@ typeset cfg_dryrun=false ...@@ -365,6 +366,10 @@ typeset cfg_dryrun=false
if [ ${_paramDryrun} = false ]; then if [ ${_paramDryrun} = false ]; then
_wd ">>>>> $_paramAction $_paramObj [${_object_name}] >> $_sMethod $_sUrl $_jsondata" _wd ">>>>> $_paramAction $_paramObj [${_object_name}] >> $_sMethod $_sUrl $_jsondata"
http.makeRequest "$_sMethod" "$_sUrl" "$_jsondata" http.makeRequest "$_sMethod" "$_sUrl" "$_jsondata"
if http.isServerError >/dev/null; then
echo "CRITICAL ERROR: Director API request failed with a server error $_sMethod $_sUrl"
exit 1
fi
if [ $_bShowResponse = true ]; then if [ $_bShowResponse = true ]; then
http.getResponseHeader http.getResponseHeader
http.getResponse http.getResponse
......
...@@ -11,13 +11,15 @@ ...@@ -11,13 +11,15 @@
# ---------------------------------------------------------------------- # ----------------------------------------------------------------------
# ah = axel.hahn@iml.unibe.ch # ah = axel.hahn@iml.unibe.ch
# 2021-03-.. init # 2021-03-.. init
# 2022-01-11 v0.7 ah shellcheck # 2022-01-11 v0.7 ah shellcheck
# 2022-02-16 v0.8 ah add --cfg param # 2022-02-16 v0.8 ah add --cfg param
# 2022-03-04 v0.9 ah abort on http 5xx error
# 2022-03-14 v0.10 ah less output and add _elog to run as a service
# ====================================================================== # ======================================================================
_product="ICINGA PASSIVE CLIENT" _product="ICINGA PASSIVE CLIENT"
_version="0.8" _version="0.10"
_license="GNU GPL 3.0" _license="GNU GPL 3.0"
_copyright='(c) 2020 Institute for Medical Education * University of Bern' _copyright='(c) 2020 Institute for Medical Education * University of Bern'
...@@ -109,10 +111,8 @@ function loopChecks(){ ...@@ -109,10 +111,8 @@ function loopChecks(){
lockpid=$(cat "${lockfile}" | cut -f 2 -d "-" | cut -f 4 -d " " | grep "[0-9]") lockpid=$(cat "${lockfile}" | cut -f 2 -d "-" | cut -f 4 -d " " | grep "[0-9]")
ps -f --pid "$lockpid" | grep "$(basename $0)" | grep loop >/dev/null ps -f --pid "$lockpid" | grep "$(basename $0)" | grep loop >/dev/null
if [ $? -eq 0 ]; then if [ $? -eq 0 ]; then
_log "ABORT: Loop seems to run already. See process with PID $lockpid" _elog "ABORT: Loop seems to run already. See process with PID $lockpid"
echo _elog $( ps -f --pid "$lockpid" )
ps -f --pid "$lockpid"
echo
exit 0 exit 0
fi fi
fi fi
...@@ -120,15 +120,15 @@ function loopChecks(){ ...@@ -120,15 +120,15 @@ function loopChecks(){
_log "---------- starting in a permanent loop" _log "---------- starting in a permanent loop"
echo "Serviceloop started $(date) - process id $$" > "${lockfile}" echo "Serviceloop started $(date) - process id $$" > "${lockfile}"
if [ $? -ne 0 ]; then if [ $? -ne 0 ]; then
_log "ABORT: Lock file is not writable ${lockfile}." _elog "ABORT: Lock file is not writable ${lockfile}."
ls -l "${lockfile}" _elog $( ls -l "${lockfile}" );
exit 1 exit 1
fi fi
_getFileSnapshot>"${snapShotStart}" _getFileSnapshot>"${snapShotStart}"
if [ $? -ne 0 ]; then if [ $? -ne 0 ]; then
_log "ABORT: Snapshot file is not writable ${snapShotStart}." _elog "ABORT: Snapshot file is not writable ${snapShotStart}."
ls -l "${snapShotStart}" _elog $( ls -l "${snapShotStart}" )
exit 1 exit 1
fi fi
while true; do while true; do
...@@ -141,19 +141,17 @@ function loopChecks(){ ...@@ -141,19 +141,17 @@ function loopChecks(){
_log "" _log ""
_getFileSnapshot>$snapShotCurrent _getFileSnapshot>$snapShotCurrent
if [ $? -ne 0 ]; then if [ $? -ne 0 ]; then
_log "ABORT: Snapshot file is not writable ${snapShotCurrent}." _elog "ABORT: Snapshot file is not writable ${snapShotCurrent}."
ls -l "${snapShotCurrent}" _elog $( ls -l "${snapShotCurrent}" )
exit 1 exit 1
fi fi
diff $snapShotStart $snapShotCurrent >/dev/null diff $snapShotStart $snapShotCurrent >/dev/null
if [ $? -ne 0 ]; then if [ $? -ne 0 ]; then
_log "ABORT: Files were updated / overwritten. The loop must be restarted.\n`diff $snapShotStart $snapShotCurrent`" _elog "ABORT: Files were updated / overwritten. The loop must be restarted.\n`diff $snapShotStart $snapShotCurrent`"
exit 1 exit 1
fi fi
icingaHostMustExist icingaHostMustExist
processAllChecks processAllChecks
echo
echo
done done
} }
# ...................................................................... # ......................................................................
...@@ -175,16 +173,13 @@ function processAllChecks(){ ...@@ -175,16 +173,13 @@ function processAllChecks(){
_log "" _log ""
_log "------ looping over all checks" _log "------ looping over all checks"
getChecks getChecks
echo
for myconfig in $(getChecks) for myconfig in $(getChecks)
do do
iCounter=$iCounter+1 iCounter=$iCounter+1
_log "--- processing [$iCounter of $iChecksTotal] $myconfig" _log "--- processing [$iCounter of $iChecksTotal] $myconfig"
processCheck "$myconfig" processCheck "$myconfig"
_log "" _log ""
echo
echo ----------------------------------------------------------------------
echo
done done
typeset -i local iLoopEnd typeset -i local iLoopEnd
iLoopEnd=$(_getUnixTs) iLoopEnd=$(_getUnixTs)
...@@ -206,7 +201,7 @@ function _parseCheckConfig(){ ...@@ -206,7 +201,7 @@ function _parseCheckConfig(){
local _myconfig="$1" local _myconfig="$1"
if [ ! -r "$_myconfig" ]; then if [ ! -r "$_myconfig" ]; then
echo "ERROR: config file is not readable [$_myconfig]" _elog "ERROR: config file is not readable [$_myconfig]"
exit 1 exit 1
fi fi
...@@ -248,6 +243,10 @@ function icingaHost(){ ...@@ -248,6 +243,10 @@ function icingaHost(){
http.setCacheTtl $_iRefreshCache http.setCacheTtl $_iRefreshCache
http.setCacheFile $_localCache http.setCacheFile $_localCache
http.makeRequest GET $_apiRequest http.makeRequest GET $_apiRequest
if http.isServerError >/dev/null; then
_elog "CRITICAL ERROR: Icinga2 API request failed with a server error GET $_apiRequest"
exit 1
fi
# set return code of GET action # set return code of GET action
http.isOk >/dev/null http.isOk >/dev/null
...@@ -268,10 +267,10 @@ function icingaHostMustExist(){ ...@@ -268,10 +267,10 @@ function icingaHostMustExist(){
if [ $? -ne 0 ]; then if [ $? -ne 0 ]; then
http.getResponse http.getResponse
if [ "$(http.getStatuscode)" = "000" ]; then if [ "$(http.getStatuscode)" = "000" ]; then
_log "ERROR: Unable to reach the Icinga node. Stopping script current monitoring actions." _elog "ERROR: Unable to reach the Icinga node. Stopping script current monitoring actions."
exit 1 exit 1
fi fi
_log "ERROR: host object for ${myHost} is not available on Icinga service (yet) - Status: $(http.getStatuscode)" _elog "ERROR: host object for ${myHost} is not available on Icinga service (yet) - Status: $(http.getStatuscode)"
echo echo
echo "ABORTING" echo "ABORTING"
echo echo
...@@ -348,8 +347,8 @@ function processCheck(){ ...@@ -348,8 +347,8 @@ function processCheck(){
eval $myFullscript $myparams > $_outfile eval $myFullscript $myparams > $_outfile
rc=$? rc=$?
if [ ! -w $_outfile ]; then if [ ! -w $_outfile ]; then
_log "${_logPrefix} ERROR: output file $_outfile is not writable." _elog "${_logPrefix} ERROR: output file $_outfile is not writable."
_log "${_logPrefix} $( ls -ld ${dir_data} $_outfile )" _elog "${_logPrefix} $( ls -ld ${dir_data} $_outfile )"
exit 1 exit 1
fi fi
typeset -i local iTsEnd=`date +%s` typeset -i local iTsEnd=`date +%s`
...@@ -395,8 +394,8 @@ function processCheck(){ ...@@ -395,8 +394,8 @@ function processCheck(){
_APIcall POST actions/process-check-result?service=${myHost}!${slot} "$data" _APIcall POST actions/process-check-result?service=${myHost}!${slot} "$data"
http.responseExport "$_response" http.responseExport "$_response"
if [ ! -w "$_response" ]; then if [ ! -w "$_response" ]; then
_log "${_logPrefix} ERROR: responsefile $_response is not writable." _elog "${_logPrefix} ERROR: responsefile $_response is not writable."
_log "${_logPrefix} $( ls -ld ${dir_data} $_response )" _elog "${_logPrefix} $( ls -ld ${dir_data} $_response )"
exit 1 exit 1
fi fi
......
...@@ -32,12 +32,19 @@ ...@@ -32,12 +32,19 @@
} }
# ...................................................................... # ......................................................................
# logging output. writes timestamp and the given message to STDOUT # logging output. writes timestamp and the given message to ${logfile}
# and ${logfile}
# params string(s) message to log # params string(s) message to log
# #
function _log(){ function _log(){
echo "`date` | $*" | tee -a ${logfile} echo "`date` | $*" >> ${logfile}
}
# echo and logging output
# params string(s) message to log
#
function _elog(){
echo "$*"
_log "$*"
} }
# ====================================================================== # ======================================================================
...@@ -232,6 +239,10 @@ function _getIpPrivate(){ ...@@ -232,6 +239,10 @@ function _getIpPrivate(){
function _APIcall(){ function _APIcall(){
http.makeRequest "$1" "$2" "$3" http.makeRequest "$1" "$2" "$3"
if http.isServerError >/dev/null; then
echo "CRITICAL ERROR: API request failed with a server error $1 $2"
exit 1
fi
http.getResponseHeader http.getResponseHeader
http.getResponse http.getResponse
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment