From a279fee8badb67ebe1d4cf242166a85a51b15e37 Mon Sep 17 00:00:00 2001
From: "Hahn Axel (hahn)" <axel.hahn@iml.unibe.ch>
Date: Tue, 11 Jan 2022 17:56:00 +0100
Subject: [PATCH] apply changes suggested by  shellcheck

---
 icinga-cli.sh | 102 +++++++++++++++++++++++++++++---------------------
 1 file changed, 60 insertions(+), 42 deletions(-)

diff --git a/icinga-cli.sh b/icinga-cli.sh
index ce521ba..3e83583 100755
--- a/icinga-cli.sh
+++ b/icinga-cli.sh
@@ -9,32 +9,34 @@
 # - curl
 # - jq
 # ----------------------------------------------------------------------
-#
+# ah = axel.hahn@iml.unibe.ch
+# 2021-03-..  init
+# 2022-01-11  v0.7  ah  shellcheck
 # ======================================================================
 
 
 _product="ICINGA PASSIVE CLIENT"
-_version="0.6"
+_version="0.7"
 _license="GNU GPL 3.0"
 _copyright='(c) 2020 Institute for Medical Education * University of Bern'
 
 typeset -i debug=0
 
 # source config ...
-. `dirname $0`/inc_getconfig.sh
+. "$( dirname $0 )/inc_getconfig.sh"
 
-. `dirname $0`/inc_functions.sh
-. `dirname $0`/inc/rest-api-client.sh
+. "$( dirname $0 )/inc_functions.sh"
+. "$( dirname $0 )/inc/rest-api-client.sh"
 
 # where to find check scripts ... first directory wins
 # dir_plugins="/opt/imlmonitor/client/plugins/ /usr/lib64/nagios/plugins"
 # dir_cfg="/etc/icinga2-passive-client"
 # dir_data="/var/tmp/icinga2-passive-client"
 # dir_logs="/var/log/icinga2-passive-client"
-logfile=${dir_logs}/execution.log
+logfile="${dir_logs}/execution.log"
 
-ch=`dirname $0`/inc/confighandler.sh
-myHost=`hostname -f`
+ch="$( dirname $0 )/inc/confighandler.sh"
+myHost=$(hostname -f)
 
 # for loop mode only: max. random sleep time
 typeset -i sleeptime=30
@@ -68,8 +70,8 @@ function findCheckScript(){
 	local _script=$1
 	for mydir in ${dir_plugins}
 	do
-		if [ -x ${mydir}/${_script} ]; then
-			echo ${mydir}/${_script}
+		if [ -x "${mydir}/${_script}" ]; then
+			echo "${mydir}/${_script}"
 		fi
 	done | head -1
 }
