Nagios script to get total and free memory


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Nagios script to get total and free memory
# 1  
Old 01-26-2014
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.
Code:
  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.
# 2  
Old 01-26-2014
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.
This User Gave Thanks to zaxxon For This Post:
# 3  
Old 01-26-2014
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.

Code:
#!/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

# 4  
Old 01-27-2014
Code:
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


Last edited by MadeInGermany; 01-27-2014 at 06:00 AM.. Reason: precise [[ ]] expression; exit with too many args
This User Gave Thanks to MadeInGermany For This Post:
# 5  
Old 01-28-2014
Thx Mr MadeInGermany, was exactly what i need.

I'll learn with this example.

Thx again.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Calculate total memory using free -m

Hi I am trying to calculate memory used by Linux System free -m total used free shared buffers cached Mem: 32109 31010 1099 0 3600 7287 -/+ buffers/cache: 20121 11987 Swap: 10239 1282 8957 Now according to my requirement Im calculating memory using below cmd free -m | awk 'NR==3{printf... (2 Replies)
Discussion started by: sam@sam
2 Replies

2. AIX

How much total and free memory I have in my aix 5.3 server?

good morning, how I can know how much total and free memory I have in my AIX 5.3 server, and this is shown in megabytes or gigabytes? Thank you very much. (4 Replies)
Discussion started by: systemoper
4 Replies

3. UNIX for Advanced & Expert Users

Out of Memory error when free memory size is large

I was running a program and it stopped and showed "Out of Memory!". at that time, the RAM used by this process is around 4G and the free memory size of the machine is around 30G. Does anybody know what maybe the reason? this program is written with Perl. the OS of the machine is Solaris U8. And I... (1 Reply)
Discussion started by: lilili07
1 Replies

4. Red Hat

Total Memory

Hi, I have a strange issue where the total memory on the server is showing low. At the moment 8 GB of memory is installed and only 3 GB is showing on the shell prompt. I am using the commands free -m and vmstat to check the memory. Please help me out in identifying the issue. With regards... (3 Replies)
Discussion started by: shabu
3 Replies

5. Solaris

command to get the total disk space (available + free)

is there a command to get the total disk space (available + free) on the solaris server ? thanks (3 Replies)
Discussion started by: sudhiroracle
3 Replies

6. Shell Programming and Scripting

Calculate total space, total used space and total free space in filesystem names matching keyword

Good afternoon! Im new at scripting and Im trying to write a script to calculate total space, total used space and total free space in filesystem names matching a keyword (in this one we will use keyword virginia). Please dont be mean or harsh, like I said Im new and trying my best. Scripting... (4 Replies)
Discussion started by: bigben1220
4 Replies

7. Solaris

how to get the more memory free space (see memory free column)

Hi all, Could please let me know how to get the more memory free space (not added the RAM) in local zone. -bash-3.00# vmstat 2 5 kthr memory page disk faults cpu r b w swap free re mf pi po fr de sr s0 s1 s1 s1 in sy cs us sy... (3 Replies)
Discussion started by: murthy76
3 Replies

8. Solaris

How to find Total and Free Physical Memory and Logical Memory in SOLARIS 9

Hi, Im working on Solaris 9 on SPARC-32 bit running on an Ultra-80, and I have to find out the following:- 1. Total Physical Memory in the system(total RAM). 2. Available Physical Memory(i.e. RAM Usage) 3. Total (Logical) Memory in the system 4. Available (Logical) Memory. I know... (4 Replies)
Discussion started by: 0ktalmagik
4 Replies

9. AIX

How to check free/total Memory in AIX

Friends , i have a question how to check the total memomry and free memory in AIX, We have vmstat ,svmon and topas commands.Which command among the will give the true figure. (6 Replies)
Discussion started by: vimalbhan
6 Replies

10. Filesystems, Disks and Memory

Total Memory/Swap Memory

I need to put a program together to determine the total, available memory and total and available swap on unix machines. I have been searching for weeks and I seem to run into dead ends. Every unix platform I look at has a different way to determine memory info. Any sugggestions or new... (4 Replies)
Discussion started by: ghe1
4 Replies
Login or Register to Ask a Question