您的位置:首页 > 其它

一个检测应用状态的脚本

2014-07-02 10:22 411 查看
#!/bin/sh
#Description: Check ICE3.0 State
#Last Update: 2014/06/11/01
WORKDIR=`pwd`

export REPORTDIR=$WORKDIR/`date "+%Y%m%d%H%M%S"`
export TG=`date "+%Y%m%d%H%M%S"`
export BEGIN=`date -d "-1 hour" "+%H:%M:%S"`
export END=`date "+%H:%M:%S"`

ECORE=2
OCORE=0
APPDIR=/opt/app/ice3.0
LOGDIR=/DataBase/ice3.0/log
DISDIR=$LOGDIR/disconnect
ERRDIR=$LOGDIR/error
SYSDIR=$LOGDIR/sys
MONDIR=$LOGDIR/monitor
ECCONF=$APPDIR/conf/com.umpay.ice.core.common.services.model.EcChannel.xml
GWCONF=$APPDIR/conf/com.umpay.ice.core.common.services.model.GwChannel.xml
REPORT=$REPORTDIR/report
RESH=$APPDIR/bin/line-report.sh
QUEDIR=$APPDIR/conf-share/MonitorConfig.xml

FTPHOST=""
FTPUSER=""
FTPPASS=""

#Initialization
#[ $UID -ne 0 ] && printf "You must use the root user to run this script !\n" && exit $ECORE
[ -d $REPORTDIR ] || mkdir -p $REPORTDIR
#[ -d $LOGDIR ] || exit $ECORE
#[ -d $APPDIR ] || exit $ECORE

#INITA
INITA()
{
printf "\n"
printf "\t\t\t Now time: `date "+%Y-%m-%d %H:%M"` \n"
printf "\n"

}

#Process Disk
PDISK()
{
printf "PROCE:\n"
JAVAPID=`ps aux|grep java|grep -v "grep java"|awk -v AP=$APPDIR '{if($12~AP) print $2}'`
if [ -z $JAVAPID ];then
printf "\t\t\t Error NO Process "
else
printf "\t\t\t OK Pid [$JAVAPID]"
fi
printf "\n"

#Disk

printf "\n"
printf "Disk:\n"
df -P |egrep -v "File|tmpfs" |sed 's/%//g'|awk '
{if($5>90)
{print "\t\t\t" " Error! "$6" \t Usage:"" " $5"%"}
else
{ print "\t\t\t" " OK "$6" \t Usage:"" " $5"%"}
}'
printf "\n"

}

#LOG
LOG(){
printf "\n"
printf "LOG:\n"
if [ -f $LOGDIR/log ];then
LSIZE1=`ls -l $LOGDIR/log |awk '{print $5}'`
sleep 30
LSIZE2=`ls -l $LOGDIR/log |awk '{print $5}'`

if [ $LSIZE2 -gt $LSIZE1 ];then
printf "\t\t\t OK SIZE $LSIZE2 > $LSIZE1 \n"
printf "\n"
else
printf "\t\t\t Error $LSIZE1 = $LSIZE2 \n"
printf "\n"
fi
else
printf "\t\t\t Error $LOGDIR/log File Not Found !"
printf "\n"
fi
}

