Python学习之pyinotify监控Linux下文件,并实现邮件报警
来源:互联网 发布:xp打开1433端口 编辑:程序博客网 时间:2024/05/07 20:44
实现功能:使用pyinotify监控httpd.conf,如果有修改,则与原文件对比,通过邮件报警,邮件内容格式采用HTML
整个功能分三部分:
环境:centos7 python2.7
目录结构:
一、邮件部分:
mail_send.py
#!/usr/bin/python#coding:utf-8import smtplibfrom diff_file import betweenDifffrom email.mime.text import MIMETextmail_user = 'xxxxxxxxx@qq.com'mail_pass = 'xxxxxxxxxxx'mail_server = 'smtp.qq.com'mail_port = 465to_user = 'xxxxxxx@rongchat.com'def send_mail(title,content): #创建一个实例,这里设置为html格式邮件 msg = MIMEText(content,_subtype = 'html',_charset = 'utf-8') msg['Subject'] = title msg['From'] = mail_user msg['To'] = to_user try: #登录smtp服务器 server = smtplib.SMTP_SSL(mail_server,mail_port) server.login(mail_user,mail_pass) #邮件发送 server.sendmail(mail_user,to_user,msg.as_string()) server.quit() return True except Exception as e: print(str(e)) return Falseif __name__ == '__main__': monitor_files = '../jiankong/test.txt' old_file = '../jiankong/test1.txt' content = betweenDiff(old_file,monitor_files) title = 'This is html file' send_mail(title,content)
二、文件对比部分:
diff_file.py
#!/usr/bin/python
#coding:utf-8import difflibdef betweenDiff(fileone,filetwo): with open(fileone) as f: lines = f.readlines() with open(filetwo) as f: new_lines = f.readlines() d = difflib.HtmlDiff() html = d.make_file(lines,new_lines) return htmlif __name__ == '__main__': fileone = '../jiankong/test1.txt' filetwo = '../jiankong/test.txt' html = betweenDiff(fileone,filetwo) print(html)
三、监控部分:
pyinotify_file.py
#!/usr/bin/python#coding:utf-8from diff_file import betweenDifffrom mail_send import send_mailfrom pyinotify import ProcessEvent,WatchManager,Notifier,ALL_EVENTSmonitor_dirs = ['../jiankong']monitor_files = ['../jiankong/test.txt']old_file = '../jiankong/test1.txt'class MyEvent(ProcessEvent): def process_IN_MODIFY(self,event): print('%s is %s.' %(event.pathname,event.maskname)) if event.pathname in monitor_files: html = betweenDiff(old_file,event.pathname) send_mail('The file content have been modified.',html) def process_IN_ACCESS(self, event): print('%s is %s.' %(event.pathname,event.maskname))def main(): vm = WatchManager() vm.add_watch(monitor_dirs,ALL_EVENTS,rec = True) en = MyEvent() notifier = Notifier(vm,en) notifier.loop()if __name__ == '__main__': main()
运行pyinotify_file.py即可
阅读全文
0 0
- Python学习之pyinotify监控Linux下文件,并实现邮件报警
- python实现监控磁盘情况,并邮件报警
- python实现监控磁盘情况,并邮件报警
- Linux 下文件监控
- Python pyinotify文件系统监控
- Python pyinotify文件系统监控
- Linux中使用shell+python实现监控系统负载+邮件报警
- python 实现邮件报警
- Python实现linux下文件备份
- 【大话QT之五】Windows与Linux下文件操作监控的实现
- 12.zabbix学习笔记:zabbix监控之邮件报警
- Python pyinotify模块实现对文档的实时监控功能
- python自动发送报警监控邮件
- Linux学习6之shell筛选当前目录下文件并逐个对其进行操作
- 监控--nagios之 邮件报警(三)
- python监控脚本,监控CPU,磁盘报警时发送邮件
- 利用python监控linux系统cpu、内存、文件系统的使用情况以及简单的邮件报警
- [Python--] Python logging模块实现邮件报警
- 【AI系统首次实现真正自主编程】利用遗传算法,完爆初级程序员
- 递归实现冒泡排序
- 都在说微服务,那么微服务的反模式和陷阱是什么(三)
- 《Windows核心编程》读书笔记七 线程调度,优先级和关联性
- 服务器软件pm2、elasticsearch后台运行操作
- Python学习之pyinotify监控Linux下文件,并实现邮件报警
- linux字符类驱动示例
- 百度对HTTPS站点全流程支持方案
- windows下使用nexus搭建maven私服(一)
- Python3.5——Json与pickle数据序列化
- 【下】安全HTTPS-全面详解对称加密,非对称加密,数字签名,数字证书和HTTPS
- 专访新浪微博黄波:千人千面,机器学习赋能用户信息流消费
- 移动Web开发基础-reset样式
- C++11 并发指南一(C++11 多线程初探)