码迷,mamicode.com
首页 > 其他好文 > 详细

磁盘空间监控--邮件报警

时间:2018-05-22 10:44:12      阅读:598      评论:0      收藏:0      [点我收藏+]

标签:mes   user   ons   usr   ade   大于   sock   psutil   popen   

#采用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‘] = ‘xxxx@163.com‘ # 收件人昵称
  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 = ["xxx@163.com",]#收件人邮箱

    disk=psutil.disk_partitions()

    disk_dict={}
    for i in disk:
    disk_dict[‘device‘]=i.device
    disk_dict[‘mountpoint‘]=i.mountpoint
    disk_num(disk_dict)

邮件接收格式:

技术分享图片

 

磁盘空间监控--邮件报警

标签:mes   user   ons   usr   ade   大于   sock   psutil   popen   

原文地址:https://www.cnblogs.com/lbqiang/p/9070510.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!