新しいログファイルが作成されたら、Grep でそのファイルを検索します。

新しいログファイルが作成されたら、Grep でそのファイルを検索します。

サーバーは 1 時間ごとに syslog_all.yyyy-mm-dd-hh の形式で新しいログ ファイルを作成し、前の 1 時間のファイルをアーカイブします。

必要なのは、ファイル名が変更されたという理由だけでコマンドを 1 時間ごとに再起動することなく、現在のログ ファイルとまだ作成されていないログ ファイルで特定の文字列を grep で検索する方法です。

現在、私は以下を行っています:

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

答え1

ここに高レベルのレシピがあります。

  1. syslogd または rsyslogd (システムで使用している方) を設定して、必要な facility/priority メッセージを現在の出力先に加えて名前付きパイプに出力します。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"

コンシューマーを実行していない場合は、名前付きパイプがいっぱいになってもシステムが継続することを確認し、これが発生しないようにする必要があります。非常に簡単なレシピを提供しました。

関連情報