Working out percentage of memory utilization


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Working out percentage of memory utilization
# 1  
Old 08-08-2017
Working out percentage of memory utilization

Hi there

I am middle of writing a script to allow me to see the % of the memory which is being used.

When I submit my script I don't get any errors but I just get a blank output.

Any pointers?

Code:
available=`free -m | grep available`

if [ "$available"="$new_linux_version_available" ]
then

mem_utalization=`sum=$(($mem_total/($mem_total - $mem_available) * 100))`

else
mem_buff_cache_used=`free -m  | grep ^-/+ | awk  '{print $3}'`
mem_utalization=`sum=$(((mem_buff_cache_used/($mem_buff_cache_used + $mem_buff_cache_free)) * 100))`

fi

echo $mem_utalization
echo $mem_buff_cache_used
echo $mem_buff_cache_free

Code:
[casupport@wycvlapph033 test]$ ./free_memory_script.sh



[casupport@wycvlapph033 test]$

No error message or anything comes back? All the variables work as they work in the command line

Code:
[casupport@wycvlapph033 ~]$ free -m  | grep ^-/+ | awk  '{print $3}'
7281
[casupport@wycvlapph033 ~]$ free -m  | grep ^-/+ | awk  '{print $4}'
8757
[casupport@wycvlapph033 ~]$

Cheers
Alex
# 2  
Old 08-08-2017
Is that the entire script? On top of variables used for calculations being undefined, I see syntactical errors as well as questionable formulae yielding values > 100% for e.g. memory utilization.
# 3  
Old 08-08-2017
Hi RudiC

Please see below the whole script

Code:
###########################################################################################
## VARIABLES
###########################################################################################
 new_linux_version_available="available"
new_linux_version_buffcache="buff/cache"
 ###########################################################################################
## Memory - TOTAL
###########################################################################################
mem_total=`free -m  | grep ^Mem | awk '{print $2}'`
 #echo $mem_total
 ###########################################################################################
## Memory - USED
###########################################################################################
mem_used=`free -m  | grep ^Mem | awk '{print $3}'`
 #echo $mem_used
 ###########################################################################################
## Memory - FREE
###########################################################################################
mem_free=`free -m  | grep ^Mem | awk '{print $4}'`
 #echo $mem_free
 ###########################################################################################
## Memory - BUFFER+CACHE
###########################################################################################
buff_cache=`free -m | grep buff/cache`
 if [ "$buff_cache"="$new_linux_version_buffcache" ]
then
 sum=`free -m  | grep ^Mem | awk '{print $6}'`
else
 mem_buffer=`free -m  | grep ^Mem | awk '{print $6}'`
mem_cache=`free -m  | grep ^Mem | awk '{print $7}'`
 sum=$(($mem_buffer + $mem_cache))
 fi
 #echo $sum
 ###########################################################################################
## Memory - AVAILABLE
###########################################################################################
available=`free -m | grep available`
 if [ "$available"="$new_linux_version_available" ]
then
 mem_available=`free -m  | grep ^Mem | awk '{print $7}'`
else
mem_buff_cache_free=`free -m  | grep ^-/+ | awk  '{print $4}'`
mem_available=`sum=$(($mem_buff_cache_free + $mem_free))`
 fi
 #echo $mem_available
###########################################################################################
## Memory - UTILIZATION
###########################################################################################
available=`free -m | grep available`
 if [ "$available"="$new_linux_version_available" ]
then
 mem_utalization=`sum=$(($mem_total/($mem_total - $mem_available) * 100))`
 else
mem_buff_cache_used=`free -m  | grep ^-/+ | awk  '{print $3}'`
mem_utalization=`sum=$(((mem_buff_cache_used/($mem_buff_cache_used + $mem_buff_cache_free)) * 100))`
 fi
 echo $mem_utalization
echo $mem_buff_cache_used
echo $mem_buff_cache_free

# 4  
Old 08-08-2017
Have you been able to identify the syntactical errors and the senseless formulae?

EDIT: I have to correct myself - those are not syntactical erors, but semantical / logical ones.

Last edited by RudiC; 08-08-2017 at 08:49 AM..
# 5  
Old 08-08-2017
Hi RudiC

I have only been able to spot one,

Quote:
mem_utalization=`sum=$(((mem_buff_cache_used/($mem_buff_cache_used + $mem_buff_cache_free)) * 100))`
There should have been a $ infront of the mem_buff
# 6  
Old 08-08-2017
In principle, yes. However, inside the ((...)) ("arithmetic expansion") you don't need the $ , see e.g. man bash.

But, you have picked the perfect line to illustrate what I was talking about:
To produce a value to be assigned to mem_utalization, you use (the deprecated `...` version of) "command substitution" which is the standard output of the command. Your command is a variable assignment (sum=...). What is the stdout of a variable assignment? Nothing! So, mem_utalization is empty.
The next one is $mem_total/($mem_total - $mem_available) which will result in a value above 100% given all values used are positive. Is that what you want?

