#!/bin/ksh # Save ten samples of top to a database, this assumes the database file exists. set -u # Exit on undefined variables CmdName=`basename $0` # Get the name of the command function Usage { echo "$CmdName " 1>&2 } Interval=10 # 10 second interval LnxTopCmd="top -b -n 1 | head -15" # One cycle of the top command taking only the details of the 15 top processes LnxLoad="uptime | sed -e 's/.*: //' -e 's/,.*//" MacTopCmd="top -o cpu -l 2 -n 15 | tail -17" # Two cycles, take the second one, which will show the top 15 processes MacLoad="uptime | awk '{print \$10}'" OS=`uname -s` if [ `uname -s` == "Darwin" ] then LoadCmd=$MacLoad TopCmd=$MacTopCmd elif [ `uname -s` == "Linux" ] then LoadCmd=$LnxLoad TopCmd=$LnxTopCmd else OS="Unknown" fi if [ $OS == "Unknown" ] then echo "Cannot determine the OS" 1>&2 else if [ $# -ne 1 ] then Usage else SQLiteFile=$1 if [ ! -f $SQLiteFile ] then echo "$CmdName: The SQLite database file, $SQLiteFile, must exist" 1>&2 else Count=0 SQLiteErr="FALSE" while [ $Count -lt 10 -a $SQLiteErr == "FALSE" ] do HostName=`uname -n | sed 's/\..*//'` DateAndTime=`date +"%Y-%m-%d %H:%M:%S"` # Current date and time in SQL format Load=`eval $LoadCmd` # Extract the current load from the uptime command TopProcs=`eval $TopCmd` echo INSERT INTO savetop VALUES \(\'$HostName\',\'$DateAndTime\',\'$Load\',\'"$TopProcs"\'\) \; | sqlite3 $SQLiteFile if [ $? -ne 0 ] then echo "SQLite gave an error, exiting" 1>&2 SQLiteErr="TRUE" else sleep $Interval Count=$((Count+1)) fi done fi fi fi ################################################################################ # Change log # # $Log: savetop.ksh,v $ # Revision 1.1 2012/09/03 22:08:06 Philip Kearns # Initial revision # #