Nagios script to get total and free memory

Hi Experts, need some help.

I`m trying to write a shell script to get free, used and total memory on our linux servers.
It's working great, but i need follow some standards to make it a real nagios plugin.
It's pretty simple, you just type two parameters to the script, check_ram -w 80 -c 90 #just an exemple.
I don't want that it displays the amount of memory if no parameters was passed to the script.
#exemple: check_ram -w -c should give error and call the function HELP without show used and free memory. But it keeping ignoring the breaks and showing the used and free memory.
w and c need have values to let script continue.

  1 #!/bin/bash
  2 # 
  3 #
  4 #
  5 #
  6 #
  7 #
  8 #Display the name of the script
  9 SCRIPTNAME="check_memory"
 10 
 11 #Display version
 12 VERSION(){
 13 echo "Version 0.8"
 14 }
 15 
 16 #Display help
 17 HELP(){
 18 echo "$SCRIPTNAME -w <warning level> -c <critical level>"
 19 echo "example: check_ram -w 80 -c 90"
 20 }
 21 
 22 #sequence parameters
 23 w=$1 
 24 c=$2
 25   
 26 while [[ $# -gt 0 ]]
 27 do
 28 case $1 in
 29         -h | --help)VERSION;HELP;;
 30         -w) test -z $w ;if [ $? != 0 ]; then HELP ; fi; break;;
 31         -c) test -z $c ;if [ $? != 0 ]; then HELP ; fi; break;;
 32         *)VERSION;HELP; break;;
 33 esac
 34 done 
 35 
 36 full=$(free -m | awk 'NR==2 {print $2}')
 37 used=$(free -m | awk 'NR==3 {print $3}')
 38 free=$(free -m | awk 'NR==3 {print $4}')
 39 
 40 percent=$(( ($free*100)/$full))
 41 
 42 if [ $used -lt $(( ($full*80)/100 )) ] || [ $free -lt $(( ($full*80)/100 )) ];then
 43 echo "OK: using $used MB, you still have $percent% of free memory"
 44 exit 0
 45 fi
 46 
 47 
 48 
                                                                                                                                                      38,1          Fim

Thank you anyway.

Put the calculation of the memory into a function too. Call it only when needed - currently you call it all the time, makes no sense. It will be executed no matter what happens in the case/esac -block.
Second thing is, if you iterate through arguments with $# -gt 0 it will always be the same. You miss the shift .
Third, have the exit 0 at the very end of the script since this is, if not aborted earlier, the good exit code you want to have.

1 Like

Thx for replay Mr Zaxxon, i changed my code.
I'm just confused how to call MEMCAL function only if user type 2 int parameters like check_ram -w 80 -c 90.
how can i check if the two parameters has int values and only after check it call the function?
I'm still trying but i can't find a solution.

#!/bin/bash
  8 #Display the name of the script
  9 SCRIPTNAME="check_memory"
 10 
 11 #Display version
 12 VERSION(){
 13 echo "Version 0.8"
 14 }
 15 
 16 #Display help
 17 HELP(){
 18 echo "$SCRIPTNAME -w <warning level> -c <critical level>"
 19 echo "example: check_ram -w 80 -c 90"
 20 }
 21 
 22 #sequence parameters
 23 warn=$1
 24 crit=$2
 25   
 26 full=$(free -m | awk 'NR==2 {print $2}')
 27 used=$(free -m | awk 'NR==3 {print $3}')
 28 free=$(free -m | awk 'NR==3 {print $4}')
 29 
 30 percent=$(( ($free*100)/$full))
 31 
 32 #calculation of the memory
 33 MEMCALC(){
 34  
 35 if [ $used -lt $(( ($full*80)/100 )) ] || [ $free -lt $(( ($full*80)/100 )) ];then
 36 echo "OK: using $used MB, you still have $percent% of free memory"
 37 exit 0
 38 
 39 elif [ $used -ge $(( ($full*80)/100 )) ] || [ $used -lt $(( ($full*90)/100 )) ];then
 40 echo "WARNING: using $used MB, you have $percent% of free memory"
 41 exit 1
 42 
 43 elif [ $used -ge $(( ($full*90)/100 )) ];then
 44 echo "CRITICAL: using $used MB, you only have $percent% of free memory"
 45 exit 2
 46 
 47 else
 48 echo "unknow"
 49 exit 3
 50 fi
 51 }
 52 
 53 case $1 in
 54         -h | --help)HELP;;
 55         *)HELP;;
 56 esac
 
checknum(){
  if [[ "$1" == *[^0-9]* || "$1" != *[0-9]* ]]
  then
    HELP; exit 3
  fi
}

w=0
c=0
while [ $# -gt 0 ]
do
  case $1 in
  -h|--help) VERSION; HELP; exit
  ;;
  -w)
    checknum $2
    w=$2
    shift 2
  ;;
  -c)
    checknum $2
    c=$2
    shift 2
  ;;
  *) HELP; exit 3
  ;;
  esac
done
1 Like

Thx Mr MadeInGermany, was exactly what i need.

I'll learn with this example.

Thx again.