시스템로그파일 관리를 위한 logrotate의 작동원리에 대하여
작성자 정보
- 관리자 작성
- 작성일
컨텐츠 정보
- 4,446 조회
- 0 추천
- 목록
본문
시스템로그파일 관리를 위한 logrotate의 작동원리에 대하여
시스템 로그파일에 대하여 로테이트, 압축, 또는 메일을 발송해주는 리눅스시스템 로그파일관리기입니다.
즉, 리눅스 시스템의 로그관리목적으로 사용되는 명령어입니다.
리눅스시스템에는 여러 개의 로그파일들이 존재합니다.
이런 로그파일들은 시스템의 사용환경에 따라서 하루에도 엄청나게 큰 용량의 로그내용을 저장하기도 합니다.
만약 이런 로그파일을 관리하지 않는다면 디스크 사용량의 초과로 시스템이 정지될 수도 있을 것입니다.
이런 경우에 logrotate를 이용하여 로그파일을 자동으로 관리할 수 있도록 설정해 두셔야합니다.
명령어위치 : /usr/sbin/logrotate
사용형식 : logrotate [-dv] [-f|--force] [-s|--state file] config_file+
다행스럽게도 리눅스에는 logrotate의 설정이 자동으로 되어있습니다.
즉, 시스템의 /var/log/디렉토리에 보시면 여러 개의 시스템로그파일들이 존재합니다.
이들 파일들이 다음과 같은 형식으로 일정한 기준에 의해 자동 관리되고 있다는 것을 확인할 수 있습니다.
[root@www ~]# ls -l /var/log 합계 4176 drwxr-xr-x 2 root root 4096 2008-12-01 22:23 ConsoleKit -rw-r--r-- 1 root root 22149 2008-12-04 23:33 Xorg.0.log -rw-r--r-- 1 root root 22149 2008-12-04 03:39 Xorg.0.log.old -rw------- 1 root root 27728 2008-12-01 22:16 anaconda.log -rw------- 1 root root 25749 2008-12-01 22:16 anaconda.syslog -rw------- 1 root root 21424 2008-12-01 22:16 anaconda.xlog drwxr-x--- 2 root root 4096 2008-12-01 22:22 audit -rw-r--r-- 2 root root 2414 2015-10-20 23:24 boot.log -rw-r--r-- 1 root root 2285 2008-12-18 23:00 boot.log-20081219 -rw-r--r-- 1 root root 2285 2008-12-23 02:09 boot.log-20081223 -rw------- 1 root utmp 1920 2008-12-03 23:40 btmp -rw------- 1 root root 23609 2015-10-21 01:01 cron -rw------- 1 root root 21521 2008-12-19 04:02 cron-20081219 -rw------- 1 root root 3758 2008-12-23 04:02 cron-20081223 drwxr-xr-x 2 lp sys 4096 2008-12-09 21:23 cups drwxr-xr-x 2 root root 4096 2008-08-02 13:22 dirmngr -rw-r--r-- 1 root root 19508 2015-10-20 23:24 dmesg -rw-r--r-- 1 root root 19508 2015-10-18 17:27 dmesg.old -rw------- 1 root root 12168 2009-01-08 20:50 faillog drwxrwx--T 2 root gdm 4096 2008-12-04 20:26 gdm drwx------ 2 root root 4096 2008-12-23 04:02 httpd -rw-r--r-- 1 root root 148044 2015-10-20 23:26 lastlog drwxr-xr-x 2 root root 4096 2009-01-08 22:01 mail -rw------- 1 root root 7371 2009-01-08 21:27 maillog -rw------- 1 root root 16240 2008-12-19 04:02 maillog-20081219 -rw------- 1 root root 2280 2008-12-23 04:02 maillog-20081223 -rw------- 1 root root 2301460 2015-10-20 23:25 messages -rw------- 1 root root 768661 2008-12-19 04:02 messages-20081219 -rw------- 1 root root 151722 2008-12-23 04:02 messages-20081223 drwxr-xr-x 2 ntp ntp 4096 2008-10-08 20:23 ntpstats drwx------ 2 root root 4096 2008-08-29 06:48 ppp drwxr-xr-x 2 root root 4096 2008-12-01 23:41 prelink -rw-r--r-- 1 root root 45798 2008-12-24 04:02 rpmpkgs -rw-r--r-- 1 root root 45798 2008-12-06 04:02 rpmpkgs-20081219 -rw-r--r-- 1 root root 45798 2008-12-19 04:04 rpmpkgs-20081223 drwx------ 3 root root 4096 2009-01-05 23:21 samba -rw------- 1 root root 63446 2015-10-21 01:00 secure -rw------- 1 root root 28346 2008-12-19 02:20 secure-20081219 -rw------- 1 root root 7144 2008-12-23 02:24 secure-20081223 drwxr-xr-x 2 root root 4096 2008-12-23 04:02 setroubleshoot -rw-r--r-- 1 root root 91 2008-12-22 23:03 spacefile -rw------- 1 root root 0 2008-12-23 04:02 spooler -rw------- 1 root root 0 2008-12-01 21:55 spooler-20081219 -rw------- 1 root root 0 2008-12-19 04:02 spooler-20081223 -rw------- 1 root root 0 2008-12-01 21:54 tallylog drwxr-xr-x 2 root root 4096 2008-10-02 23:32 vbox -rw------- 1 root root 34350 2015-10-17 23:49 vsftpd.log -rw-r--r-- 1 root root 0 2008-12-01 22:23 wpa_supplicant.log -rw-rw-r-- 1 root utmp 345600 2015-10-20 23:26 wtmp -rw------- 1 root root 11025 2015-10-17 01:19 xferlog -rw-r--r-- 1 root root 2845 2009-01-14 16:16 yum.log [root@www ~]# |
위의 예에서 보시는 바와 같이 /var/log디렉토리에 존재하는 리눅스 시스템의 여러 가지 로그파일들은 일정한 기준으로 잘라져서 보관되고 있다는 것을 알 수 있습니다.
즉, 리눅스에는 초기설정으로 시스템의 로그파일에 logrotate설정을 적용해 두고 있습니다.
당연한 얘기겠지만, logrotate는 다음과 같이 RPM패키지를 설치해야 시스템에 설치됩니다.
[root@www services]# rpm -qa | grep logrotate logrotate-3.7.7-1.fc10.i386 [root@www services]# [root@www services]# rpm -ql logrotate-3.7.7-1.fc10.i386 /etc/cron.daily/logrotate /etc/logrotate.conf /etc/logrotate.d /usr/sbin/logrotate /usr/share/doc/logrotate-3.7.7 /usr/share/doc/logrotate-3.7.7/CHANGES /usr/share/doc/logrotate-3.7.7/COPYING /usr/share/man/man8/logrotate.8.gz /var/lib/logrotate.status [root@www services]# |
여기서 우리는 무엇보다도 logrotate의 시스템 설정에 대해서 정확하게 이해하는 것이 중요합니다.
첫번째 : /usr/sbin/logrotate파일
logrotate의 주된 실행파일입니다.
하루에 한번씩 cron에 의해 자동으로 실행되는 파일입니다.
즉, /etc/cron.daily 디렉토리에는 하루에 한번씩 자동으로 실행되는 cron설정파일들이 존재합니다.
이 디렉토리에 존재하는 여러 개의 파일들 가운데 logrotate라는 파일이 하나 있으며 다음은 그 파일을 ls로 확인한 것입니다.
[root@www ~]# ls -l /etc/cron.daily/logrotate -rwxr-xr-x 1 root root 180 2003-07-10 04:36 /etc/cron.daily/logrotate [root@www ~]#
|
그리고 다음은 /etc/cron.daily/logrotate파일의 내용을 cat으로 확인한 것입니다.
[root@www ~]# cat /etc/cron.daily/logrotate #!/bin/sh
/usr/sbin/logrotate /etc/logrotate.conf EXITVALUE=$? if [ $EXITVALUE != 0 ]; then /usr/bin/logger -t logrotate "ALERT exited abnormally with [$EXITVALUE]" fi exit 0 [root@www ~]# |
위의 내용에서 우리는 하루에 한번씩 “/usr/sbin/logrotate /etc/logrotate.conf”이 실행된다는 사실을 알 수 있습니다.
즉, 하루에 한번씩 logrotate이 실행하면서 /etc/logrotate.conf설정파일을 참조하여 실행을 한다는 것입니다.
따라서 우리는 logrotate의 실행방식을 이해하기 위하여 logrotate가 어떻게 실행되는가를 정의해 둔 /etc/logrotate.conf파일을 살펴볼 필요성이 있습니다.
다음을 보십시오.
두번째 : /etc/logrotate.conf파일
다음은 /etc/logrotate.conf파일의 주된 내용입니다.
아래의 예는 설정파일의 내용을 확인한 것입니다.
즉, 이 파일은 logrotate실행의 글로벌(Global)한 설정파일입니다.
“Global”이라는 말의 의미는 잘 아실 것이라 믿고 이에 대한 부연설명은 생략합니다.
[root@www ~]# cat /etc/logrotate.conf # see "man logrotate" for details # rotate log files weekly weekly # keep 4 weeks worth of backlogs rotate 4 # create new (empty) log files after rotating old ones create # use date as a suffix of the rotated file dateext # uncomment this if you want your log files compressed compress # RPM packages drop log rotation information into this directory include /etc/logrotate.d
# no packages own wtmp and btmp -- we'll rotate them here /var/log/wtmp { monthly create 0664 root utmp minsize 1M rotate 1 } /var/log/btmp { missingok monthly create 0600 root utmp rotate 1 } # system-specific logs may be also be configured here. [root@www ~]# |
위의 주요 설정내용을 간단히 설명하면 다음과 같습니다.
weekly
주단위로 로그파일을 로테이트(자른다)시킵니다.
rotate 4
4주간의 로그파일을 보관합니다.
즉, 4주이상 지난 파일은 삭제합니다.
create
로그파일을 로테이트한 후에 빈 로그파일을 새로 생성해 둡니다.
compress
로그파일을 압축하고자 할 때 사용하는 설정입니다.
즉, 디스크용량등의 문제로 로테이트된 로그파일들을 압축하여 저장합니다.
/var/log/wtmp {
monthly
create 0664 root utmp
rotate 1
}
개별로그파일들의 설정을 위와 같이 할 수 있습니다.
즉, 위의 설정은 /var/log/wtmp파일의 개별 로테이트설정입니다.
따라서 다른 로그파일들(예, /var/log/messages, /var/log/secure등)도 이와 같이 개별적인 설정이 가능합니다.
만약 개별설정이 되어있지 않다면 전체설정내용을 기본 적용합니다.
include /etc/logrotate.d
로그관리를 위하여 /etc/logrotate.d디렉토리에 존재하는 설정파일들을 불러들여서 적용합니다.
이 디렉토리(/etc/logrotate.d)에는 로테이트할 개별 로그파일들의 설정파일이 존재합니다.
따라서 우리는 logrotate의 개별로그파일의 설정파일이 존재하는 /etc/logrotate.d디렉토리를 살펴보아야 합니다.
다음을 보십시오.
세번째 : /etc/logrotate.d 디렉토리
logrotate에 적용될 각종 로그파일들을 보관하고 있는 디렉토리입니다.
아래는 이 디렉토리에 존재하는 파일들 입니다.
모두 logrotate에 의해서 관리되는 로그파일들에 대한 설정파일들 입니다.
[root@www ~]# ls -l /etc/logrotate.d/ 합계 60 -rw-r--r-- 1 root root 290 2008-08-21 07:00 bittorrent -rw-r--r-- 1 root root 71 2008-12-09 21:23 cups -rw-r--r-- 1 root root 73 2007-10-03 22:13 dirmngr -rw-r--r-- 1 root root 174 2004-11-18 20:59 httpd -rw-r----- 1 root named 163 2008-10-07 19:28 named -rw-r--r-- 1 root root 136 2008-08-29 06:48 ppp -rw-r--r-- 1 root root 329 2008-06-03 18:07 psacct -rw-r--r-- 1 root root 61 2008-10-31 17:31 rpm -rw-r--r-- 1 root root 121 2008-10-16 06:36 setroubleshoot -rw-r--r-- 1 root root 154 2008-09-26 22:54 snmpd -rw-r--r-- 1 root root 393 2008-10-25 05:09 squid -rw-r--r-- 1 root root 228 2008-09-15 21:39 syslog -rw-r--r-- 1 root root 95 2008-09-09 12:56 vsftpd.log -rw-r--r-- 1 root root 100 2008-10-14 00:15 wpa_supplicant -rw-r--r-- 1 root root 100 2008-10-29 23:30 yum [root@www ~]# |
즉, 이 디렉토리에는 logrotate에 의해서 관리될 개별 로그파일들의 설정파일들이 존재합니다.
리눅스 시스템에 존재하는 그 어떤 로그파일이라도 여기에 설정을 해두면 logrotate의 관리대상이 됩니다.
이 디렉토리에 존재하는 개별 설정파일에서 설정되지 않은 항목들은 앞에서 말씀 드린 Global한 logrotate의 설정파일인 /etc/logrotate.conf파일의 설정내용의 적용을 받게 됩니다.
그렇다면 이제 이 디렉토리에 존재하는 여러 개의 개별 설정파일들 가운데 하나의 내용을 확인해 보겠습니다.
네번째 : /etc/logrotate.d/httpd 파일
아래의 파일은 아파치웹서버의 2개의로그파일 즉, access_log파일과 error_log파일의관리를 logrotate가 어떻게 할 것인가를 설정한 파일입니다.
그 내용을 보면 /var/log/httpd디렉토리에 존재하는 *log파일(access_log파일과 error_log파일을 의미함)에 대하여 missingok와 notifempty, sharescripts, 그리고 postrotate~endscript설정내용을 적용합니다.
따라서 아파치 웹로그파일들은 이 설정의 적용을 받아서 적당한 크기(size)가 되거나 지정된 기간이 되면 자동으로 잘려지게(rotate)되어 별도로 관리됩니다.
이것이 logrotate가 하는 역할입니다.
[root@www ~]# cat /etc/logrotate.d/httpd /var/log/httpd/*log { missingok notifempty sharedscripts postrotate /bin/kill -HUP `cat /var/run/httpd.pid 2>/dev/null` 2> /dev/null || true endscript } [root@www ~]# |
다섯번째 : /etc/logrotate.d/syslog파일
이번에는 logrotate의 설정파일 가운에 가장 중요한 설정파일이라고 할 수 있는 syslog설정파일의 내용을 살펴보겠습니다.
즉, 다음은 /etc/logrotate.d/디렉토리에 있는 syslog라는 파일의 내용입니다.
[root@www ~]# cat /etc/logrotate.d/syslog /var/log/messages /var/log/secure /var/log/maillog /var/log/spooler /var/log/boot.log /var/log/cron { sharedscripts postrotate /bin/kill -HUP `cat /var/run/syslogd.pid 2> /dev/null` 2> /dev/null || true endscript } [root@www ~]# |
즉, 위의 파일내용은 /var/log디렉토리에 존재하는 거의 모든 로그파일들의 관리방식에 대해서 정의하고 있는 파일입니다.
즉, /var/log/messages파일, /var/log/secure파일, /var/log/maillog파일, /var/log/spooler파일, /var/log/boot.log파일, 그리고 /var/log/cron로그파일들의 관리방식에 대해서 정의한 파일입니다.
앞서도 말씀 드렸지만, 기본적으로 적용될 내용들은 이미 /etc/logrotate.conf파일에 설정을 하고 있습니다.
그리고 위에서 설정한 postrotate와 endscript사이에 존재하는 내용은 이 것이 실행된 후에 syslogd를 재시작하는 내용입니다.
이렇게 logrotate는 시스템의 모든 로그파일들을 관리하고 있으며, 또한 logrotate에 의해 관리되도록 설정하려면 /etc/logrotate.d디렉토리에 설정내용을 추가하면 됩니다.
그리고 다음은 필자가 가끔씩 질문 받는 내용으로서 logrotate와 rotatelogs, 그리고 logresolve에 대하여 구분하고자 언급하는 것이므로 참고로 알아두시기 바랍니다.
[참고사항] logrotate, rotatelogs, 그리고 logresolve에 대하여
logrotate는 여기서 설명하고 있는 logrotate에 대한 설명과 함께 아래와 같이 참고하시기 바랍니다.
rotatelogs
아파치 웹로그파일 관리 유틸리티입니다.
즉, 아파치웹로그파일을 지정된 조건으로 로테이트시킵니다.
logresolve
아파치웹서버의 로그파일을 대상으로 IP주소를 호스트명(도메인네임)으로 변경해주는 유용한 유틸리티입니다.
|
관련자료
-
이전
-
다음