#Ecport
ECPORT(){
printf "\n"
printf "EC Port Listen:\n"
if [ -f $ECCONF ];then
PORTNUM=`grep "port=" $ECCONF|awk 'BEGIN{FS="="}{print $3}'|sed -e 's/\&.*//g'|wc -l`
if [ $PORTNUM -eq 2 ];then
ECPORT1=`grep "port=" $ECCONF|awk 'BEGIN{FS="="}{print $3}'|sed -e 's/\&.*//g'|sed -n '1p'`
ECPORT2=`grep "port=" $ECCONF|awk 'BEGIN{FS="="}{print $3}'|sed -e 's/\&.*//g'|sed -n '2p'`
if [ $ECPORT1 -eq $ECPORT2 ];then
export ALECPORT=$ECPORT1
netstat -nutl |awk '{print $4}'|grep -q $ECPORT1
if [ $? -eq 0 ];then
printf "\t\t\t OK \t Port $ECPORT1 is Listening\n"
printf "\n"
else
printf "\t\t\t Error \t Port $ECPORT1 is not Listening \n"
printf "\n"
fi
else
for i in `grep "port=" $ECCONF|awk 'BEGIN{FS="="}{print $3}'|sed -e 's/\&.*//g'`
do
netstat -nutl |awk '{print $4}'|grep -q $i
if [ $? -eq 0 ];then
printf "\t\t\t OK \tPort $i is Listening\n"
printf "\n"
else
printf "\t\t\t Error \tPort $i is not Listening \n"
printf "\n"
fi
done
fi

elif [ $PORTNUM -eq 3 ];then
ECPORT1=`grep "port=" $ECCONF|awk 'BEGIN{FS="="}{print $3}'|sed -e 's/\&.*//g'|sed -n '1p'`
ECPORT2=`grep "port=" $ECCONF|awk 'BEGIN{FS="="}{print $3}'|sed -e 's/\&.*//g'|sed -n '2p'`
ECPORT3=`grep "port=" $ECCONF|awk 'BEGIN{FS="="}{print $3}'|sed -e 's/\&.*//g'|sed -n '3p'`
if [ $ECPORT1 -eq $ECPORT2 ] && [ $ECPORT1 -eq $ECPORT3 ];then
netstat -nutl |awk '{print $4}'|grep -q $ECPORT1
if [ $? -eq 0 ];then
printf "\t\t\t OK \tPort $ECPORT1 is Listening\n"
printf "\n"
else
printf "\t\t\t Error \tPort $ECPORT1 is not Listening \n"
printf "\n"
fi
elif [ $ECPORT1 -eq $ECPORT2 ] || [ $ECPORT2 -eq $ECPORT3 ];then
for i in $ECPORT1 $ECPORT3
do
netstat -nutl |awk '{print $4}'|grep -q i
if [ $? -eq 0 ];then
printf "\t\t\t OK \tPort $i is Listening\n"
printf "\n"
else
printf "\t\t\t Error \tPort $i is not Listening \n"
printf "\n"
fi
done
else
for i in `grep "port=" $ECCONF|awk 'BEGIN{FS="="}{print $3}'|sed -e 's/\&.*//g'`
do
netstat -nutl |awk '{print $4}'|grep -q $i
if [ $? -eq 0 ];then
printf "\t\t\t OK \tPort $i is Listening\n"
printf "\n"
else
printf "\t\t\t Error \tPort $i is not Listening \n"
printf "\n"
fi
done
fi
else
for i in `grep "port=" $ECCONF|awk 'BEGIN{FS="="}{print $3}'|sed -e 's/\&.*//g'`
do
netstat -nutl |awk '{print $4}'|grep -q $i
if [ $? -eq 0 ];then
printf "\t\t\t OK Port $i is Listen\n"
printf "\n"
else
printf "\t\t\t Error Port $i is not Listening \n"
printf "\n"
fi
done

fi
else
printf "\t\t\t Error\t Ec Conf File is Not Fond \n"
fi

}
#B->M->G
BMG(){
#B->M
printf "\n"
printf "B->M Socket State: \n"
if [ -f $ECCONF ];then
if [ ! -z $ALECPORT ];then
netstat -ant|sed -e '1d'|awk '{if($6~"ESTABLISHED") print $0}'|awk '{print $4}'|grep -q $ALECPORT
if [ $? -eq 0 ];then
ENUM=`netstat -ant|sed -e '1d'|awk '{if($6~"ESTABLISHED") print $0}'|awk '{print $4}'|grep $ALECPORT|grep -v "127.0.0.1"|wc -l`
printf "\t\t\t OK\t B->M Local Port $ALECPORT is ESTABLISHED\n"
printf "\t\t\t $ENUM\t Links\n"
else
printf "\t\t\t Error\t B->M No Socket Link Local Port $ALECPORT\n"
fi
else
for i in `grep "port=" $ECCONF|awk 'BEGIN{FS="="}{print $3}'|sed -e 's/\&.*//g'`
do
netstat -ant|sed -e '1d'|awk '{if($6~"ESTABLISHED") print $0}'|awk '{print $4}'|grep -q $i
if [ $? -eq 0 ];then
ENUM=`netstat -ant|sed -e '1d'|awk '{if($6~"ESTABLISHED") print $0}'|awk '{print $4}'|grep $i|grep -v "127.0.0.1"|wc -l`
printf "\t\t\t OK\t B->M Local Port $i is ESTABLISHED\n"
printf "\t\t\t $ENUM\t Links\n"
else
printf "\t\t\t Error\t B->M No Socket Link Local Port $i\n"
fi
printf "\n"
done
fi
else
printf "\t\t\t Error\t Ec Conf File is Not Fond\n"
fi

#M->G

printf "\n"
printf "M->G Socket State: \n"
if [ -f $GWCONF ];then
for i in `grep "port=" $GWCONF|sed -e 's/.*port=//g' -e 's/&.*//g'`
do
netstat -ant|sed -e '1d'|awk '{if($6~"ESTABLISHED") print $0}'|awk '{print $5}'|grep -q $i
if [ $? -eq 0 ];then
GWADD=`grep "host=" $GWCONF |sed -e 's/.*host=//g' -e 's/&.*//g'`
GNUM=`netstat -ant|sed -e '1d'|awk '{if($6~"ESTABLISHED") print $0}'|awk -v ADD=$GWADD -v PO=$i '$5~ADD":"PO {n++} END {print n+0}'`
printf "\t\t\t OK \t M->G Remort Port $i is ESTABLISHED \n"
printf "\t\t\t $GNUM \t Links\n"
else
printf "\t\t\t Error\t M->G No Socket Link Remort Port $i\n"
fi
done
printf "\n"
else
printf "\t\t\t Error\t GW Conf File is Not Fond \n"
fi

}
#DIS
DIS(){
printf "\n"
printf "Disconnect: \n"
DSDIR="$LOGDIR/disconnect"
if [ -f $DSDIR ];then
awk -v B=$BEGIN -v E=$END '$2>B && $2<E && $3=="EcChannel" {n++} END {print"\t\t\t" " EC Disconnect:\t" n+0 "\t1Hours"}' < $DISDIR
awk -v B=$BEGIN -v E=$END '$2>B && $2<E && $3=="GwChannel" {n++} END {print"\t\t\t" " GW Disconnect:\t" n+0 "\t1Hours"}' < $DISDIR
printf "\n"
else
printf "\t\t\t OK Disconnect Log File is Not Fond \n"
fi

}

