• python 脚本监控硬件磁盘状态并发送报警邮件


    $ cat checkdisk.py 
    #!/usr/bin/python
    # -*- coding: utf-8 -*-
    
    import os
    import socket
    import smtplib
    from email.mime.text import MIMEText
    from email.header import Header
    
    mail_host = "smtp.exmail.qq.com"
    mail_user = "yunwei@ws.com"
    mail_pass = "yNfY65Gra"
    Hostname = socket.gethostname() 
    print (Hostname)
    Message = Hostname + '^^__^^' + 'Disk is Failed!!!' +'请尽快修复磁盘'
    print (Message)
    
    sender = 'yunwei-monitor@donews.com'
    receivers = ['lixng@ws.com','yunw@do.com']
    
    def CheckDisk():
        #三个参数:第一个为文本内容,第二个 plain 设置文本格式,第三个 utf-8 设置编码
        #message = MIMEText('%s Disk is Failed','plain','utf-8') %(Hostname)
        message = MIMEText(Message,'plain','utf-8')
        message['From'] = Header("Disk is Failed ", 'utf-8') # 发送者
        message['To'] = Header("运维", 'utf-8')   # 接收者
        subject = '%s Disk is Failed' %Hostname
        message['Subject'] = Header(subject,'utf-8')
        try:
            smtpobj = smtplib.SMTP()
            smtpobj.connect(mail_host,25)
            smtpobj.login(mail_user,mail_pass)
            smtpobj.sendmail(sender,receivers,message.as_string())
            print("邮件发送成功")
        except smtplib.SMTPException:
            print("Error: 无法发送邮件")
    
    
    
    file1 = "/tmp/disk.txt"
    os.system("""/root/check_disk_status_V1/MegaCli64 -PDList -aAll |grep "Firmware state" |grep -E "Unconfigured|Failed" > %s""" % file1)
    
    print (os.path.getsize(file1))
    if os.path.getsize(file1) != 0:
            CheckDisk()
    
    
    file2 = "/tmp/disk2.txt"
    os.system("""/root/check_disk_status_V1/hpacucli ctrl all show config  |grep "Failed" > %s""" % file2)
    
    print (os.path.getsize(file2))
    if os.path.getsize(file2) != 0:
            CheckDisk()
    

      

  • 相关阅读:
    Scrum 冲刺博客第五篇
    Scrum 冲刺博客第四篇
    Scrum 冲刺博客第三篇
    ajax send()
    form action中get post传递参数的问题
    struts2 iterator中if标签的使用
    表格内容自动换行
    从js向Action传中文参数出现乱码问题的解决方法
    java开发环境搭建
    Can not find the tag library descriptor for "http://java.sun.com/jsp/jstl/
  • 原文地址:https://www.cnblogs.com/lixinliang/p/13926098.html
Copyright © 2020-2023  润新知