From a9dced742858074b5e1300605493546952a7028c Mon Sep 17 00:00:00 2001
From: "Hahn Axel (hahn)" <axel.hahn@unibe.ch>
Date: Wed, 30 Aug 2023 16:18:54 +0200
Subject: [PATCH] reverse return code in ph.hasParamoption

---
 check_mysqlserver                     | 17 +++++++++--------
 check_psqlserver                      | 25 +++++++++++++------------
 docs/20_Checks/inc_pluginfunctions.md |  8 +++++---
 inc_pluginfunctions                   |  9 +++++----
 4 files changed, 32 insertions(+), 27 deletions(-)

diff --git a/check_mysqlserver b/check_mysqlserver
index bb9ac83..3f1ac92 100755
--- a/check_mysqlserver
+++ b/check_mysqlserver
@@ -20,11 +20,12 @@
 # 2021-06-24  v0.2  <axel.hahn@iml.unibe.ch>  check if query cache is enabled as a shared function
 # 2023-06-06  v0.3  <axel.hahn@unibe.ch>      some shell fixes
 # 2023-07-28  v1.0  <axel.hahn@unibe.ch>      update help page; fix [client]: command not found
+# 2023-08-30  v1.1  <axel.hahn@unibe.ch>      reverse return code in ph.hasParamoption to unix like return codes: 0=true; <>0 = false
 # ======================================================================
 
 . $( dirname $0 )/inc_pluginfunctions
 
-export self_APPVERSION=1.0
+export self_APPVERSION=1.1
 
 # --- set HOME
 HOME=/etc/icingaclient
