在建立新日誌檔案時使用 Grep 命令

在建立新日誌檔案時使用 Grep 命令

伺服器每小時都會建立一個格式為 syslog_all.yyyy-mm-dd-hh 的新日誌文件,並存檔前一小時的文件。

我需要的是一種方法來 grep 查找特定字串的當前和尚未創建的日誌文件,而不必僅僅因為文件名已更改而每小時重新啟動命令。

目前我這樣做:

tail -f syslog_all.2017-04-25-09 | egrep -i --line-buffered "string1" | egrep -i "(.*first.*|.*second.*|.*third.*)"

答案1

這是一個高級食譜。

  1. 設定 syslogd 或 rsyslogd(無論您的系統使用哪一個)以將所需的設施/優先權訊息輸出到命名管道以及它現在所在的位置。摘自man rsyslog.conf
Named pipes

   This  version  of  rsyslogd(8) has support for logging output to
   named pipes (fifos). A fifo or named pipe can be used as a  des‐
   tination  for  log messages by prepending a pipe symbol ('|') to
   the name of the file. This is handy for debugging. Note that the
   fifo  must  be  created  with the mkfifo(1) command before rsys‐
   logd(8) is started.

我的中有一個例子/etc/rsyslog.d/50-default.conf

daemon.*;mail.*;\
    news.err;\
    *.=debug;*.=info;\
    *.=notice;*.=warn       |/tmp/rt_monitor
  1. 建立一個命名管道並使用 tail 和 grep 從管道中讀取和搜尋。

    mkfifo /tmp/rt_monitor; tail -f /tmp/rt_monitor | grep "alert string"

如果您沒有讓消費者運行,您應該檢查命名管道已滿時系統是否繼續運行,並防止這種情況發生,我給了您一個非常殘酷的秘訣。

相關內容