#! /bin/sh


##########################################################################################################################################
#                                                                                                                                        #
#       AUTHER  : MAHENDRA B. PATIL                                                                                                      #
#       DATE    : 03-DEC-2012                                                                                                            #
#       PURPOSE : To monitor system parameters				                                                                 #
#		  Load Avarage	(i.e. current , 5 mins before , 10 mins before)								 #
#		  Database Connections (i.e Connections at 3306 port)									 #
#		  Total process count													 #
#		  Total Http Connections												 #
#		  NW Bandwidth at Specified Port (i.e. KB_transfer per sec & KB_received per sec)					 #
#		  CPU utilization % (%user , %system , iowait)										 #
#		  Memory utilization In MB (used , free , cached)									 #
#		  Swap utilization In KB (used)												 #
#		  Mod jk connection (i.e. Total , at 8009 , at 9009 , at 10009 , at 11009 )						 #
#		  Jboss Server Status (i.e node1 , node2 , node3 , node4)								 #
#																	 #
#																	 #
#	Note    : some parameter monitoring requires sysstat (i.e. sar ,  mpstat , iostat command)					 #
#		  Keep these sysstat commands at /usr/local/bin/									 #
#		  Use atleast sysstat version 10.0.5											 #
#																	 #
##########################################################################################################################################



NW_PORT_MONITOR="$1"

if [ -z "${NW_PORT_MONITOR}" ]; then
   echo -e "Please Specify N/W Port To be Monitored"
   exit 0
fi



echo "-------------------------------------------------------------------------------------------------------"


###############LOAD AVARGE##########################
# It show last 3 load avarage i.e. current , 5 mins before , 10 mins before
loadavg=`uptime | cut -d ',' -f4,5,6|cut -d ':' -f2|sed 's/ //g'`

###############DATABASE CONNECTION##########################
#[i.e. Connections at 3306 port]
dbconn=`netstat -nto|grep 3306|grep ESTABLISHED|wc -l`

####################TOTAL PROCESS RUNNING ON SYSTEM###############
total_processes=`ps -e|wc -l`

###############TOTAL HTTP THREADS##########################
#[i.e. Total Http instances]
httpthread=`ps -ef|grep httpd|grep -v "grep"|wc -l` 

############Calculating BANDWIDTH AT Specified Port (Example eth0)##########
#Total Bytes Received & Transfer on Port Specified Port (Example eth0) in KB 
nw_port_log=`/usr/local/bin/sar -n DEV 1  1|grep $NW_PORT_MONITOR | grep 'Average'`
kb_received_per_sec=`echo $nw_port_log|awk '{print $5}'`
kb_transfer_per_sec=`echo $nw_port_log|awk '{print $6}'`


################CPU UTILIZATION##########################
#CPU UTILIZATION IN %
cpu_utilize=`/usr/local/bin/mpstat| grep "all"`
cpu_user=`echo $cpu_utilize|awk '{print $3}'`
cpu_system=`echo $cpu_utilize|awk '{print $5}'`
cpu_iowait=`echo $cpu_utilize|awk '{print $6}'`

###############MEMORY UTILIZATION##########################
#MEMORY UTILIZATION IN MB
mem=`free -m | grep "Mem:"`
mem_used=`echo $mem|awk '{print $3}'`
mem_free=`echo $mem|awk '{print $4}'`
mem_cache=`echo $mem|awk '{print $7}'`

###############SWAP UTILIZATION##########################
#SWAP UTILIZATION IN KB
swap_log=`free -k | grep "Swap:"`
swap_used=`echo $swap_log|awk '{print $3}'`

###############MOD JK Connection##########################
#JBOSS MOD JK Connection Count
let jkconn_8009=`netstat -nto|grep 8009|grep ESTABLISHED|wc -l`/2
let jkconn_9009=`netstat -nto|grep 9009|grep ESTABLISHED|wc -l`/2
let jkconn_10009=`netstat -nto|grep 10009|grep ESTABLISHED|wc -l`/2
let jkconn_11009=`netstat -nto|grep 11009|grep ESTABLISHED|wc -l`/2

#TOTAL MOD JK Connection
let jkconn=jkconn_8009+jkconn_9009+jkconn_10009+jkconn_11009;

###############JBOSS STATUS##########################
#It show Every jboss node status whether it is UP or DOWN
jbossStatus1=`ps -ef|grep java|grep -v grep|egrep jboss-4.0.2.node1|wc -l|awk '{if($0>0) {print "UP"} else {print "DOWN"}}'`
jbossStatus2=`ps -ef|grep java|grep -v grep|egrep jboss-4.0.2.node2|wc -l|awk '{if($0>0) {print "UP"} else {print "DOWN"}}'`
jbossStatus3=`ps -ef|grep java|grep -v grep|egrep jboss-4.0.2.node3|wc -l|awk '{if($0>0) {print "UP"} else {print "DOWN"}}'`
jbossStatus4=`ps -ef|grep java|grep -v grep|egrep jboss-4.0.2.node4|wc -l|awk '{if($0>0) {print "UP"} else {print "DOWN"}}'`




echo "`date +"%d/%b/%Y:%T"` load_avg:$loadavg dbconn:$dbconn total_process:$total_processes http_thread:$httpthread NW_kb_received/sec:$kb_received_per_sec  NW_kb_transfer/sec:$kb_transfer_per_sec cpu%user:$cpu_user cpu%system:$cpu_system cpu%iowait:$cpu_iowait mem_used(mb):$mem_used mem_free(mb):$mem_free mem_cache(mb):$mem_cache swap_used(kb):$swap_used jkconn_total:$jkconn jkconn_8009:$jkconn_8009 jkconn_9009:$jkconn_9009 jkconn_10009:$jkconn_10009 jkconn_11009:$jkconn_11009 jboss_node1:$jbossStatus1 jboss_node2:$jbossStatus2 jboss_node3:$jbossStatus3 jboss_node4:$jbossStatus4" 




echo "-------------------------------------------------------------------------------------------------------"