#!/bin/sh

# Unix flight recorder

# Purpose: provide sys administration data about subsystem
# status/performance and running processes to supplement sar
# in monitoring performance
#
# by Aleksey Tsalolikhin <alex@lifesurvives.com>, Sep 2007
# feedback welcome


# with TTL 300 seconds, add a cron jobs like this to
# collect 5 minutes of data:
#
# 0,5,10,15,20,25,30,35,40,45,50,55 * * * * ufr.sh

# and something like the below to clean out the log files every 4 days

# Clean up UFR logs  -- HP Unix version
# 0 3 * * * nice find /var/log/ufr/*   -type d -a -mtime 4 -exec rm -rf {} \; 2>/dev/null

# Clean up UFR logs  -- Linux version (uses GNU find)
# 0 3 * * * nice find /var/log/ufr/*/* -maxdepth 0 -mtime +5 -exec rm -r {} \; >/dev/null 2>/dev/null


TTL=300 	# how many seconds we should run
INTERVAL=1	# how often to sample, used as argument
		# to vmstat, etc.

DATADEPOT=/var/log/ufr
MONTHYEAR=`date +%h%Y`	# e.g. Sep2007
DATE=`date +%d` 	# date of the month 
TIMESTAMP=`date +%H:%M`

DATADIR=$DATADEPOT/$MONTHYEAR/$DATE/$TIMESTAMP

mkdir -p $DATADIR || echo failed to mkdir $DATADIR



OPERATING_SYSTEM=`uname -s`

# set up the arguments for "top", which is different
# from OS to OS

# we'll run top every 10 seconds instead of every second to
# reduce load on the OS
TTL_OVER_10=`echo $TTL/10|bc`
if [ $OPERATING_SYSTEM = "Linux" ] ;
then TOP_ARGS=" -b -d 10 -n $TTL_OVER_10"
elif [ $OPERATING_SYSTEM = "HP-UX" ]
then TOP_ARGS=" -s 10 -d $TTL_OVER_10"
fi


vmstat $INTERVAL $TTL > $DATADIR/vmstat.$INTERVAL.$TTL &
iostat -x $INTERVAL $TTL > $DATADIR/iostat.$INTERVAL.$TTL &
mpstat $INTERVAL $TTL > $DATADIR/mpstat.$INTERVAL.$TTL &
ps auwx > $DATADIR/ps &
who > $DATADIR/who &
top $TOP_ARGS > $DATADIR/top &
free > $DATADIR/free