#Que
QUE(){

printf "\n"
printf "Queue: \n"
printf "\n"
GWNUM=`grep mtspeed $GWCONF |sed -e 's/.*mtspeed="//g' -e 's/" mtperiod.*//g'`
GWPERCEN=`grep "GWMQ objectName=" $QUEDIR |sed -e 's/.*alertProportion="//g' -e 's/">//g'`
ECPERCEN=`grep "ECMQ objectName=" $QUEDIR |sed -e 's/.*alertProportion="//g' -e 's/">//g'`
ECNUM1=`grep "queue size" $ECCONF |sed -e 's/.*size="//g' -e 's/"\/>//g'|sed -n '1p'`
ECNUM2=`grep "queue size" $ECCONF |sed -e 's/.*size="//g' -e 's/"\/>//g'|sed -n '2p'`
[ $ECNUM1 -eq $ECNUM2 ] && export ECNUM=$ECNUM1
QGW=`echo "scale=0;$GWNUM /100 * $GWPERCEN "|bc`
#echo $QGW
QEC=`echo "scale=0;$ECNUM /100 * $ECPERCEN "|bc`
printf "\t\t\t GW \tQUEUE \n"
printf "\n"
GQUEUENUM=`grep "Q-GwChannel" $MONDIR |tail -1 |awk -F "total" '{print NF-2}'`
for i in `seq 0 $GQUEUENUM`
do
A=`expr $i + 1`

GWSIZE=`grep "Q-GwChannel-1" $MONDIR |tail -1 |sed -e 's/.*QueueMonitor://g' -e 's/\[//g' -e 's/\]//g'|awk -v A=$A -F "Q-GwChanel-1|mem" '{print $A}'|awk -F ":" '{print $NF}'`
if [ $GWSIZE -lt $QGW ];then
printf "\t\t\t OK \tQ-GwChannel-1-$i Total:$GWSIZE\n"
else
printf "\t\t\t Error \tQ-GwChannel-1-$i Total:$GWSIZE\n"
fi

done
printf "\n"
printf "\t\t\t EC \tQUEUE \n"
printf "\n"
EQUEUENUM=`grep "Q-EcChannel" $MONDIR |tail -1 |awk -F "EcChannel" '{print NF-1}'`
#echo $EQUEUENUM
for i in `seq 1 $EQUEUENUM`
do
SO=`expr $EQUEUENUM + 1`
A=`expr $i + 1`
ECSIZE=`grep "Q-EcChannel-1" $MONDIR |tail -1|sed -e 's/.*QueueMonitor://g' -e 's/\[//g' -e 's/\]//g'|awk -v A=$A -F "Q-EcChannel-" '{print $A}'|sed -e 's/.\://g'`
if [ $ECSIZE -lt $QEC ];then
printf "\t\t\t OK \tQ-EcChannel-$EQUEUENUM \t$ECSIZE\n"
else
printf "\t\t\t Error \tQ-EcChannel-$EQUEUENUM \t$ECSIZE\n"
fi
EQUEUENUM=`expr $EQUEUENUM - 1`
done
#printf "\t\t\t $QGW\n"
#printf "\t\t\t $QEC\n"

printf "\n"
}

