#!/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}}'` #"tz" will contain the timezone tz=$(grep -B1 "OpenEdge Release" $file | head -1 | awk '{print $5}') #"ty" will contain the year ty=$(grep -B1 "OpenEdge Release" 2012-07-20.cyhoqad.cyb.com.au.appservers | head -1 | awk '{print $6}') #"tm" represents the first timestamp tm=$(head -1 $file | awk '{print $4}') # Internal awk array should correspond to: # # type[i,:] -- 2 3 4 5 6 7 8 9 # TIME record Appserver ACTIVE BUSY LOCKED AVAILABLE CLIENTSNOW CLIENTSPEAK CLIENT_Q_NOW # type[10,:] 11 12 13 14 15 # CLIENT_Q_MAX TOTAL_RQS RQ_WAIT_MAX(ms) RQ_WAIT_AVG(ms) RQ_DURATN_MAX(ms) RQ_DURATN_AVG(ms) #################################################### # STEP 1: PROCESS ALL APPSERVERS INTO ONE TEMP FILE #################################################### more $file | awk -vTZ=$tz -vTM=$tm -vTY=$ty 'BEGIN{indx=0;record=0 print "TIME record Appserver ACTIVE BUSY LOCKED AVAILABLE CLIENTSNOW CLIENTSPEAK 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[2,record]=$4; } if(indx==1){ if(match($0,"Active")&&(match($0,"Agent")||match($0,"Server"))){type[3,record]=$4}; if(match($0,"Busy")){type[4,record]=$4} ; if(match($0,"Locked")){type[5,record]=$4}; if(match($0,"Available ")){type[6,record]=$4}; if(match($0,"Active Clients")){gsub("[(,)]","");type[7,record]=$6;type[8,record]=$7}; if(match($5,TZ)&&match($6,TY)){type[1,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[1,1]=TM; #This is the exception for the very first timestamp 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 sed -i "1iSPACE TEXT TEXT TEXT DEC DEC DEC DEC DEC DEC DEC DEC DEC DEC DEC DEC DEC" $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 rm ./tempfile -f rm ./Broker-PIDs -f 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 $3 -s "QAD Sentinel Alert -- Customer Raypak: Appserver(s) have not changed state in over $limit seconds" < temp fi sleep $limit done