계속 늘어나는 로그파일을 감지하려면?

정인철의 이미지

로그파일을 분석해보려고 프로그램을 만들고 있습니다.

예를들어서 실시간 로그분석을 하려면.
http 로그 access_log를 분석하려면 어떻게 해야 할까요?

즉, 로그 기록은 접속이 이루어질때 마다 로그 파일 밑에 추가 되어 늘어날텐데.
이를 어떻게 감지(탐지)를 하고 증가된 로그 라인만을 읽어 올 수 있을까요?

좋은 방법좀 알려주십시오.

감사합니다.

lukseung의 이미지

tail 프로그램 소스를 구해서 보세요. tail에 -f 옵션을 주면 파일에 추가되는 내용이 있을때마다 화면에 뿌려주니.. 도움이 되실겁니다.

kall의 이미지

음....꼼수에 가까울지 모르지만...

apache로그를 파일이 아닌 디비서버로 저장해서

쿼리를 통해서 일정시간 이후에 저장된 로그만 뽑아내도 되지 않을까요?

----
자신을 이길 수 있는자는
무슨짓이든 할수있다..
즉..무서운 넘이란 말이지 ^-_-^
나? 아직 멀었지 ㅠㅠ

stoneshim의 이미지

분석할때마다 파일 끝의 offset을 어딘가에다가 저장해놓으면 되지 않을까요?

우리 모두 리얼리스트가 되자. 그러나 가슴에 이룰 수 없는 꿈을 가지자

sunyzero의 이미지

파일을 열고 select를 쓰면 됩니다.

select()를 호출하면서 추가된 데이터가 있는지 검사하고 있음 읽고 그러면 아주 심플하게 끝납니다.

========================================
* The truth will set you free.

nic7의 이미지

제가 쓰고 있는 로그파일 감시기 입니다.
[error] 라는 단어가 나오면 저한테 그 내용을 메일로 보내주는건데,
어째튼 작동은 잘합니다.
데몬으로 작동합니다.
소스중에는 저도 잘 모르고 적은부분도 있는데, 틀린부분이 있으면 지적 부탁드릴께요
Perl cookbook 을 보시면 커지는 파일 관리하는법이 잘 나와있습니다.
저도 거기꺼 보고 이리저리 만들었습니다.
도움이 되시길..


#!/usr/bin/perl

use strict;
use Mail::Mailer;

my $pid  = fork;
exit if $pid;
die "couldn't fork: $!" unless defined($pid);

use POSIX;
POSIX::setsid() or die "Can't start a new session : $!";

my $time_to_die = 0;

sub signal_handler {
   $time_to_die = 1;
}

$SIG{INT} = $SIG{TERM} = $SIG{HUP} = \&signal_handler;

my $naptime = 1;
my (@msg, $first_line, $error_comment, $mailer) = ();

use IO::Handle;

open(LOGFILE, "/usr/local/apache/logs/nic7.com-error_log") or die "can't open /usr/local/apache/logs/nic7.com-error_log: $!";

seek LOGFILE, 2, 2;

until ($time_to_die)
{

   while (<LOGFILE>) {
       if( $_ =~ /\[error\]/ && $_ !~ /File does not exist/)
       {
           $first_line =  $_;
           @msg = <LOGFILE>;
           $error_comment =  "<font size=2>" . $first_line . "<br>" . join("<br>",@msg);

           $mailer = Mail::Mailer->new('sendmail');
           $mailer->open(
                           {
                               'Content-type' => 'text/html',
                               'charset' => 'euc-kr',
                               From => '[LogError]<fuckingusa@whitehouse.org>',
                               To   => 'fuckingusa@whitehouse.org',
                               Subject => $error_comment,
                           }
                       );
           print $mailer $error_comment;
           $mailer->close();

           ($first_line, @msg, $error_comment) = ();
       }
   }
   sleep $naptime;
   LOGFILE->clearerr();

}

꽃들아 네 멋대로 피어라

stoneshim의 이미지

예를들어서 실시간 로그분석을 하려면. 

'실시간 로그분석'이라는 의미가 로그분석기가 계속 떠있고, 지속적으로 증가하는 로그의 내용을 '실시간으로' 계속해서 분석하는 것이라면 sunyzero님께서 말씀하신 대로 로그 파일을 open하고 select()하는 방식이 좋을 것입니다.

로그분석기가 떠 있는 상태가 아니라, cron 테이블에 등록해서 일정 시간마다 분석하는 경우라면 로그분석기가 실행될 때마다 로그파일의 크기를 어딘가에 저장해놓고, 다음번 실행할때 로그파일의 크기 이후부터 검사하는 방식을 사용해야 할 것 같습니다.
물론, 로그 파일을 어드민이 삭제하거나 수정하는 경우에는 별도의 조치를 취해야 할것입니다.

우리 모두 리얼리스트가 되자. 그러나 가슴에 이룰 수 없는 꿈을 가지자

댓글 달기

Filtered HTML

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

BBCode

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param>
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

Textile

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • You can use Textile markup to format text.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Markdown

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • Quick Tips:
    • Two or more spaces at a line's end = Line break
    • Double returns = Paragraph
    • *Single asterisks* or _single underscores_ = Emphasis
    • **Double** or __double__ = Strong
    • This is [a link](http://the.link.example.com "The optional title text")
    For complete details on the Markdown syntax, see the Markdown documentation and Markdown Extra documentation for tables, footnotes, and more.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Plain text

  • HTML 태그를 사용할 수 없습니다.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 줄과 단락은 자동으로 분리됩니다.
댓글 첨부 파일
이 댓글에 이미지나 파일을 업로드 합니다.
파일 크기는 8 MB보다 작아야 합니다.
허용할 파일 형식: txt pdf doc xls gif jpg jpeg mp3 png rar zip.
CAPTCHA
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.