How to check swap space in linux and system’s paging

Shell Scripting

# PLATFORM: AIX, Solaris, HP-UX and Linux Only
#
# PURPOSE: This shell script is used to produce a report of
# the system’s paging or swap space statistics including:
#
# Total paging space in MB, MB of Free paging space,
# MB of Used paging space, % of paging space Used, and
# % of paging space Free
#
# set -x # Uncomment to debug this shell script
# set -n # Uncomment to check command syntax without any execution
#
###########################################################
################ DEFINE VARIABLES HERE ####################
PC_LIMIT=65 # Upper limit of Swap space percentage
# before notification
THISHOST=$(hostname) # Host name of this machine
###########################################################
################ INITIALIZE THE REPORT ####################
echo “Swap Space Report for $THISHOST\n”
date
###########################################################
################ DEFINE FUNCTIONS HERE ####################
function SUN_swap_mon
{
############# CAPTURE AND PROCESS THE DATA ################
# Use two awk statements to extract the $9 and $11 fields
# from the swap -s command output
SW_USED=$(swap -s | awk ‘{print $9}’ | cut -dk -f1)
SW_FREE=$(swap -s | awk ‘{print $11}’ | cut -dk -f1)
# Add SW_USED to SW_FREE to get the total swap space ((SW_TOTAL = SW_USED + SW_FREE))

# Calculate the percent used and percent free using the
# bc utility in a here documentation with command substitution
PERCENT_USED=$(bc <<EOF
scale=4
($SW_USED / $SW_TOTAL) * 100
EOF
)
PERCENT_FREE=$(bc <<EOF
scale=4
($SW_FREE / $SW_TOTAL) * 100
EOF
)
# Convert the KB measurements to MB measurements
((SW_TOTAL_MB = SW_TOTAL / 1000))
((SW_USED_MB = SW_USED / 1000))
((SW_FREE_MB = SW_FREE / 1000))
# Produce the remaining part of the report
echo “Total Amount of Swap Space:${SW_TOTAL_MB}MB”
echo “Total KB of Swap Space Used:${SW_USED_MB}MB”
echo “Total KB of Swap Space Free:${SW_FREE_MB}MB”
echo “Percent of Swap Space Used:${PERCENT_USED}%”
echo “Percent of Swap Space Free:${PERCENT_FREE}%”
# Grab the integer portion of the percent used
INT_PERCENT_USED=$(echo $PERCENT_USED | cut -d. -f1)
# Check to see if the percentage used maximum threshold
# has been exceeded
if (( PC_LIMIT <= INT_PERCENT_USED ))
then
# Percent used has exceeded the threshold, send notification
tput smso # Turn on reverse video!
echo “\n\nWARNING: Swap Space has Exceeded the ${PC_LIMIT}% Upper
Limit!\n”
tput rmso # Turn off reverse video!
fi

echo “\n”
}
###########################################################
function Linux_swap_mon
{
free -m | grep -i swap | while read junk SW_TOTAL SW_USED SW_FREE
do
# Use the bc utility in a here document to calculate
# the percentage of free and used swap space.
PERCENT_USED=$(bc <<EOF
scale=4
($SW_USED / $SW_TOTAL) * 100
EOF
)
PERCENT_FREE=$(bc <<EOF
scale=4
($SW_FREE / $SW_TOTAL) * 100
EOF
)
# Produce the rest of the paging space report:
echo “Total Amount of Swap Space:${SW_TOTAL}MB”
echo “Total KB of Swap Space Used:${SW_USED}MB”
echo “Total KB of Swap Space Free:${SW_FREE}MB”
echo “Percent of Swap Space Used:${PERCENT_USED}%”
echo “Percent of Swap Space Free:${PERCENT_FREE}%”
# Grab the integer portion of the percent used to
# test for the over limit threshold
INT_PERCENT_USED=$(echo $PERCENT_USED | cut -d. -f1)
if (( PC_LIMIT <= INT_PERCENT_USED ))
then
tput smso
echo “\n\nWARNING: Paging Space has Exceeded the \
${PC_LIMIT}% Upper Limit!\n”
tput rmso

fi
done
echo “\n”
}
###########################################################
function HP_UX_swap_mon
{
# Start a while read loop by using the piped in input from
# the swapinfo -tm command output.
swapinfo -tm | grep dev | while read junk SW_TOTAL SW_USED \
SW_FREE PERCENT_USED junk2
do
# Calculate the percentage of free swap space
((PERCENT_FREE = 100 – $(echo $PERCENT_USED | cut -d% -f1) ))
echo “Total Amount of Swap Space:${SW_TOTAL}MB”
echo “Total MB of Swap Space Used:${SW_USED}MB”
echo “Total MB of Swap Space Free:${SW_FREE}MB”
echo “Percent of Swap Space Used:${PERCENT_USED}”
echo “Percent of Swap Space Free:${PERCENT_FREE}%”
# Check for paging space exceeded the predefined limit
if (( PC_LIMIT <= $(echo $PERCENT_USED | cut -d% -f1) ))
then
# Swap space is over the predefined limit, send notification
tput smso # Turn on reverse video!
echo “\n\nWARNING: Swap Space has Exceeded the\
${PC_LIMIT}% Upper Limit!\n”
tput rmso # Turn reverse video off!
fi
done
echo “\n”
}

