From 51205c8177021ae8470769f8484af3240f27a8c8 Mon Sep 17 00:00:00 2001
From: "Hahn Axel (hahn)" <axel.hahn@unibe.ch>
Date: Thu, 22 Jun 2023 10:09:05 +0200
Subject: [PATCH] no more tmpfile

---
 check_ceph_diskfree | 31 +++++++++++++++----------------
 1 file changed, 15 insertions(+), 16 deletions(-)

diff --git a/check_ceph_diskfree b/check_ceph_diskfree
index 3167e17..63fd29e 100755
--- a/check_ceph_diskfree
+++ b/check_ceph_diskfree
@@ -19,11 +19,11 @@
 # 2020-03-05  v1.1  <axel.hahn@iml.unibe.ch> switch to ph.* helper functions
 # 2023-04-24  v1.2  <axel.hahn@unibe.ch>     update for newer ceph versions
 # 2023-05-09  v1.3  <axel.hahn@unibe.ch>     add help
+# 2023-06-19  v1.4  <axel.hahn@unibe.ch>     no more tmpfile
 # ======================================================================
 
 . $(dirname $0)/inc_pluginfunctions
 
-tmpfile=/tmp/ceph_df_output__$$
 outfile=/tmp/check_ceph_df_out__$$
 
 typeset -i iWarning=0
@@ -31,7 +31,7 @@ typeset -i iCritical=0
 
 
 self_APPNAME=$( basename $0 | tr [:lower:] [:upper:] )
-self_APPVERSION=1.3
+self_APPVERSION=1.4
 
 function showHelp(){
 cat <<EOF
@@ -76,9 +76,9 @@ typeset -i iWarnLimit=$(     ph.getValueWithParam 70 w "$@")
 typeset -i iCriticalLimit=$( ph.getValueWithParam 90 c "$@")
 
 
-if ! sudo /bin/ceph df > $tmpfile 2>&1
+if ! data=$( sudo /bin/ceph df 2>&1 )
 then
-    rm -f $tmpfile
+    echo "$data"
     ph.abort "UNKNOWN: ceph is not available or no sudo permissions to execute ceph commands."
 fi
 
@@ -86,13 +86,14 @@ rm -f $outfile 2>/dev/null
 isHeader=0
 area=
 
-grep "[a-zA-Z]" $tmpfile | while read line
+out=$( 
+grep "[a-zA-Z]" <<< "$data" | while read line
 do
     newArea=$(echo $line | grep "^\-\-\- [A-Z]*" | sed "s#--- ##" | sed "s# ---##" )
     lineStatus="          "
-    if [ ! -z "$newArea" ]; then
-    echo>>$outfile
-        area=$(echo $newArea | cut -f 1 -d ":")
+    if [ -n "$newArea" ]; then
+        echo
+        area=$(echo "$newArea" | cut -f 1 -d ":")
         isHeader=1
 
         # Position of disk usage in the sections
@@ -111,10 +112,8 @@ do
             percentUsed=$(echo $line | awk -v ipos=$iPos '{ print $ipos }' | sed "s#\..*##")
             if [ $percentUsed -ge $iWarnLimit ]; then
                 if [ $percentUsed -ge $iCriticalLimit ]; then
-                    iCritical=$iCritical+1
                     lineStatus="CRITICAL  "
                 else
-                    iWarning=$iWarning+1
                     lineStatus="WARNING   "
                 fi
             else
@@ -122,9 +121,12 @@ do
             fi
         fi
     fi
-    echo "$lineStatus $line" >> $outfile
+    echo "$lineStatus $line"
 done
+)
 
+iWarning=$(  grep -c "^WARNING"  <<< "$out" )
+iCritical=$( grep -c "^CRITICAL" <<< "$out" )
 
 if [ $iCritical -gt 0 ]; then
     ph.setStatus "critical"
@@ -135,10 +137,10 @@ else
 fi
 
 ph.status "Disksize on Ceph cluster and its pools - warnings: $iWarning ($iWarnLimit %) .. critcal: $iCritical ($iCriticalLimit %)"
-cat $outfile
+echo "$out"
 
 # global size status is in TOTAL...
-totalLine="$( grep '^TOTAL' $tmpfile | sed 's#iB##g' )"
+totalLine="$( grep '^TOTAL' <<< "$data" | sed 's#iB##g' )"
 
 # echo "DEBUG: totalLine = $totalLine"
 # DEBUG: totalLine = TOTAL  18 T  18 T  428 G   428 G       2.30
@@ -155,9 +157,6 @@ ph.perfadd "global-total"    "${iTotal}"    "" "" 0 ${iTotal}
 ph.perfadd "global-avail"    "${iAvail}"    "" "" 0 ${iTotal}
 ph.perfadd "global-used"     "${iUsed}"     "" "" 0 ${iTotal}
 
-# cleanup
-rm -f $tmpfile $outfile 2>/dev/null
-
 ph.exit
 
 # ----------------------------------------------------------------------
-- 
GitLab