RadarURL
Skip to content
조회 수 1162 추천 수 0 댓글 0
?

단축키

Prev이전 문서

Next다음 문서

크게 작게 위로 아래로 댓글로 가기 인쇄
?

단축키

Prev이전 문서

Next다음 문서

크게 작게 위로 아래로 댓글로 가기 인쇄
상황 : 아파치 로그 파일을 주기적으로(하루 단위로 rotation) 압축하여 일 별 관리하고 싶을 경우

기존 아파치 설정  httpd.conf
CustomLog    "|/program/httpd-2.2.20/bin/rotatelogs -l /program/httpd-2.2.20/logs/%y%m%d.sample.activity_log 86400" combined env=!nolog
ErrorLog     "|/program/httpd-2.2.20/bin/rotatelogs -l /program/httpd-2.2.20/logs/%y%m%d.sample.error_log 86400"

* 하루 단위로 rotation 하고 있었지만 압축 저장하고 있지 않음.

해결방법 : 리눅스 기본 logrotate 활용!


* logrotate란? 

로그 파일(시스템 로그)을 rotates, compresses, and mails 을 할 수 있다.
설정 파일을 변경해도 관련 프로세스를 새로 시작할 필요 없이 cron 데몬이 주기적으로 실행 시켜준다.

$ vi /etc/cron.daily 

위 cron을 확인해보면 logrotate 설정 부분을 확인할 수 있다.

logrotate 관련 파일

 - /usr/sbin/logrotate : 데몬의 위치 및 데몬프로그램
 - /etc/logrotate.conf : 설정 파일.
 - /etc/logrotate.d : logrotate를 적용할 로그 파일 보관 디렉토리.
 - /var/lib/logrotate.status : logrotate가 작업 내역 보관 파일.
 - /etc/cron.daily/logrotate : logrotate : cron 에 의해 일 단위로 실행한다.


동작 순서를 살펴보면 
1. cron.daily 에서 /usr/sbin/logrotate 호출 
2. /usr/sbin/logrotate 에서 /etc/logrotate.conf 설정파일 참조 
3. /etc/logrotate.conf 설정 파일에서 /etc/logrotate.d 참조 ( logrotate.conf 파일 안에 "include /etc/logrotate.d")


logrotate가 정상 동작 하는지 최소한의 설정으로 확인해 보자

$ sudo vi /etc/logrotate.d/apache

/program/apache/logs/access_log {
  size +1k
  missingok
  notifempty
  create 0600 root root
  compress
  dateext
  postrotate
    /usr/bin/killall -HUP httpd
  endscript
}

각 옵션들은 잠시 뒤에 상세 설명하고 우선 당장 실행 시켜 보자.

루트 권한으로 아래 명령 실행.

$ /usr/sbin/logrotate -f /etc/logrotate.conf

