增加服务器监控
1、脚本文件monitor.sh
#!/bin/bash PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:/usr/java/default/bin:~/bin export PATH cmsFraction=90 warningFgcTimes=10 warningMsg="" warningMsgPre="" LOG_DIR="/opt/logs/monitor" #发送报警邮件方法 function sendEmail { from="wms-test-139@scm.com" to="****@qq.com" cc="****@qq.com" subject="* 139 Memory Warning *" if [ -n "$warningMsg" ]; then #上一次邮件发送时间 latestMailTime=`cat $LOG_DIR/lmail 2>/dev/null` now=`date +%s` if [ -z "$latestMailTime" ] || [ $(echo "$now - $latestMailTime > 60*30" | bc) = 1 ]; then warningMsg=$warningMsgPre"\n\n"$warningMsg echo -e "Error:"$warningMsg echo -e "From:$from\nTo:$to\nCC:$cc\nSubject:$subject\n\n$warningMsg" | sendmail -t echo $now > $LOG_DIR/lmail fi fi } #获取order-site的进程号 orderSitePid=`ps -ef|grep outstock-web|grep com.caucho.server.resin.Resin|gawk '{print $2}'` if [ -z "$orderSitePid" ]; then warningMsg="order-site process does not exist!\n" warningMsgPre="1(null)," sendEmail exit 0 fi #获取heap使用情况 gcUtils=(`jstat -gcutil $orderSitePid | sed -n '2p' | gawk '{print $3, $4, $8}'`) edenPercent=${gcUtils[0]} oldPercent=${gcUtils[1]} fgc=${gcUtils[2]} prevOldPercent=`cat $LOG_DIR/old 2>/dev/null` prevFgc=`cat $LOG_DIR/fgc 2>/dev/null` if [ $(echo "$oldPercent > $cmsFraction" | bc) = 1 ]; then if [ -n "$prevOldPercent" ] && [ $(echo "$prevOldPercent > $cmsFraction" | bc) = 1 ]; then #如果当前老年代内存使用超过70%,并且前一次统计内存时也超过了70%,说明老年代处于饱和,内存要不够用了 warningMsg=$warningMsg"Memory of the Old Generation lasted over "$cmsFraction"%!\n" warningMsgPre=$warningMsgPre"2($oldPercent)," fi fi if [ -n "$prevFgc" ] && [ $(echo "$fgc - $prevFgc > $warningFgcTimes" | bc) = 1 ]; then #如果老年代gc次数比前一次统计时增长了10次,说名老年代gc太频繁,程序要卡顿了 warningMsg=$warningMsg"The FGC times ware too frequent!" warningMsgPre=$warningMsgPre"3($fgc)" fi sendEmail echo $oldPercent > $LOG_DIR/old echo $fgc > $LOG_DIR/fgc echo -n "--";date "+%Y-%m-%d %H:%M:%S" jstat -gcutil $orderSitePid echo ""
2、上传到linux服务器,如果执行不了,注意使用dos2unix 命令将dos文件转换为unix文件
yum install dos2unix
dos2unix order_monitor.sh
3、新建monitor文件夹
/opt/logs/monitor 放日志
/opt/script/monitor 放脚本文件
4、增加1分钟计划执行
*/1 * * * * sh /opt/script/monitor/order_monitor.sh 1>>/opt/logs/monitor/stdout.log 2>>/opt/logs/monitor/stderr.log