新しいログファイルを生成するときは、Grepコマンドを使用してください。

新しいログファイルを生成するときは、Grepコマンドを使用してください。

サーバーは、1 時間ごとに 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"

コンシューマが実行されていない場合は、名前付きパイプがいっぱいになってもシステムが実行され続けていることを確認し、これが発生しないようにする必要があります。私は非常に残酷なヒントを与えた。

おすすめ記事