• 监控系统脚本


    #!/bin/sh
    #FileName: Monitor.sh
    #make by GuoYanLiang
    #source ~/.bash_profile
    export LANG="zh_CN.GB2312"
    export LC_ALL="zh_CN.GB2312"
    export LC_CTYPE="zh_CN.GB2312"
    #locale
    CheckDir="/root/gyl/Monitor"
    [ ! -d "$CheckDir" ] && mkdir -p $CheckDir
    cd $CheckDir
    MailFile=/root/gyl/Monitor/MailSendFile.log
    MonitorTMP=/root/gyl/Monitor/MonitorTMP.log
    MonitorLog=/root/gyl/Monitor/logs/Monitor_$(date +"%Y%m%d").log
    cp -f /dev/null $MailFile
    cp -f /dev/null $MonitorTMP
    #cp -f /dev/null $MonitorLog
    #===================================================================init===================================================================
    init()
    {
    IPAddress=`/sbin/ifconfig | grep 'inet addr:' | head -1 | awk '{print $2}' | sed 's/addr://g'`;
    NOWTIME=$(date "+%F %T");
    MAILTO="280637803@qq.com,13866112949@139.com";
    MAIL_SUB="服务器性能监控";
    MAIL_FROM="运维组";
    MAIL_SERVER="192.168.18.8"
    MAIL_SERVER_PORT="22"
    ROOT_ID_RSA=~/.ssh/18.8_id_rsa
    unset StateTMP
    unset State
    MailSendFlag=0
    }
    #===================================================================mhead===================================================================
    mhead()
    {
    echo "" >>$MailFile
    echo "服务器 : $IPAddress" >>$MailFile
    echo "时  间 : $NOWTIME" >>$MailFile
    echo -e "状  态 : $State" >>$MailFile
    echo "" >>$MailFile
    }
    #===================================================================smail===================================================================
    smail()
    {
    scp -i $ROOT_ID_RSA -P $MAIL_SERVER_PORT $MailFile root@$MAIL_SERVER:$MailFile
    ssh -i $ROOT_ID_RSA -p $MAIL_SERVER_PORT root@$MAIL_SERVER  "export LANG=zh_CN.GB2312;export LC_ALL=zh_CN.GB2312;export LC_CTYPE=zh_CN.GB2312;mail -s $MAIL_SUB $MAILTO -F $MAIL_FROM < $MailFile;cp /dev/null $MailFile;"
    }
    #===================================================================Monitor_System===================================================================
    Monitor_System()
    {
    distrib="unknown"
    if [ -f /etc/SuSE-release ] ; then
    distrib="`head -1 /etc/SuSE-release`"
    fi
    if [ -f /etc/redhat-release ] ; then
    distrib="`head -1 /etc/redhat-release`"
    fi
    HostName=`hostname`
    KernelVer=`uname -r`
    CPUNum=`cat /proc/cpuinfo | grep "processor" | wc -l `
    CPUMHz=`cat /proc/cpuinfo | grep MHz | uniq | awk -F: '{print $2}'`
    MemTotal=`free -m | head -2 | tail -1 | awk '{print $2}'`
    MemFree=`free -m | head -3 | tail -1 | awk '{print $3}'`
    echo "系统检测时间 : " `date +%Y"."%m"."%d" "%k":"%M":"%S` >> $MonitorTMP
    echo "主机 : " $HostName >> $MonitorTMP
    echo "IP地址 : " $IPAddress >> $MonitorTMP
    echo "系统版本 : " $distrib >> $MonitorTMP
    echo "系统内核 : " $KernelVer >> $MonitorTMP
    echo "CPU数目 : " $CPUNum 个 >> $MonitorTMP
    echo "CPU频率 : " $CPUMHz MHz >> $MonitorTMP
    echo "总内存 : " $MemTotal M >> $MonitorTMP
    echo "空闲内存 : " $MemFree M >> $MonitorTMP
    echo "系统进程数 : " `ps -efl | wc -l` 个 >> $MonitorTMP
    echo "系统启动时间 : " `uptime | awk -F, '{print $1,$2}'` >> $MonitorTMP
    echo "系统检测时间 : " `date +%Y"."%m"."%d" "%k":"%M":"%S` >> $MailFile
    echo "主机 : " $HostName >> $MailFile
    echo "IP地址 : " $IPAddress >> $MailFile
    echo "系统版本 : " $distrib >> $MailFile
    echo "系统内核 : " $KernelVer >> $MailFile
    echo "CPU数目 : " $CPUNum 个 >> $MailFile
    echo "CPU频率 : " $CPUMHz MHz >> $MailFile
    echo "总内存 : " $MemTotal M >> $MailFile
    echo "空闲内存 : " $MemFree M >> $MailFile
    echo "系统进程数 : " `ps -efl | wc -l` 个 >> $MailFile
    echo "系统启动时间 : " `uptime | awk -F, '{print $1,$2}'` >> $MailFile
    Upday=`uptime | awk '{print $3}'`
    if [ $Upday -lt 1 ]
    then
    MailSendFlag=1
    echo "服务器最近有重启,请检查" >> $MonitorTMP
    echo "服务器最近有重启,请检查" >> $MailFile
    StateTMP=" 系统启动时间 : "`uptime | awk -F, '{print $1,$2}'`",服务器最近有重启,请检查"
    fi
    }
    #===================================================================Monitor_memory===================================================================
    # Monitor the MemFree utilization if exceed the threshold
    Monitor_memory()
    {
    MemThreshold=45
    MemTotal=`free -m | head -2 | tail -1 | awk '{print $2}'`
    MemFree=`free -m | head -3 | tail -1 | awk '{print $3}'`
    MemPercentage=`expr $MemFree * 100 / $MemTotal`
    echo "" >>$MonitorTMP;
    echo "内存使用情况:" >>$MonitorTMP;
    free -m >>$MonitorTMP;
    echo "" >>$MonitorTMP;
    echo "内存占用率: $MemPercentage % = ($MemFree/$MemTotal)*100%" >>$MonitorTMP
    if [ $MemPercentage -gt $MemThreshold ]; then
    MailSendFlag=1
    echo "内存占用率 $MemPercentage 高于阀值 $MemThreshold %" >>$MonitorTMP
    echo "" >>$MailFile
    echo "内存使用情况:" >>$MailFile
    free -m >>$MailFile
    echo "" >>$MailFile
    echo "内存占用率 $MemPercentage 高于阀值 $MemThreshold %" >>$MailFile
    StateTMP="$StateTMP 内存占用为 $MemPercentage 高于阀值 $MemThreshold %"
    fi
    }
    #===================================================================Monitor_cpu===================================================================
    # Monitor the CPU Max,Avg utilization if exceed the threshold
    Monitor_cpu()
    {
    CPUAvgThreshold=8
    CPUMaxThreshold=15
    if [ -x /usr/bin/vmstat ] ; then
    CPUMax=0
    CPUTotal=0
    for x in `vmstat 2 6 | tail -5 | awk '{print 100-$15}'`
    do
    if [ $x -gt $CPUMax ]; then
    CPUMax="$x"
    fi
    CPUTotal=`expr $CPUTotal + $x`
    done
    CPUAvg=`expr $CPUTotal / 5`
    echo "" >>$MonitorTMP
    echo "CPU最大占用率 : " $CPUMax >>$MonitorTMP
    if [ $CPUMax -gt $CPUMaxThreshold ]; then
    MailSendFlag=1
    echo "CPU最大占用率 $CPUMax 高于阀值 $CPUMaxThreshold %" >>$MonitorTMP
    echo "" >>$MailFile
    echo "CPU最大占用率 $CPUMax 高于阀值 $CPUMaxThreshold %" >>$MailFile
    StateTMP="$StateTMP CPU最大占用率 $CPUMax 高于阀值 $CPUMaxThreshold %"
    fi
    echo "" >>$MonitorTMP
    echo "CPU平均占用率 : " $CPUAvg >>$MonitorTMP
    if [ $CPUAvg -gt $CPUAvgThreshold ]; then
    MailSendFlag=1
    echo "CPU平均占用率 $CPUAvg 高于阀值 $CPUAvgThreshold %" >>$MonitorTMP
    echo "" >>$MailFile
    echo "CPU平均占用率 $CPUAvg 高于阀值 $CPUAvgThreshold %" >>$MailFile
    StateTMP="$StateTMP CPU平均占用率 $CPUAvg 高于阀值 $CPUAvgThreshold %"
    fi
    fi
    }
    #===================================================================Monitor_Process_CPU===================================================================
    # Check the ProcessCPU Maximum utilization if exceed the threshold
    Monitor_Process_CPU()
    {
    ProcessCPUThreshold=30
    echo "" > Process_CPU.log
    echo " Top5: 单个进程CPU占用率 : " >> Process_CPU.log
    ps aux | head -1 >> Process_CPU.log
    ps aux | grep -v "ps aux" | grep -v "Monitor.sh" | grep -v USER | grep -v wireless-bak | sort -nr -k 3 | head -5 >> Process_CPU.log
    cat Process_CPU.log >>$MonitorTMP
    ProcessCPU=`cat Process_CPU.log | grep -v Top5 | grep -v USER | head -2 | tail -1 | awk '{print $3}' | cut -f 1 -d "."`
    if [ $ProcessCPU -gt $ProcessCPUThreshold ]; then
    MailSendFlag=1
    echo "" >>$MonitorTMP
    echo "单个进程CPU占用率最大为 $ProcessCPU 高于阀值 $ProcessCPUThreshold %" >> $MonitorTMP
    echo "" >>$MailFile
    cat Process_CPU.log >>$MailFile
    echo "" >>$MailFile
    echo "单个进程CPU占用率最大为 $ProcessCPU 高于阀值 $ProcessCPUThreshold %" >> $MailFile
    StateTMP="$StateTMP 单个进程CPU占用率最大为 $ProcessCPU 高于阀值 $ProcessCPUThreshold %"
    fi
    }
    #===================================================================Monitor_Process_MEM===================================================================
    # Check the ProcessMEM Maximum utilization if exceed the threshold
    Monitor_Process_MEM()
    {
    ProcessMEMThreshold=20
    echo "" > Process_MEM.log
    echo " Top5: 单个进程内存占用率 : " >> Process_MEM.log
    ps aux | head -1 >> Process_MEM.log
    ps aux | grep -v "ps aux" | grep -v "Monitor.sh" | grep -v USER | grep -v wireless-bak | sort -nr -k 4 | head -5 >> Process_MEM.log
    cat Process_MEM.log >>$MonitorTMP
    ProcessMEM=`cat Process_MEM.log | grep -v Top5 | grep -v USER | head -2 | tail -1 | awk '{print $4}' | cut -f 1 -d "."`
    if [ $ProcessMEM -gt $ProcessMEMThreshold ]; then
    MailSendFlag=1
    echo "" >>$MonitorTMP
    echo "单个进程内存占用率最大为 $ProcessMEM 高于阀值 $ProcessMEMThreshold %" >> $MonitorTMP
    echo "" >>$MailFile
    cat Process_MEM.log >>$MailFile
    echo "" >>$MailFile
    echo "单个进程内存占用率最大为 $ProcessMEM 高于阀值 $ProcessMEMThreshold %" >> $MailFile
    StateTMP="$StateTMP 单个进程内存占用率最大为 $ProcessMEM 高于阀值 $ProcessMEMThreshold %"
    fi
    }
    #===================================================================Monitor_Disk===================================================================
    Monitor_Disk()
    {
    DiskThreshold=88
    DiskNum=0
    echo "" >> $MonitorTMP
    echo "磁盘分区占用率:" >> $MonitorTMP
    df -hP >> $MonitorTMP
    for x in `df -hP | grep / | awk '{print $5}' | sed 's/%//g'`
    do
    if [ $x -gt $DiskThreshold ]; then
    DiskNum=`expr $DiskNum + 1`
    fi
    done
    if [ $DiskNum -gt 0 ] ; then
    MailSendFlag=1
    echo "" >> $MonitorTMP
    echo "磁盘空间占用高于阀值 $DiskThreshold %" >> $MonitorTMP
    echo "" >> $MailFile
    echo "磁盘分区占用率:" >> $MailFile
    df -hP >> $MailFile
    echo "" >> $MailFile
    echo "$DiskNum 个磁盘分区占用率高于阀值 $DiskThreshold %" >> $MailFile
    StateTMP="$StateTMP $DiskNum 个磁盘分区占用率高于阀值 $DiskThreshold %"
    fi
    }
    #===================================================================Monitor_process===================================================================
    Monitor_process()
    {
    OS_Process=(java httpd)
    OS_Process_Num=${#OS_Process[@]}
    for (( i=0; i<$OS_Process_Num; i++ ))
    do
    Process_Num=0
    Process_Num=`ps -efl | grep ${OS_Process[i]} | grep -v grep | wc -l`
    echo "" >>$MonitorTMP
    echo "${OS_Process[i]} 有 $Process_Num 个进程运行." >>$MonitorTMP
    if [ $Process_Num -eq 0 ] ; then
    MailSendFlag=1
    echo "${OS_Process[i]} 进程不存在" >>$MonitorTMP
    echo "" >>$MailFile
    echo "业务进程信息:" >>$MailFile
    echo "${OS_Process[i]} 有 $Process_Num 个进程运行." >>$MailFile
    echo "${OS_Process[i]} 进程不存在" >>$MailFile
    StateTMP="$StateTMP ${OS_Process[i]} 进程不存在"
    fi
    done
    }
    #===================================================================Monitor_Ping===================================================================
    Monitor_Ping()
    {
    Ping_Num=10
    Ping_Time=0.02
    DesIP=(192.168.28.22 192.168.28.26 192.168.18.49)
    DesIP_Num=${#DesIP[@]}
    echo "" >>$MonitorTMP
    echo "网络Ping检测:" >>$MonitorTMP
    for (( j=0; j<$DesIP_Num; j++ ))
    do
    ping -i $Ping_Time -c $Ping_Num ${DesIP[j]} > Ping_Out.log
    echo "" >>$MonitorTMP
    echo `expr $j + 1`" ,Ping测试: ping -i $Ping_Time -c $Ping_Num ${DesIP[j]}" >>$MonitorTMP
    cat Ping_Out.log >>$MonitorTMP
    Ping_Loss=`cat Ping_Out.log | grep % | awk '{print $6}' | sed 's/%//g'`
    if [ $Ping_Loss -gt 0 ] ; then
    MailSendFlag=1
    echo "" >>$MailFile
    echo "Ping测试: ping -i $Ping_Time -c $Ping_Num ${DesIP[j]} 丢包率: $Ping_Loss % ,请检查网络" >>$MailFile
    cat Ping_Out.log >>$MailFile
    StateTMP="$StateTMP Ping ${DesIP[j]} 丢包率: $Ping_Loss % "
    fi
    done
    }
    #===================================================================Monitor_Ifconfig===================================================================
    Monitor_Ifconfig()
    {
    ErrorsNum=0
    echo "" >>$MonitorTMP
    echo "网卡信息(ifconfig):" >> $MonitorTMP
    /sbin/ifconfig >> $MonitorTMP
    for x in `ifconfig | grep errors | awk '{print $3}' | sed 's/errors://g'`
    do
    if [ $x -gt 0 ]; then
    ErrorsNum=`expr $ErrorsNum + 1`
    fi
    done
    if [ $ErrorsNum -gt 0 ] ; then
    MailSendFlag=1
    echo "网卡数据包存在错误" >> $MonitorTMP
    echo "" >> $MailFile
    echo "网卡信息(ifconfig):" >> $MailFile
    ifconfig >> $MailFile
    echo "网卡数据包存在错误" >> $MailFile
    StateTMP="$StateTMP 网卡数据包存在错误"
    fi
    }
    #===================================================================Monitor_Config===================================================================
    Monitor_Config()
    {
    ConfigName=(ifcfg-bond0 ifcfg-eth0 ifcfg-eth1 network resolv.conf hosts rc.local mount fstab crontab iptables route)
    ConfigName_Num=${#ConfigName[@]}
    ConfigCheck=("cat /etc/sysconfig/network-scripts/ifcfg-bond0" "cat /etc/sysconfig/network-scripts/ifcfg-eth0" "cat /etc/sysconfig/network-scripts/ifcfg-eth1" "cat /etc/sysconfig/network" "cat /etc/resolv.conf" "cat /etc/hosts" "cat /etc/rc.local" "mount" "cat /etc/fstab" "crontab -l" "/sbin/iptables -L" "/sbin/route")
    echo "" >>$MonitorTMP
    echo "监控系统配置信息:" >>$MonitorTMP
    for (( k=0; k<$ConfigName_Num; k++ ))
    do
    echo "" > ${ConfigName[k]}.log
    ${ConfigCheck[k]} >> ${ConfigName[k]}.log
    echo "" >>$MonitorTMP
    echo `expr $k + 1`" , ${ConfigName[k]} 配置:" >> $MonitorTMP
    cat ${ConfigName[k]}.log >> $MonitorTMP
    if [ -f ${ConfigName[k]}.ref ]
    then
    diff ${ConfigName[k]}.log ${ConfigName[k]}.ref > ${ConfigName[k]}.diff
    if [ `cat ${ConfigName[k]}.diff | wc -l` -gt 0 ]
    then
    MailSendFlag=1
     
    `expr $SectionN + 1`
     
    echo "" >>$MonitorTMP
    echo `expr $k + 1`" , ${ConfigName[k]} 原配置:" >>$MonitorTMP
    cat ${ConfigName[k]}.ref >>$MonitorTMP
    echo "" >>$MonitorTMP
    echo `expr $k + 1`" , ${ConfigName[k]} 配置更改内容:" >>$MonitorTMP
    cat ${ConfigName[k]}.diff >>$MonitorTMP
    echo "" >> $MailFile
    echo "${ConfigName[k]} 现配置:" >> $MailFile
    cat ${ConfigName[k]}.log >> $MailFile
    echo "" >> $MailFile
    echo "${ConfigName[k]} 原配置:" >> $MailFile
    cat ${ConfigName[k]}.ref >> $MailFile
    echo "" >> $MailFile
    echo "${ConfigName[k]} 配置更改内容:" >> $MailFile
    cat ${ConfigName[k]}.diff >> $MailFile
    StateTMP="$StateTMP ${ConfigName[k]} 配置已经更改"
    fi
    cp -f ${ConfigName[k]}.log ${ConfigName[k]}.ref
    else
    cp -f ${ConfigName[k]}.log ${ConfigName[k]}.ref
    fi
    done
    }
    #===================================================================Monitor_Main===================================================================
    init;
    Monitor_System;
    Monitor_memory;
    Monitor_cpu;
    Monitor_Process_CPU;
    Monitor_Process_MEM;
    Monitor_Disk;
    Monitor_process;
    Monitor_Ping;
    Monitor_Ifconfig;
    Monitor_Config;
    echo "" >> $MonitorLog
    echo "" >> $MonitorLog
    echo ===================================================== 开始: $NOWTIME ===================================================== >> $MonitorLog
    echo "" >> $MonitorLog
    cat $MonitorTMP >> $MonitorLog
    echo "" >> $MonitorLog
    echo ===================================================== 结束: $(date "+%F %T") ===================================================== >> $MonitorLog
    echo "" >> $MonitorLog
    echo "" >> $MonitorLog
    if [ $MailSendFlag != 0 ] ; then
    State=$StateTMP
    mhead;
    smail;
    echo OK
    fi

  • 相关阅读:
    CentOS 6.3下安装腾达USB无线网卡遇到的问题及解决方法
    验证resneXt,densenet,mobilenet和SENet的特色结构
    比较语义分割的几种结构:FCN,UNET,SegNet,PSPNet和Deeplab
    比较 VGG, resnet和inception的图像分类效果
    强化学习基础
    深度学习的知识点
    卷积神经网络CNN
    信息论的知识点
    理论机器学习
    计算理论基础
  • 原文地址:https://www.cnblogs.com/zgqbky/p/12029683.html
Copyright © 2020-2023  润新知