From c3601f8c3a21de6c3721e5daba8940292b6dea69 Mon Sep 17 00:00:00 2001
From: "Hahn Axel (hahn)" <axel.hahn@unibe.ch>
Date: Mon, 19 Jun 2023 14:41:47 +0200
Subject: [PATCH] ceph status: add help; no more tmpfile

---
 check_ceph_status | 73 +++++++++++++++++++++++++++++++++++------------
 1 file changed, 54 insertions(+), 19 deletions(-)

diff --git a/check_ceph_status b/check_ceph_status
index d9c59ed..aa18e59 100755
--- a/check_ceph_status
+++ b/check_ceph_status
@@ -19,12 +19,15 @@
 # 2021-03-31  v1.2  <axel.hahn@iml.unibe.ch>  estimate remaining time of ceph recovery
 # 2021-04-12  v1.3  <axel.hahn@iml.unibe.ch>  if degraded items are 0 delete init file too
 # 2023-04-24  v1.4  <axel.hahn@unibe.ch>      update for newer ceph versions
+# 2023-06-19  v1.5  <axel.hahn@unibe.ch>      add help and param support; no more tmpfile
 # ======================================================================
 
 . $(dirname $0)/inc_pluginfunctions
 
-initfile=/tmp/ceph-status-not-ok-start
-tmpfile=/tmp/ceph-status.out_$$
+initfile="/tmp/ceph-status-not-ok-start-$USER"
+
+self_APPNAME=$( basename $0 | tr [:lower:] [:upper:] )
+self_APPVERSION=1.5
 
 iSleep=3
 doLoop=1
@@ -39,24 +42,52 @@ line="__________________________________________________________________________
 # FUNCTIONS
 # ----------------------------------------------------------------------
 
+function showHelp(){
+    _self=$(basename $0)
+cat <<EOF
+______________________________________________________________________
+
+$self_APPNAME 
+v$self_APPVERSION
+
+(c) Institute for Medical Education - University of Bern
+Licence: GNU GPL 3
+______________________________________________________________________
+
+Show ceph health status.
+The state of the check switches to warning if HEALTH_WARN was detected
+and is error of other HEALTH values than HEALTH_WARN or HEALTH_OK.
+
+In the output is the complete output of the command "ceph status".
+
+If degraded objects are found it shows the progress of repair process.
+
+SYNTAX:
+$_self
+
+OPTIONS:
+    -h or --help   show this help.
+
+EOF
+}
+
 function readCephStatus(){
-        sudo ceph status >$tmpfile
-        if [ $? -ne 0 ]; then
-                rm -f $tmpfile
+        if ! data=$( sudo /bin/ceph status 2>&1 ); then
+                echo "$data"
                 ph.abort "UNKNOWN: ceph is not available or no sudo permissions to execute ceph commands."
         fi
 }
 function getCephStatus(){
-        cat $tmpfile | grep "health:" | awk '{ print $2 }' 
+        grep "health:" <<< "$data" | awk '{ print $2 }' 
 }
 function getTotalObjects(){
-        cat $tmpfile | grep "pgs:.*objects degraded" | awk '{ print $2 }' | cut -f 2 -d "/"
+        grep "pgs:.*objects degraded"  <<< "$data" | awk '{ print $2 }' | cut -f 2 -d "/"
 }
 function getDegraded(){
-        cat $tmpfile | grep "pgs:.*objects degraded" | awk '{ print $2 }' | cut -f 1 -d "/"
+        grep "pgs:.*objects degraded"  <<< "$data" | awk '{ print $2 }' | cut -f 1 -d "/"
 }
 function getMisplaced(){
-        cat $tmpfile | grep ".*objects misplaced" | awk '{ print $2 }' | cut -f 1 -d "/"
+        grep ".*objects misplaced"     <<< "$data" | awk '{ print $2 }' | cut -f 1 -d "/"
 }
 
 
@@ -64,6 +95,14 @@ function getMisplaced(){
 # MAIN
 # ----------------------------------------------------------------------
 
+# --- check param -h
+case "$1" in
+    "--help"|"-h")
+        showHelp
+        exit 0
+        ;;
+    *)
+esac
 
 readCephStatus
 if [ ! -f $initfile ]; then
@@ -95,11 +134,11 @@ fi
 ph.status "Ceph status is $sCephStatus"
 echo
 
-while [ ! "$sCephStatus" = "HEALTH_OK" -a $doLoop = 1 ]; do
+while [ ! "$sCephStatus" = "HEALTH_OK" ] && [ $doLoop = 1 ]; do
 
         typeset -i iObjCount=$iDeg+$iMis
 
-        iTsNow=`date +%s`
+        iTsNow=$( date +%s )
 
         typeset -i iDegNow=$(getDegraded)
         typeset -i iMisNow=$(getMisplaced)
@@ -114,7 +153,7 @@ while [ ! "$sCephStatus" = "HEALTH_OK" -a $doLoop = 1 ]; do
         # typeset -i iDeltaMis2=$iLastMis-$iMisNow
 
 
-        if [ $iTsDelta -gt 0 ]; then
+        if [ $iDegNow+$iMisNow -gt 0 ]  && [ $iTsDelta -gt 0 ]; then
                 typeset -i iDegPerMin=$iDoneDeg/$iTsDelta*60
                 if [ $iDegPerMin -gt 0 ]; then
                         # timeByDeg=`echo $iTsDelta*$iObjCount/$iDoneDeg/60 - $iTsDelta/60 | bc`
@@ -180,9 +219,7 @@ while [ ! "$sCephStatus" = "HEALTH_OK" -a $doLoop = 1 ]; do
         printf "misplaced  $tbl" "$iMis"     "$iMisNow" "$iDoneMis"   "$iMisPerMin" "$_timeByMis"
         printf "total      $tbl" $iObjCount  $iNowTotal  $iDoneTotal  " "           "$sTimeLeft"
         echo $line
-        cat $tmpfile
-        rm -f $tmpfile 2>/dev/null
-
+        echo "$data"
 
         if [ $doSingleLoop = 1 ]; then
                 doLoop=0
@@ -194,12 +231,10 @@ while [ ! "$sCephStatus" = "HEALTH_OK" -a $doLoop = 1 ]; do
 
 done
 
-test $doSingleLoop = 0 -o "$sCephStatus" = "HEALTH_OK" && cat $tmpfile
+test $doSingleLoop = 0 -o "$sCephStatus" = "HEALTH_OK" && echo "$data"
 echo
-echo --- DONE `date`
+echo --- DONE $( date )
 test "$sCephStatus" = "HEALTH_OK" && rm -f $initfile 2>/dev/null
 test $iDeg -eq 0 && rm -f $initfile 2>/dev/null
-rm -f $tmpfile 2>/dev/null
-
 
 ph.exit
-- 
GitLab