zoukankan      html  css  js  c++  java
  • shell脚本-监控及邮件提醒

    首先写一个邮件提醒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'] = '服务器报警请注意!'
        msg['From'] = 'python4_mail@163.com'
        msg['To'] = 'longbaby1101@qq.com'
        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)

    然后写自己的监控脚本

    #/bin/bash
    bu=`free | awk 'NR==2{print $6}'`
    to=`free | awk 'NR==2{print $2}'`
    mem=`expr "scale=2;$bu/$to" |bc -l | cut -d. -f2`
    if(($mem >= 70))
            then
            msg="TIME:$(date +%F_%T)
                     HOSTNAME:$(hostname)
                     IPADDR:$(ifconfig |awk 'NR==2{print $2}')
                     MSG:内存high了high了!已经用了${mem}%"
                echo $msg
            /usr/bin/pymail.py $msg
    fi
    systemctl status nginx
    if(($?!=0))
            then
                    msg="TIME:$(date +%F_%T)
                     HOSTNAME:$(hostname)
                     IPADDR:$(ifconfig |awk 'NR==2{print $2}')
                     MSG:   Nginx 进程出现异常请注意查看!"
                echo $msg
            /usr/bin/pymail.py $msg
    fi
    systemctl status nfs
    if(($?!=0))
            then
                    msg="TIME:$(date +%F_%T)
                     HOSTNAME:$(hostname)
                     IPADDR:$(ifconfig |awk 'NR==2{print $2}')
                     MSG:   NFS 进程出现异常请注意查看!"
                echo $msg
            /usr/bin/pymail.py $msg
    fi

    之后再定时任务中写入每分钟执行一次

    把两个nginx 和 nfs 服务关闭

    等待一分钟之后

  • 相关阅读:
    P3501 [POI2010]ANT-Antisymmetry
    P3498 [POI2010]KOR-Beads(hash表)
    UVA10298 Power Strings
    UVA1714 Keyboarding(bfs)
    P4289 [HAOI2008]移动玩具(bfs)
    Ubuntu分辨率太小的解决方案
    Ubuntu分辨率太小的解决方案
    主板亮红灯,显示器没信号
    主板亮红灯,显示器没信号
    VS注释与取消注释快捷键
  • 原文地址:https://www.cnblogs.com/DragonFire/p/6612690.html
Copyright © 2011-2022 走看看