zoukankan      html  css  js  c++  java
  • 【shell脚本】自动监控tomcat服务===autoCheck.sh和发送告警邮件脚本auto_monitor_service.sh

    自动监控tomcat服务,当tomcat服务挂掉时自动重启

    一、脚本内容

    [root@localhost ]# cat /root/autoCheck.sh 
    #!/bin/bash
    startTomcat=/usr/local/tomcat-7/bin/startup.sh
    tomcatLog=/tmp/tomcatLog
    tomcatID=`ps -ef | grep tomcat-7 | grep -w 'tomcat-7' | grep -v 'grep' | wc -l`
    Monitor()
    {
      #while true;do
      #tomcatID=`ps -ef | grep tomcat-7 | grep -w 'tomcat-7' | grep -v 'grep' | wc -l`
      echo "[info] 开始监控tomcat...[$(date +'%F %H:%M:%S')]"
      if [ $tomcatID -lt 1 ];then
        echo "[error] tomcat进程不存在!tomcat开始自动重启..."
        echo "[info] $startTomcat,请稍候..."
        sleep 3
        $startTomcat
        echo "[info] 服务启动成功..."
      fi
      echo "======================================================"
      #sleep 1
      #done
    }
    Monitor >> $tomcatLog

    分析:

      1)先定义tomcat服务启动目录startTomcat

      2)定义日志输出目录tomcatLog

      3)查询的tomcat进程号,通过wc -l统计是否存在该进程号,存在则大于1,不存在则小于1

      4)判断是否存在tomcat服务进程,小于1则服务不存在,睡眠3秒重启tomcat服务,并将启动信息输出到日志文件中

    二、设置定时执行任务

    设置为每一分钟执行一次监控脚本

    [root@localhost ]# crontab -l
    */1 * * * * /root/autoCheck.sh

    检查服务并发送告警邮件

    [root@rhel8 shell]# cat auto_monitor_service.sh 
    #!/bin/bash
    # auto monitor server
    # by author tanbaobao 2020/06/12
    
    echo -e  "33[31m 33[1m"
    EMAIL="email.txt"
    DATE=`date`
    NETWORK_NAME=ens160
    
    cat <<EOF
    ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    ++++++++++++++++++++ welcom to use auto monitor system +++++++++++++++++++
    ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    EOF
    
    if [ -z $1 ];then
        echo -e "33[32mUsage: { sh $0 nginx | mysql | server_list.txt}33[0m"
        echo
        exit
    fi
    
    # if [ ! -f $EMAIL ];then
    #    touch $EMAIL
    # else
    #    rm -rf $EMAIL
    #    touch $EMAIL
    # fi
    
    sleep 2
    M_IPADDR=`ifconfig $NETWORK_NAME|grep -w "inet"|awk '{print $2}'`
    if [ -f "$1" -a "$1" == "server_list.txt" ];then
        for i in `cat server_list.txt|grep -v "#"`
        do
            count=`ps -ef | grep $i|grep -v "grep"|grep -v "auto_monitor_service"|wc -l`
            if [ $count -eq 0 ];then        
    cat >$EMAIL <<EOF
    ***************** Email Server Info******************
    通知类型:故障
    服务:$i
    主机:$M_IPADDR
    状态:警告
    日期/时间:$DATE
    额外信息:
    CRITICAL - $i Server Connection Refused,Please Check.
    *****************************************************
    EOF
                dos2unix $EMAIL
                echo -e "33[32mThe Monitor $i Warning,Please Check.33[0m"
                mail -s "$M_IPADDR $i Warning" qq邮箱@qq.com < $EMAIL >>/dev/null 2>&1
            else    
                echo -e "33[32mThe Monitor $i Server 200 OK!33[0m"
            fi
            echo -e "
    33[32m------------------------------------------------------------33[1m"
            echo "Done."
        done
    else
        count=`ps -ef | grep $1|grep -v "grep"|grep -v "auto_monitor_service"|wc -l`
        if [ $count -eq 0 ];then
    cat >$EMAIL <<EOF
    ***************** Email Server Info******************
    通知类型:故障
    服务:$1
    主机:$M_IPADDR
    状态:警告
    日期/时间:$DATE
    额外信息:
    CRITICAL - $1 Server Connection Refused,Please Check.
    *****************************************************
    EOF
            dos2unix $EMAIL
            echo -e "33[32mThe Monitor $1 Warning,Please Check.33[0m"
            mail -s "$M_IPADDR $1 Warning" qq邮箱@qq.com < $EMAIL >>/dev/null 2>&1    
        else
            echo -e "33[32mThe Monitor $1 Server 200 OK!33[0m"
        fi
        echo -e "
    33[32m------------------------------------------------------------33[1m"
        echo "Done."
    fi
  • 相关阅读:
    给自己新申请的阿里云服务器ECS免费搭建WDCP环境
    maven run 配置jre VM arguments配置 (转)
    JSON.stringify 语法实例讲解(转)
    Vue-cli proxyTable 解决开发环境的跨域问题(转)
    JS几种数组遍历方式以及性能分析对比(转 未经测试,先mark)
    调用web接口跨域问题
    Jquery获取浏览器窗口和Body长宽
    idea tab页签颜色不明显,自定义颜色解决。
    疯狂Java学习笔记(009)
    疯狂Java学习笔记(001)
  • 原文地址:https://www.cnblogs.com/HeiDi-BoKe/p/11610984.html
Copyright © 2011-2022 走看看