-
Hahn Axel (hahn) authoredHahn Axel (hahn) authored
Shared functions
All scripts start with sourcing a shared bash file
. $( dirname $0 )/inc_pluginfunctions
In that script are several functions starting with prefix ph. (=plugin helper)
Available functions
This is a list in alphabetic order
ph.abort
Shows error message and exit with status unknown.
Syntax:
ph.abort [TEXT]
Parameters:
- TEXT {string} The text message to show
Example:
if ! data=$( sudo /bin/ceph df 2>&1 )
then
echo "$data"
ph.abort "UNKNOWN: ceph is not available or no sudo permissions to execute ceph commands."
fi
ph.execIfReady
Execute a command and repeat max. MAXTRIES times if it fails.
Syntax:
ph.execIfReady [FUNCTION] [ [WAITTIME] [MAXTRIES] ]
Parameters:
- FUNCTION {string} - command line to execute
- WAITTIME {integer} - optional: sleeptime in sec before repeating again; default: 5
- MAXTRIES {integer} - optional: max number of tries; default: 3
Example:
ph.execIfReady "timedatectl show >$tmpfile; grep '^NTPSynchronized' $tmpfile >/dev/null"
cat $tmpfile | grep "^NTPSynchronized" >/dev/null
if [ $? -ne 0 ]; then
ph.setStatus "unknown"
ph.status "timesync: timedatectl has no line NTP synchronized"
cat $tmpfile
else
cat $tmpfile | grep "^NTPSynchronized=yes" >/dev/null
if [ $? -eq 0 ]; then
ph.status "timesync: a timesync service is active on this ${myos} host"
else
ph.setStatus "critical"
ph.status "timesync: there is no active timesync - you need to activate ntpd or timesyncd on this ${myos} host"
fi
fi
# /timedatectl
ph.exit
Use at the end to send performance data and exit plugin with set statuscode.
This is the last line of your check script.
Syntax:
ph.exit
(no parameters)
ph.getFileAge
get age of a given file in sec
The file must be reachable by the monitoring user.
Syntax:
ph.getFileAge [FILE]
Parameters:
- FILE {string} - filename
ph.getOS
get operating system as lowercase - centos/ debian/ ubuntu ...
Syntax:
ph.getOS
(no parameters)
Example:
$ bash -c ". inc_pluginfunctions; ph.getOS"
manjaro
In a script:
distro=$( ph.getOS )
case $distro in
"centos"|"almalinux")
# ... do something
;;
"debian"|"ubuntu")
# ... do something
;;
*)
ph.abort "UNKNOWN: distro [$distro] was detected but is not supported (yet)."
;;
esac
ph.getOSMajor
get OS Major version as integer, i.e. 7 on a CentOS7
Syntax:
ph.getOSMajor
(no parameters)
ph.getValueWithParam
Return default value or its override from command line.
Syntax:
ph.getValueWithParam VALUE PARAMNAME "$@"
Parameters:
- VALUE {integer} - the default value if the parameter (2nd param) was not set in the command line
- PARAMNAME {char} - a single letter for the parameter to search
- PARAM LIST {string} - list of all parameters to check; use "$@" set all current params from command line
Example:
# set default / override from command line params
typeset -i iWarnLimit; iWarnLimit=$( ph.getValueWithParam 70 w "$@")
typeset -i iCriticalLimit; iCriticalLimit=$( ph.getValueWithParam 90 c "$@")
This will set variable iWarnLimit based on CLI parameter -w [value] ... if it does not exist it gets the default 75.
ph.hasParamoption
check if a letter was used as command line option and return as 0 (=true) or 1 (=false)
Syntax:
ph.hasParamoption PARAMNAME "$@"
Parameters:
- PARAMNAME{char} - a single letter for the parameter to search
- PARAM LIST{string} - list of all parameters to check; use "$@" set all current params from command line
Example:
Show a help if command line param -h
was given - or no parameter.
ph.hasParamoption "h" "$@" ; bOptHelp=$?
if [ $bOptHelp -eq 0 -o $# -lt 1 ]; then
_usage
exit 0
fi
ph.perfadd
Add performance data. Their output will be written with ph.exit
. So you are free to add perfomance data anywhere within your check script.
Syntax:
ph.perfadd LABEL VALUE [ WARNLEVEL CRITICAL [ MIN MAX ] ]
Parameters:
- LABEL {string} - a label for the performance value; it must be unique for your script; The given value will be cleaned to lowercase; other chars than a-z, 0-9, minus will be deleted.
- VALUE {integer|float} - your value; default (if missing): 0
- WARNLEVEL {integer} - optional: warning level; not handled by graphite; default: empty string
- CRITICAL {integer} - optional: critical level; not handled by graphite; default: empty string
- MIN {integer} - optional: minimum value of graph; default: empty string
- MAX {integer} - optional: maximum value of graph;; default: empty string
Example:
Add values with label and value only:
data2=$( echo "$netdata" | sort | uniq -c )
iEst=$( echo "$data2" | grep ESTABLISHED | awk '{ print $1 }' )
iListen=$( echo "$data2" | grep LISTEN | awk '{ print $1 }' )
iWait=$( echo "$data2" | grep TIME_WAIT | awk '{ print $1 }' )
ph.perfadd "established" "$iEst"
ph.perfadd "listen" "$iListen"
ph.perfadd "time-wait" "$iWait"
ph.exit
Add values and define limits for the graph: I will draw it from 0 to a fixed maximum value like for a given physical maximum or 100 for percent values.
Remark that here are empty values for warning and critical to skip them.
ph.perfadd "memory-total" "${ramTotal}MB" "" "" 0 ${ramTotal}
ph.perfadd "memory-used" "${ramUsed}MB" "" "" 0 ${ramTotal}
ph.perfadd "memory-avail" "${ramAvail}MB" "" "" 0 ${ramTotal}
ph.exit