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


Posted in Python onApril 23, 2016

核心代码:

#!/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获取CPU、内存和硬盘等windowns系统信息的2个例子
Apr 15 Python
pygame学习笔记(2):画点的三种方法和动画实例
Apr 15 Python
Python使用MYSQLDB实现从数据库中导出XML文件的方法
May 11 Python
Python的Django框架中消息通知的计数器实现教程
Jun 13 Python
Mac中升级Python2.7到Python3.5步骤详解
Apr 27 Python
Python with语句上下文管理器两种实现方法分析
Feb 09 Python
python实现FTP文件传输的方法(服务器端和客户端)
Mar 20 Python
python画环形图的方法
Mar 25 Python
matplotlib 对坐标的控制,加图例注释的操作
Apr 17 Python
Python用dilb提取照片上人脸的示例
Oct 26 Python
matplotlib交互式数据光标mpldatacursor的实现
Feb 03 Python
用Python实现Newton插值法
Apr 17 Python
web.py 十分钟创建简易博客实现代码
Apr 22 #Python
在windows下快速搭建web.py开发框架方法
Apr 22 #Python
基于python实现的抓取腾讯视频所有电影的爬虫
Apr 22 #Python
Python开发之快速搭建自动回复微信公众号功能
Apr 22 #Python
Django小白教程之Django用户注册与登录
Apr 22 #Python
python中PIL安装简单教程
Apr 21 #Python
Python for Informatics 第11章之正则表达式(四)
Apr 21 #Python
You might like
php UTF8 文件的签名问题
2009/10/30 PHP
php 自写函数代码 获取关键字 去超链接
2010/02/08 PHP
php遍历文件夹和文件列表示例分享
2014/03/11 PHP
遍历echsop的region表形成缓存的程序实例代码
2016/11/01 PHP
浅谈PHP中的错误处理和异常处理
2017/02/04 PHP
Yii2使用表单上传文件的实例代码
2017/08/03 PHP
javascript实现上传图片前的预览(TX的面试题)
2007/08/20 Javascript
学习ExtJS table布局
2009/10/08 Javascript
关于Jquery操作Cookie取值错误的解决方法
2013/08/26 Javascript
关于js中for in的缺陷浅析
2013/12/02 Javascript
jQuery超赞的评分插件(8款)
2015/08/20 Javascript
JS实现的简单鼠标跟随DiV层效果完整实例
2015/10/31 Javascript
js跨域请求数据的3种常用的方法
2015/12/01 Javascript
浅述节点的创建及常见功能的实现
2016/12/15 Javascript
jQuery插件HighCharts实现的2D面积图效果示例【附demo源码下载】
2017/03/15 Javascript
深入理解vue路由的使用
2017/03/24 Javascript
JS实现搜索关键词的智能提示功能
2017/07/07 Javascript
Vue列表页渲染优化详解
2017/07/24 Javascript
js如何实现元素曝光上报
2019/08/07 Javascript
ant-design表单处理和常用方法及自定义验证操作
2020/10/27 Javascript
[01:16:13]DOTA2-DPC中国联赛 正赛 SAG vs Dragon BO3 第一场 2月22日
2021/03/11 DOTA
python中执行shell的两种方法总结
2017/01/10 Python
python装饰器常见使用方法分析
2019/06/26 Python
python3实现二叉树的遍历与递归算法解析(小结)
2019/07/03 Python
Python matplotlib生成图片背景透明的示例代码
2019/08/30 Python
解决python和pycharm安装gmpy2 出现ERROR的问题
2020/08/28 Python
北美三大旅游网站之一:Travelocity加拿大
2016/08/20 全球购物
澳大利亚首屈一指的鞋类品牌:Tony Bianco
2018/03/13 全球购物
廉价连衣裙和婚纱礼服在线销售:Tbdress
2019/02/28 全球购物
德国二手设计师时装和复古时装跳蚤市场:Mädchenflohmarkt
2020/11/09 全球购物
趣味运动会活动方案
2014/02/12 职场文书
买房协议书
2014/04/11 职场文书
基层党员四风问题自我剖析材料
2014/09/29 职场文书
加薪通知
2015/04/25 职场文书
探究Mysql模糊查询是否区分大小写
2021/06/11 MySQL
在虚拟机中安装windows server 2008的图文教程
2022/06/28 Servers