From 9f1b6c829e8976d4283e0d695aedb0b378318cbf Mon Sep 17 00:00:00 2001 From: "Hahn Axel (hahn)" <axel.hahn@iml.unibe.ch> Date: Wed, 31 Aug 2022 14:36:04 +0200 Subject: [PATCH] shellfix corrections --- inc_pluginfunctions | 63 ++++++++++++++++++++++++--------------------- 1 file changed, 34 insertions(+), 29 deletions(-) diff --git a/inc_pluginfunctions b/inc_pluginfunctions index ffdb366..f9f50fc 100644 --- a/inc_pluginfunctions +++ b/inc_pluginfunctions @@ -29,6 +29,7 @@ # 2019-10-29 added setExitcode # 2020-03-05 v1.2 <axel.hahn@iml.unibe.ch> switch to ph.* helper functions # 2020-09-01 v1.3 <axel.hahn@iml.unibe.ch> added ph.hasParamoption +# 2022-08-31 v1.4 <axel.hahn@iml.unibe.ch> shellfix corrections # ====================================================================== @@ -49,7 +50,7 @@ ph_cmd__params="$@" # abort a check and exit with status "unknown" function ph.abort(){ ph.setStatus "unknown" - echo $* + echo "$*" ph.exit } @@ -77,20 +78,20 @@ function ph.exit(){ # detect LINUX DISTRO as lowercase # returns one of centos|debian|ubuntu|... function ph.getOS(){ - distro= + local distro= if [ -z $distro ]; then # centos7, debian, manjaro, ubuntu - distro=`cat /etc/*-release | grep "^ID=" | cut -f 2 -d "="` + distro=$( grep "^ID=" /etc/*-release | cut -f 2 -d "=" ) fi if [ -z $distro ]; then # debian6,7, ubuntu 10,12 .. maybe unneeded. - distro=`cat /etc/issue | head -1 | grep "^[a-zA-Z]" | cut -f 1 -d " "` + distro=$( head -1 /etc/issue | grep "^[a-zA-Z]" | cut -f 1 -d " " ) fi # sanitize: lowercase, remove " - distro=`echo $distro | tr -d '"' | tr [:upper:] [:lower:]` + distro=$( echo $distro | tr -d '"' | tr [:upper:] [:lower:] ) if [ -z $distro ]; then ph.abort "UNKNOWN: distro was not detected." @@ -104,12 +105,12 @@ function ph.getOS(){ # returns an integer, i.e. 7 on CentOS7 function ph.getOSMajor(){ local _version= - _version=`cat /etc/*-release | egrep "^(VERSION_ID|DISTRIB_RELEASE)=" | head -1 | cut -f 2 -d "=" | sed 's#"##g' | cut -f 1 -d "."` + _version=$( grep -E "^(VERSION_ID|DISTRIB_RELEASE)=" /etc/*-release | head -1 | cut -f 2 -d "=" | sed 's#"##g' | cut -f 1 -d "." ) if [ -z "$_version" ]; then _version="?" exit 1 fi - echo $_version + echo "$_version" } # helper to use the default _value or override it with a found param @@ -136,7 +137,7 @@ function ph.getValueWithParam(){ ;; esac done - echo $_value + echo "$_value" } # check if a letter was used as command line option and return as 0 (=no) or 1 (=yes) @@ -159,7 +160,7 @@ function ph.hasParamoption(){ while getopts "abcdefghijklmnopqrstuvwxyz" _opt; do # echo "DEBUG: testing $_sParam in ${_opt} ..." case "${_opt}" in - $_sParam) + "$_sParam") echo "1" return 1 ;; @@ -255,7 +256,8 @@ function ph.status(){ # example: 12M returns 2^30 from ending "M" # param value with ending scale [none]=1 K=Kilo M=Mega G=Giga function ph._getExp(){ - local _unit=`echo $1 | sed "s#[0-9]##g"` + local _unit + _unit=$( echo $1 | sed "s#[0-9]##g" ) test -z "$_unit" && echo 1 @@ -311,14 +313,17 @@ function ph.getFileAge(){ echo $(($(date +%s) - $(date +%s -r "$1"))) } +# get file for storage of last value +# global string dir_data custom path; default is /tmp/icinga_counter/ function ph._getStorefile(){ local varName=$1 local mydir="/tmp/icinga_counter" - test -z $dir_data || mydir="${dir_data}/_counter" - local _basename=`basename $0` + test -n "$dir_data" && mydir="${dir_data}/_counter" + local _basename + _basename=$(basename $0) - test -d ${mydir} || mkdir -p ${mydir} + test -d "${mydir}" || mkdir -p "${mydir}" echo "${mydir}/${_basename}${varName}.lastvalue" } @@ -327,7 +332,7 @@ function ph._getStorefile(){ function ph._savecounter() { local varName=$1 local value=$2 - local sStoreFile=`ph._getStorefile "${varName}"` + local sStoreFile=$(ph._getStorefile "${varName}") #echo "DEBUG: `date +%s`:${value} \> ${sStoreFile}" # echo "`date +%s`:${value}" > "${sStoreFile}" echo ${value} > "${sStoreFile}" @@ -335,7 +340,7 @@ function ph._savecounter() { function ph._getageoflastvalue() { local varName=$1 - local sStoreFile=`ph._getStorefile "${varName}"` + local sStoreFile=$(ph._getStorefile "${varName}") ph.getFileAge "${sStoreFile}" # local ilast=`cat "${sStoreFile}" 2>/dev/null | cut -f 1 -d ":" ` # local inow=`date +%s` @@ -343,7 +348,7 @@ function ph._getageoflastvalue() { } function ph._readlastvalue(){ local varName=$1 - local sStoreFile=`ph._getStorefile "${varName}"` + local sStoreFile=$(ph._getStorefile "${varName}") # cat "${sStoreFile}" 2>/dev/null | cut -f 2 -d ":" cat "${sStoreFile}" 2>/dev/null } @@ -351,14 +356,15 @@ function ph._readlastvalue(){ # init usage for performance data # it creates a filename based on the started check script function ph._perfinit(){ - local _basename=`basename $0` - ph_perfdatafile="/tmp/perfdata_`echo $_basename | sed "s#[^a-z0-9\-]#_#g"`" + local _basename + _basename=$(basename $0) + ph_perfdatafile="/tmp/perfdata_$(echo $_basename | sed "s#[^a-z0-9\-]#_#g")" rm -f "${ph_perfdatafile}" 2>/dev/null } # generate label for performance data value function ph._getperflabel(){ - echo $1 | tr [:upper:] [:lower:] | sed "s#[^a-z0-9\-]##g" + echo "$1" | tr [:upper:] [:lower:] | sed "s#[^a-z0-9\-]##g" } # get speed of change of a counter value # param1: string variable name @@ -371,7 +377,6 @@ function ph.perfdeltaspeed(){ local bcParam= - typeset -i newvalue=0 typeset -i deltaFactor=1 test "$deltaUnit" = "s" && deltaFactor=1 @@ -382,7 +387,7 @@ function ph.perfdeltaspeed(){ test "$isFloat" = "float" && bcParam="-l" # get last value - local lastvalue=`ph._readlastvalue "${varName}"` + local lastvalue=$(ph._readlastvalue "${varName}") if [ "$lastvalue" = "" ]; then # retvalue="[no last value]" retvalue="" @@ -395,11 +400,11 @@ function ph.perfdeltaspeed(){ # retvalue="[reset data]" retvalue="" else - local iage=`ph._getageoflastvalue "${varName}"` + local iage=$(ph._getageoflastvalue "${varName}") test $iage = 0 && iage=1 - local delta=`echo "${value}-$lastvalue" | bc $bcParam ` - local deltaspeed=`echo "${delta}*${deltaFactor}/(${iage})" | bc $bcParam` + local delta=$(echo "${value}-$lastvalue" | bc $bcParam ) + local deltaspeed=$(echo "${delta}*${deltaFactor}/(${iage})" | bc $bcParam) retvalue=$deltaspeed fi fi @@ -407,7 +412,7 @@ function ph.perfdeltaspeed(){ #echo DEBUG sData="${sData} '${varName}'=${retvalue}" # sGDData="${sGDData} '${varName}'=${retvalue}" - echo ${retvalue} + echo "${retvalue}" # DEBUG - wird in Nagios sichtbar # gdAddLabel "... ${varName} absolute: ${value} -> delta: ${delta} -> derive: ${retvalue} ($iage sec)" @@ -425,7 +430,7 @@ function ph.perfadd(){ if [ -z "$ph_perfdatafile" ]; then ph._perfinit fi - local _label=`ph._getperflabel "$1"` + local _label=$(ph._getperflabel "$1") local _value=$2 local _w=$3 local _c=$4 @@ -435,7 +440,7 @@ function ph.perfadd(){ if [ -z "$_min" ]; then _min=0 fi - echo "${_label}=${_value};${_w};${_c};${_min};${_max}" >>${ph_perfdatafile} + echo "${_label}=${_value};${_w};${_c};${_min};${_max}" >>"${ph_perfdatafile}" } @@ -445,8 +450,8 @@ function ph.perfshow(){ if [ ! -z "$ph_perfdatafile" ]; then if [ -f "$ph_perfdatafile" ]; then echo -n " |" - cat ${ph_perfdatafile} | tr "\n" " " - rm -f ${ph_perfdatafile} + cat "${ph_perfdatafile}" | tr "\n" " " + rm -f "${ph_perfdatafile}" fi fi } -- GitLab