diff --git a/icinga-cli.sh b/icinga-cli.sh index 74c36a21f1bdcb72793067b4abe9444a67c9a638..eb9e28fe63837f2b52d79b6d1ce7900762a56830 100755 --- a/icinga-cli.sh +++ b/icinga-cli.sh @@ -11,9 +11,10 @@ # ---------------------------------------------------------------------- # ah = axel.hahn@iml.unibe.ch # 2021-03-.. init -# 2022-01-11 v0.7 ah shellcheck -# 2022-02-16 v0.8 ah add --cfg param -# 2022-03-04 v0.9 ah abort on http 5xx error +# 2022-01-11 v0.7 ah shellcheck +# 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 # ====================================================================== @@ -110,10 +111,8 @@ function loopChecks(){ 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 if [ $? -eq 0 ]; then - _log "ABORT: Loop seems to run already. See process with PID $lockpid" - echo - ps -f --pid "$lockpid" - echo + _elog "ABORT: Loop seems to run already. See process with PID $lockpid" + _elog $( ps -f --pid "$lockpid" ) exit 0 fi fi @@ -121,15 +120,15 @@ function loopChecks(){ _log "---------- starting in a permanent loop" echo "Serviceloop started $(date) - process id $$" > "${lockfile}" if [ $? -ne 0 ]; then - _log "ABORT: Lock file is not writable ${lockfile}." - ls -l "${lockfile}" + _elog "ABORT: Lock file is not writable ${lockfile}." + _elog $( ls -l "${lockfile}" ); exit 1 fi _getFileSnapshot>"${snapShotStart}" if [ $? -ne 0 ]; then - _log "ABORT: Snapshot file is not writable ${snapShotStart}." - ls -l "${snapShotStart}" + _elog "ABORT: Snapshot file is not writable ${snapShotStart}." + _elog $( ls -l "${snapShotStart}" ) exit 1 fi while true; do @@ -142,19 +141,17 @@ function loopChecks(){ _log "" _getFileSnapshot>$snapShotCurrent if [ $? -ne 0 ]; then - _log "ABORT: Snapshot file is not writable ${snapShotCurrent}." - ls -l "${snapShotCurrent}" + _elog "ABORT: Snapshot file is not writable ${snapShotCurrent}." + _elog $( ls -l "${snapShotCurrent}" ) exit 1 fi diff $snapShotStart $snapShotCurrent >/dev/null 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 fi icingaHostMustExist processAllChecks - echo - echo done } # ...................................................................... @@ -176,16 +173,13 @@ function processAllChecks(){ _log "" _log "------ looping over all checks" getChecks - echo for myconfig in $(getChecks) do iCounter=$iCounter+1 _log "--- processing [$iCounter of $iChecksTotal] $myconfig" processCheck "$myconfig" _log "" - echo - echo ---------------------------------------------------------------------- - echo + done typeset -i local iLoopEnd iLoopEnd=$(_getUnixTs) @@ -207,7 +201,7 @@ function _parseCheckConfig(){ local _myconfig="$1" if [ ! -r "$_myconfig" ]; then - echo "ERROR: config file is not readable [$_myconfig]" + _elog "ERROR: config file is not readable [$_myconfig]" exit 1 fi @@ -250,7 +244,7 @@ function icingaHost(){ http.setCacheFile $_localCache http.makeRequest GET $_apiRequest if http.isServerError >/dev/null; then - echo "CRITICAL ERROR: Icinga2 API request failed with a server error GET $_apiRequest" + _elog "CRITICAL ERROR: Icinga2 API request failed with a server error GET $_apiRequest" exit 1 fi @@ -273,10 +267,10 @@ function icingaHostMustExist(){ if [ $? -ne 0 ]; then http.getResponse 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 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 "ABORTING" echo @@ -353,8 +347,8 @@ function processCheck(){ eval $myFullscript $myparams > $_outfile rc=$? if [ ! -w $_outfile ]; then - _log "${_logPrefix} ERROR: output file $_outfile is not writable." - _log "${_logPrefix} $( ls -ld ${dir_data} $_outfile )" + _elog "${_logPrefix} ERROR: output file $_outfile is not writable." + _elog "${_logPrefix} $( ls -ld ${dir_data} $_outfile )" exit 1 fi typeset -i local iTsEnd=`date +%s` @@ -400,8 +394,8 @@ function processCheck(){ _APIcall POST actions/process-check-result?service=${myHost}!${slot} "$data" http.responseExport "$_response" if [ ! -w "$_response" ]; then - _log "${_logPrefix} ERROR: responsefile $_response is not writable." - _log "${_logPrefix} $( ls -ld ${dir_data} $_response )" + _elog "${_logPrefix} ERROR: responsefile $_response is not writable." + _elog "${_logPrefix} $( ls -ld ${dir_data} $_response )" exit 1 fi diff --git a/inc_functions.sh b/inc_functions.sh index 2cb5268ff2ccf71a83888774a777074d21562cf5..2f5609fc8e4d08328a0858f51197f1551dead2e2 100644 --- a/inc_functions.sh +++ b/inc_functions.sh @@ -32,12 +32,19 @@ } # ...................................................................... - # logging output. writes timestamp and the given message to STDOUT - # and ${logfile} + # logging output. writes timestamp and the given message to ${logfile} # params string(s) message to 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 "$*" } # ======================================================================