새 로그 파일이 생성될 때 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"

소비자를 실행하지 않고 명명된 파이프가 가득 차면 시스템이 계속 작동하는지 확인하고 이러한 일이 발생하지 않도록 해야 합니다. 제가 아주 조잡한 방법을 알려 드렸습니다.

관련 정보