###########################################################
function AIX_paging_mon
{
################ DEFINE VARIABLES HERE ####################
PAGING_STAT=/tmp/paging_stat.out # Paging Stat hold file
############# CAPTURE AND PROCESS THE DATA ################
# Load the data in a file without the column headings
lsps -s | tail +2 > $PAGING_STAT
# Start a while loop and feed the loop from the bottom using
# the $PAGING_STAT file as redirected input
while read TOTAL PERCENT
do
# Clean up the data by removing the suffixes
PAGING_MB=$(echo $TOTAL | cut -d ‘MB’ -f1)
PAGING_PC=$(echo $PERCENT | cut -d% -f1)
# Calculate the missing data: %Free, MB used and MB free
(( PAGING_PC_FREE = 100 – PAGING_PC ))
(( MB_USED = PAGING_MB * PAGING_PC / 100 ))
(( MB_FREE = PAGING_MB – MB_USED ))
# Produce the rest of the paging space report:
echo “Total MB of Paging Space:$TOTAL”
echo “Total MB of Paging Space Used:${MB_USED}MB”
echo “Total MB of Paging Space Free:${MB_FREE}MB”
echo “Percent of Paging Space Used:${PERCENT}”
echo “Percent of Paging Space Free:${PAGING_PC_FREE}%”
# Check for paging space exceeded the predefined limit
if ((PC_LIMIT <= PAGING_PC))
then
# Paging space is over the limit, send notification
tput smso # Turn on reverse video!
echo “\n\nWARNING: Paging Space has Exceeded the ${PC_LIMIT}%
\
Upper Limit!\n”
tput rmso # Turn off reverse video

fi
done < $PAGING_STAT
rm -f $PAGING_STAT
# Add an extra new line to the output
#echo “\n”
}
###########################################################
################## BEGINNING OF MAIN ######################
###########################################################
# Find the Operating System and execute the correct function
case $(uname) in
AIX) AIX_paging_mon
;;
HP-UX) HP_UX_swap_mon
;;
Linux) Linux_swap_mon
;;
SunOS) SUN_swap_mon
;;
*) echo “\nERROR: Unsupported Operating System…EXITING…\n”
exit 1
;;
esac

Example Output:

“Swap Space Report for localhost.localdomainn”
Sun Jul 15 04:58:13 IST 2012
“Total Amount of Swap Space:3519MB”
“Total KB of Swap Space Used:0MB”
“Total KB of Swap Space Free:3519MB”
“Percent of Swap Space Used:0%”
“Percent of Swap Space Free:100.0000%”

 

In case of any ©Copyright or missing credits issue please check CopyRights page for faster resolutions.

