Python监控文件夹 && 发送邮件
直接上代码:
- # 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')
参考资料:
Python监控文件夹 && 发送邮件的更多相关文章
- Python 的 pyinotify 模块 监控文件夹和文件的变动
官方参考: https://github.com/seb-m/pyinotify/wiki/Events-types https://github.com/seb-m/pyinotify/wiki/I ...
- Python监控文件变化:watchdog
Python监控文件变化有两种库:pyinotify和watchdog.pyinotify依赖于Linux平台的inotify,后者则对不同平台的的事件都进行了封装.也就是说,watchdog跨平台. ...
- python 遍历文件夹 文件
python 遍历文件夹 文件 import os import os.path rootdir = "d:\data" # 指明被遍历的文件夹 for parent,dirn ...
- python 关于文件夹的操作
在python中,文件夹的操作主要是利用os模块来实现的, 其中关于文件夹的方法为:os.lister() , os.path.join() , os.path.isdir() # path 表示文 ...
- Python打包文件夹的方法小结(zip,tar,tar.gz等)
本文实例讲述了Python打包文件夹的方法.分享给大家供大家参考,具体如下: 一.zip ? 1 2 3 4 5 6 7 8 9 10 11 import os, zipfile #打包目录为zip文 ...
- python操作txt文件中数据教程[3]-python读取文件夹中所有txt文件并将数据转为csv文件
python操作txt文件中数据教程[3]-python读取文件夹中所有txt文件并将数据转为csv文件 觉得有用的话,欢迎一起讨论相互学习~Follow Me 参考文献 python操作txt文件中 ...
- Storm监控文件夹变化 统计文件单词数量
监控指定文件夹,读取文件(新文件动态读取)里的内容,统计单词的数量. FileSpout.java,监控文件夹,读取新文件内容 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 ...
- 【.Net 学习系列】-- FileSystemWatcher 监控文件夹新生成文件,并在确认文件没有被其他程序占用后将其移动到指定文件夹
监控文件夹测试程序: using System; using System.Collections.Generic; using System.IO; using System.Linq; using ...
- 用Python打开文件夹
用Python读取文件夹, 然后打开文件 下面读取到文件的每一个内容, 然后加上路径 import os path = r'../Downloads/text/content' for filenam ...
随机推荐
- 第二章:shell变量
查看所有全局和局部变量:delare和set 查看所有全局变量:env 定义环境变量: 用户变量在家目录下的~/.bash_profile和~/.bashrc中设置 全局变量在/etc/profile ...
- 如何把Excel表暴力拆分了,python两段代码帮你搞定
前言 本文的文字及图片来源于网络,仅供学习.交流使用,不具有任何商业用途,版权归原作者所有,如有问题请及时联系我们以作处理. 作者:老方玩编程 PS:如有需要Python学习资料的小伙伴可以加点击下方 ...
- Daily Scrum 12/23/2015
Process: Zhaoyang: Compile the Caffe IOS version and make it run in the IOS9. Yandong: Finish the Az ...
- Cobalt Strike系列教程第六章:安装扩展
Cobalt Strike系列教程分享如约而至,新关注的小伙伴可以先回顾一下前面的内容: Cobalt Strike系列教程第一章:简介与安装 Cobalt Strike系列教程第二章:Beacon详 ...
- vue使用trackingjs
前言:因为公司是做人工智能-AI的,所有一个web数据平台为了装X,需要做个人脸登陆.前台需要把人脸的base64发给后台去做人脸校验. 功能很简单,需要注意的是web需要实现“调用摄像头”和“自动拍 ...
- JS-Array-新增方法
1. filter( ) var arr = [5,4,3,2,1]; newarr = arr.filter((item)=>{ return item<3 }) ; // => ...
- angularjs: draggable js
var startX = 0, startY = 0, x = 0, y = 0, minDragRang = 50; var targetContainer = angular.element(do ...
- SpringBoot系列(十一)拦截器与拦截器链的配置与使用详解,你知道多少?
往期推荐 SpringBoot系列(一)idea新建Springboot项目 SpringBoot系列(二)入门知识 springBoot系列(三)配置文件详解 SpringBoot系列(四)web静 ...
- 获取 保存 系统信息 [Windows]
出处:https://www.technig.com/find-windows-10-system-information/ 在“运行”里,运行 msinfo32. System Informatio ...
- Golang Map实现(四) map 的赋值和扩容
title: Golang Map 实现 (四) date: 2020-04-28 18:20:30 tags: golang map 操作,是map 实现中较复杂的逻辑.因为当赋值时,为了减少has ...