$ 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()