12-7 内核及系统日志
2016.6.25
12-7-1分析日志文件
- 分析日志文件的目的在于通过浏览日志查找关键信息、对系统服务进程进行调试,以及判断发生故障的原因等
- 对于大多数文本格式的日志文件(如内核及系统日志、大多数的程序日志),只要使用tail、more、less、cat等文本处理工具就可以查看日志内容
而对于一些二进制格式的日志文件(如用户日志),则需要使用特定的查询命令
12-7-2内核及系统日志
- rsyslog服务所使用的配置文件为/etc/rsyslog.conf 通过查看该文件中的内容,可以了解到系统默认的日志设置
- grep -v "^$" /etc/rsyslog.conf
- 从配置文件/etc/rsyslog.conf中可以看到,收rsyslogd服务管理的紫日文件都是linux系统中最主要的日志文件,它们记录了linux系统中内核、用户认证、邮件、计划任务等最基本的系统消息
/etc/rsyslog.conf
[root@test2 ~]# vi /etc/rsyslog.conf
# rsyslog v5 configuration file
# For more information see /usr/share/doc/rsyslog-*/rsyslog_conf.html
# If you experience problems, see http://www.rsyslog.com/doc/troubleshoot.html
#### MODULES ####
$ModLoad imuxsock # provides support for local system logging (e.g. via logger command)
$ModLoad imklog # provides kernel logging support (previously done by rklogd)
#$ModLoad immark # provides --MARK-- message capability
# Provides UDP syslog reception
#$ModLoad imudp
#$UDPServerRun 514
# Provides TCP syslog reception
#$ModLoad imtcp
#$InputTCPServerRun 514
#### GLOBAL DIRECTIVES ####
# Use default timestamp format
$ActionFileDefaultTemplate RSYSLOG_TraditionalFileFormat
# File syncing capability is disabled by default. This feature is usually not required,
# not useful and an extreme performance hit
#$ActionFileEnableSync on
# Include all config files in /etc/rsyslog.d/
$IncludeConfig /etc/rsyslog.d/*.conf
#### RULES ####
# Log all kernel messages to the console.
# Logging much else clutters up the screen.
#kern.* /dev/console
# Log anything (except mail) of level info or higher.
# Don't log private authentication messages!
*.info;mail.none;authpriv.none;cron.none /var/log/messages
# The authpriv file has restricted access.
authpriv.* /var/log/secure
# Log all the mail messages in one place.
mail.* -/var/log/maillog
# Log cron stuff
cron.* /var/log/cron
# Everybody gets emergency messages
*.emerg *
uucp,news.crit /var/log/spooler
# Save boot messages also to boot.log
local7.* /var/log/boot.log
# ### begin forwarding rule ###
# The statement between the begin ... end define a SINGLE forwarding
# rule. They belong together, do NOT split them. If you create multiple
# forwarding rules, duplicate the whole block!
# Remote Logging (we use TCP for reliable delivery)
#
# An on-disk queue is created for this action. If the remote host is
# down, messages are spooled to disk and sent when it is up again.
#$WorkDirectory /var/lib/rsyslog # where to place spool files
#$ActionQueueFileName fwdRule1 # unique name prefix for spool files
#$ActionQueueMaxDiskSpace 1g # 1gb space limit (use as much as possible)
#$ActionQueueSaveOnShutdown on # save messages to disk on shutdown
#$ActionQueueType LinkedList # run asynchronously
#$ActionResumeRetryCount -1 # infinite retries if host is down
# remote host is: name/ip:port, e.g. 192.168.0.1:514, port optional
#*.* @@remote-host:514
# ### end of the forwarding rule ###
# A template to for higher precision timestamps + severity logging
$template SpiceTmpl,"%TIMESTAMP%.%TIMESTAMP:::date-subseconds% %syslogtag% %syslogseverity-text%:%msg:::sp-if-no-1st-sp%%msg:::drop-last-lf%\n"
:programname, startswith, "spice-vdagent" /var/log/spice-vdagent.log;SpiceTmpl
85,1 Bot
*.info代表info等级以上的所有相关信息都会写入到/var/log/messages mail.none代表mail不写
12-7-3日志消息级别
在linux内核中,根据日志消息的重要程度不同。将其分为不同的优先级别(数字等级越小,优先级越高,消息越重要)
0 EMERG(紧急):会导致主机系统不可用的情况
1 ALERT(警告):必须马上采取措施解决问题
2 CRIT(严重):比较严重的情况
3 ERR(错误):运行出现错误
4 WARNING(提醒):可能影响系统功能,需要提醒用户的重要事件
5 NOTICE(注意):不会影响正常功能,但是需要注意的事件
6 INFO (信息):一般信息
7 DEBUG(调试):程序或系统调试信息等
12-7-4日志文件分析
- 内核及大多数系统消息都会被记录到公共日志文件/var/log/messages中,而其他一些程序消息被记录到各自独立地日志文件中
- 对于rsyslog服务统一管理的大部分日志文件,使用的日志记录格式基本上都是相同的。以公共日志/var/log/messages文件的记录格式为例,其中每一个行表示一条日志消息,每一条消息均包括以下四个字段:
- 时间标签:消息发出的日期和事件
- 主机名:生成消息的计算机的名称
- 子系统名称:发出消息的的应用程序的名称
- 消息:消息的具体内容