diff --git a/director-cli.sh b/director-cli.sh index 1a30e7cb1f685aea071bc4e28882ad58fb7452dc..a6311bb37508e1f0260ff3d539de47f03b71b5a0 100755 --- a/director-cli.sh +++ b/director-cli.sh @@ -13,12 +13,11 @@ # - use puppet facts # - UniBe network and group names - see hostCreate() # +# ---------------------------------------------------------------------- +# ah = axel.hahn@iml.unibe.ch +# 2022-02-16 v0.2 ah add --cfg param # ====================================================================== -. `dirname $0`/inc_getconfig.sh -. `dirname $0`/inc_functions.sh -. `dirname $0`/inc/rest-api-client.sh - tmpfile=/tmp/outcurl.tmp tmpfile2=/tmp/outcurl2.tmp @@ -733,6 +732,10 @@ Director actions Other parameters + --cfg CONFIGFILE + load a costom config file; default: ./inc_getconfig.sh + This must be the 1st parameter to be processed. + --debug enable debug output. @@ -761,24 +764,33 @@ echo echo "##### DIRECTOR HELPER $MY_NAME - $MY_IP" echo +if [ "$1" = "--cfg" ] && [ -n "$2" ]; then + echo "INFO: loading custom config [$2]..." + . "${2}" + shift 2 +else + . "$( dirname $0 )/inc_getconfig.sh" +fi +. `dirname $0`/inc_functions.sh +. `dirname $0`/inc/rest-api-client.sh + + +if [ $# -eq 0 ]; then + showHelp + exit 0 +fi + cd `dirname $0` ls ./`basename $0` >/dev/null || exit 1 _initVars - # ensure that ./inc_getconfig.sh was loaded if [ -z "${dir_cfg}" ]; then echo ERROR: Client is not installed/ configured yet on this machine. exit 1 fi -if [ $# -eq 0 ]; then - showHelp - exit 0 -fi - - while [ $# -gt 0 ]; do case "$1" in diff --git a/icinga-cli.sh b/icinga-cli.sh index ce521bad631acae048f6e9eb26ef01684ebf3588..7081f1216c8b9da16ef1891cdf5ac22c8fe8f970 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 +# 2022-02-16 v0.8 ah add --cfg param # ====================================================================== _product="ICINGA PASSIVE CLIENT" -_version="0.6" +_version="0.8" _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 # 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 ... @@ -433,13 +451,16 @@ A new local check will be added to Icinga while running it the first time. GENERAL PARAMETERS + --cfg CONFIGFILE + load a costom config file; default: ./inc_getconfig.sh + This must be the 1st parameter to be processed. + --help or -h or -? show this help and abort. --version or -v show the version abd abort - SERVICE ACTIONS --list @@ -520,6 +541,20 @@ ________________________________________________________________________________ EOBANNER +if [ "$1" = "--cfg" ] && [ -n "$2" ]; then + echo "INFO: loading custom config [$2]..." + . "${2}" + shift 2 +else + . "$( dirname $0 )/inc_getconfig.sh" +fi +. "$( dirname $0 )/inc_functions.sh" +. "$( dirname $0 )/inc/rest-api-client.sh" + +if [ $# -eq 0 ]; then + showHelp + exit 0 +fi if [ -z "${dir_cfg}" ]; then echo ERROR: $_product is not installed/ configured yet on this machine. @@ -530,10 +565,7 @@ icingaHostMustExist touch ${logfile} -if [ $# -eq 0 ]; then - showHelp - exit 0 -fi + while [ $# -gt 0 ]; do