#!/bin/bash
i=0
pid=0
total_cpu=0
total_java=0
total_load=0
while [ $i -lt 3 ]
do
CPU=`top -b -n 2|grep Cpu|awk '{print $2}'|sed -n '2p'|cut -f 1 -d "."`
LOAD=`top -b -n 2|grep load|awk '{print $12}'|sed -n '2p'|cut -f 1 -d "."`
JAVA=`top -b -n 2 -p $(netstat -tlnp|grep 8088|awk '{print $7}'|awk -F '/' '{print $1}')|grep java|awk '{print $9}'|sed -n '2p'|cut -f 1 -d "."`
total_cpu=`expr $total_cpu + $CPU`
total_java=`expr $total_java + $JAVA`
total_load=`expr $total_load + $LOAD`
let "i++"
sleep 1
done
echo "total_cpu:" $total_cpu"%"
echo "total_load:" $total_load
echo "total_java:" $total_java "%"
if [ $total_cpu -gt 240 ] || [ $total_load -gt 10 ]||[ $total_java -gt 300 ];then
#`expr $a + $b`
echo `date '+%Y-%m-%d %H:%M:%S'` " total_cpu is ["$total_cpu"%] total_load is [" $total_load "] total_java is [" $total_java "%] " | mail -s "[service error] moko_site" diguanglin@moko.cc
fi
if [ $total_java -gt 360 ];then
TomcatID=$(ps -ef |grep tomcat |grep -w '/opt/tomcat'|grep -v 'grep'|awk '{print $2}')
# tomcat启动程序(这里注意tomcat实际安装的路径)
StartTomcat=/usr/tomcat/bin/startup.sh
TomcatCache=/usr/tomcat/work
kill -9 $TomcatID # 杀掉原tomcat进程
sleep 5
rm -rf $TomcatCache # 清理tomcat缓存
$StartTomcat #启动tomcat服务
echo `date '+%Y-%m-%d %H:%M:%S'` " total_cpu is ["$total_cpu"%] total_load is [" $total_load "] total_java is [" $total_java "%] " | mail -s "[restart moko_site ] moko_site " diguanglin@moko.cc
fi
echo `date '+%Y-%m-%d %H:%M:%S'` "total_cpu is ["$total_cpu"%] total_load is [" $total_load "] total_java is [" $total_java "%] " >> /home/logs/tomcat/java_cpu.log