zoukankan      html  css  js  c++  java
  • 增加服务器监控

    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"
    
    "$warningMsg
                echo -e "Error:"$warningMsg
                echo -e "From:$from
    To:$to
    CC:$cc
    Subject:$subject
    
    $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!
    "
        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"%!
    "
            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
  • 相关阅读:
    Liunx安装MQTT服务器
    ORACLE 把一个用户的权限给与另一个用户
    liunx 后台运行python代码
    ORACLE APEX 交互式网格动态操作
    oracle 创建表字段
    oracle 触发器
    ORACLE 程序包
    ORACLE 游标基本使用
    oracle 不等于 查询列中含有null
    liunx安装oracle 客户端
  • 原文地址:https://www.cnblogs.com/wangzhanhua/p/10461109.html
Copyright © 2011-2022 走看看