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

fix ph.toUnit with float values; shell fixes

parent 21206833
No related branches found
No related tags found
1 merge request!122Update docs
......@@ -36,6 +36,7 @@
# 2023-04-24 v1.8 <axel.hahn@unibe.ch> fix unit conversion
# 2023-05-05 v1.9 <axel.hahn@unibe.ch> user specific counter directory
# 2023-05-17 v1.10 <axel.hahn@unibe.ch> ph.getOS searches in os-release first
# 2023-06-22 v1.11 <axel.hahn@unibe.ch> fix ph.toUnit with float values; shell fixes
# ======================================================================
......@@ -45,13 +46,9 @@ typeset -i ph_cfg__EXIT_WARNING=1
typeset -i ph_cfg__EXIT_CRITICAL=2
typeset -i ph_cfg__EXIT_UNKNOWN=3
declare ph_perfdatafile=
# declare -a ph_perfdata
typeset -i ph_perfcounter=0
# save command line params
ph_cmd__params="$@"
typeset -i ph_cfg__EXIT_CODE
declare ph_perfdatafile=
# abort a check and exit with status "unknown"
function ph.abort(){
......@@ -86,24 +83,24 @@ function ph.exit(){
function ph.getOS(){
local distro=
if [ -z $distro ]; then
if [ -z "$distro" ]; then
# centos7, debian, manjaro, ubuntu
distro=$( grep "^ID=" /etc/os-release | cut -f 2 -d "=" )
fi
if [ -z $distro ]; then
if [ -z "$distro" ]; then
distro=$( grep "^ID=" /etc/*-release | cut -f 2 -d "=" )
fi
if [ -z $distro ]; then
if [ -z "$distro" ]; then
# debian6,7, ubuntu 10,12 .. maybe unneeded.
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
if [ -z "$distro" ]; then
ph.abort "UNKNOWN: distro was not detected."
fi
......@@ -190,19 +187,19 @@ function ph.hasParamoption(){
# param integer|string 0..3 or ok|warning|critical|unknown
function ph.setStatus(){
case $1 in
$ph_cfg__EXIT_OK|"ok"):
"$ph_cfg__EXIT_OK"|"ok"):
ph_cfg__EXIT_CODE=$ph_cfg__EXIT_OK
ph_cfg__EXIT_STATUS="OK"
;;
$ph_cfg__EXIT_WARNING|"warning"):
"$ph_cfg__EXIT_WARNING"|"warning"):
ph_cfg__EXIT_CODE=$ph_cfg__EXIT_WARNING
ph_cfg__EXIT_STATUS="WARNING"
;;
$ph_cfg__EXIT_CRITICAL|"critical"):
"$ph_cfg__EXIT_CRITICAL"|"critical"):
ph_cfg__EXIT_CODE=$ph_cfg__EXIT_CRITICAL
ph_cfg__EXIT_STATUS="CRITICAL"
;;
$ph_cfg__EXIT_UNKNOWN|"unknown"):
"$ph_cfg__EXIT_UNKNOWN"|"unknown"):
ph_cfg__EXIT_CODE=$ph_cfg__EXIT_UNKNOWN
ph_cfg__EXIT_STATUS="UNKNOWN"
;;
......@@ -267,7 +264,7 @@ function ph.status(){
# param value with ending scale [none]=1 K=Kilo M=Mega G=Giga
function ph._getExp(){
local _unit
_unit=$( echo $1 | sed "s#[0-9]##g" )
_unit=${1//[0-9\.]/}
test -z "$_unit" && echo 1
......@@ -300,11 +297,11 @@ function ph.toUnit(){
local _value=$1
local _unit=$2
local _multiply=`ph._getExp $_value`
local _divisor=`ph._getExp $_unit`
local _multiply; _multiply=$( ph._getExp "$_value" )
local _divisor; _divisor=$( ph._getExp "$_unit" )
# echo "DEBUG ... $_divisor .. $_multiply"
echo "$(echo $_value | tr -d "[:alpha:]" )*${_multiply}/$_divisor" | bc
echo "$(echo "$_value" | tr -d "[:alpha:]" )*${_multiply}/$_divisor" | bc
}
......@@ -346,7 +343,7 @@ function ph._getStorefile(){
function ph._savecounter() {
local varName=$1
local value=$2
local sStoreFile=$(ph._getStorefile "${varName}")
local sStoreFile; sStoreFile=$(ph._getStorefile "${varName}")
#echo "DEBUG: `date +%s`:${value} \> ${sStoreFile}"
# echo "`date +%s`:${value}" > "${sStoreFile}"
echo ${value} > "${sStoreFile}"
......@@ -357,7 +354,7 @@ function ph._savecounter() {
# param string varName variable name of a value to store
function ph._getageoflastvalue() {
local varName=$1
local sStoreFile=$(ph._getStorefile "${varName}")
local sStoreFile; sStoreFile=$(ph._getStorefile "${varName}")
ph.getFileAge "${sStoreFile}"
# local ilast=`cat "${sStoreFile}" 2>/dev/null | cut -f 1 -d ":" `
# local inow=`date +%s`
......@@ -368,7 +365,7 @@ function ph._getageoflastvalue() {
# param string varName variable name of a value to store
function ph._readlastvalue(){
local varName=$1
local sStoreFile=$(ph._getStorefile "${varName}")
local sStoreFile; sStoreFile=$(ph._getStorefile "${varName}")
# cat "${sStoreFile}" 2>/dev/null | cut -f 2 -d ":"
cat "${sStoreFile}" 2>/dev/null
}
......@@ -420,11 +417,11 @@ function ph.perfdeltaspeed(){
# retvalue="[reset data]"
retvalue=""
else
local iage=$(ph._getageoflastvalue "${varName}")
test $iage = 0 && iage=1
local iage; 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; delta=$(echo "${value}-$lastvalue" | bc $bcParam )
local deltaspeed; deltaspeed=$(echo "${delta}*${deltaFactor}/(${iage})" | bc $bcParam)
retvalue=$deltaspeed
fi
fi
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment