#!/bin/bash
#This is a very simple system monitering script.
#Written by RS-MikeK 05/09/03
#Modified by RS-Nate 06/18/03

#Set the time between snapshots for formating see: man sleep
SLEEP_TIME="1m"

#this sets the number of records we keep
NUM_INTERVALS=60

#The base directory under which to build the directory where snapshots are stored.
# You *must* put a slash at the end.
ROOT_DIR="~/"

# Expand ~ characters
T1=$(echo sa\~a${HOME}a)
T2=$(echo $ROOT_DIR | sed -e $T1)
ROOT_DIR=$T2

if [ ! -d ${ROOT_DIR} ] ; then
        echo $ROOT_DIR is not a directory
        exit 1
fi

if [ ! -w ${ROOT_DIR} ] ; then
        echo $ROOT_DIR is not writable
        exit 1
fi

#makes sure we can create the logs there
[ ! -d ${ROOT_DIR}system-snapshot ] && mkdir ${ROOT_DIR}system-snapshot

#clears variable
current_interval=1
#clear data from previous runs
rm -f ${ROOT_DIR}system-snapshot/*

for ((;;)) ; do
        #Clear the previous log
        [ -e ${ROOT_DIR}system-snapshot/$current_interval.log ] && rm ${ROOT_DIR}system-snapshot/$current_interval.log
        date >> ${ROOT_DIR}system-snapshot/$current_interval.log
        cat /proc/loadavg >> ${ROOT_DIR}system-snapshot/$current_interval.log
        cat /proc/meminfo >> ${ROOT_DIR}system-snapshot/$current_interval.log
        vmstat 1 10 >> ${ROOT_DIR}system-snapshot/$current_interval.log
        ps auwwxf >> ${ROOT_DIR}system-snapshot/$current_interval.log
        netstat -anp >> ${ROOT_DIR}system-snapshot/$current_interval.log

        rm -rf ${ROOT_DIR}system-snapshot/current
        ln -s ${ROOT_DIR}system-snapshot/$current_interval.log ${ROOT_DIR}system-snapshot/current

        sleep $SLEEP_TIME

        let current_interval=$current_interval+1
        [ $current_interval -gt $NUM_INTERVALS ] && current_interval=1
done


