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

fix line breaks of sent output, shell check fixes

parent 468c0a73
No related branches found
No related tags found
1 merge request!31replace confighandler; fix new line of icingaclient response
...@@ -21,12 +21,12 @@ ...@@ -21,12 +21,12 @@
# 2023-02-17 v0.14 ah add hostnamme into CFGSTORAGE # 2023-02-17 v0.14 ah add hostnamme into CFGSTORAGE
# 2023-02-17 v0.15 ah check inc_getconfig.sh exists and hostname -f has a value # 2023-02-17 v0.15 ah check inc_getconfig.sh exists and hostname -f has a value
# 2023-11-01 v0.16 ah generate json with "jo" # 2023-11-01 v0.16 ah generate json with "jo"
# 2023-11-02 v0.17 ah generate json with "jo" # 2023-11-02 v0.17 ah fix line breaks of sent output, shell check fixes
# ====================================================================== # ======================================================================
_product="ICINGA PASSIVE CLIENT" _product="ICINGA PASSIVE CLIENT"
_version="0.16" _version="0.17"
_license="GNU GPL 3.0" _license="GNU GPL 3.0"
_copyright='(c) Institute for Medical Education * University of Bern' _copyright='(c) Institute for Medical Education * University of Bern'
...@@ -103,14 +103,6 @@ function _getFileSnapshot(){ ...@@ -103,14 +103,6 @@ function _getFileSnapshot(){
# #
function loopChecks(){ function loopChecks(){
# TODO-MEMORY-CHECK
# echo ${MY_NAME} | egrep "^(kvm4|icinga)"
# echo ${MY_NAME} | egrep "^(monitortest)"
# if [ $? -ne 0 ]; then
# echo "HARD EXIT - DO NOT EXECUTE ANY CHECK ON $MY_NAME"
# exit 1
# fi
local lockfile local lockfile
lockfile="${dir_data}/loop.pid" lockfile="${dir_data}/loop.pid"
local snapShotStart local snapShotStart
...@@ -129,6 +121,8 @@ function loopChecks(){ ...@@ -129,6 +121,8 @@ function loopChecks(){
fi fi
_log "---------- starting in a permanent loop" _log "---------- starting in a permanent loop"
icingaHostMustExist
echo "Serviceloop started $(date) - process id $$" > "${lockfile}" echo "Serviceloop started $(date) - process id $$" > "${lockfile}"
if [ $? -ne 0 ]; then if [ $? -ne 0 ]; then
_elog "ABORT: Lock file is not writable ${lockfile}." _elog "ABORT: Lock file is not writable ${lockfile}."
...@@ -145,23 +139,21 @@ function loopChecks(){ ...@@ -145,23 +139,21 @@ function loopChecks(){
while true; do while true; do
# typeset -i local iSleep=$(($RANDOM%$sleeptime)) # typeset -i local iSleep=$(($RANDOM%$sleeptime))
# sleep minimum is half of $sleeptime # sleep minimum is half of $sleeptime
typeset -i local iSleep=$(($RANDOM%$sleeptime/2+$sleeptime/2)) local iSleep
typeset -i iSleep=$(($RANDOM%$sleeptime/2+$sleeptime/2))
_log "sleeping $iSleep sec ..." _log "sleeping $iSleep sec ..."
sleep $iSleep sleep $iSleep
_log "______________________________________________________________________" _log "______________________________________________________________________"
_log "" _log ""
_getFileSnapshot>$snapShotCurrent if ! _getFileSnapshot > "$snapShotCurrent"; then
if [ $? -ne 0 ]; then
_elog "ABORT: Snapshot file is not writable ${snapShotCurrent}." _elog "ABORT: Snapshot file is not writable ${snapShotCurrent}."
_elog $( ls -l "${snapShotCurrent}" ) _elog $( ls -l "${snapShotCurrent}" )
exit 1 exit 1
fi fi
diff $snapShotStart $snapShotCurrent >/dev/null if ! diff "$snapShotStart" "$snapShotCurrent" >/dev/null; then
if [ $? -ne 0 ]; then _elog "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
processAllChecks processAllChecks
done done
} }
...@@ -172,13 +164,15 @@ function loopChecks(){ ...@@ -172,13 +164,15 @@ function loopChecks(){
# #
function processAllChecks(){ function processAllChecks(){
# loop over all defined checks # loop over all defined checks
typeset -i local iChecksTotal local iChecksTotal; typeset -i iChecksTotal
local iCounter; typeset -i iCounter
local iLoopStart; typeset -i iLoopStart
local iLoopEnd; typeset -i iLoopEnd
local iLoopTime; typeset -i iLoopTime
iChecksTotal=$(getChecks | wc -l) iChecksTotal=$(getChecks | wc -l)
typeset -i local iCounter
iCounter=0 iCounter=0
_rc_all=0 _rc_all=0
typeset -i local iLoopStart
iLoopStart=$(_getUnixTs) iLoopStart=$(_getUnixTs)
_log "" _log ""
...@@ -191,48 +185,19 @@ function processAllChecks(){ ...@@ -191,48 +185,19 @@ function processAllChecks(){
_log "" _log ""
done done
typeset -i local iLoopEnd
iLoopEnd=$(_getUnixTs) iLoopEnd=$(_getUnixTs)
typeset -i local iLoopTime
iLoopTime=$iLoopEnd-$iLoopStart iLoopTime=$iLoopEnd-$iLoopStart
_log "------ loop done - needed $iLoopTime sec - rc=$_rc_all" _log "------ loop done - needed $iLoopTime sec - rc=$_rc_all"
} }
# ......................................................................
#
# parse a config file and set global vars:
# checkName
# checkCommand
# checkInterval
# param string full path of a config file
#
function UNUSED_parseCheckConfig(){
local _myconfig="$1"
if [ ! -r "$_myconfig" ]; then
_elog "ERROR: config file is not readable [$_myconfig]"
exit 1
fi
# EXAMPLE a config contains ...
# checkname=check_cronstatus
# command=check_cronstatus -param1 -param2
# interval=60
checkName=$(cat $_myconfig | grep ^checkname= | cut -f 2 -d "=")
checkCommand=$(cat $_myconfig | grep ^command= | cut -f 2 -d "=")
checkInterval=$(cat $_myconfig | grep ^interval= | cut -f 2 -d "=")
}
# actions for icinga host # actions for icinga host
# param string action; "get" only # param string action; "get" only
function icingaHost(){ function icingaHost(){
local _logPrefix="${MY_NAME} :: API |" local _logPrefix="${MY_NAME} :: API |"
local _apiRequest=objects/hosts/${MY_NAME} local _apiRequest=objects/hosts/${MY_NAME}
local _localCache=${dir_data}/host_${MY_NAME}_deployed-at-icinga.txt local _localCache=${dir_data}/host_${MY_NAME}_deployed-at-icinga.txt
typeset -i local _iRefreshCache=120 local _iRefreshCache; typeset -i _iRefreshCache=120
local sAction=$1 local sAction=$1
...@@ -305,20 +270,23 @@ function processCheck(){ ...@@ -305,20 +270,23 @@ function processCheck(){
local _myconfig=$1 local _myconfig=$1
local _force=$2 local _force=$2
typeset -i local iPipes local iPipes; typeset -i iPipes
typeset -i local iCheckStart local iCheckStart; typeset -i iCheckStart
iCheckStart=$(_getUnixTs)
_parseCheckConfig "${_myconfig}"
local _logPrefix="${checkName} |"
_log "${_logPrefix} INFO: every ${checkInterval} sec: ${checkCommand}"
local _outfile=${dir_data}/service__check__${checkName}__output.txt local _outfile=${dir_data}/service__check__${checkName}__output.txt
local _output local _output
local _FoundErrors local _FoundErrors
local _response=${dir_data}/service__check__${checkName}__icinga_response.txt local _response=${dir_data}/service__check__${checkName}__icinga_response.txt
typeset -i local _rc=0 local _rc; typeset -i _rc=0
local iTsEnd; typeset -i iTsEnd
local iTsStart; typeset -i iTsStart
iCheckStart=$(_getUnixTs)
_parseCheckConfig "${_myconfig}"
local _logPrefix="${checkName} |"
_log "${_logPrefix} INFO: every ${checkInterval} sec: ${checkCommand}"
_initHttp _initHttp
...@@ -328,7 +296,6 @@ function processCheck(){ ...@@ -328,7 +296,6 @@ function processCheck(){
_log "${_logPrefix} INFO: Never executed before" _log "${_logPrefix} INFO: Never executed before"
doRun=1 doRun=1
else else
# typeset -i iAgeLastRun=$(($(date +%s) - $(date +%s -r "$_outfile")))
typeset -i iAgeLastRun typeset -i iAgeLastRun
iAgeLastRun=$(_getFileAge "$_outfile") iAgeLastRun=$(_getFileAge "$_outfile")
_log "${_logPrefix} INFO: last run was $iAgeLastRun sec ago ... vs Interval = $checkInterval ... sleeptime = $sleeptime" _log "${_logPrefix} INFO: last run was $iAgeLastRun sec ago ... vs Interval = $checkInterval ... sleeptime = $sleeptime"
...@@ -355,37 +322,35 @@ function processCheck(){ ...@@ -355,37 +322,35 @@ function processCheck(){
# --- this executes the check plugin ... # --- this executes the check plugin ...
# #
_log "${_logPrefix} starting $myFullscript $myparams" _log "${_logPrefix} starting $myFullscript $myparams"
typeset -i local iTsStart=`date +%s` iTsStart=$(date +%s)
# $myFullscript $myparams | tee $_outfile eval $myFullscript $myparams > "$_outfile"
eval $myFullscript $myparams > $_outfile
rc=$? rc=$?
if [ ! -w $_outfile ]; then if [ ! -w $_outfile ]; then
_elog "${_logPrefix} ERROR: output file $_outfile is not writable." _elog "${_logPrefix} ERROR: output file $_outfile is not writable."
_elog "${_logPrefix} $( ls -ld ${dir_data} $_outfile )" _elog "${_logPrefix} $( ls -ld ${dir_data} $_outfile )"
exit 1 exit 1
fi fi
test $rc -gt 0 && _FoundErrors+="Exitcode of check is $rc (greater zero).\n" test $rc -gt 0 && _FoundErrors+="Exitcode of check is greater zero ... "
typeset -i local iTsEnd=`date +%s` iTsEnd=$(date +%s)
# outPerfdata=`grep '|' $_outfile | cut -f 2 -d '|'` outPerfdata=$(grep '|' $_outfile | rev | cut -f 1 -d '|' | rev | grep "=")
outPerfdata=`grep '|' $_outfile | rev | cut -f 1 -d '|' | rev`
_output="$( cat "$_outfile" )" _output="$( cat "$_outfile" )"
_echo _echo
_echo -------- check output: _echo -------- check output:
_echo "$_output" _echo "$_output"
iPipes=$( grep -o "|" < "$_outfile" | wc -l ) iPipes=$( grep -o "|" < "$_outfile" | wc -l )
_echo "Pipe chars: $iPipes" # _echo "Pipe chars: $iPipes"
if [[ $iPipes -gt 1 ]]; then if [[ $iPipes -gt 1 ]]; then
_elog "WARNING - pipes were found in plugin output" _elog "WARNING - pipes were found in plugin output"
_elog "command was: $myFullscript $myparams" _elog "command was: $myFullscript $myparams"
_log "${_logPrefix} WARNING - pipes were found in plugin output" _log "${_logPrefix} WARNING - pipes were found in plugin output"
fi fi
# sed -i "s#|#:#g" "$_outfile"
_echo _echo
# echo -------- extracted performance data:
# echo $outPerfdata #
# echo # --- extract performance data:
#
if ! test -n "$outPerfdata" && echo "$outPerfdata" | grep "=[0-9\.]*;[0-9\.]*;[0-9\.]*;[0-9\.]*;" >/dev/null; then if ! test -n "$outPerfdata" && echo "$outPerfdata" | grep "=[0-9\.]*;[0-9\.]*;[0-9\.]*;[0-9\.]*;" >/dev/null; then
_elog "WARNING - this does not look like performance data: $outPerfdata" _elog "WARNING - this does not look like performance data: $outPerfdata"
_elog "command was: $myFullscript $myparams" _elog "command was: $myFullscript $myparams"
...@@ -395,29 +360,24 @@ function processCheck(){ ...@@ -395,29 +360,24 @@ function processCheck(){
_log "${_logPrefix} check command finished with returncode $rc" _log "${_logPrefix} check command finished with returncode $rc"
_rc=$_rc+$rc _rc=$_rc+$rc
# #
# --- send check result to Icinga # --- send check result to Icinga
# fields of the object # fields of the object
# https://icinga.com/docs/icinga2/latest/doc/12-icinga2-api/#process-check-result # https://icinga.com/docs/icinga2/latest/doc/12-icinga2-api/#process-check-result
#
data="$( jo \
commandAsJson="$(jq -nR --arg data """${myFullscript} $myparams""" '$data')" check_source=${MY_NAME} \
outAsJson="$( jq -nR --arg data """${_output}""" '$data')" check_command="""${myFullscript} $myparams""" \
exit_status=$rc \
local JSONPARAMS=" ttl=$checkInterval \
check_source=${MY_NAME} execution_start=$iTsStart \
check_command=${commandAsJson} execution_end=$iTsEnd \
exit_status=$rc performance_data="\"${outPerfdata}\"" \
ttl=$checkInterval plugin_output="""${_output}"""
execution_start=$iTsStart )"
execution_end=$iTsEnd
performance_data="\"${outPerfdata}\"" slot="$(_getName4Svcathost ${checkName} | sed 's# #%20#g')"
plugin_output=$outAsJson
"
data=$( eval jo -p -d. $JSONPARAMS )
slot="`_getName4Svcathost ${checkName} | sed 's# #%20#g'`"
_log "${_logPrefix} starting POST of data to monitoring server" _log "${_logPrefix} starting POST of data to monitoring server"
_echo POST actions/process-check-result?service=${MY_NAME}!${slot} "$data" _echo POST actions/process-check-result?service=${MY_NAME}!${slot} "$data"
...@@ -430,18 +390,20 @@ function processCheck(){ ...@@ -430,18 +390,20 @@ function processCheck(){
fi fi
# --- check if data were sent successfully # --- check if data were sent successfully
# fgrep "HTTP/1.1 200" ${_response} >/dev/null if http.isOk >/dev/null; then
# _testHttpOk ${_response} >/dev/null
http.isOk >/dev/null
if [ $? -eq 0 ]; then
_log "${_logPrefix} rc=$rc - OK, response was sent to Icinga" _log "${_logPrefix} rc=$rc - OK, response was sent to Icinga"
else else
_elog "${_logPrefix} rc=$rc - WARNING: $(http.getStatuscode) the check response was NOT sent to Icinga" local httpcode; httpcode=$(http.getStatuscode)
_rc=$_rc+1 _rc=$_rc+1
_FoundErrors+="Response was not sent to Icinga." case $httpcode in
404) _FoundErrors+="Icinga response $httpcode: host or service check was not created in Icinga yet ... " ;;
*) _FoundErrors+="Icinga response was not sent to Icinga: $httpcode ... " ;;
esac
_echo _echo
_echo For Debugging: _elog "${_logPrefix} rc=$rc - $_FoundErrors"
_echo "$data" # _echo
# _echo For Debugging:
# _echo "$data"
_log "$data" _log "$data"
fi fi
...@@ -454,11 +416,13 @@ function processCheck(){ ...@@ -454,11 +416,13 @@ function processCheck(){
# add current result to global returncode # add current result to global returncode
_rc_all=$_rc_all+$_rc _rc_all=$_rc_all+$_rc
typeset -i local iCheckEnd=`_getUnixTs` local iCheckEnd; typeset -i iCheckEnd
typeset -i local iCheckTime=$iCheckEnd-$iCheckStart local iCheckTime; typeset -i iCheckTime
iCheckEnd=$(_getUnixTs)
iCheckTime=$iCheckEnd-$iCheckStart
_log "${_logPrefix} finished after $iCheckTime sec with returncode $_rc" _log "${_logPrefix} finished after $iCheckTime sec with returncode $_rc"
test $_rc -eq 0 || (_echo; _echo "ERRORS: [${checkName}] $_FoundErrors"; _echo; _echo)
} }
# ---------------------------------------------------------------------- # ----------------------------------------------------------------------
# help # help
...@@ -466,7 +430,7 @@ function processCheck(){ ...@@ -466,7 +430,7 @@ function processCheck(){
# show help text # show help text
function showHelp(){ function showHelp(){
self=`basename $0` self=$(basename $0)
cat <<EOH cat <<EOH
...@@ -489,7 +453,7 @@ GENERAL PARAMETERS ...@@ -489,7 +453,7 @@ GENERAL PARAMETERS
show this help and abort. show this help and abort.
--version or -v --version or -v
show the version abd abort show the version abd abort. It must be the 1st parameter.
SERVICE ACTIONS SERVICE ACTIONS
...@@ -538,11 +502,7 @@ DEBUGGING ...@@ -538,11 +502,7 @@ DEBUGGING
EOH EOH
} }
function showVersion(){
echo "$_license"
echo "$_copyright"
echo
}
# ---------------------------------------------------------------------- # ----------------------------------------------------------------------
# #
# MAIN # MAIN
...@@ -551,6 +511,7 @@ function showVersion(){ ...@@ -551,6 +511,7 @@ function showVersion(){
. "$( dirname $0 )/inc_functions.sh" . "$( dirname $0 )/inc_functions.sh"
# show version without header
if [ "$1" = "--version" ] || [ "$1" = "-v" ]; then if [ "$1" = "--version" ] || [ "$1" = "-v" ]; then
echo "$( basename $0) v$_version" echo "$( basename $0) v$_version"
echo "$_license .. $_copyright" echo "$_license .. $_copyright"
...@@ -601,9 +562,6 @@ if [ -z "${dir_cfg}" ]; then ...@@ -601,9 +562,6 @@ if [ -z "${dir_cfg}" ]; then
exit 1 exit 1
fi fi
icingaHostMustExist
touch ${logfile} touch ${logfile}
...@@ -615,11 +573,6 @@ do ...@@ -615,11 +573,6 @@ do
showHelp showHelp
exit 0 exit 0
;; ;;
'--version' | '-v')
showVersion
exit 0
;;
'--list') '--list')
getChecks getChecks
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment