python监控网站目录文件夹和文件
很多公司的网站服务器都会有自己监控网站文件夹工具,或者是脚本,我知道的有自己写php来监控,有用shell脚本来监控,有用python来监控,至于其他软件的这里就不说了,今天我们就说用python来监控网站文件夹和文件.
系统:centos 7(64位)
软件版本:python 2.7
cat /root/soft_shell/check-wwwfile.py
#!/usr/bin/env python #coding=utf-8 import os,datetime,pyinotify,logging class MyEventHandler(pyinotify.ProcessEvent): logging.basicConfig(level=logging.INFO,filename='/var/log/monitor.log') #自定义写入那个文件,可以自己修改 logging.info("Starting monitor...") #def process_IN_ACCESS(self, event): #print "ACCESS event:", event.pathname #logging.info("ACCESS event : %s %s" % (os.path.join(event.path,event.name),datetime.datetime.now())) def process_IN_CREATE(self, event): #print "CREATE event:", event.pathname logging.info("CREATE event : %s %s" % (os.path.join(event.path,event.name),datetime.datetime.now())) def process_IN_DELETE(self, event): #print "DELETE event:", event.pathname logging.info("DELETE event : %s %s" % (os.path.join(event.path,event.name),datetime.datetime.now())) def process_IN_MODIFY(self, event): #print "MODIFY event:", event.pathname logging.info("MODIFY event : %s %s" % (os.path.join(event.path,event.name),datetime.datetime.now())) def main(): # watch manager wm = pyinotify.WatchManager() wm.add_watch('/var/www/vhosts', pyinotify.ALL_EVENTS, rec=True) #/tmp是可以自己修改的监控的目录 # event handler eh = MyEventHandler() # notifier notifier = pyinotify.Notifier(wm, eh) notifier.loop() if __name__ == '__main__': main()
在使用之前,要安装pyinotify组件,没有安装的话请执行:
yum -y install python-pip
pip install pyinotify
当上面两步执行完了后,再使用监控脚本,要后台执行的话,执行:
setsid python /root/soft_shell/check-wwwfile.py
最后查看monitor.log日志.
好了,可以看到有写入数据的,这样就可以方便以后查看.
ps:http://www.cnblogs.com/hollyspirit/p/3182828.html
ps:http://www.cnblogs.com/hollyspirit/p/3182828.html
评论: