0

i'm noob in bash.

I want to create check so if 1 path is dead i want to get warning message,more than 1 path down-critical, no dead path-all is OK

i would use this command and it's output:

powermt display dev=all 

CHECK_DEGRADED=/usr/local/bin/sudo /sbin/powermt display | grep dead| wc -l
 if [ $CHECK_DEGRADED -eq 1 ]; then

status=1
statustxt=WARNING

 else if [ $CHECK_DEGRADED -gt 1 ];  then

status=2
statustxt=CRITICAL


else

status=0
statustxt=OK

 fi 

output should be something like: Path is in $statustxt state

1 Answers1

0

First of all Welcome to Stack Overflow.

  1. declare an array of path
  2. scan on all of them
  3. check if the path exist
  4. if exist increment a counter
  5. then check the counter for exit status

Something like that should be fine:

deadPath=0
 declare -a pathArray=(/usr/local/bin/sudo /sbin/powermt display)
 for i in "${pathArray[@]}"
 do
   if [ -d "$i" ]; then
      # Will enter here if path exists
      echo "Path $i Exists"
   else
    deadPath=$[$deadPath+1] 
   fi
done
if (( deadPath > 1 )); then
  echo "Critical"
  exit 2
elif (( deadPath == 1 )); then
  echo "Warning"
  exit 1
else 
  echo "OK"
  exit 0
fi
Raman Sailopal
  • 12,320
  • 2
  • 11
  • 18
DanieleO
  • 462
  • 1
  • 7
  • 20
  • Thanks Daniele i'll give it a try !1 –  Jul 25 '17 at 14:58
  • For Nagios nrpe you will need to exit each condition with the status, 0 for OK, 1 for problem and 2 for critical – Raman Sailopal Jul 25 '17 at 15:00
  • Yes @dragan.vucanovic, just replace the echo with the exit status. Approved review. – DanieleO Jul 25 '17 at 15:01
  • No, the echo will be needed to return a message to the Nagios console. It will need the exit status AND the echo (only one line of text) – Raman Sailopal Jul 25 '17 at 15:03
  • and i would like at the end to add final output so i added something like this: echo "Path is in $statustxt state" but then check_mk complains "Exception in check plugin "local",invalid line in agent section <> OK" –  Jul 25 '17 at 15:05
  • @dragan.vucanovic You mean at the end of the script? – DanieleO Jul 25 '17 at 15:06
  • Exit command terminates the script, so you can't add something after the fi part. You can modifiy the echo statement before the exit value – DanieleO Jul 25 '17 at 15:16
  • i followed this example https://mathias-kettner.de/checkmk_localchecks.html –  Jul 25 '17 at 15:25
  • It's a little bit differet, in my script i just exit the result value. In your example it printout at the end of the script the exit status and other info. You can do both way – DanieleO Jul 25 '17 at 15:27
  • Dragan, you will be able to echo variables back in the echo statement but you will need to make sure that any problematic characters are escaped correctly. – Raman Sailopal Jul 25 '17 at 16:24
  • @dragan.vucanovic Please provide a feedback on my answer if is all complete. Thanks. – DanieleO Jul 26 '17 at 07:35