linux shell脚本监控tomcat OOM

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/chushoutaizhong/article/details/81903603

testOOM1.sh

#!/bin/sh
tomcatDir='/home/XXX/YYY/apache-tomcat-7.0.70/'
tomcatLog='logs/catalina.out'
tomcatLogTemp='logs/catalina_temp.out'

echo "testOOM1.sh 启动 $tomcatDir$tomcatLog"
if [ ! -f "$tomcatDir$tomcatLogTemp" ];then
echo "new create"
 touch "$tomcatDir$tomcatLogTemp"
fi
echo "tomcatLogTemp :  $tomcatDir$tomcatLogTemp"
tail -f -s 5 "$tomcatDir$tomcatLog" | grep --line-buffer '^java.lang.OutOfMemoryError:*'  > "$tomcatDir$tomcatLogTemp" &

该脚本用于将catalina.out的指定以java.lang.OutOfMemoryError:开头的内容 重定向到指定文件中

tail命令注意 -f 循环输出 -s 指定多久打印一次  单位秒

其中 tail -f 多次grep过滤输出可能会出现的问题 内容不能及时输出到文件中 需要使用 --line-buffer 属性

1. 查看日志文件最后200行

tail -200 日志文件(打印出日志文件的最后200条信息)

tail -f -n 200 日志文件  (实时打印最新的日志信息)

tail -200 n 日志文件(实时打印最新的日志信息)

tail -n +200 日志文件(从第200 行开始显示日志信息)

2. 查看日志文件的前200行

head -n 200 日志文件

3. 查看日志文件的中间指定行

cat 日志文件 |  head -n 200 | tail -n + 101

4. grep过滤文件内容并打印

grep 443文件 (查看文件中中含有8080字段的行)

grep -n 443文件(确定8080字段在文件中的行数)

grep -n -i "nginx" 文件 (确定ggpush在文件中的行数并且忽略大小写)

grep 'mapper' *.cnf (匹配当前目录下所有后缀为cnf文件含有过滤字段的内容)

5. 过滤掉指定的日志信息

grep -v '关键字'

6. 查看日志文件中带有指定关键词的日志信息

tail -f  文件名 | grep 关键字 | grep 关键字

7. tail -f 多次grep过滤输出可能会出现的问题

对日志记录做多次grep过滤输出,格式如下:
tail -f log | grep xxx | grep yyy
发现grep失效,无法做正确输出。google研究了一下,原因如下:
管道 | 是全缓冲的,一般来说buffer_size为4096,有些是8192。不管具体值多少,只有buffer_size满了,才会看到输出。
在操作里  >>file 这个操作也是全缓冲的。调整如下
tail -f log | grep --line-buffer xxx | grep --line-buffer yyy
结果输出正常。
grep当带上了 --line-buffer 的时候,每输出一行,就刷新一次。
在unix里,块设备和普通文件,以及管道都是全缓冲的。

对日志分析告警和实时监控可以考虑如下形式:

tail -f $LOGF | while read var
do
case "$var" in
...
...
esac
done

testOOM.sh

#!/bin/sh
tomcatDir='/home/XXX/YYY/apache-tomcat-7.0.70/'
tomcatLog='logs/catalina.out'
tomcatLogTemp='logs/catalina_temp.out'
shOMM='/home/liuhua/testOOM1.sh'

ps -ef  | grep 'tail -f -s 5'| grep -v 'grep'|awk '{print $2}'|xargs kill -9
sleep 2

"$shOMM"
echo "tomcatLog : "$tomcatDir$tomcatLog
while true
do
 sleep 10
  exceptionTemp=`tail -n 1 $tomcatDir$tomcatLogTemp`
  #echo "get exceptionTemp value : "$exceptionTemp
  if [ -n "$exceptionTemp" ] ;then
  echo "stopped start"
   pid=`ps -ef  | grep ${tomcatDir}| grep -v 'grep' | grep -v 'tail -f -s 5'|awk '{print $2}'`
   echo "当前tomcat pid : $pid"
   if [ -n "$pid" ] ; then
    #${tomcatDir}/bin/shutdown.sh
    ps -ef  | grep ${tomcatDir}| grep -v 'grep'|grep -v 'tail -f -s 5'|awk '{print $2}'|xargs kill -9
   fi
    echo "stopped ${tomcatDir} end"
   sleep 50
   pid=`ps -ef  | grep ${tomcatDir}| grep -v 'grep'|grep -v 'tail -f -s 5'|awk '{print $2}'`
   echo "是否已经启动了tomcat 其pid : $pid"
   if [ -z "$pid" ] ; then
     echo "启动tomcat"
     ${tomcatDir}/bin/startup.sh
    else echo "tomcat 已经启动"
   fi
   sleep 1
   varpid=`ps -ef  | grep 'tail -f -s 5'| grep -v 'grep'|awk '{print $2}'`
   echo "tail shell pid :  $varpid"
   if [ -z "$varpid" ] ;then
    echo "starting testOOM1.sh "
    "$shOMM"
    echo "started testOOM1.sh"
   fi
    echo "清空 $tomcatDir$tomcatLogTemp"
    echo '' > $tomcatDir$tomcatLogTemp
  fi
done
 

用于重启tomcat 

其中shell的判断 

if ps -ef  | grep ${tomcatDir}| grep -v 'grep' && ${tomcatDir}/bin/shutdown.sh && sleep 5 && ps -ef  | grep ${tomcatDir}| grep -v 'grep'|awk '{print $2}'|xargs kill -9  ;then

当命令的执行结果为0则为真 为1则为假  指令执行结果可以使用 $? 查看

 if [ -n "$pid" ] ; then
    #${tomcatDir}/bin/shutdown.sh
    ps -ef  | grep ${tomcatDir}| grep -v 'grep'|grep -v 'tail -f -s 5'|awk '{print $2}'|xargs kill -9
   fi

当pid中的内容不为空 为真  注意 if [ -n "$pid" ] 的[]前后之间都有空格不然报错 还有注意$pid必须使用引号(保险)   

if [ -z "$varpid" ] ;then
    echo "starting testOOM1.sh "
    "$shOMM"
    echo "started testOOM1.sh"
   fi

当为空时为真

部分内容转自https://blog.csdn.net/qq_39338585/article/details/81736609

猜你喜欢

转载自blog.csdn.net/chushoutaizhong/article/details/81903603
OOM