Pages

Wednesday, December 16, 2015

Sample script to capture OS level statistics


Requirement:-

To capture OS level all statistics for a particular interval of time with a gap of say X seconds

Inputs:-

Number of times statistics to be captured and number of seconds interval between two consecutive runs for capturing statistics. Script needs to be executed with super user privileges as most command used are allowed only for super user.

# sh osstats.sh
Provide an integer arguments for repetation and sleep time
Usage : osstats.sh 2 60
      Where, 2 -> Number of Iterations

           , 60 -> Wait interval between two interations in seconds

Outputs:-

Separate files starting with name respective command name would be generated in the folder from where script is being executed.

# sh osstats.sh 2 10
Capturing Logs for netstat -n  !!Wed Dec 16 23:22:30 EST 2015 !!
Capturing Logs for netstat -nautp  !!Wed Dec 16 23:22:30 EST 2015 !!
Capturing Logs for ps -Afps -Af  !!Wed Dec 16 23:22:30 EST 2015 !!
Capturing Logs for lsof  !!Wed Dec 16 23:22:31 EST 2015 !!
Capturing Logs for netstat -o  !!Wed Dec 16 23:22:32 EST 2015 !!
Capturing Logs for ps-Amf  !!Wed Dec 16 23:22:32 EST 2015 !!
Capturing Logs for top  !!Wed Dec 16 23:22:32 EST 2015 !!
Capturing Logs for iotop  !!Wed Dec 16 23:22:32 EST 2015 !!
Capturing Logs for ifconfig  !!Wed Dec 16 23:22:33 EST 2015 !!
Capturing iostat for ifconfig  !!Wed Dec 16 23:22:33 EST 2015 !!
!! Done with Run Number 1 going to sleep for 10 seconds!!
Capturing Logs for netstat -n  !!Wed Dec 16 23:22:45 EST 2015 !!
Capturing Logs for netstat -nautp  !!Wed Dec 16 23:22:45 EST 2015 !!
Capturing Logs for ps -Afps -Af  !!Wed Dec 16 23:22:45 EST 2015 !!
Capturing Logs for lsof  !!Wed Dec 16 23:22:45 EST 2015 !!
Capturing Logs for netstat -o  !!Wed Dec 16 23:22:45 EST 2015 !!
Capturing Logs for ps-Amf  !!Wed Dec 16 23:22:45 EST 2015 !!
Capturing Logs for top  !!Wed Dec 16 23:22:45 EST 2015 !!
Capturing Logs for iotop  !!Wed Dec 16 23:22:46 EST 2015 !!
Capturing Logs for ifconfig  !!Wed Dec 16 23:22:46 EST 2015 !!
Capturing iostat for ifconfig  !!Wed Dec 16 23:22:46 EST 2015 !!

!! Done with Run Number 2 going to sleep for 10 seconds!!

Script:-

#!/bin/bash
# To Capture OS statitics usefule for performance level troubleshooting
##
# Variable Initialization
##
dt=`date +%Y-%m-%d-%S-%M-%H`
ScriptDir=/var/log/osstats

## Function to actually capture data in seperate files
function capture()
{
for x in $(echo $num) ; do
  echo "---------------------------------------------------" >> $ScriptDir/netstat-n_output_$dt.log 2>&1
  echo "Capturing Logs for netstat -n  !!"`date`" !!  "
  /bin/netstat -n >> $ScriptDir/netstat-n_output_$dt.log 2>&1
  echo "---------------------------------------------------" >> $ScriptDir/netstat-nautp_$dt.log 2>&1
  echo "Capturing Logs for netstat -nautp  !!"`date`" !!  "
  /bin/netstat -nautp >> $ScriptDir/netstat-nautp_$dt.log 2>&1
  echo "---------------------------------------------------" >> $ScriptDir/ps-Af_$dt.log 2>&1
  echo "Capturing Logs for ps -Afps -Af  !!"`date`" !!  "
  /bin/ps -Af >> $ScriptDir/ps-Af_$dt.log 2>&1
  echo "---------------------------------------------------" >> $ScriptDir/lsof_$dt.log 2>&1
  echo "Capturing Logs for lsof  !!"`date`" !!  "
  /usr/sbin/lsof >> $ScriptDir/lsof_$dt.log 2>&1
  echo "---------------------------------------------------" >> $ScriptDir/netstat-o_$dt.log 2>&1
  echo "Capturing Logs for netstat -o  !!"`date`" !!  "
  /bin/netstat -o >> $ScriptDir/netstat-o_$dt.log 2>&1
  echo "---------------------------------------------------" >> $ScriptDir/ps-Amf_$dt.log 2>&1
  echo "Capturing Logs for ps-Amf  !!"`date`" !!  "
  /bin/ps -Amf  >> $ScriptDir/ps-Amf_$dt.log 2>&1
  echo "---------------------------------------------------" >> $ScriptDir/top-n-1-b_$dt.log 2>&1
  echo "Capturing Logs for top  !!"`date`" !!  "
  /usr/bin/top -n 1 -b >> $ScriptDir/top-n-1-b_$dt.log 2>&1
  echo "---------------------------------------------------" >> $ScriptDir/iotop-n-1-b_$dt.log 2>&1
  echo "Capturing Logs for iotop  !!"`date`" !!  "
  /usr/sbin/iotop -n 1 -b  >> $ScriptDir/iotop-n-1-b_$dt.log 2>&1
  echo "---------------------------------------------------" >> $ScriptDir/ifconfig_output_$dt.log 2>&1
  echo "Capturing Logs for ifconfig  !!"`date`" !!  "
  /sbin/ifconfig >> $ScriptDir/ifconfig_output_$dt.log 2>&1
  echo "---------------------------------------------------" >> $ScriptDir/iostat-xm_output_$dt.log 2>&1
  echo "Capturing iostat for ifconfig  !!"`date`" !!  "
  /usr/bin/iostat -xm 1 3 >> $ScriptDir/iostat-mx_output_$dt.log 2>&1
  echo "!! Done with Run Number $x going to sleep for $seconds seconds!!"
  sleep $seconds
done
}

##
# Logic to check and ensure input arguments requirement
if [ $# -ne 2 ]; then
  echo "Provide an integer arguments for repetation and sleep time"
  echo "Usage : "$0 2 60" "
  echo "      Where, 2 -> Number of Iterations"
  echo "           , 60 -> Wait interval between two interations in seconds"
else
  num=`seq $1`
  seconds=$2
  capture

fi


Script is very basic and can be used with Ansible or setup having multiple Linux OS servers to be checked for performance analysis.

Back To Top

No comments:

Post a Comment