#MON
MON(){
printf "\n"
printf "Mon: \n"
printf "\n"
if [ -f $MONDIR ];then
tail -100 $MONDIR > $REPORTDIR/monitor
printf "\t\t\t 100 line to monitor file \n"

else
printf "\t\t\t Error Monitor File not found !\n"
fi
printf "\n"
}

#ERROR SYS LINE
ESYSLINE(){

printf "\n"
printf "ERR: \n"
printf "\n"
#ERROR
if [ -f $ERRDIR ];then
awk -v B="$BEGIN.0" -v E="$END.0" '$2>B && $2<E {print}' $ERRDIR > $REPORTDIR/error
printf "\t\t\t 1 hour before to error file \n"

else
printf "\t\t\t Error Error File not found !"
fi
printf "\n"

#SYS

printf "\n"
printf "SYS: \n"
printf "\n"
if [ -f $SYSDIR ];then
awk -v B="$BEGIN.0" -v E="$END.0" '$2>B && $2<E {print}' $SYSDIR > $REPORTDIR/sys
printf "\t\t\t 1 hour before to sys file \n"

else
printf "\t\t\t Error error SYS File not found !\n"
fi
printf "\n"

#LILE
printf "\n"
printf "Line_Report: \n"
printf "\n"
if [ -f $RESH ];then
/bin/sh $RESH
else
printf "\t\t\t Error File $RESH Not Found \n"

fi
}

#TGZ
TGZ(){
if [ -d $REPORTDIR ];then
tar czf $TG.tar.gz $TG
[ $? -eq 0 ] && rm -rf $TG
else
printf "\t\t\t REPORT Directory not found \n"
fi

}

#PUT
PUT(){

ftp -n<<EOF
open $FTPHOST
user $FTPUSER $FTPPASS
cd ~/
put $TG.tar.gz
close
bye
EOF
}

#TEM
TEM(){
#for((i>=$1;i<=$2;++i))
for i in `seq $1 $2`
do
tput sc
echo -ne "Please Waiting ... ... $i%"
sleep 0.1
tput rc
((++i))

done
}
SUM=1
B=10
FUN=(INITA PDISK LOG ECPORT BMG DIS QUE MON ESYSLINE TGZ)
while [ $SUM -le 100 ];
do
for j in ${FUN[*]}
do
INVERT=`expr 100 / ${#FUN[*]}`
$j >>$REPORT
TEM $SUM $B
export B=`expr $B + $INVERT`
export SUM=`expr $SUM + $INVERT`
done
done
printf "\n"

printf "Done \n"
#EOF
#for i in INITA PRO DISK LOG ECPORT BMG DIS MON ESYSLINE TGZ
#do
# $i | read BE ED
# do
# for j in `seq $BE $ED`
# do
# $i
# tput sc
# echo -ne "Please Waiting ... .... $j%"
# sleep 0.1
# tput rc
# ((j++))
# done
# done
#printf "\n"
#printf "Done \n"




本文出自 “Crazy_Linux” 博客,请务必保留此出处http://mkernel.blog.51cto.com/8015041/1433368
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: