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
번호 제목 글쓴이 날짜 조회 수
162 리눅스(Linux) 디렉토리 구조 JAESOO 2016.10.20 602
161 ssh서버가 비밀번호를 거부했습니다. 다시 시도하십시오. JAESOO 2016.08.26 708
160 [리눅스] IP 설정 변경 하기 JAESOO 2016.08.26 717
159 리눅스 설치후 초기설정해야 할 것들 [2] JAESOO 2016.07.13 675
158 리눅스 설치후 초기설정해야 할 것들 [1] JAESOO 2016.07.13 702
157 쉘 프로그래밍을 이용한 시스템 관리 기법 JAESOO 2016.05.12 540
156 AIX 자주 쓰이는 관리 명령 모음 JAESOO 2016.05.12 554
155 AIX 시스템상의 core, SMT(Simultaneous Multi Threading) 수 확인하기 JAESOO 2016.05.12 604
154 리눅스 호스트명 변경 JAESOO 2016.05.11 439
153 AIX 서버 기초 JAESOO 2016.05.11 560
152 AIX Admin Study 교육 자료 JAESOO 2016.05.11 547
151 성능 엔지니어링 대한 접근 방법 (Performance tuning) JAESOO 2016.05.05 489
150 [AIX] 파일시스템 관리 (du, df) JAESOO 2016.05.04 552
149 IBM AIX Admin (사용자 DISK 관리) JAESOO 2016.05.04 504
148 [UNIX] 유닉스 기본명령어 JAESOO 2016.05.04 455
147 [AIX] 파일시스템 용량 늘리기 JAESOO 2016.05.04 558
146 유닉스 상에서 ls 출력물을 MB 단위로 출력해주는 옵션 JAESOO 2016.05.04 446
145 ls 명령시 날짜 전체가 보이도록 하기 JAESOO 2016.05.02 438
144 리눅스 logrotate 에서 로그파일 강제로 rotate 시키기 JAESOO 2016.03.31 663
143 리눅스 시간 맞추기 : Time Server와 Sync (rdate, date) JAESOO 2016.03.19 733
Board Pagination Prev 1 2 3 4 5 6 7 8 9 Next
/ 9

PageViews   Today : 5091 Yesterday : 5037 Total : 21961143  /  Counter Status   Today : 4945 Yesterday : 4602 Total : 1187668

Edited by JAESOO

sketchbook5, 스케치북5

sketchbook5, 스케치북5

나눔글꼴 설치 안내


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

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

설치 취소