直接上代码:

# python3
# -*- coding: utf-8 -*-
# 2017/06/16 by luohan from email.mime.text import MIMEText
from email.header import Header
from email.utils import parseaddr, formataddr
import smtplib
import time import os def get_size(start_path = '.'):
total_size = 0
for dirpath, dirnames, filenames in os.walk(start_path):
for f in filenames:
fp = os.path.join(dirpath, f)
total_size += os.path.getsize(fp)
return total_size / (1024 * 1024 * 1024) def _format_addr(s):
name, addr = parseaddr(s)
return formataddr((Header(name, 'utf-8').encode(), addr)) def send_mail():
from_addr = '**@**.com'
passwd = '***'
to_addrs = ['123@qq.com', '234@qq.com'] msg = MIMEText('统计服务器/dev/shm内存报警', 'plain', 'utf-8')
msg['From'] = from_addr
msg['To'] = ','.join(to_addrs)
msg['Subject'] = Header('线上服务器报警', 'utf-8').encode() try:
smtp_server = 'smtp.exmail.qq.com'
server = smtplib.SMTP_SSL(smtp_server, 465)
server.login(from_addr, passwd)
server.sendmail(from_addr, to_addrs, msg.as_string())
with open('/home/jobs/mail.log', 'a') as f:
print('{}: send success'.format(time.time()), file=f)
except smtplib.SMTPException as e:
with open('/home/jobs/mailerr.log', 'a') as f:
print('{}: send failed, {}'.format(time.time(), e), file=f)
finally:
server.quit() def check_dir_size(target_dir):
dirsize = get_size(target_dir)
# 超过12G,总大小16G
if dirsize > 12:
send_mail() check_dir_size('/dev/shm')

参考资料:

http://www.liaoxuefeng.com/wiki/0014316089557264a6b348958f449949df42a6d3a2e542c000/001432005226355aadb8d4b2f3f42f6b1d6f2c5bd8d5263000

Python监控文件夹 && 发送邮件的更多相关文章

  1. Python 的 pyinotify 模块 监控文件夹和文件的变动

    官方参考: https://github.com/seb-m/pyinotify/wiki/Events-types https://github.com/seb-m/pyinotify/wiki/I ...

  2. Python监控文件变化:watchdog

    Python监控文件变化有两种库:pyinotify和watchdog.pyinotify依赖于Linux平台的inotify,后者则对不同平台的的事件都进行了封装.也就是说,watchdog跨平台. ...

  3. python 遍历文件夹 文件

    python 遍历文件夹 文件   import os import os.path rootdir = "d:\data" # 指明被遍历的文件夹 for parent,dirn ...

  4. python 关于文件夹的操作

    在python中,文件夹的操作主要是利用os模块来实现的, 其中关于文件夹的方法为:os.lister() , os.path.join() , os.path.isdir() #  path 表示文 ...

  5. Python打包文件夹的方法小结(zip,tar,tar.gz等)

    本文实例讲述了Python打包文件夹的方法.分享给大家供大家参考,具体如下: 一.zip ? 1 2 3 4 5 6 7 8 9 10 11 import os, zipfile #打包目录为zip文 ...

  6. python操作txt文件中数据教程[3]-python读取文件夹中所有txt文件并将数据转为csv文件

    python操作txt文件中数据教程[3]-python读取文件夹中所有txt文件并将数据转为csv文件 觉得有用的话,欢迎一起讨论相互学习~Follow Me 参考文献 python操作txt文件中 ...

  7. Storm监控文件夹变化 统计文件单词数量

    监控指定文件夹,读取文件(新文件动态读取)里的内容,统计单词的数量. FileSpout.java,监控文件夹,读取新文件内容 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 ...

  8. 【.Net 学习系列】-- FileSystemWatcher 监控文件夹新生成文件,并在确认文件没有被其他程序占用后将其移动到指定文件夹

    监控文件夹测试程序: using System; using System.Collections.Generic; using System.IO; using System.Linq; using ...

  9. 用Python打开文件夹

    用Python读取文件夹, 然后打开文件 下面读取到文件的每一个内容, 然后加上路径 import os path = r'../Downloads/text/content' for filenam ...

随机推荐

  1. Python Requests-学习笔记(8)-重定向与请求历史

    重定向与请求历史 默认情况下,除了 HEAD, Requests会自动处理所有重定向. 可以使用响应对象的 history 方法来追踪重定向. Response.history 是一个:class:R ...

  2. Linux环境安装Docker

    1. 使用APT安装 # 更新数据源 apt-get update # 安装所需依赖 apt-get -y install apt-transport-https ca-certificates cu ...

  3. shell map数据结构的实现

    前言     Bash默认不支持二维数组,如果我们想实现map 数据结构,可以安装如下的方式来进行构造   预备知识     eval:  它是shell内建命令,用于字符串的解析.它会首先扫描命令行 ...

  4. vue 全局自定义组件

    1.vue文件 <template> <div style="position: absolute;bottom: 10px;text-align: center;widt ...

  5. AJ学IOS(16)UI之XIB自定义Cell实现团购UI

    AJ分享,必须精品 先看效果图 自定义Cell 本次主要是自定义Cell的学习 实现自定义Cell主要有三种方法:按照使用的频繁度排序: XIB > 纯代码 > StoryBoard XI ...

  6. 从3dMax导出供threeJS使用的带动作模型与加载

    评论区发现的建议,最近没空测试,先贴这 还有好多人说找不到插件的 https://pan.baidu.com/s/1Q5g0... 密码:b43e . 应该是他们现在只是维护blender,只有这个的 ...

  7. TcxLookupComboBox

    1.绑定数据源显示 cxLookupComboBox1.Properties.DropDownAutoSize:=true; //设置下拉列表为自适应宽度 cxLookupComboBox1.Prop ...

  8. 使用 selenium 实现谷歌以图搜图爬虫

    使用selenium实现谷歌以图搜图 实现思路 原理非常简单,就是利用selenium去操作浏览器,获取到想要的链接,然后进行图片的下载,和一般的爬虫无异. 用到的技术:multiprocessing ...

  9. [YII2] 去除自带头部以及底部右下角debug调试功能

    YII2 去除自带头部以及底部右下角debug调试功能

  10. 解决利用hibernate连接mysql时无法插入汉字的问题

    1.先修改mysql数据库的配置文件my.ini,此文件放在mysql安装文件的根目录下.找到default-character-set属性,并将其值更改为utf8(不是utf-8),将default ...