zoukankan      html  css  js  c++  java
  • 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()
    

      

  • 相关阅读:
    "Key Violation" with ClientDataSet
    c# 的关键字 params,out,ref
    eval && JSON.parse
    json2.js
    C#中的索引器
    call , apply , caller , callee
    iphone&ipad图标去除高亮的光圈效果
    调用系统路线导航
    调科大讯飞出现的问题
    得到汉字首字母在表中的顺序位置
  • 原文地址:https://www.cnblogs.com/lixinliang/p/13926098.html
Copyright © 2011-2022 走看看