Memory

Instant Messages About Memory Usage

A shell script that reports current memory utilization

An easy way to review memory utilization at any moment is with the free command. For example:

free -m
              total        used        free      shared  buff/cache   available
Mem:            989         207         177          12         604         621
Swap:          1023           7        1016

There are nine values shown. All we need to do is pivot them so that they display nicely in a vertical format.

#!/bin/bash
. /etc/profile
. /root/.profile

HN=`hostname`
MEM1=`free -m |awk "NR==2"|awk '{print $2}'`
MEM2=`free -m |awk "NR==2"|awk '{print $3}'`
MEM3=`free -m |awk "NR==2"|awk '{print $4}'`
MEM4=`free -m |awk "NR==2"|awk '{print $5}'`
MEM5=`free -m |awk "NR==2"|awk '{print $6}'`
MEM6=`free -m |awk "NR==2"|awk '{print $7}'`
MEM7=`free -m |awk "NR==3"|awk '{print $2}'`
MEM8=`free -m |awk "NR==3"|awk '{print $3}'`
MEM9=`free -m |awk "NR==3"|awk '{print $4}'`

KK=`printf "<KEY> Memory Report $HN
Total Memory: $MEM1 MB
Used Memory: $MEM2 MB
Free Memory: $MEM3 MB
Shared Memory: $MEM4 MB
Buffer Cache: $MEM5 MB
Available Memory: $MEM6 MB
Total Swap: $MEM7 MB
Used Swap: $MEM8 MB
Free Swap: $MEM9 MB"`

if [ -z "$KK" ]; then
  echo "free command failed?"
else
  /usr/local/bin/monitor.chat.sh "$KK"
  echo "Memory for $HN reported."
fi

Now keeping an eye on memory utilization is as easy as opening your mobile phone!

Last modified November 9, 2020