• 编写监控脚本,监控集群内所有服务存活状态,内存、磁盘剩余率检测,异常则发送报警邮件


    发送邮件Python程序

    #!/usr/bin/python
    # -*- coding: UTF-8 -*-
    import sys
    import smtplib
    import email.mime.multipart
    import email.mime.text
    
    server = 'smtp.163.com'
    port = '25'
    
    def sendmail(server,port,user,pwd,msg):
        smtp = smtplib.SMTP()
        smtp.connect(server,port)
        smtp.login(user, pwd)
        smtp.sendmail(msg['from'], msg['to'], msg.as_string())
        smtp.quit()
        print('Email has send out !')
    
    
    if __name__ == '__main__':
        msg = email.mime.multipart.MIMEMultipart()
        msg['Subject'] = 'WARNNING'
        msg['From'] = 'python4_mail@163.com'
        msg['To'] = 'xxxxxx@xxx.xxx'
        user = 'python4_mail'
        pwd = 'sbalex3714'
        content='%s
    %s' %('
    '.join(sys.argv[1:4]),' '.join(sys.argv[4:]))
    
        txt = email.mime.text.MIMEText(content, _charset='utf-8')
        msg.attach(txt)
    
        sendmail(server,port,user,pwd,msg)
    

      编辑为/usr/bin/mail,添加可执行权限

    监控脚本

    #!/bin/bash
    
    cpu_limit=90
    mem_limit=90
    disk_inode_limit=90
    disk_space_limit=90
    disk='/dev/sda1'
    
    function bc_install(){
        rpm -qa |grep '^bc.*$' >> /dev/null
        if [[ $? -ne 0 ]]; then
            yum install bc -y
        fi
    }
    
    function nettools_install(){
        rpm -qa |grep net-tools >> /dev/null
        if [[ $? -ne 0 ]]; then
            yum install net-tools -y
        fi
    }
    
    function monitor_cpu(){
        cpu_free=`vmstat 1 5 |awk 'NR>=3{x = x + $15} END {print x/5}' |awk -F. '{print $1}'`
        cpu_use=$((100-cpu_free))
        if [[ $cpu_use -gt $cpu_limit ]]; then
            msg="TIME:$(date +%F_%T)
                 HOSTNAME:$(hostname)
                 IPADDR:$(ifconfig |awk 'NR==2{print $2}')
                 MSG:CPU usage exceeds the limit,current value is ${cpu_use}%"
            echo $msg
            /usr/bin/mail $msg
        fi
    }
    
    function monitor_mem(){
        mem_total=`free |awk 'NR==2{print $2}'`
        mem_use=`free |awk 'NR==2{print $3}'`
        mem_per=`echo "scale=2;$mem_use/$mem_total" |bc -l|cut -d. -f2`
        if [[ $mem_per -gt $mem_limit ]]; then
                msg="TIME:$(date +%F_%T)
                     HOSTNAME:$(hostname)
                     IPADDR:$(ifconfig |awk 'NR==2{print $2}')
                     MSG:Memory usage exceeds the limit,current value is ${mem_per}%"
                echo $msg
                /usr/bin/mail $msg
        fi
    }
    
    function monitor_disk_inode(){
        inode_use=`df -i $disk |awk 'NR==2{print $5}' |cut -d% -f1`
        if [[ $inode_use -gt $disk_inode_limit ]]; then
                msg="TIME:$(date +%F_%T)
                     HOSTNAME:$(hostname)
                     IPADDR:$(ifconfig |awk 'NR==2{print $2}')
                     MSG:Disk inode usage exceeds the limit,current value is ${inode_use}%"
                echo $msg
                /usr/bin/mail $msg
        fi
    }
    
    function monitor_disk_space(){
        space_use=`df $disk |awk 'NR==2{print $5}'|cut -d% -f1`
        if [[ $space_use -gt $disk_space_limit ]]; then
                msg="TIME:$(date +%F_%T)
                     HOSTNAME:$(hostname)
                     IPADDR:$(ifconfig |awk 'NR==2{print $2}')
                     MSG:Disk space usage exceeds the limit,current value is ${space_use}%"
                echo $msg
                /usr/bin/mail $msg
        fi
    }
    
    bc_install
    nettools_install
    monitor_cpu &>> /tmp/monitor.log
    monitor_mem &>> /tmp/monitor.log
    monitor_disk_inode &>> /tmp/monitor.log
    monitor_disk_space &>> /tmp/monitor.log
    

      

  • 相关阅读:
    P4718 [模板]Pollard-Rho算法
    python爬虫模板
    Codeforces1248F. Catowice City
    P3980 [NOI2008]志愿者招募 (费用流)
    P2805 [NOI2009]植物大战僵尸 (拓扑排序 + 最小割)
    P3157 [CQOI2011]动态逆序对
    P2634 [国家集训队]聪聪可可 (点分治)
    HDU6703 array (线段树)
    Codeforces750E. New Year and Old Subsequence (线段树维护DP)
    Codeforces301D. Yaroslav and Divisors
  • 原文地址:https://www.cnblogs.com/freelandun/p/6754814.html
Copyright © 2020-2023  润新知