
技术周报:Syslog 日志记录 [技术天地]
2008-7-3 15:15 Thursday
以后争取每周捣鼓出一篇关于技术的文章来,哪怕是剽窃。毕竟咱也是个程序猿,该多关心下技术。
由于工作需要经常要记录各种系统运行行为的日志,包括各种异常的发生,已备查。用的最多的就是unix下的syslog日志系统。这份文档基本囊括了syslog的基本使用和配置方法。不感兴趣请绕行。
该文章系转载,原作者不详!!
最初, syslog只是为了sendmail而设计的消息日志工具,由于它提供了一个中心控制点,使得syslog非常好用和易配置,因此当今很多程序都使用syslog来发送它们的记录信息。syslog是一种强大的日志记录方式,不但可以将日志保存在本地文件中,还可以根据设置将syslog记录发送到网络上的另一台主机中。 支持syslog方式的系统启动了syslogd守护进程,这个程序从本地的Unix套接字和监听在514端口(UDP)上的Internet套接字,来获得syslog的记录。本机中进程使用syslog系统调用发送来syslog记录,然后由syslogd将他们保存到正确的文件或发送到网络上另一台运行syslogd主机中去。
syslogd的设置文件为/etc/syslog.conf,定义消息对应的相应目标,一条消息可以达到多个目标,也可能被忽略。
# $Id: syslog.conf,v 1.9 1998/10/14 21:59:55 nate Exp $
#
# Spaces are NOT valid field separators in this file.
# Consult the syslog.conf(5) manpage.
*.err;kern.debug;auth.notice;mail.crit /dev/console
*.notice;kern.debug;lpr.info;mail.crit;news.err /var/log/messages
mail.info /var/log/maillog
lpr.info /var/log/lpd-errs
cron.* /var/cron/log
*.err root
*.notice;news.err root
*.alert root
*.emerg *
!ppp
*.* /var/log/ppp.log
syslog.conf的配置可以分为两个部分,第一部分用于区分消息的类型,另一个用于设置消息发送的目的地。通常,消息的类型包括消息的产生者,例如kern表示内核产生的消息,auth表示认证系统产生的消息,等等,还包括消息的级别,例如emerg表示非常重要的紧急信息,alert表示系统告警状态,crit表示关键状态,err表示一般的错误信息,warning表示警告信息,notice表示提示信息,但还不是错误,info表示一般信息,debug表示调试信息等,因此一个消息的类型可能为:kern.debug、mail.info等,但页可以使用通配符*进行匹配。
从上面的 syslog.conf 的设置可以看出,系统正常运行中的很多重要的信息,如错误信息*.err、内核调试信息kern.debuf、认证报告auth.notice等被直接输出的console中,另外还有一些比较重要的信息被输出到/var/log/messages文件中,发送邮件的记录将被保存在/var/log/maillog文件中,打印记录为/var/log/lpd-errs等,使得管理员可以根据这些文件来查询相关记录,进行统计或寻找系统问题。其中使用syslog记录的messages文件中包括root登录的信息、用户多次登录失败的尝试等对系统安全相当重要的信息,因此也是系统遭受攻击之后,攻击者会根据syslog.conf中设置试图清除相关文件中自己的登录记录。因此对于安全性要求更高的系统,可以尝试将syslog发送到另一台计算机上,或者输出到一些设备文件中,如在打印机上立即打印输出。
日志管理 newsyslog
系统会使用 newsyslog 定期检查syslog输出的messages文件和maillog文件,将旧数据压缩保存为备份文件,如messages.1.gz等。这是一项非常重要的功能,否则系统日志将不断的增长,直至占满所有磁盘空间。
Newsyslog可以使用两种方式来管理日志文件,一种是定期管理日志,另一种为按照设置的大小管理日志,然后可以生成多个日志备份文件。它的配置文件为/etc/newsyslog.conf,系统管理员可以根据这里的已有配置,添加对自己的日志文件的管理。
logfilename
Name of the system log file to be archived, or the literal string ``<default>''. The special default entry will only be used if a log file name is given as a command line argument to newsyslog(8), and if that log file name is not matched by any other line in the configuration file.
欲被压缩的 system log 的完整路径。
[owner:group]
This optional field specifies the owner and group for the archive file. The `:' is essential regardless if the owner or group field is left blank or contains a value. The field may be numeric, or a name which is present in /etc/passwd or /etc/group.
log 的拥有群组。
mode
Specify the file mode of the log file and archives.
log 的档案权限,一般是 600、640 … 等。
count
Specify the maximum number of archive files which may exist. This does not consider the current log file.
产生档案的数目。例:all.log count 为 7 ,则产生下列
all.log all.0.bz2 all.1.bz2 all.2.bz2 all.3.bz2 all.4.bz2 all.5.bz2 all.6.bz2 all.7.bz2
count 是从 0 ~ 7 还有一个现在正在使用的 log 檔。
size
When the size of the log file reaches size in kilobytes, the log file will be trimmed as described above. If this field contains an asterisk (`*'), the log file will not be trimmed based on size.
填入的数字代表到达多少 KB 就进行压缩1), * 则代表不管档案大小,时间到就压缩。
when
设定要备份 log 的时间,有两种方式。
ISO 8601 restricted time format
The lead-in character for a restricted ISO 8601 time is an `@' sign. The particular format of the time in restricted ISO 8601 is: [[[[[cc]yy]mm]dd][T[hh[mm[ss]]]]]. Optional date fields default to the appropriate component of the current date; optional time fields default to midnight; hence if today is January 22, 1999, the following date specifications are all equivalent:
`19990122T000000'
`990122T000000'
`0122T000000'
`22T000000'
`T000000'
`T0000'
`T00'
`22T'
`T'
`'
假设时间是 2006-07-26 10:05:45 ,则标准格式可写成 20060726T100545 ,但通常我们都是设定到日及时间,如下:
@T00 ⇒ 代表每日 0 时执行
@01T05 ⇒ 代表每个月的 01 日 05 时执行
Day, week, and month time format
The lead-in character for day, week, and month specification is a `$' sign. The particular format of day, week, and month specification is: [Dhh], [Ww[Dhh]], and [Mdd[Dhh]], respectively. Optional time fields default to midnight. The ranges for day and hour specifications are:
hh hours, range 0..23
w day of week, range 0..6, 0 = Sunday
dd day of month, range 1..31, or one of the letters
`L' or `l' to specify the last day of the month.
Some examples:
$D0 rotate every night at midnight (same as @T00)
$D23 rotate every day at 23:00 (same as @T23)
$W0D23 rotate every week on Sunday at 23:00
$W5D16 rotate every week on Friday at 16:00
$M1D0 rotate at the first day of every month at midnight
(i.e., the start of the day; same as @01T00)
$M5D6 rotate on every 5th day of month at 6:00 (same as @05T06)
FreeBSD 专属时间格式M W D (月、周、天、小时) 表达方式:(前面要加上 $符号)
$M5D6 # 代表每个月第 5 天的 早上 6 点
$W0D4 # 代表每周第一天(星期天)早上 4 点
$MLH22 # 每月最后一天的晚上10点执行
flags
This optional field is made up of one or more characters that specify any special processing to be done for the log files matched by this line. The following are valid flags:
B indicates that the log file is a binary file, or has some special format. Usually newsyslog(8) inserts an ASCII message into a log file during rotation. This message is used to indicate when, and sometimes why the log file was rotated. If B is specified, then that informational message will not be inserted into the log file.
C indicates that the log file should be created if it does not already exist, and if the -C option was also specified on the command line.
J indicates that newsyslog(8) should attempt to save disk space by compressing the rotated log file using bzip2(1).
Z indicates that newsyslog(8) should attempt to save disk space by compressing the rotated log file using gzip(1).
备份时压缩或处理的格式,例:
Z # 使用 gzip 压缩。
J # 使用 bzip2 压缩。
B # 使用 二位 文件。
C # 如果不存在,会帮忙建立此檔。
[/pid_file]
This optional field specifies the file name containing a daemon's process ID or to find a group process ID if the U flag was specified. If this field is present, a signal_number is sent the process ID contained in this file. If this field is not present, then a SIGHUP signal will be sent to syslogd(8), unless the N flag has been specified. This field must start with `/' in order to be recognized properly.
pid 文件的绝对路径,有设定则会送 signal_number 给指定的程序,但一般不会设定。不过像要备份 apache log 时就需要用到,备份完 apache log 需要重新启动 /var/run/httpd.pid ,否则 apache log 备份完后会因为找不到 Process ID ,而无法在产生新的 apache log 。
例如下列:
/var/log/httpd-access.log 640 13 * $M1D0 Z /var/run/httpd.pid
[sig_num]
This optional field specifies the signal number that will be sent to the daemon process (or to all processes in a process group, if the U flag was specified). If this field is not present, then a SIGHUP signal will be sent.
送给该 daemon 程序的 signal number ,预设是 SIGHUP 。
标签: 技术