#!/bin/ksh

# Any DB2 memory swapped out to paging space reduces performance, but there's no built-in way of seeing what is swapped out at AIX 5.3:
# this script joins the outputs from the ps vg and ps -eaf commands to identify the highest consumers of paging space.
# Initiate at 00:05 and snapshot every n minutes until midnight.
      #  ps vg | head
      #     PID    TTY STAT  TIME PGIN  SIZE   RSS   LIM  TSIZ   TRS %CPU %MEM COMMAND
      # PGIN is the closest indicator of paging, SIZE is (more or less) the data size - we assume the program code itself is likely cached.

      # ps -eaf | head
      #     UID     PID    PPID   C    STIME    TTY  TIME CMD
# Alex Levy, http://www.sustainablesoftware.net 14.09.2010

> top_pagers
today=$(date | awk '{print $1}') 
day=$(date | awk '{print $1}')

while [[ $day == $today ]]
do
      ps vg | awk '{print $1,$5,$6}' |grep -v PID > a                     #strip header, capture PID, PGIN and SIZE
      ps -eaf | awk '{print $2, $1, $8, $9, $10}' | grep -v UID > b #strip header, capture PID, UID, CMD and arguments if any
      sort a > a.sort         # sort both files on PID
      sort b > b.sort   
      join -j 1 -o 1.1 2.2 1.2 1.3 2.3 2.4 2.5 a.sort b.sort > c # join on PID
      sort -nr +1 -2 -t\t c > c.sort # sort by highest PGIN
      date >> top_pagers
      lsps -a >> top_pagers
      print "PID \tUID \tPGIN \tSIZE \tCOMMAND" >> top_pagers
      head -10 c.sort >> top_pagers
      print "\n" >> top_pagers
      sleep 300
      day=$(date | awk '{print $1}')
done