In your above script you're running free, grep, and awk umpteen times, each causing a (costly) process creation - why not do it all in a single pass? Try
Code:
free | awk '/^Mem/ {print "Total:", $2, ", free:", $4, ", used:", $3, ", something:", ($2-$7)/$2*100, "%"}' OFMT="%.2f"
Total: 1918976 , free: 100452 , used: 955060 , something: 63.88 %

without assessing your interpretation of free's numbers / columns. You can even have the distinction between various linux versions done in that one small awk script ...
This User Gave Thanks to RudiC For This Post:
# 7  
Old 08-08-2017
Hi RudiC

Thank you for your reply and your help with this.

Just a quick question regarding the final part of this code:

Code:
($2-$7)/$2*100, "%"}' OFMT="%.2f"

($2 - $7) are we taking the second variable away from the seventh?

And the final part I don't quite understand is that two decimal places?

Cheers
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Disk Space Utilization in HTML format working in one environment and not working on the other

Hi Team, I have written the shell script which returns the result of the disk space filesystems which has crossed the threshold limit in HTML Format. Below mentioned is the script which worked perfectly on QA system. df -h | awk -v host=`hostname` ' BEGIN { print "<table border="4"... (13 Replies)
Discussion started by: Harihsun
13 Replies

2. UNIX for Beginners Questions & Answers

Working out percentage in shell script

Hi All, I currently have a shell script which is pulling multiple counters from various sources. Due to the counters being cumulative counters I've got some code to work out the delta from the last reading and current which is working fine. The problem i have now is being able to work out the... (8 Replies)
Discussion started by: mutley2202
8 Replies

3. AIX

Need help on memory utilization.

I have run the utility nmon in aix 6.1, and found memory utilization is 99.9% in physical. and pressed h key and then t , in that it is not showing single process which is consuming memory resources. please help me how to find out actual memory utilization. wheather 99% is real memory... (1 Reply)
Discussion started by: manoj.solaris
1 Replies

4. Red Hat

Incorrect Percentage for Memory Utilization.

I am running the command /usr/bin/free -b | awk '/Mem:|cache:/ {print $2,$3}' | sed 'N;s/\n/ /' | awk '{print ($4*100)/$1}' to find out Memory utilization, it is showing 205%, please suggest me what changes are required, below is output of free -b command. $ free -b total ... (2 Replies)
Discussion started by: manoj.solaris
2 Replies

5. Red Hat

CPU Utilization and Memory Utilization of Services and Applications

Hi, i am new to linux/RHEL 6.0 and i have two questions. 1) How to get the CPU utilization and Memory Utilization of all Services running currently? 2) How to get the CPU utilization and Memory Utilization of all Applications running currently? Please help me to find the script. ... (2 Replies)
Discussion started by: nossam
2 Replies

6. Solaris

[DOUBT] Memory high in idle process on Solaris 10 (Memory Utilization > 90%)

Hi Experts, Our servers running Solaris 10 with SAP Application. The memory utilization always >90%, but the process on SAP is too less even nothing. Why memory utilization on solaris always looks high? I have statement about memory on solaris, is this true: Memory in solaris is used for... (4 Replies)
Discussion started by: edydsuranta
4 Replies

7. HP-UX

memory utilization

command for checking memory utilization in HP -UX (2 Replies)
Discussion started by: tushar_spatil
2 Replies

8. Shell Programming and Scripting

Calculating Swap percentage utilization

Hello All, I am trying to create a script that will give me the processes that consume swap in %. i am using the below line to get it done. virtual=`echo "$virtual/$swp*100"|bc -l|sed -e "s/\(\.\).*/\1/g"` but getting the following output after running it. .039 .110 I want the... (3 Replies)
Discussion started by: ajaincv
3 Replies

9. UNIX for Dummies Questions & Answers

Working out the percentage between two values

Hi there, I am totally new to Unix, I am trying to work out the percentage between two values in a ksh shell script and assign the result to a variable. Value1=577 Values2=244 So the calculation would be as follows: ((Value1 - Value2) / Value1) * 100 How would I be able to achieve... (2 Replies)
Discussion started by: NextLevelAndi
2 Replies

10. HP-UX

How to determine cpu&memory percentage usage per user

Using HP-UX v11 Need to monitor cpu and memory usage, total for system and separately for each user in command-line mode. Found out next ways to monitor total cpu usage under hp-ux: 1) vmstat, also shows free memory 2) sar -M ps -eo user,pcpu - does not work, means 'user-defined format'... (4 Replies)
Discussion started by: hp-ux-user
4 Replies
Login or Register to Ask a Question