Updated: Corrected grep syntax problem.
Hello.
Nothing much to do today other than completing some pending documentations for my junior analysts, so I’ve decided to write a helper script to kill some time.
This bash script will display top ten processes which utilizing swap filesystem. It could be very useful if you want to troubleshoot any swap related issue. I have tested it on Oracle Enterprise Linux and it should be working fine on any Redhat based distribution.
#!/bin/bash # GSS Linux: Script to find Top 20 processes utilizing swap filesystem. # Author: Azwan_Ngali (azwan.ngali[AT]gmail.com) # Linux blusmurf 2.6.32-042stab081.3 #1 SMP Mon Sep 9 20:07:47 MSK 2013 i686 SWAPFREE=$(cat /proc/meminfo | grep SwapFree: | awk -F " " '{print $2}') SWAPTOTAL=$(cat /proc/meminfo | grep SwapTotal: | awk -F " " '{print $2}') SWAPUSAGE=$(echo "$SWAPTOTAL - $SWAPFREE" | bc) SWAPPERCENTAGE=$(echo "scale=3;($SWAPUSAGE / $SWAPTOTAL) * 100" | bc) COUNTER=0 printf "\n" printf "Displaying Top 20 processes which utilization swap filesystem \n" printf "Current swap utilization (kB): $SWAPFREE / $SWAPTOTAL ($SWAPPERCENTAGE%%)\n" printf "\n" printf "%-25s %-25s %-25s\n" "PID" "Swap Utilization (kB)" "Process Name" printf "%1s\n" "------------------------------------------------------------------------------------------------------------------------" # Send ps ax output to temporary text file. ps ax | awk '{print $1 " " $5}' > /tmp/psax_temp.txt for x in $(grep Swap /proc/[1-9]*/smaps | grep -v '\W0 kB' | tr -s ' ' | cut -d' ' -f-2 | sort -t' ' -k2 -n | tr -d ' ' | tail -20); do SWAPUSAGE=$(echo $x | cut -d: -f3) PID=$(echo $x | cut -d/ -f3) PROCNAME=$(grep -w $PID /tmp/psax_temp.txt | awk '{print $2}') printf "%-25s %-25s %-25s\n" $PID $SWAPUSAGE $PROCNAME COUNTER=$((COUNTER+1)) done # No process found. if [ $COUNTER -eq 0]; then printf "No process found\n" fi # Remove temp file. rm -f /tmp/psax_temp.txt
Note 1: Processes may appear multiple times if they have multiple memory regions swapped (and these are large)
Note 2: If the above script produces no output, then it could be that none of the currently running processes in /proc/*/smaps are using swap.
You can test that by simply running:
grep Swap /proc/[1-9]*/smaps | grep -v '\W0 kB'
Important thing to keep in mind is that the aforementioned script will only show the active processes that have memory swapped at that point in time, meaning at the time when the script was run. It might be quite possible that the system has already swapped a chunk of memory and that is visible on free output but the script shows no output. Point being, this script shows the current swapping activity and cannot be used for historical data gathering. For that purpose, sar can be used.
Output example
[root@oracle healthcheck]# ./swap.sh Displaying Top 20 processes which utilization swap filesystem Current swap utilization (kB): 7247736 / 16778232 (56.800%) PID Swap Utilization (kB) Process Name ------------------------------------------------------------------------------------------------------------------------ 22640 15420 /stornext/snfs1/eradmin/ggs/misc_oc10.2_11gV4/replicat 22440 15528 /stornext/snfs1/eradmin/ggs/misc_oc11.1_11gV4/extract 31065 16208 /stornext/snfs1/eradmin/ggs/siebeldwngrd_oc11.2_11gV4/replicat 3004 17372 /stornext/snfs1/eradmin/ggs/misc_oc10.2_11gV4/extract 17756 17560 /stornext/snfs1/eradmin/ggs/misc_tc14.0_11gV1_2/replicat 4453 18148 /stornext/snfs1/eradmin/ggs/misc_tc14.0_11gV1_1/replicat 668 19436 /stornext/snfs1/eradmin/ggs/misc_oc10.2_11gV4/extract 4454 22516 /stornext/snfs1/eradmin/ggs/misc_tc14.0_11gV1_1/replicat 9052 23292 /opt/dell/srvadmin/sbin/dsm_sa_datamgrd 4450 28996 /stornext/snfs1/eradmin/ggs/misc_tc14.0_11gV1_1/replicat 4460 33824 /stornext/snfs1/eradmin/ggs/misc_tc14.0_11gV1_1/replicat 17754 38468 /stornext/snfs1/eradmin/ggs/misc_tc14.0_11gV1_2/replicat 30233 42912 /stornext/snfs1/eradmin/ggs/misc_tc14.0_11gV1_2/replicat 4449 43308 /stornext/snfs1/eradmin/ggs/misc_tc14.0_11gV1_1/replicat 9052 63488 /opt/dell/srvadmin/sbin/dsm_sa_datamgrd 9052 63488 /opt/dell/srvadmin/sbin/dsm_sa_datamgrd 9052 65536 /opt/dell/srvadmin/sbin/dsm_sa_datamgrd 9052 65536 /opt/dell/srvadmin/sbin/dsm_sa_datamgrd 9052 65536 /opt/dell/srvadmin/sbin/dsm_sa_datamgrd 4467 4717844 /stornext/snfs1/eradmin/ggs/misc_tc14.0_11gV1_1/replicat [root@oracle healthcheck]#
Toodles.
Be First to Comment