-f 옵션은 강제 실행 옵션이다 (Tells logrotate to force the rotation, even if it doesn't think this is necessary)

$ /usr/sbin/logrotate -d /etc/logrotate.conf

-d 옵션 : 디버그 모드 (Turns on debug mode and implies -v. In debug mode, no changes will be made to the logs or to the logrotate state file.)

* 주의사항 : f 옵션이나 d 옵션 하나만 넣고 실행해야 한다. (두 옵션을 동시에 넣고 실행시 정상 동작안되서 한동안 삽질....)

* logrotate 옵션
  • copytruncate : Truncate the original log file to zero size in place after creating a copy, instead of moving the old log file and optionally creating a new one 
copytruncate옵션을 활용하면 postrotate를 통한 httpd 재시작 없이 무중단 로깅이 가능하다.

/path/to/log {
  daily
  copytruncate
  create 0700 root root
  compress
  notifempty
  missingok
  dateext
}

  • rotate 30(숫자)  : log파일 30개 이상 되면 삭제
  • maxage 30(숫자) : 30일 이산된 로그 파일 삭제
  • size : 지정한 용량이 되면 로그로테이트를 실행한다. 10k, 10M 이런식으로 지정한다.
  • create : [권한 유저 그룹] 으로 rotation된 로그파일 생성
  • notifempty : log 내용이 없으면 rotation 하지 않는다.
  • ifempty : 로그파일이 비어있는 경우에도 로테이트한다.
  • monthly : 월 단위로 로테이트 한다.
  • daily : 월 단위로 로테이트 한다.
  • weekly : 월 단위로 로테이트 한다.
  • compress : rotate 된 로그 gzip 압축
  • nocompress : 압축을 원치 않는다.
  • mail admin@mail : 로테이트 설정에 의해 보관주기가 끝난 파일을 메일로 발송한다.
  • mailfirst admin@mail : 로테이트시 신규파일 이전의 로그를 메일로 발송한다.
  • nomail : 메일로 통보받지 않음.
  • errors admin@mail : 로테이트 실행시 에러가 발생하면 이메일로 통보한다.
  • prerotate-endscript : 사이의 명령어를 로그파일 처리전에 실행한다.
  • postrotate-endscript : 사이의 명령어를 로그파일 처리후에 실행한다.
  • extension : 로테이트 후 생성되는 파일의 확정자를 지정한다.
  • copytruncate : 이옵션을 넣지 않으면 현재 사용중인 로그를 다른이름으로 move하고 새로운 파일을 생성한다.
이 외의 옵션은 하단 참조에 link를 참조하거나 man logrotate를 확인하면 된다. 

보통은 rotation 후 아래 postrotate를 통해 httpd 를 재시작 해준다. 

postrotate
    /usr/bin/killall -HUP httpd
endscript

이 때 두가지 대안이 있다. 

1. killall -HUP 프로세스이름  (예> /usr/bin/killall -HUP httpd)
2. kill -HUP 프로세스번호 (예> /usr/bin/kill -HUP `cat /daum/program/apache/logs/httpd.pid 2> /dev/null` 2> /dev/null || true)


참조 링크 

logrotate 정의 : http://manpages.ubuntu.com/manpages/precise/man8/logrotate.8.html
How to Rotate Apache Log Files in Linux : http://www.thegeekstuff.com/2011/07/rotate-apache-logs/
logrotate example : http://www.thegeekstuff.com/2010/07/logrotate-examples/



출처 : http://blueskai.tistory.com/101

?

List of Articles
번호 제목 글쓴이 날짜 조회 수
33 리눅스(Linux) 디렉토리 구조 JAESOO 2016.10.20 603
32 [리눅스] IP 설정 변경 하기 JAESOO 2016.08.26 717
31 리눅스 설치후 초기설정해야 할 것들 [2] JAESOO 2016.07.13 675
30 리눅스 설치후 초기설정해야 할 것들 [1] JAESOO 2016.07.13 702
29 리눅스 호스트명 변경 JAESOO 2016.05.11 439
28 리눅스 logrotate 에서 로그파일 강제로 rotate 시키기 JAESOO 2016.03.31 663
27 리눅스 시간 맞추기 : Time Server와 Sync (rdate, date) JAESOO 2016.03.19 733
26 리눅스(Linux)에서 하위 디렉토리까지 파일 찾기, 찾아서 지우기 JAESOO 2015.02.24 1232
25 데비안 리눅스 로그 관리 JAESOO 2015.01.13 689
24 리눅스 로그파일관리 (logrotate) JAESOO 2015.01.13 723
» 리눅스 logrotate를 활용한 로그 관리 (compress, lotate) JAESOO 2015.01.13 1162
22 리눅스로 MRTG 트래픽분석서버 구축 JAESOO 2014.12.26 730
21 리눅스 - 디스크 검사 및 파일 시스템 복구 (badblocks, fsck) JAESOO 2014.12.26 3600
20 리눅스 버전 확인(centos, redhat) 하는 명령어 JAESOO 2014.11.18 1234
19 리눅스 자동 백업 스크립트 JAESOO 2014.11.18 1080
18 리눅스 스케줄 설정 crontab JAESOO 2014.11.18 1271
17 리눅스 스케줄러 crontab 이용하기 JAESOO 2014.10.20 1376
16 리눅스 호스트명 변경 JAESOO 2014.10.10 1431
15 리눅스 명령어 : find 활용법 JAESOO 2014.08.09 1839
14 리눅스 전원 끄기 - shutdown과 halt JAESOO 2014.06.24 2144
Board Pagination Prev 1 2 Next
/ 2

PageViews   Today : 11533 Yesterday : 5037 Total : 21967585  /  Counter Status   Today : 11161 Yesterday : 4602 Total : 1193884

Edited by JAESOO

sketchbook5, 스케치북5

sketchbook5, 스케치북5

나눔글꼴 설치 안내


이 PC에는 나눔글꼴이 설치되어 있지 않습니다.

이 사이트를 나눔글꼴로 보기 위해서는
나눔글꼴을 설치해야 합니다.

설치 취소