Sponsored Content
Top Forums Shell Programming and Scripting Nagios script to get total and free memory Post 302885442 by berveglieri on Sunday 26th of January 2014 10:10:43 AM
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.
 

10 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

5. 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

6. 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

7. 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

8. 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

9. 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

10. 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
MEMORY_GET_USAGE(3)							 1						       MEMORY_GET_USAGE(3)

memory_get_usage - Returns the amount of memory allocated to PHP

SYNOPSIS
int memory_get_usage ([bool $real_usage = false]) DESCRIPTION
Returns the amount of memory, in bytes, that's currently being allocated to your PHP script. PARAMETERS
o $real_usage - Set this to TRUE to get the real size of memory allocated from system. If not set or FALSE only the memory used by emalloc() is reported. RETURN VALUES
Returns the memory amount in bytes. CHANGELOG
+--------+---------------------------------------------------+ |Version | | | | | | | Description | | | | +--------+---------------------------------------------------+ | 5.2.1 | | | | | | | Compiling with --enable-memory-limit is no | | | longer required for this function to exist. | | | | | 5.2.0 | | | | | | | $real_usage was added. | | | | +--------+---------------------------------------------------+ EXAMPLES
Example #1 A memory_get_usage(3) example <?php // This is only an example, the numbers below will // differ depending on your system echo memory_get_usage() . " "; // 36640 $a = str_repeat("Hello", 4242); echo memory_get_usage() . " "; // 57960 unset($a); echo memory_get_usage() . " "; // 36744 ?> SEE ALSO
memory_get_peak_usage(3), memory_limit. PHP Documentation Group MEMORY_GET_USAGE(3)
All times are GMT -4. The time now is 05:45 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy