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

apply changes suggested by shellcheck

parent 4dfce302
No related branches found
No related tags found
2 merge requests!2Add param for cfgfile,!1WIP: apply changes suggested by shellcheck
...@@ -9,32 +9,34 @@ ...@@ -9,32 +9,34 @@
# - curl # - curl
# - jq # - jq
# ---------------------------------------------------------------------- # ----------------------------------------------------------------------
# # ah = axel.hahn@iml.unibe.ch
# 2021-03-.. init
# 2022-01-11 v0.7 ah shellcheck
# ====================================================================== # ======================================================================
_product="ICINGA PASSIVE CLIENT" _product="ICINGA PASSIVE CLIENT"
_version="0.6" _version="0.7"
_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'
typeset -i debug=0 typeset -i debug=0
# source config ... # source config ...
. `dirname $0`/inc_getconfig.sh . "$( dirname $0 )/inc_getconfig.sh"
. `dirname $0`/inc_functions.sh . "$( dirname $0 )/inc_functions.sh"
. `dirname $0`/inc/rest-api-client.sh . "$( dirname $0 )/inc/rest-api-client.sh"
# where to find check scripts ... first directory wins # where to find check scripts ... first directory wins
# dir_plugins="/opt/imlmonitor/client/plugins/ /usr/lib64/nagios/plugins" # dir_plugins="/opt/imlmonitor/client/plugins/ /usr/lib64/nagios/plugins"
# dir_cfg="/etc/icinga2-passive-client" # dir_cfg="/etc/icinga2-passive-client"
# dir_data="/var/tmp/icinga2-passive-client" # dir_data="/var/tmp/icinga2-passive-client"
# dir_logs="/var/log/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 ch="$( dirname $0 )/inc/confighandler.sh"
myHost=`hostname -f` myHost=$(hostname -f)
# for loop mode only: max. random sleep time # for loop mode only: max. random sleep time
typeset -i sleeptime=30 typeset -i sleeptime=30
...@@ -68,8 +70,8 @@ function findCheckScript(){ ...@@ -68,8 +70,8 @@ function findCheckScript(){
local _script=$1 local _script=$1
for mydir in ${dir_plugins} for mydir in ${dir_plugins}
do do
if [ -x ${mydir}/${_script} ]; then if [ -x "${mydir}/${_script}" ]; then
echo ${mydir}/${_script} echo "${mydir}/${_script}"
fi fi
done | head -1 done | head -1
} }
...@@ -77,7 +79,7 @@ function findCheckScript(){ ...@@ -77,7 +79,7 @@ function findCheckScript(){
# helper used function in loopChecks # helper used function in loopChecks
# get a snapshot of a few files # get a snapshot of a few files
function _getFileSnapshot(){ function _getFileSnapshot(){
ls -l `dirname $0`/* ${dir_cfg}/* ls -l $(dirname $0)/* ${dir_cfg}/*
} }
...@@ -96,30 +98,34 @@ function loopChecks(){ ...@@ -96,30 +98,34 @@ function loopChecks(){
# exit 1 # exit 1
# fi # fi
local lockfile="${dir_data}/loop.pid" local lockfile
local snapShotStart=${dir_data}/`basename $0`-start.fingerprint lockfile="${dir_data}/loop.pid"
local snapShotCurrent=${dir_data}/`basename $0`-last.fingerprint local snapShotStart
snapShotStart=${dir_data}/$(basename $0)-start.fingerprint
local snapShotCurrent
snapShotCurrent=${dir_data}/$(basename $0)-last.fingerprint
if [ -f "${lockfile}" ]; then if [ -f "${lockfile}" ]; then
local lockpid=`cat "${lockfile}" | cut -f 2 -d "-" | cut -f 4 -d " " | grep "[0-9]"` local lockpid
ps -f --pid $lockpid | grep "`basename $0`" | grep loop >/dev/null 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 if [ $? -eq 0 ]; then
_log "ABORT: Loop seems to run already. See process with PID $lockpid" _log "ABORT: Loop seems to run already. See process with PID $lockpid"
echo echo
ps -f --pid $lockpid ps -f --pid "$lockpid"
echo echo
exit 0 exit 0
fi fi
fi fi
_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}." _log "ABORT: Lock file is not writable ${lockfile}."
ls -l "${lockfile}" 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}." _log "ABORT: Snapshot file is not writable ${snapShotStart}."
ls -l "${snapShotStart}" ls -l "${snapShotStart}"
...@@ -157,28 +163,33 @@ function loopChecks(){ ...@@ -157,28 +163,33 @@ function loopChecks(){
# #
function processAllChecks(){ function processAllChecks(){
# loop over all defined checks # loop over all defined checks
typeset -i local iChecksTotal=`getChecks | wc -l` typeset -i local iChecksTotal
typeset -i local iCounter=0 iChecksTotal=$(getChecks | wc -l)
typeset -i local iCounter
iCounter=0
_rc_all=0 _rc_all=0
typeset -i local iLoopStart=`_getUnixTs` typeset -i local iLoopStart
iLoopStart=$(_getUnixTs)
_log "" _log ""
_log "------ looping over all checks" _log "------ looping over all checks"
getChecks getChecks
echo 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 ---------------------------------------------------------------------- echo ----------------------------------------------------------------------
echo echo
done done
typeset -i local iLoopEnd=`_getUnixTs` typeset -i local iLoopEnd
typeset -i local iLoopTime=$iLoopEnd-$iLoopStart iLoopEnd=$(_getUnixTs)
typeset -i local iLoopTime
iLoopTime=$iLoopEnd-$iLoopStart
_log "------ loop done - needed $iLoopTime sec - rc=$_rc_all" _log "------ loop done - needed $iLoopTime sec - rc=$_rc_all"
} }
...@@ -192,9 +203,9 @@ function processAllChecks(){ ...@@ -192,9 +203,9 @@ function processAllChecks(){
# param string full path of a config file # param string full path of a config file
# #
function _parseCheckConfig(){ function _parseCheckConfig(){
local _myconfig=$1 local _myconfig="$1"
if [ ! -r $_myconfig ]; then if [ ! -r "$_myconfig" ]; then
echo "ERROR: config file is not readable [$_myconfig]" echo "ERROR: config file is not readable [$_myconfig]"
exit 1 exit 1
fi fi
...@@ -204,13 +215,14 @@ function _parseCheckConfig(){ ...@@ -204,13 +215,14 @@ function _parseCheckConfig(){
# command=check_cronstatus -param1 -param2 # command=check_cronstatus -param1 -param2
# interval=60 # interval=60
checkName=`cat $_myconfig | grep ^checkname= | cut -f 2 -d "="` checkName=$(cat $_myconfig | grep ^checkname= | cut -f 2 -d "=")
checkCommand=`cat $_myconfig | grep ^command= | cut -f 2 -d "="` checkCommand=$(cat $_myconfig | grep ^command= | cut -f 2 -d "=")
checkInterval=`cat $_myconfig | grep ^interval= | cut -f 2 -d "="` checkInterval=$(cat $_myconfig | grep ^interval= | cut -f 2 -d "=")
} }
# actions for icinga host
# param string action; "get" only
function icingaHost(){ function icingaHost(){
local _logPrefix="${myHost} :: API |" local _logPrefix="${myHost} :: API |"
local _apiRequest=objects/hosts/${myHost} local _apiRequest=objects/hosts/${myHost}
...@@ -246,16 +258,20 @@ function icingaHost(){ ...@@ -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(){ function icingaHostMustExist(){
_log "check if the host [${myHost}] exists on Icinga ..." _log "check if the host [${myHost}] exists on Icinga ..."
icingaHost get icingaHost get
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." _log "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`" _log "ERROR: host object for ${myHost} is not available on Icinga service (yet) - Status: $(http.getStatuscode)"
echo echo
echo "ABORTING" echo "ABORTING"
echo echo
...@@ -263,7 +279,7 @@ function icingaHostMustExist(){ ...@@ -263,7 +279,7 @@ function icingaHostMustExist(){
echo "- you must create the host on director (check director-cli.sh --hr)" echo "- you must create the host on director (check director-cli.sh --hr)"
echo "- the director must deploy the host to icinga daemon" echo "- the director must deploy the host to icinga daemon"
echo echo
rm -f ${dir_data}/service__check* 2>/dev/null rm -f "${dir_data}"/service__check* 2>/dev/null
exit 1 exit 1
fi fi
_log "OK, found." _log "OK, found."
...@@ -280,9 +296,10 @@ function processCheck(){ ...@@ -280,9 +296,10 @@ function processCheck(){
local _myconfig=$1 local _myconfig=$1
local _force=$2 local _force=$2
typeset -i local iCheckStart=`_getUnixTs` typeset -i local iCheckStart
iCheckStart=$(_getUnixTs)
_parseCheckConfig ${_myconfig} _parseCheckConfig "${_myconfig}"
local _logPrefix="${checkName} |" local _logPrefix="${checkName} |"
_log "${_logPrefix} INFO: every ${checkInterval} sec: ${checkCommand}" _log "${_logPrefix} INFO: every ${checkInterval} sec: ${checkCommand}"
...@@ -295,12 +312,13 @@ function processCheck(){ ...@@ -295,12 +312,13 @@ function processCheck(){
# --- check last run ... if never or > $interval then execute # --- check last run ... if never or > $interval then execute
doRun=0 doRun=0
if [ ! -f $_outfile ]; then if [ ! -f "$_outfile" ]; then
_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=$(($(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" _log "${_logPrefix} INFO: last run was $iAgeLastRun sec ago ... vs Interval = $checkInterval ... sleeptime = $sleeptime"
iAgeLastRun=$iAgeLastRun+$sleeptime iAgeLastRun=$iAgeLastRun+$sleeptime
if [ $iAgeLastRun -gt $checkInterval ]; then if [ $iAgeLastRun -gt $checkInterval ]; then
...@@ -314,12 +332,12 @@ function processCheck(){ ...@@ -314,12 +332,12 @@ function processCheck(){
fi fi
if [ $doRun -ne 0 ]; then if [ $doRun -ne 0 ]; then
myscript=`echo $checkCommand | cut -f 1 -d " "` myscript=$(echo "$checkCommand" | cut -f 1 -d " ")
myFullscript=`findCheckScript $myscript` myFullscript=$(findCheckScript "$myscript")
if [ -z "$myFullscript" ]; then if [ -z "$myFullscript" ]; then
_log "${_logPrefix} ERROR: $myscript was not found in any plugin dir" _log "${_logPrefix} ERROR: $myscript was not found in any plugin dir"
else else
myparams=`echo $checkCommand | grep " " | cut -f 2- -d " "` myparams=$( echo $checkCommand | grep " " | cut -f 2- -d " " )
# #
# --- this executes the check plugin ... # --- this executes the check plugin ...
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment