我希望使用 logrotate 将日志发送到我的邮箱

我希望使用 logrotate 将日志发送到我的邮箱

严格来说,这并不是一个关于编程的问题,而是一个日志处理问题。

无论如何。我的公司有多个客户,每个客户都有一组日志,我非常希望他们通过电子邮件发送给我。

现在,另一个先决条件是它们通过简单的 HTML 突出显示。

一切都很好,我已经成功为给定的日志类型制作了一个高亮显示。因此,我所做的就是使用logrotateprerotate内容将日志作为电子邮件发送。示例:

/var/log/a.log /var/log/b.log {
  日常的
  missok
  复制截断

  预旋转
    /usr/bin/python /home/foo/hilight_logs /var/log/{a,b}.log | /usr/sbin/sendmail -FLog\ 邮件程序[电子邮件保护] [电子邮件保护]
  结束语
}

这种方法的问题基本上是logrotate:它会对说明符中指定的每个日志文件运行命令,而且据我所知,没有办法知道正在处理哪个日志文件。(无论如何这实际上没有什么帮助。)

除了在不同的机器上重复同样的操作logrotate多达 10 次之外,我唯一能做的就是每晚都陷入日志垃圾邮件的泥潭。今天我厌倦了,所以我问了。

答案1

尝试用prerotate.替换firstaction,这样您的邮件命令将仅对块中定义的所有日志执行一次。

这是手册页中的文本:

firstaction/endscript
          The lines between firstaction and endscript (both of which must appear on lines by themselves) are executed once before all log files that match the
          wildcarded pattern are rotated, before prerotate script is run and only if at least one log will actually be  rotated.  These  directives  may  only
          appear inside a log file definition. If the script exits with error, no further processing is done. See also lastaction.

相关内容