#!/bin/bash
############################################
#检查配置文件中的进程是否存在,并且根据配 #
#置文件中进程的CPU占用率阕值 内存占用率阕 #
#值来确定是否需要记录 #
############################################
#配置文件格式
#CPU占用率阕值 内存占用率阕值 要检查的进程名
#比如
#CPU阕值 内存阕值 进程名
#5 10 isupgw
#中间使用空格分隔
rq=` date +"%Y-%m-%d %H:%M:%S" `
#设置运行目录
EXEPATH="/home/ssmn/CMcheck"
#设置输出的文件名
OUTFILE="websercheck.log"
#检查配置文件是否存在
if [ ! -r $EXEPATH/syscheck.conf ]
then
echo "Disposition document syscheck.conf does not exist"
exit 0
fi
#检查是否存在日志文件
if [ ! -w $EXEPATH/$OUTFILE ]
then
touch $EXEPATH/$OUTFILE
fi
IFS=" "
while read -a PRO
do
touch $EXEPATH/syscheck.tmp
#生成存储程序运行情况的文件
ps -A -o pcpu -o pmem -o args |grep ${PRO[2]} > $EXEPATH/syscheck.tmp
#删除有grep的行
cat $EXEPATH/syscheck.tmp | sed /grep/d > $EXEPATH/syscheck1.tmp
rm $EXEPATH/syscheck.tmp
mv $EXEPATH/syscheck1.tmp $EXEPATH/syscheck.tmp
#初始化变量
PRO_STATE="no"
CPU_STATE=0
MEM_STATE=0
CPU_STATEU="no"
MEM_STATEU="no"
#读取程序运行状态文件
CPU_STATE=$(awk 'BEGIN{temp=0;}{temp=($1>temp)?$1:temp}END{print temp}' $EXEPATH/syscheck.tmp)
MEM_STATE=$(awk 'BEGIN{temp=0;}{temp=($2>temp)?$2:temp}END{print temp}' $EXEPATH/syscheck.tmp)
cat $EXEPATH/syscheck.tmp|grep ${PRO[2]}>>NULL
if [ $? -eq 0 ]
then
PRO_STATE="yes"
fi
#检查程序是否运行
if [ $PRO_STATE = "no" ]
then
echo "$rq ${PRO[2]} Procedure has not moved" >> $EXEPATH/$OUTFILE
continue
fi
#检查程序CPU占用率
CPU_STATEU=$(awk 'BEGIN{if('$CPU_STATE'>'${PRO[0]}'){print "yes"}else{print "no"}}')
if [ $CPU_STATEU = "yes" ]
then
echo "$rq ${PRO[2]} Procedure CPU takes leads to be high,Take rate achieves $CPU_STATE" >> $EXEPATH/$OUTFILE
fi
#检查程序内存占用率
MEM_STATEU=$(awk 'BEGIN{if('$MEM_STATE'>'${PRO[1]}'){print "yes"}else{print "no"}}')
if [ $MEM_STATEU = "yes" ]
then
echo "$rq ${PRO[2]} Procedure Procedure memory takes leads to be
high,Take rate achieves $MEM_STATE" >> $EXEPATH/$OUTFILE
fi
done < $EXEPATH/syscheck.conf
rm $EXEPATH/syscheck.tmp
exit 0
阅读(1441) | 评论(0) | 转发(0) |