#!/bin/ksh
#
# $Revision: 1.6 $ $Date: 2011-04-24 18:21:12-04 $
# $Source: /doc/sitelog/bk002-centos/perflog/RCS/perflog,v $
# $Host: sys8.com $
# $UUID: 163d6d3d-f680-3541-a2d8-f09a449ebbdc $
#
# NAME:
#    perflog
#
# SYNOPSIS:
#    perflog [-hv] dir
#
# DESCRIPTION:
#    Performance log intended to be run from cron.
#
# OPTIONS:
#    -h, --help      print usage information
#    -v, --version   print the version and exit
#
#    dir   is the output directory for logfiles.
#
# INSTALL:
#    root# mkdir /var/log/perflog
#    root# chown adm:mis /var/log/perflog
#    root# chmod 2770 /var/log/perflog
#
#    Then install perflog script and modify adm's crontab.
#
# SAMPLE CRON ENTRY:
#    */10   *   *   *   *   /path/to/perflog /var/log/perflog
#
# AUTHOR:
#    Option-handling based on FSF configure scripts.
#
#    Karl Vogel <vogelke+unix@pobox.com>
#    Sumaria Systems, Inc.

PATH=/bin:/sbin:/usr/sbin:/usr/bin:/usr/local/libexec:/usr/local/bin
BLOCKSIZE=1m
export PATH BLOCKSIZE
umask 027
tag=${0##*/}

# ======================== FUNCTIONS =============================
#
# die: prints an optional argument to the syslog and exits.
#    A common use for "die" is with a test:
#         test -f /etc/passwd || die "no passwd file"
#    This works in subshells and loops, but may not exit with
#    a code other than 0.

logmsg () {
    logger -t $tag "$*"
}

die () {
    logmsg "error: $*"
    exit 1
}

# usage: prints an optional string plus part of the comment
#   header (if any) to stderr, and exits with code 1.

usage () {
    lines=$(egrep -n '^# (NAME|AUTHOR)' $0 | sed -e 's/:.*//')

    (
        case "$#" in
            0)  ;;
            *)  echo "usage error: $*"; echo ;;
        esac

        case "$lines" in
            "") ;;

            *)  set $(echo $lines | sed -e 's/ /,/')
                sed -n ${1}p $0 | sed -e 's/^#//g' |
                    egrep -v AUTHOR:
                ;;
        esac
    ) 1>&2

    exit 1
}

# version: prints the current version to stdout.

version () {
    lsedscr='s/RCSfile: //
    s/.Date: //
    s/,v . .Revision: /  v/
    s/\$//g'

    lrevno='$RCSfile: perflog,v $ $Revision: 1.6 $'
    lrevdate='$Date: 2011-04-24 18:21:12-04 $'
    echo "$lrevno $lrevdate" | sed -e "$lsedscr"
    exit 0
}

# ======================== MAIN PROGRAM ==========================

ac_invalid="invalid option; use --help to show usage"

for ac_option
do
    # main switch
    case "$ac_option" in
        -h | -help | --help | --hel | --he)
            usage ;;
    
        -v | -version | --version | --versio |\
        --versi | --vers)
            version ;;
    
        -*) die "$ac_option: $ac_invalid" ;;
        *)  argv="$argv $ac_option" ;;
    esac
done

#
# Real work starts here.  Sanity check on arguments.
#

set X $argv; shift
case "$#" in
    0)  usage "not enough arguments" ;;
    1)  dir="$1" ;;
    *)  usage "too many arguments" ;;
esac

test -d "$dir" || usage "arg must be a directory"

#
# Take measurements every 30 seconds for just under one hour;
# we want to make sure the script finishes before being
# invoked again by cron.
#

interval=30
count=118   # (3600 seconds / 30 second intervals) minus slop.

#
# If a "stop" file is present, bail out of the script
# so it can be safely modified and restarted.
#

test -f "$dir/stop" && die "stop file found"

#
# Collect {io,vm,mp}stat info hourly, the rest at 10-min intervals.
#

set X $(date "+%Y/%m%d/%H%M"); shift
current=$1

cd $dir
test -d $current || mkdir -p $current
test -d $current || die "$current: cannot create"
cd $current || die "$current: cannot cd"

if pgrep -u adm iostat > /dev/null
then
    true
else
    # I/O stats.
    iostat -xz $interval $count > iostat &

    # Memory stats.
    vmstat $interval $count > vmstat &

    # CPU stats.
    mpstat -A $interval $count > mpstat &
fi

# Network setup.
ifconfig -a > ifconfig
(ping -c5 rc17; echo; ping -c5 fs002) > ping
netstat -i | egrep -v '^lo|^Kernel' > netstat

# Process table.
ps ax -o user,pid,pgid,class,pri,rss,time,args > ps

# Disk stats.
( df; echo; df -i ) |
  awk '{printf "%-12s %9s %6s %9s %5s  %s\n", $1,$2,$3,$4,$5,$6}' > df

# Swap information.
cat /proc/swaps > swap

# Filename cache.
vmstat -s > cache

# Memory.
cat /proc/meminfo > meminfo

# Uptime and load.
uptime > uptime
uname -a > uname

# RAID status.
( grep . /sys/block/md?/md/mismatch_cnt; echo; cat /proc/mdstat ) > raid

# Wait for backgrounded jobs.
wait
exit 0
