#!/bin/bash ################################################## # Program: Process-Appservers # Author: pao # Date: Jan 9 2009 # # # Description: # ------------ # 1. Script to post-process raw appserver data and extract info in a form # suitable for import and plotting into Excel # # # Possible Extentions & Limitations: # ---------------------------------- # 1. Initially, can be used as a template for all possible alerts (Application,DB, Server, Network etc.) # 2. Currently lack of "traps" to catch possible errors within script # 3. Needs to be made portable and generic for all OS's # # # # Modification History # -------------------- # Jan 9 2009 pao: # Created # # June 10 2009 pao: # Modify to extract info for: # Active Clients (now, peak) # Client Queue Depth (cur, max) : (0, 3) # Total Requests : 177081 # Rq Wait (max, avg) : (7975 ms, 1 ms) # Rq Duration (max, avg) : (581607 ms, 52 ms) ################################################## #useage message if [ $# -eq 0 ]; then echo -e " ======================================= $0 ======================================= Usage: process-appservers-total Description: ------------ Example: -------- " exit fi #set variables for later use file=$1 #input file name hostname=`hostname` #Get hostname for reference #"aps" will contain a list of all the appservers aps=`cat $file | awk '{if(match($0,"Broker Name")){aps[$4]+=1}}END{for(i in aps){print i}}'` # Internal awk array should correspond to: # # # # type[9,:] = Client Que Depth Now # type[10,:] = Client Que Depth Max # type[11,:] = Tot Requests # type[12,:] = Rq Wait Max # type[13,:] = Rq Wait Avg # type[12,:] = Rq Duration Max # type[13,:] = Rq Duration Avg #################################################### # STEP 1: PROCESS ALL APPSERVERS INTO ONE TEMP FILE #################################################### more $file | awk 'BEGIN{indx=0;record=0 print "record Appserver ACTIVE BUSY LOCKED AVAILABLE CLIENTSNOW CLIENTSPEAK TIME CLIENT_Q_NOW CLIENT_Q_MAX TOTAL_RQS RQ_WAIT_MAX(ms) RQ_WAIT_AVG(ms) RQ_DURATN_MAX(ms) RQ_DURATN_AVG(ms)" } { if(match($0,"Broker Name")){ record+=1;indx=1; type[1,record]=$4; } if(indx==1){ if(match($0,"Active")&&(match($0,"Agent")||match($0,"Server"))){type[2,record]=$4}; if(match($0,"Busy")){type[3,record]=$4} ; if(match($0,"Locked")){type[4,record]=$4}; if(match($0,"Available ")){type[5,record]=$4}; if(match($0,"Active Clients")){gsub("[(,)]","");type[6,record]=$6;type[7,record]=$7}; # if(match($2,"AVAILABLE")&&match($13,"2012")){type[8,record]=$14;indx=0}; if(match($5,"EST")&&match($6,"2012")){type[8,record+1]=$4;indx=0}; if(match($5,"BST")&&match($6,"2012")){type[8,record+1]=$4;indx=0}; if(match($5,"CET")&&match($6,"2012")){type[8,record+1]=$4;indx=0}; if(match($5,"CST")&&match($6,"2012")){type[8,record+1]=$4;indx=0}; if(match($5,"CDT")&&match($6,"2012")){type[8,record+1]=$4;indx=0}; if(match($5,"MET")&&match($6,"2012")){type[8,record+1]=$4;indx=0}; if(match($5,"EDT")&&match($6,"2012")){type[8,record+1]=$4;indx=0}; if(match($5,"PDT")&&match($6,"2012")){type[8,record+1]=$4;indx=0}; if(match($5,"USAST")&&match($6,"2012")){type[8,record+1]=$4;indx=0}; if(match($0,"Client Queue")){gsub("[(,)]","");type[9,record]=$7;type[10,record]=$8} if(match($0,"Total Requests")){type[11,record]=$4}; if(match($0,"Rq Wait")){gsub("[(,)]","");type[12,record]=$6;type[13,record]=$8} if(match($0,"Rq Duration")){gsub("[(,)]","");type[14,record]=$6;type[15,record]=$8} } } END{ type[8,1]="|"; #A hardcode shortcut for the first time entry for(i=1;i tempfile #################################################### # STEP 2: PROCESS THE TEMPFILE AND EXTRACT INDIVIDUAL # APPSERVER INFO. INTO SEPERATE APPSERVER FILES # for i in $(echo $aps | awk '{n=split($0,a," ");for(i=1;i<=n;i++){print a[i]}}'); do opf=`echo $file.$i".csv"` head -1 tempfile > $opf grep $i tempfile >> $opf done #################################################### # STEP 3: LIST ALL APPSERVER AND BROKER PIDS # [[ -f Broker-PIDs ]] && rm Broker-PIDs cat $file | awk '/^[0-9]/{print $1}' | sort | uniq >> Broker-PIDs exit END{for(i in aps){print i, aps[i]}}' exit while ls do tmpfile='tempfile' [[ -f $tmpfile ]] && rm $tmpfile #temporary file to write to now=`date +"%s"` #seconds since 1970 aps=`nsman -i NS1 -q | awk '{if (match($1,"AS")||match($1,"WS")){split($1,a,".");i++;b[i]=a[2]}}END{for(j=1;j<=i;j++){print b[j]}}'` #get a list of current appservers to monitor for i in $aps; do asbman -i $i -q | \ #Begin awk processing here awk -v limit=$limit -v aps=$i 'BEGIN{flag=0;napps=0} { if(match($1,"[0-9]")) { napps++; diff=calctime(2,$0)-calctime(1,$0); if (diff>limit&&(match($2,"LOCKED")||match($2,"SENDING"))){ state[$2]+=1; if(flag==0){ print "\n ",aps, "\n---------------------------";flag=1};print $0} } } END{ for(i in state){sum=sum+state[i]}; if (sum==0){exit}; print "** "aps":"; sum=0; for(i in state){print "** "state[i]" of "napps,"appservers ("state[i]*100/napps" %) in a ", i, "state for over",limit,"seconds"} } # print calctime(2,$0), diff,limit; function calctime(a,str) { b=str gsub(",","",b) gsub(":"," ",b) split(b,t," ") if (a==1){indx=7}else{indx=12} match("JanFebMarAprMayJunJulAugSepOctNovDec",t[indx]) t[indx] = sprintf("%02d",(RSTART+2)/3) if (a==1) {arg=t[9]" "t[indx]" "t[8]" "t[10]" "t[11]" 00" } else { arg=t[14]" "t[indx]" "t[13]" "t[15]" "t[16]" 00"} return mktime(arg) } ' >> $tmpfile done email=`grep "\*\*" $tmpfile | awk -v agentpercent=$agentpercent '{split($6,a,"(");if(a[2]>agentpercent){print "yes";exit}}'` sendalert='yes' if [[ "$email" = "yes" ]] #Send email if alert/threshold condition is raised then #format appserver name list for correct display apsop=`echo $aps | awk 'BEGIN{op=""}{n=split($0,a," ");for(i=1;i<=n;i++){op=" "a[i]"\n"}}END{print op}'` echo -e "\n------------------------------------------- THIS IS A QAD SENTINEL ALERT ------------------------------------------- On server: $hostname The following appservers are running: $apsop The following condition exists: \n" > temp grep "\*\*" $tmpfile >> temp echo -e "\n---------- Details: ----------" >> temp grep -v '\*\*' $tmpfile >> temp #Mail admin about request mail -s "QAD Sentinel Alert -- Customer Raypak: Appserver(s) have not changed state in over $limit seconds" $3 < temp fi sleep $limit done