@@ -77,7 +79,7 @@ function findCheckScript(){
 # helper used function in loopChecks
 # get a snapshot of a few files
 function _getFileSnapshot(){
-  ls -l `dirname $0`/* ${dir_cfg}/*
+  ls -l $(dirname $0)/* ${dir_cfg}/*
 }
 
 
@@ -96,30 +98,34 @@ function loopChecks(){
       #   exit 1
       # fi
 
-      local lockfile="${dir_data}/loop.pid"
-      local snapShotStart=${dir_data}/`basename $0`-start.fingerprint
-      local snapShotCurrent=${dir_data}/`basename $0`-last.fingerprint
+      local lockfile
+      lockfile="${dir_data}/loop.pid"
+      local snapShotStart
+      snapShotStart=${dir_data}/$(basename $0)-start.fingerprint
+      local snapShotCurrent
+      snapShotCurrent=${dir_data}/$(basename $0)-last.fingerprint
       if [ -f "${lockfile}" ]; then
-        local 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
+        local lockpid
+        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
+          ps -f --pid "$lockpid"
           echo
           exit 0
         fi
       fi
 
       _log "---------- starting in a permanent loop"
-      echo "Serviceloop started `date` - process id $$" > "${lockfile}"
+      echo "Serviceloop started $(date) - process id $$" > "${lockfile}"
       if [ $? -ne 0 ]; then
           _log "ABORT: Lock file is not writable ${lockfile}."
           ls -l "${lockfile}"
           exit 1
       fi
 
-      _getFileSnapshot>${snapShotStart}
+      _getFileSnapshot>"${snapShotStart}"
       if [ $? -ne 0 ]; then
           _log "ABORT: Snapshot file is not writable ${snapShotStart}."
           ls -l "${snapShotStart}"
@@ -157,28 +163,33 @@ function loopChecks(){
 #
 function processAllChecks(){
   # loop over all defined checks
-  typeset -i local iChecksTotal=`getChecks | wc -l`
-  typeset -i local iCounter=0
+  typeset -i local iChecksTotal
+  iChecksTotal=$(getChecks | wc -l)
+  typeset -i local iCounter
+  iCounter=0
 
   _rc_all=0
-  typeset -i local iLoopStart=`_getUnixTs`
+  typeset -i local iLoopStart
+  iLoopStart=$(_getUnixTs)
 
   _log ""
   _log "------ looping over all checks"
   getChecks
   echo
-  for myconfig in `getChecks`
+  for myconfig in $(getChecks)
   do
     iCounter=$iCounter+1
     _log "--- processing [$iCounter of $iChecksTotal] $myconfig"
-    processCheck $myconfig
+    processCheck "$myconfig"
     _log ""
     echo
     echo ----------------------------------------------------------------------
     echo
   done
-  typeset -i local iLoopEnd=`_getUnixTs`
-  typeset -i local iLoopTime=$iLoopEnd-$iLoopStart
+  typeset -i local iLoopEnd
+  iLoopEnd=$(_getUnixTs)
+  typeset -i local iLoopTime
+  iLoopTime=$iLoopEnd-$iLoopStart
 
   _log "------ loop done - needed $iLoopTime sec - rc=$_rc_all"
 }
@@ -192,9 +203,9 @@ function processAllChecks(){
 # param  string  full path of a config file
 #
 function _parseCheckConfig(){
-  local _myconfig=$1
+  local _myconfig="$1"
 
-  if [ ! -r $_myconfig ]; then
+  if [ ! -r "$_myconfig" ]; then
     echo "ERROR: config file is not readable [$_myconfig]"
     exit 1
   fi
@@ -204,13 +215,14 @@ function _parseCheckConfig(){
   # 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 "="`
+  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
+# param  string  action; "get" only
 function icingaHost(){
   local _logPrefix="${myHost} :: API |"
   local _apiRequest=objects/hosts/${myHost}
@@ -246,16 +258,20 @@ function icingaHost(){
 
 }
 
+# for check on the beginning of the script:
+# execute a check only if the host exists on icinga2
+# global  string  myHost 
+# global  string  dir_data
 function icingaHostMustExist(){
   _log "check if the host [${myHost}] exists on Icinga ..."
   icingaHost get
   if [ $? -ne 0 ]; then
     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."
       exit 1
     fi
-    _log "ERROR: host object for ${myHost} is not available on Icinga service (yet) - Status: `http.getStatuscode`"
+    _log "ERROR: host object for ${myHost} is not available on Icinga service (yet) - Status: $(http.getStatuscode)"
     echo
     echo "ABORTING"
     echo
@@ -263,7 +279,7 @@ function icingaHostMustExist(){
     echo "- you must create the host on director (check director-cli.sh --hr)"
     echo "- the director must deploy the host to icinga daemon"
     echo
-    rm -f ${dir_data}/service__check* 2>/dev/null
+    rm -f "${dir_data}"/service__check* 2>/dev/null
     exit 1
   fi
   _log "OK, found."
@@ -280,9 +296,10 @@ function processCheck(){
   local _myconfig=$1
   local _force=$2
 
-  typeset -i local iCheckStart=`_getUnixTs`
+  typeset -i local iCheckStart
+  iCheckStart=$(_getUnixTs)
 
-  _parseCheckConfig ${_myconfig}
+  _parseCheckConfig "${_myconfig}"
   local _logPrefix="${checkName} |"
   _log "${_logPrefix} INFO: every ${checkInterval} sec: ${checkCommand}"
 
@@ -295,12 +312,13 @@ function processCheck(){
 
   # --- check last run ... if never or > $interval then execute
   doRun=0
-  if [ ! -f $_outfile ]; then
+  if [ ! -f "$_outfile" ]; then
     _log "${_logPrefix} INFO: Never executed before"
     doRun=1
   else
     # typeset -i iAgeLastRun=$(($(date +%s) - $(date +%s -r "$_outfile")))
-    typeset -i iAgeLastRun=`_getFileAge "$_outfile"`
+    typeset -i iAgeLastRun
+    iAgeLastRun=$(_getFileAge "$_outfile")
     _log "${_logPrefix} INFO: last run was $iAgeLastRun sec ago ... vs Interval = $checkInterval ... sleeptime = $sleeptime"
     iAgeLastRun=$iAgeLastRun+$sleeptime
     if [ $iAgeLastRun -gt $checkInterval ]; then
@@ -314,12 +332,12 @@ function processCheck(){
   fi
 
   if [ $doRun -ne 0 ]; then
-    myscript=`echo $checkCommand | cut -f 1 -d " "`
-    myFullscript=`findCheckScript $myscript`
+    myscript=$(echo "$checkCommand" | cut -f 1 -d " ")
+    myFullscript=$(findCheckScript "$myscript")
     if [ -z "$myFullscript" ]; then
       _log "${_logPrefix} ERROR: $myscript was not found in any plugin dir"
     else
-      myparams=`echo $checkCommand | grep " " | cut -f 2- -d " "`
+      myparams=$( echo $checkCommand | grep " " | cut -f 2- -d " " )
 
       #
       # --- this executes the check plugin ...
-- 
GitLab