3 Responses

  1. Jaquice Stone says:

    Hi Ramakanta. I’m looking at your paging script and trying to run this on AIX 7.1 but keep getting the error below.
    Is there any modifications that needs to be done from different versions of AIX.
    Thank You.

    AIX_paging_mon[16]: 1024MB: bad number

    • Ramakanta says:

      Try using below:

      #/bin/ksh
      echo -e “—————- IBM-AIX SYSTEM INFORMATION ————————-” >> /tmp/info.tmp.01.$$$
      echo -e “HOSTNAME: $HOSTNAME” >> /tmp/info.tmp.01.$$$
      user=`who | wc -l`
      echo -e “User name: $USER (Login name: $LOGNAME)” >> /tmp/info.tmp.01.$$$
      echo -e “Current Shell: $SHELL” >> /tmp/info.tmp.01.$$$
      echo -e “Home Directory: $HOME” >> /tmp/info.tmp.01.$$$
      echo -e “Your O/s Type: $OSTYPE” >> /tmp/info.tmp.01.$$$
      echo -e “PATH: $PATH” >> /tmp/info.tmp.01.$$$
      echo -e “Current directory: `pwd`” >> /tmp/info.tmp.01.$$$
      echo -e “Current date: `date`” >> /tmp/info.tmp.01.$$$
      echo -e “Currently Logged user(s): `w` ” >> /tmp/info.tmp.01.$$$
      echo -e “——————————————————————–” >> /tmp/info.tmp.01.$$$

      echo -e “—————- OS INFO ————————-” >> /tmp/info.tmp.01.$$$

      echo -e “OS Info: `uname -a`” >> /tmp/info.tmp.01.$$$
      echo -e “HARDWARE_BITMODE: `getconf HARDWARE_BITMODE`” >> /tmp/info.tmp.01.$$$
      echo -e “KERNEL_BITMODE: `getconf KERNEL_BITMODE`” >> /tmp/info.tmp.01.$$$
      echo -e “CPU/KERNEL BIT: `prtconf|grep 64`” >> /tmp/info.tmp.01.$$$
      echo -e “——————————————————————–” >> /tmp/info.tmp.01.$$$
      echo -e “———————– File Descriptors —————————” >> /tmp/info.tmp.01.$$$
      echo -e “Ulimit(HARD) Settings: `ulimit -aH`” >> /tmp/info.tmp.01.$$$
      echo -e ” ” >> /tmp/info.tmp.01.$$$
      echo -e “Ulimit(SOFT) Settings: `ulimit -aS`” >> /tmp/info.tmp.01.$$$

      echo -e “——————————————————————–” >> /tmp/info.tmp.01.$$$
      echo -e “Disk Space: `df -k`” >> /tmp/info.tmp.01.$$$
      if [ -f /etc/shells ]
      then
      echo -e “Available Shells: ” >> /tmp/info.tmp.01.$$$
      echo -e “`cat /etc/shells`” >> /tmp/info.tmp.01.$$$
      fi

      if [ -f /etc/filesystems ]
      then
      echo -e “File Systems: ” >> /tmp/info.tmp.01.$$$
      echo -e “`cat /etc/filesystems`” >> /tmp/info.tmp.01.$$$
      fi

      echo -e “——————————————————————–” >> /tmp/info.tmp.01.$$$
      echo -e “Computer CPU Information:” >> /tmp/info.tmp.01.$$$
      echo -e “——————————————————————–” >> /tmp/info.tmp.01.$$$
      prtconf |grep -i processor >> /tmp/info.tmp.01.$$$
      echo -e “SwapMemory: `swap -s`” >> /tmp/info.tmp.01.$$$
      vmstat 2 5 >> /tmp/info.tmp.01.$$$
      echo -e “——————————————————————–” >> /tmp/info.tmp.01.$$$
      echo -e “——————- PAGESIZE INFORMATION ” >> /tmp/info.tmp.01.$$$
      pagesize >> /tmp/info.tmp.01.$$$
      echo -e “——————————————————————–” >> /tmp/info.tmp.01.$$$
      echo -e “——————- Process Information ————————–” >> /tmp/info.tmp.01.$$$
      ps -ef >> /tmp/info.tmp.01.$$$
      echo -e “——————- REAL MEM Information ————————–” >> /tmp/info.tmp.01.$$$
      lsattr -El sys0 -a realmem >> /tmp/info.tmp.01.$$$
      #topas -P 1 2 >> /tmp/info.tmp.01.$$$
      echo -e “——————- Netstat Information ————————–” >> /tmp/info.tmp.01.$$$
      netstat -an >>/tmp/info.tmp.01.$$$
      echo -e “——————————————————————–” >> /tmp/info.tmp.01.$$$
      echo -e “——————————————————————–” >> /tmp/info.tmp.01.$$$
      echo -e “——————- DISK INFORMATION ————————–” >> /tmp/info.tmp.01.$$$
      lsdev -Cc disk >> /tmp/info.tmp.01.$$$
      echo -e “——————————————————————–” >> /tmp/info.tmp.01.$$$
      echo -e “——————- DEVICES INFORMATION ————————-” >> /tmp/info.tmp.01.$$$
      lsdev >> /tmp/info.tmp.01.$$$
      echo -e “——————————————————————–” >> /tmp/info.tmp.01.$$$
      echo -e “File System (Mount):” >> /tmp/info.tmp.01.$$$
      echo -e “——————————————————————–” >> /tmp/info.tmp.01.$$$
      mount >> /tmp/info.tmp.01.$$$
      echo -e “——————————————————————–” >> /tmp/info.tmp.01.$$$
      echo -e “————– set command information ————————-” >> /tmp/info.tmp.01.$$$
      set >> /tmp/info.tmp.01.$$$
      cat /tmp/info.tmp.01.$$$ |more

  2. Vijay S says:

    Very Superb Ram. Thank You so much.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.