@@ -194,12 +195,12 @@ function _verify_cache(){
 # MAIN
 # ----------------------------------------------------------------------
 
-bOptInstall=$(   ph.hasParamoption "i" "$@")
-bOptUninstall=$( ph.hasParamoption "u" "$@")
-bOptHelp=$(      ph.hasParamoption "h" "$@")
+ph.hasParamoption "i" "$@"; bOptInstall=$?
+ph.hasParamoption "u" "$@"; bOptUninstall=$?
+ph.hasParamoption "h" "$@"; bOptHelp=$?
 
-if [ $bOptHelp -eq 1 -o $# -lt 1 ]; then
-    showHelp
+if [ $bOptHelp -eq 0 -o $# -lt 1 ]; then
+    echo "showHelp"
     exit 0
 fi
 
@@ -207,7 +208,7 @@ fi
 ph.require mysql
 
 # --- install
-if [ $bOptInstall -eq 1 -a "$( whoami )" = "root" ]; then
+if [ $bOptInstall -eq 0 -a "$( whoami )" = "root" ]; then
     if [ -f $cfgfile ]; then
         ph.status "SKIP installation. config file already exists: $cfgfile."
         ph.exit
@@ -223,7 +224,7 @@ if [ $bOptInstall -eq 1 -a "$( whoami )" = "root" ]; then
 fi
 
 # --- uninstall
-if [ $bOptUninstall -eq 1 -a "$( whoami )" = "root" ]; then
+if [ $bOptUninstall -eq 0 -a "$( whoami )" = "root" ]; then
     HOME=/root
 
     _uninstall
diff --git a/check_psqlserver b/check_psqlserver
index 59af0f0..909a35c 100755
--- a/check_psqlserver
+++ b/check_psqlserver
@@ -14,17 +14,18 @@
 # https://www.postgresql.org/docs/current/monitoring-stats.html
 # ----------------------------------------------------------------------
 # 2023-06-07  v0.2  <axel.hahn@unibe.ch>
-# 2023-06-08  v0.3  <axel.hahn@unibe.ch>  show unknown if database connection fails
-# 2023-06-08  v0.4  <axel.hahn@unibe.ch>  get summary for cronflicts and problems
-# 2023-06-09  v0.5  <axel.hahn@unibe.ch>  deltaunit can be set as parameter
-# 2023-06-13  v0.6  <axel.hahn@unibe.ch>  no output on activity; update replication check
-# 2023-06-16  v0.7  <axel.hahn@unibe.ch>  update help text
+# 2023-06-08  v0.3  <axel.hahn@unibe.ch>      show unknown if database connection fails
+# 2023-06-08  v0.4  <axel.hahn@unibe.ch>      get summary for cronflicts and problems
+# 2023-06-09  v0.5  <axel.hahn@unibe.ch>      deltaunit can be set as parameter
+# 2023-06-13  v0.6  <axel.hahn@unibe.ch>      no output on activity; update replication check
+# 2023-06-16  v0.7  <axel.hahn@unibe.ch>      update help text
+# 2023-08-30  v0.8  <axel.hahn@unibe.ch>      reverse return code in ph.hasParamoption to unix like return codes: 0=true; <>0 = false
 # ======================================================================
 
 
 . $(dirname $0)/inc_pluginfunctions
 self_APPNAME=$( basename $0 | tr [:lower:] [:upper:] )
-self_APPVERSION=0.7
+self_APPVERSION=0.8
 
 # --- other vars...
 cfgfile=/etc/icingaclient/.psql.conf
@@ -176,11 +177,11 @@ function renderCounters(){
 # MAIN
 # ----------------------------------------------------------------------
 
-bOptInstall=$(   ph.hasParamoption "i" "$@")
-bOptUninstall=$( ph.hasParamoption "u" "$@")
-bOptHelp=$(      ph.hasParamoption "h" "$@")
+ph.hasParamoption "i" "$@"; bOptInstall=$?
+ph.hasParamoption "u" "$@"; bOptUninstall=$?
+ph.hasParamoption "h" "$@"; bOptHelp=$?
 
-if [ $bOptHelp -eq 1 -o $# -lt 1 ]; then
+if [ $bOptHelp -eq 0 -o $# -lt 1 ]; then
     _usage
     exit 0
 fi
@@ -189,7 +190,7 @@ fi
 # ph.require mysql
 
 # --- install
-if [ $bOptInstall -eq 1 -a "$( whoami )" = "root" ]; then
+if [ $bOptInstall -eq 0 -a "$( whoami )" = "root" ]; then
     if [ -f $cfgfile ]; then
         ph.status "SKIP installation. config file already exists: $cfgfile."
         ph.exit
@@ -203,7 +204,7 @@ if [ $bOptInstall -eq 1 -a "$( whoami )" = "root" ]; then
 fi
 
 # --- uninstall
-if [ $bOptUninstall -eq 1 -a "$( whoami )" = "root" ]; then
+if [ $bOptUninstall -eq 0 -a "$( whoami )" = "root" ]; then
 
     _uninstall
     rm -f $cfgfile
diff --git a/docs/20_Checks/inc_pluginfunctions.md b/docs/20_Checks/inc_pluginfunctions.md
index 325f857..51773cb 100644
--- a/docs/20_Checks/inc_pluginfunctions.md
+++ b/docs/20_Checks/inc_pluginfunctions.md
@@ -186,7 +186,7 @@ This will set variable iWarnLimit based on CLI parameter -w [value] ... if it do
 
 ### ph.hasParamoption
 
-check if a letter was used as command line option and return as 0 (=no) or 1 (=yes)
+check if a letter was used as command line option and return as 0 (=true) or 1 (=false)
 
 Syntax:
 
@@ -201,10 +201,12 @@ Parameters:
 
 Example:
 
+Show a help if command line param ``-h`` was given - or no parameter.
+
 ```bash
-bOptHelp=`ph.hasParamoption "h" "$@"`
+ph.hasParamoption "h" "$@" ; bOptHelp=$?
 
-if [ $bOptHelp -eq 1 -o $# -lt 1 ]; then
+if [ $bOptHelp -eq 0 -o $# -lt 1 ]; then
     _usage
     exit 0
 fi
diff --git a/inc_pluginfunctions b/inc_pluginfunctions
index 9771ef5..aabe5d8 100644
--- a/inc_pluginfunctions
+++ b/inc_pluginfunctions
@@ -38,6 +38,7 @@
 # 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
 # 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
 # ======================================================================
 
 
@@ -169,13 +170,13 @@ function ph.hasParamoption(){
     # echo "DEBUG: testing $_sParam in ${_opt} ..."
     case "${_opt}" in
       "$_sParam")
-        echo "1"
-        return 1
+        # echo "0"
+        return 0
         ;;
     esac
   done
-  echo "0"
-  return 0
+  # echo "1"
+  return 1
 }
 
 
-- 
GitLab