直接上代码:

  1. # python3
  2. # -*- coding: utf-8 -*-
  3. # 2017/06/16 by luohan
  4.  
  5. from email.mime.text import MIMEText
  6. from email.header import Header
  7. from email.utils import parseaddr, formataddr
  8. import smtplib
  9. import time
  10.  
  11. import os
  12.  
  13. def get_size(start_path = '.'):
  14. total_size = 0
  15. for dirpath, dirnames, filenames in os.walk(start_path):
  16. for f in filenames:
  17. fp = os.path.join(dirpath, f)
  18. total_size += os.path.getsize(fp)
  19. return total_size / (1024 * 1024 * 1024)
  20.  
  21. def _format_addr(s):
  22. name, addr = parseaddr(s)
  23. return formataddr((Header(name, 'utf-8').encode(), addr))
  24.  
  25. def send_mail():
  26. from_addr = '**@**.com'
  27. passwd = '***'
  28. to_addrs = ['123@qq.com', '234@qq.com']
  29.  
  30. msg = MIMEText('统计服务器/dev/shm内存报警', 'plain', 'utf-8')
  31. msg['From'] = from_addr
  32. msg['To'] = ','.join(to_addrs)
  33. msg['Subject'] = Header('线上服务器报警', 'utf-8').encode()
  34.  
  35. try:
  36. smtp_server = 'smtp.exmail.qq.com'
  37. server = smtplib.SMTP_SSL(smtp_server, 465)
  38. server.login(from_addr, passwd)
  39. server.sendmail(from_addr, to_addrs, msg.as_string())
  40. with open('/home/jobs/mail.log', 'a') as f:
  41. print('{}: send success'.format(time.time()), file=f)
  42. except smtplib.SMTPException as e:
  43. with open('/home/jobs/mailerr.log', 'a') as f:
  44. print('{}: send failed, {}'.format(time.time(), e), file=f)
  45. finally:
  46. server.quit()
  47.  
  48. def check_dir_size(target_dir):
  49. dirsize = get_size(target_dir)
  50. # 超过12G,总大小16G
  51. if dirsize > 12:
  52. send_mail()
  53.  
  54. 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. 第二章:shell变量

    查看所有全局和局部变量:delare和set 查看所有全局变量:env 定义环境变量: 用户变量在家目录下的~/.bash_profile和~/.bashrc中设置 全局变量在/etc/profile ...

  2. 如何把Excel表暴力拆分了,python两段代码帮你搞定

    前言 本文的文字及图片来源于网络,仅供学习.交流使用,不具有任何商业用途,版权归原作者所有,如有问题请及时联系我们以作处理. 作者:老方玩编程 PS:如有需要Python学习资料的小伙伴可以加点击下方 ...

  3. Daily Scrum 12/23/2015

    Process: Zhaoyang: Compile the Caffe IOS version and make it run in the IOS9. Yandong: Finish the Az ...

  4. Cobalt Strike系列教程第六章:安装扩展

    Cobalt Strike系列教程分享如约而至,新关注的小伙伴可以先回顾一下前面的内容: Cobalt Strike系列教程第一章:简介与安装 Cobalt Strike系列教程第二章:Beacon详 ...

  5. vue使用trackingjs

    前言:因为公司是做人工智能-AI的,所有一个web数据平台为了装X,需要做个人脸登陆.前台需要把人脸的base64发给后台去做人脸校验. 功能很简单,需要注意的是web需要实现“调用摄像头”和“自动拍 ...

  6. JS-Array-新增方法

    1. filter( ) var arr = [5,4,3,2,1]; newarr = arr.filter((item)=>{ return item<3 }) ;  // => ...

  7. angularjs: draggable js

    var startX = 0, startY = 0, x = 0, y = 0, minDragRang = 50; var targetContainer = angular.element(do ...

  8. SpringBoot系列(十一)拦截器与拦截器链的配置与使用详解,你知道多少?

    往期推荐 SpringBoot系列(一)idea新建Springboot项目 SpringBoot系列(二)入门知识 springBoot系列(三)配置文件详解 SpringBoot系列(四)web静 ...

  9. 获取 保存 系统信息 [Windows]

    出处:https://www.technig.com/find-windows-10-system-information/ 在“运行”里,运行 msinfo32. System Informatio ...

  10. Golang Map实现(四) map 的赋值和扩容

    title: Golang Map 实现 (四) date: 2020-04-28 18:20:30 tags: golang map 操作,是map 实现中较复杂的逻辑.因为当赋值时,为了减少has ...