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?
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
[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
[casupport@wycvlapph033 ~]$ free -m | grep ^-/+ | awk '{print $3}'
7281
[casupport@wycvlapph033 ~]$ free -m | grep ^-/+ | awk '{print $4}'
8757
[casupport@wycvlapph033 ~]$
Cheers
Alex
RudiC
August 8, 2017, 6:54am
2
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.
Hi RudiC
Please see below the whole script
###########################################################################################
## 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
RudiC
August 8, 2017, 7:40am
4
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.
Hi RudiC
I have only been able to spot one,
There should have been a $ infront of the mem_buff
RudiC
August 8, 2017, 8:41am
6
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
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 ...
1 Like
Hi RudiC
Thank you for your reply and your help with this.
Just a quick question regarding the final part of this 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
RudiC
August 8, 2017, 10:37am
8
We are subtracting field 7 ("available") from field 2 ("total"), as you do in your script above.
The OFMT
is an awk
variable defining the "format for printing numbers; initially = "%.6g"." ( man awk
), and yes, we use it for the percentage output.
1 Like