磁盘空间监控--邮件报警
Posted lbqiang
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了磁盘空间监控--邮件报警相关的知识,希望对你有一定的参考价值。
#采用psutil获取磁盘信息,判断磁盘占用空间是否大于预设值
#!/usr/bin/env python
#coding:utf-8
import psutil
import socket
import subprocess
import smtplib
from email.mime.text import MIMEText
import smtplib
from email.mime.text import MIMEText
from email.header import Header
import os
def send_mail(subject):
message = MIMEText(subject,‘plain‘, ‘utf-8‘)
message[‘From‘] = mail_user # 发件人昵称
message[‘subject‘]=subject #邮件主题
message[‘To‘] = ‘[email protected]‘ # 收件人昵称
try:
smtpObj = smtplib.SMTP()
smtpObj.connect(mail_host)
smtpObj.login(mail_user,mail_pass)
smtpObj.sendmail(mail_user,receiver, message.as_string())
print("邮件发送成功")
except smtplib.SMTPException as e:
print("Error: 无法发送邮件")
except Exception as error:
print(error)
def disk_num(disk,**kwargs):
disk[‘usage‘]=psutil.disk_usage(disk[‘mountpoint‘])
if disk[‘usage‘][3] > 50:
ip=os.popen(‘curl ip.6655.com/ip.aspx‘).readlines()[0] #获取服务器公网ip
xxx=‘目录‘ + str(disk[‘mountpoint‘]) + ‘,‘ + ‘挂载磁盘:‘+ str(disk[‘device‘]) + ‘,‘+ str(‘目前使用百分比%s‘%(disk[‘usage‘][3])+ ‘服务器ip:‘ + str(ip))
send_mail(xxx)
if __name__=="__main__":
mail_host=‘smtp.163.com‘
mail_user="发件人邮箱"
mail_pass="邮箱密码"
receiver = ["[email protected]",]#收件人邮箱
disk=psutil.disk_partitions()
disk_dict={}
for i in disk:
disk_dict[‘device‘]=i.device
disk_dict[‘mountpoint‘]=i.mountpoint
disk_num(disk_dict)
邮件接收格式:
以上是关于磁盘空间监控--邮件报警的主要内容,如果未能解决你的问题,请参考以下文章