5

基于inotify的Linux文件实时监控扫描

 3 years ago
source link: https://misakikata.github.io/2020/09/%E5%9F%BA%E4%BA%8Einotify%E7%9A%84Linux%E6%96%87%E4%BB%B6%E5%AE%9E%E6%97%B6%E7%9B%91%E6%8E%A7%E6%89%AB%E6%8F%8F/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

watchfile

项目地址:https://github.com/MisakiKata/watchfile

  1. #第三方库
  2. pip install pyinotify
  3. #使用supervisor来守护进程
  4. apt-get install supervisor
  5. yum install supervisor
  1. #安装完成后需要启动,按需要修改配置文件
  2. supervisord -c /etc/supervisor/supervisord.conf
  3. #配置文件例子
  4. ; supervisor config file
  5. [unix_http_server]
  6. file=/var/run/supervisor.sock ; (the path to the socket file)
  7. chmod=0700 ; sockef file mode (default 0700)
  8. [supervisord]
  9. logfile=/var/log/supervisor/supervisord.log ; (main log file;default $CWD/supervisord.log)
  10. pidfile=/var/run/supervisord.pid ; (supervisord pidfile;default supervisord.pid)
  11. childlogdir=/var/log/supervisor ; ('AUTO' child log dir, default $TEMP)
  12. ; the below section must remain in the config file for RPC
  13. ; (supervisorctl/web interface) to work, additional interfaces may be
  14. ; added by defining them in separate rpcinterface: sections
  15. [rpcinterface:supervisor]
  16. supervisor.rpcinterface_factory = supervisor.rpcinterface:make_main_rpcinterface
  17. [supervisorctl]
  18. serverurl=unix:///var/run/supervisor.sock ; use a unix:// URL for a unix socket
  19. ; The [include] section can just contain the "files" setting. This
  20. ; setting can list multiple files (separated by whitespace or
  21. ; newlines). It can also contain wildcards. The filenames are
  22. ; interpreted as relative to this file. Included files *cannot*
  23. ; include files themselves.
  24. [include]
  25. files = /data/vscode/watchfile/watchfile.conf #修改自己的配置文件地址
  26. 由于调用了河马扫描程序来检测文件,所以大量文件同时变更时,检测会慢,建议系统做大量变更时可以停掉程序。
  1. 在config.ini 中修改自己的配置,因为使用多线程来监控多目录,建议使用多目录配置
  2. 运行 python3 daemon.py来查看输出和测试邮件
  3. 异常处理
  4. 查看输出日志的时候,如果出现wd=-1
  5. 需要修改 vim /etc/sysctl.conf
  6. fs.inotify.max_user_watches = 128000 #决定了同时同一用户可以监控的目录数量
  7. 日志中看到Event Queue Overflow
  8. max_queued_events太小需要调整参数

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK