From 539fedc7e90a73d98beed59624a7dd25fd7986d0 Mon Sep 17 00:00:00 2001 From: "Hahn Axel (hahn)" <axel.hahn@unibe.ch> Date: Thu, 14 Sep 2023 12:38:04 +0200 Subject: [PATCH] fix broken pipe in journallog --- docs/20_Checks/inc_pluginfunctions.md | 17 +++++++++++++++++ inc_pluginfunctions | 18 +++++++++++++++++- 2 files changed, 34 insertions(+), 1 deletion(-) diff --git a/docs/20_Checks/inc_pluginfunctions.md b/docs/20_Checks/inc_pluginfunctions.md index 51773cb..3d29275 100644 --- a/docs/20_Checks/inc_pluginfunctions.md +++ b/docs/20_Checks/inc_pluginfunctions.md @@ -389,6 +389,23 @@ if [ $iWarnLimit -gt 0 -a $iCriticalLimit -gt 0 ]; then fi ``` +### ph.showtimer + +Show a time in seconds and milliseconds since start (since source of inc_pluginfunctions.sh). +You can use it to show an execution time. + +Syntax: + +```text +ph.showtimer +``` + +It has no parameters. + +```shell +2.410 sec +``` + ### ph.status Show the current status (set by ``ph.setStatus``) as Text. diff --git a/inc_pluginfunctions b/inc_pluginfunctions index 3740c11..d21ca19 100644 --- a/inc_pluginfunctions +++ b/inc_pluginfunctions @@ -40,6 +40,7 @@ # 2023-08-24 v1.12 <axel.hahn@unibe.ch> toUnit got 3rd param for count of digits after "." # 2023-08-30 v1.13 <axel.hahn@unibe.ch> reverse return code in ph.hasParamoption to unix like return codes: 0=true; <>0 = false # 2023-09-05 v1.14 <axel.hahn@unibe.ch> ph.require - show error below status line +# 2023-09-14 v1.15 <axel.hahn@unibe.ch> add ph.showtimer; fix broken pipe messages in journallog # ====================================================================== @@ -51,6 +52,8 @@ typeset -i ph_cfg__EXIT_UNKNOWN=3 typeset -i ph_cfg__EXIT_CODE +ph_timer_start=$( date +%s.%N ) + declare ph_perfdatafile= # abort a check and exit with status "unknown" @@ -72,6 +75,19 @@ function ph.require(){ fi } +# get time in sec and milliseconds since start +# no parameter is required +function ph.showtimer(){ + local timer_end; timer_end=$( date +%s.%N ) + local totaltime; totaltime=$( awk "BEGIN {print $timer_end - $ph_timer_start }" ) + local sec_time; sec_time=$( echo "$totaltime" | cut -f 1 -d "." ) + test -z "$sec_time" && sec_time=0 + + local ms_time; ms_time=$( echo "$totaltime" | cut -f 2 -d "." | cut -c 1-3 ) + + echo "$sec_time.$ms_time sec" +} + # ---------------------------------------------------------------------- # exit a check plugin function ph.exit(){ @@ -311,7 +327,7 @@ function ph.toUnit(){ local _dots - test $_digits -gt 0 && _dots=$( yes "." | head -$_digits | tr -d "\n" ) + test $_digits -gt 0 && _dots=$( yes "." 2>/dev/null | head -$_digits | tr -d "\n" ) test $_digits -gt 0 && _bc+=" -l | grep -o '.*\\.${_dots}'" echo "$(echo "$_value" | tr -d "[:alpha:]" )*${_multiply}/$_divisor" | eval "$_bc" | sed "s#^\.#0.#" -- GitLab