Theo thời gian tôi nhận thấy một số nhật ký /var/lognhư auth, kernvà messagesđang trở nên rất lớn. Tôi đã thực hiện logrotatecác mục cho họ:
$ cat /etc/logrotate.d/auth.log
/var/log/kern.log {
rotate 5
daily
}
$ cat /etc/logrotate.d/kern.log
/var/log/kern.log {
rotate 5
daily
}
$ cat /etc/logrotate.d/messages
/var/log/messages {
rotate 5
daily
postrotate
/bin/killall -HUP syslogd
endscript
}
Ngoài ra tôi có compresstùy chọn được kích hoạt:
$ grep compress /etc/logrotate.conf
# uncomment this if you want your log files compressed
compress
Điều này hoạt động rất tốt cho auth.log, kern.logvà những người khác, có nghĩa là mỗi bản ghi đó được nén và xoay, với 5 ngày cuối cùng của bản ghi được giữ lại. /var/log/messagestuy nhiên không được nén, dẫn đến cách hơn 5 ngày nhật ký:
$ ls /var/log/messages*
/var/log/messages /var/log/messages-20100213
/var/log/messages-20100201 /var/log/messages-20100214
/var/log/messages-20100202 /var/log/messages-20100215
/var/log/messages-20100203 /var/log/messages-20100216
/var/log/messages-20100204 /var/log/messages-20100217
/var/log/messages-20100205 /var/log/messages-20100218
/var/log/messages-20100206 /var/log/messages-20100219
/var/log/messages-20100207 /var/log/messages-20100220
/var/log/messages-20100208 /var/log/messages-20100221
/var/log/messages-20100209 /var/log/messages-20100222
/var/log/messages-20100210 /var/log/messages-20100223
/var/log/messages-20100211 /var/log/messages-20100224
/var/log/messages-20100212
Như được giải thích trong một logrotatecâu hỏi khác trên ServerFault , các nhật ký cũ (rất có thể) không bị xóa vì các kết thúc tệp khác nhau đối với mỗi tệp. Điều này dường như là do các tập tin không được nén.
Tôi có thể làm gì để /var/log/messagesnén và xoay vòng trong 5 ngày cuối cùng của nhật ký được giữ lại giống như tất cả các tệp nhật ký khác của tôi? Tôi đang thiếu gì?
EDIT 1 : thông tin bổ sung theo yêu cầu trong câu trả lời đầu tiên.
Tôi đang chạy Gentoo Linux. /etc/logrotate.confTập tin của tôi
$ cat /etc/logrotate.conf
# $Header: /var/cvsroot/gentoo-x86/app-admin/logrotate/files/logrotate.conf,v 1.3 2008/12/24 20:49:10 dang Exp $
#
# Logrotate default configuration file for Gentoo Linux
#
# See "man logrotate" for details
# rotate log files weekly
weekly
#daily
# 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
# packages can drop log rotation information into this directory
include /etc/logrotate.d
notifempty
nomail
noolddir
# no packages own lastlog or wtmp -- we'll rotate them here
/var/log/wtmp {
monthly
create 0664 root utmp
rotate 1
}
/var/log/btmp {
missingok
monthly
create 0600 root utmp
rotate 1
}
/etc/logrotate.d chứa các tệp cấu hình tùy chỉnh của tôi như đã đề cập ở trên cùng với các cấu hình cho mysql, rsync, v.v. được cài đặt bởi các gói đó.
Root của tôi crontabtrống:
$ sudo crontab -l
no crontab for root
Tôi đã kiểm tra tất cả mọi /etc/cron.{daily,hourly,monthly,weekly}thứ liên quan đến syslog và có một đoạn script xoay /var/log/syslogvà /var/log/auth.log.
Tiếp theo, tôi đã tạo một tệp cấu hình /var/log/messages-only logrotatetheo đề xuất của CarpeNoctem:
$ cat logrotate-messages
weekly
rotate 4
create
dateext
compress
notifempty
nomail
noolddir
/var/log/messages {
rotate 5
daily
postrotate
/bin/killall -HUP syslogd
endscript
}
Sau đó tôi chạy logrotatethủ công:
$ logrotate -d logrotate-messages -f
reading config file logrotate-messages
reading config info for /var/log/messages
Handling 1 logs
rotating pattern: /var/log/messages forced from command line (5 rotations)
empty log files are not rotated, old logs are removed
considering log /var/log/messages
log needs rotating
rotating log /var/log/messages, log->rotateCount is 5
dateext suffix '-20100224'
glob pattern '-[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]'
glob finding old rotated logs failed
renaming /var/log/messages to /var/log/messages-20100224
creating new /var/log/messages mode = 0644 uid = 0 gid = 0
running postrotate script
running script with arg /var/log/messages : "
/bin/killall -HUP syslogd
"
compressing log with: /bin/gzip
$ which gzip
/bin/gzip
$ file /bin/gzip
/bin/gzip: ELF 32-bit LSB executable, Intel 80386, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.9, stripped
Theo nhật ký ở trên, đã logrotatenén nhật ký bằng / bin / gzip, nhưng tôi không thấy tệp tin được nén trong /var/log. Ngoài ra, globalbing cho các tập tin xoay cũ không thành công.
EDIT 2 : thêm đầu ra gỡ lỗi của logrotatechạy sau khi thêm .gzhậu tố vào /var/log/message-*các tệp cũ .
Chúng tôi bắt đầu với:
$ ls /var/log/messages*
/var/log/messages /var/log/messages-20100222.gz
/var/log/messages-20100219.gz /var/log/messages-20100223.gz
/var/log/messages-20100220.gz /var/log/messages-20100224.gz
/var/log/messages-20100221.gz
Sau đó chạy logrotatevới tệp cấu hình tùy chỉnh của chúng tôi:
$ logrotate -d logrotate-messages -f
reading config file logrotate-messages
reading config info for /var/log/messages
Handling 1 logs
rotating pattern: /var/log/messages forced from command line (5 rotations)
empty log files are not rotated, old logs are removed
considering log /var/log/messages
log needs rotating
rotating log /var/log/messages, log->rotateCount is 5
dateext suffix '-20100224'
glob pattern '-[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]'
removing /var/log/messages-20100219.gz
removing old log /var/log/messages-20100219.gz
destination /var/log/messages-20100224.gz already exists, skipping rotation
Lần này, toàn logrotatecầu thành công và tìm thấy logfile nén thứ sáu, dự định loại bỏ nó. Các tập tin không thực sự được gỡ bỏ; Tôi đoán đó là vì chúng tôi đang chạy trong chế độ gỡ lỗi.
Tôi tò mò liệu kích hoạt delaycompresstùy chọn cho /var/log/messagessẽ giúp. Tôi đã kích hoạt nó và sẽ kiểm tra kết quả vào sáng hôm sau.