使用tail -f监视日志文件 [英] Monitor a log file using tail -f

查看:246
本文介绍了使用tail -f监视日志文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

需要一些指导

如果需要使用脚本发送警报,请使用tail-f监视应用程序日志文件(日志每天轮换).

Monitor a application log file(log rotation is daily)using tail-f, if exception in log file need to send alert using script.

推荐答案

仅使用tail -f很难构建日志监视器. tail确实有一个+c选项可以从特定位置读取,但是在其周围构建良好的代码将非常棘手.

It's hard to build a log monitor with just tail -f. tail does have a +c option to read from a particular position, but it would be tricky to build good code around it.

这是您需要的:

  • 一个读取控制文件,存储从日志文件读取的最后一个字节位置
  • 使用读取控制文件执行增量读取(使用seek)并基于读取的行进行模式匹配和通知的脚本,并更新读取的控制文件
  • 可以使用索引节点号命名读取的控制文件,以便即使重命名或移动了日志文件,增量逻辑也可以继续工作
  • a read control file that stores the last byte position read from the log file
  • a script that uses the read control file to do incremental reads (using seek) and does pattern matching and notification based on the lines read, and updates the read control file
  • the inode number could be used for naming the read control file so that the incremental logic continues to work even if the log file is renamed or moved

以上脚本可以作为守护程序运行,也可以作为cron作业定期执行.我强烈建议为此使用Perl,Ruby,Python甚至Java/C/C ++.

The above script could either run as a daemon or execute periodically as a cron job. I would strongly suggest using Perl, Ruby, Python, or even Java/C/C++ for this.

这篇关于使用tail -f监视日志文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