Linux 发邮件磁盘空间监控(python)

 更新时间:2016年04月23日 10:55:13   投稿:mdxy-dxy  
这篇文章主要介绍了Linux发邮件磁盘空间监控功能,python实现,需要的朋友可以参考下

核心代码:

#!/usr/bin/python
# -*- coding: UTF-8 -*-


import smtplib
import os
import commands,time 
from email.mime.text import MIMEText
#from email import MIMEText
disk_free=os.popen('df -lh')
list_disk=disk_free.read()
mailto_list=["2880329185@qq.com","2881280685@qq.com","2880089704@qq.com","2880329180@qq.com"]
mail_host="smtp.139.com" 
mail_user="user"  
mail_pass="password"  
mail_postfix="jljgl@.com" 
use01=commands.getstatusoutput("df -lh|awk '{print $4}'|grep '%'|awk -F '%' '{print $1}'|grep -v Use")
use02=commands.getstatusoutput("df -lh|awk '{print $5}'|grep '%'|awk -F '%' '{print $1}'|grep -v Use")
getrecord=commands.getstatusoutput("cat /home/oracle/script/mail.log|awk '{print $1}'")
gettime=commands.getstatusoutput("cat /home/oracle/script/mail.log|awk '{print $2}'")


free01=use01[1]
free02=use02[1]+'\n'+use01[1]
new_time=time.strftime("%Y%m%d", time.localtime())


def send_mail(to_list,sub,content):
  me=mail_postfix
  msg = MIMEText(content,_subtype='plain',_charset='gb2312')
  msg['Subject'] = sub
  msg['From'] = me
  msg['To'] = ";".join(to_list)
  try:
    server = smtplib.SMTP()
    server.connect(mail_host)
    server.login(mail_user,mail_pass)
    server.sendmail(me, to_list, msg.as_string())
    server.close()
    return True
  except SyntaxError:
    pass
    return False
if __name__ == '__main__':
    if free02>30:
        id=0
        if new_time>gettime[1]:
            while 1:
                getrecord=commands.getstatusoutput("cat /home/oracle/script/mail.log|awk '{print $1}'")
                id=id+1
                '''
                jl=open('/home/oracle/script/dd.log')
                record=jl.read()
                '''
                line_l=str(id)+' '+new_time




                df=open('/home/oracle/script/mail.log','w')
                df.writelines(line_l)
                df.close()
                
                if send_mail(mailto_list,"hello",list_disk):
                    print "发送成功"
                else:
                    print "发送失败"
                if getrecord[1]>0:
                    break
        else:
            line_m=str(0)+' '+new_time
            free=open('/home/oracle/script/mail.log','w')
            free.writelines(line_m)
            free.close()

相关文章

  • Python编程基础之构造方法和析构方法详解

    Python编程基础之构造方法和析构方法详解

    这篇文章主要为大家详细介绍了Python的构造方法和析构方法,使用Python编程基础,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2022-01-01
  • django orm 通过related_name反向查询的方法

    django orm 通过related_name反向查询的方法

    今天小编就为大家分享一篇django orm 通过related_name反向查询的方法,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2018-12-12
  • python实现将中文日期转换为数字日期

    python实现将中文日期转换为数字日期

    这篇文章主要介绍了python实现将中文日期转换为数字日期,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2020-07-07
  • Python自动重新加载模块详解(autoreload module)

    Python自动重新加载模块详解(autoreload module)

    这篇文章主要介绍了Python自动重新加载模块详解(autoreload module),具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2020-04-04
  • Python中的ceil()方法使用教程

    Python中的ceil()方法使用教程

    这篇文章主要介绍了Python中的ceil()方法使用教程,是Python入门中必会的方法之一,需要的朋友可以参考下
    2015-05-05
  • 利用Python实现自定义连点器

    利用Python实现自定义连点器

    这篇文章主要介绍了如何利用Python实现自定义连点器,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2022-08-08
  • 深入理解python虚拟机GIL详解

    深入理解python虚拟机GIL详解

    在目前的 CPython 当中一直有一个臭名昭著的问题就是 GIL (Global Interpreter Lock ),就是全局解释器锁,他限制了 Python 在多核架构当中的性能,在本篇文章当中我们将详细分析一下 GIL 的利弊和 GIL 的 C 的源代码
    2023-10-10
  • Python对PDF文件的常用操作方法详解

    Python对PDF文件的常用操作方法详解

    PDF是Portable Document Format的缩写,这类文件通常使用`.pdf`作为其扩展名。在日常开发工作中,最容易遇到的就是从PDF中读取文本内容以及用已有的内容生成PDF文档这两个任务。本文为大家介绍了几个Python中常见的PDF操作,需要的可以参考一下
    2022-05-05
  • Python使用plt.boxplot() 参数绘制箱线图

    Python使用plt.boxplot() 参数绘制箱线图

    这篇文章主要介绍了Python使用plt.boxplot() 参数绘制箱线图 ,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2020-06-06
  • 解决json中ensure_ascii=False的问题

    解决json中ensure_ascii=False的问题

    这篇文章主要介绍了解决json中ensure_ascii=False的问题,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2020-04-04

最新评论