8、日志处理
1 系统日志
可以记录日志的系统服务为
facility
The facility argument is used to specify what type of program is logging the message. This lets the
configuration file specify that messages from different facilities will be handled differently.
LOG_AUTH security/authorization messages
LOG_AUTHPRIV security/authorization messages (private)
LOG_CRON clock daemon (cron and at)
LOG_DAEMON system daemons without separate facility value
LOG_FTP ftp daemon
LOG_KERN kernel messages (these can't be generated from user processes)
LOG_LOCAL0 through LOG_LOCAL7
reserved for local use
LOG_LPR line printer subsystem
LOG_MAIL mail subsystem
LOG_NEWS USENET news subsystem
LOG_SYSLOG messages generated internally by syslogd(8)
LOG_USER (default)
generic user-level messages
LOG_UUCP UUCP subsystem
日志分为8个等级
level
This determines the importance of the message. The levels are, in order of decreasing importance:
LOG_EMERG system is unusable
LOG_ALERT action must be taken immediately
LOG_CRIT critical conditions
LOG_ERR error conditions
LOG_WARNING warning conditions
LOG_NOTICE normal, but significant, condition
LOG_INFO informational message
LOG_DEBUG debug-level message
系统的syslog记录配置文件为/etc/rsyslog.conf
,这个文件定义了各种日志文件的记录类型和记录位置。详见下文摘录的RULES部分
31 #### RULES ####
32 # Log all kernel messages to the console.
33 # Logging much else clutters up the screen.
34 #kern.* /dev/console
35 # Log anything (except mail) of level info or higher.
36 # Don't log private authentication messages!
37 *.info;mail.none;authpriv.none;cron.none /var/log/messages
38 # The authpriv file has restricted access.
39 authpriv.* /var/log/secure
40 # Log all the mail messages in one place.
41 mail.* /var/log/maillog
42 # Log cron stuff
43 cron.* /var/log/cron
44 # Everybody gets emergency messages
45 *.emerg :omusrmsg:*
46 # Save news errors of level crit and higher in a special file.
47 uucp,news.crit /var/log/spooler
48 # Save boot messages also to boot.log
49 local7.* /var/log/boot.log
日志记录的配置分两部分服务的名称
.日志的等级
,例如mail.info、cron.warning等等
如第37行配置所示,除了mail、authpriv(认证)、cron(计划任务)之外的所有大于等于info等级的日志记录在/var/log/messages
文件中,可参照添加自定义日志,添加后需重启rsyslog服务
cron.err /var/log/cronerr.log
[root@node1 ~]$ systemctl restart rsyslog.service
如第39行配置所示,authpriv(认证)所有等级的日志记录在/var/log/secure
文件中,用户所有的登录信息都能在其中看到
日志也可传给其他服务器保存,
authpriv.* @@192.168.8.66:514
#一个@是udp传输,两个@是tcp传输
2 使用tail -f 查看日志
使用tail -f
持续显示日志
定位问题时使用tail -f LOG | egrep -i
显示错误的前后5行信息
tail -f /var/log/messages | egrep -i -A 5 -B 5 'err|warn'
或
tail -f /var/log/messages | egrep -i -C 10 'err|warn'
如果不知道日志信息在哪个文件下,可以使用*通配符,显示所有日志来判断
tail -f /var/log/*
3 服务的日志
服务本身的日志文件位置一般在/etc/服务文件夹/XX.conf
文件中会配置,如httpd的日志位置在/var/log/httpd/
目录下
服务启动失败的报错信息需要查看系统日志,服务启动后的各种错误就需要查看服务本身的日志文件了
4 logrotate日志轮询
4.1 logrotate.conf配置
为防止日志文件无限增大,Linux默认做了日志轮询处理,每隔一段时间对日志进行打包,并定期删除旧的日志,打包新的日志
日志轮询的配置文件为/etc/logrotate.conf
![](https://img.haomeiwen.com/i24811079/5ec8317f809fea66.png)
/etc/logrotate.d/
目录下定义了除主配置文件外,对其他主要服务的切割操作配置。有nginx
、syslog
、yum
等
[root@web01 nginx]$ ll /etc/logrotate.d/
total 20
-rw-r--r--. 1 root root 91 Nov 28 2019 bootlog
-rw-r--r-- 1 root root 351 Oct 29 23:21 nginx
-rw-r--r--. 1 root root 224 Nov 28 2019 syslog
-rw-r--r--. 1 root root 100 Oct 31 2018 wpa_supplicant
-rw-r--r--. 1 root root 103 Apr 2 2020 yum
4.2 logrotate常用参数
参数 | 解释 |
---|---|
compress | 启用压缩,指的是轮替后的旧日志,这里默认用的是gzip压缩的 |
nocompress | 不启用压缩 |
uncompresscmd | 解压日志,默认是gunzip |
daily | 每天轮替选项 |
dateext | 轮替的日志文件会附加上一个短横线和YYYYMMDD格式的时间戳 |
delaycompress | 将以前的日志文件压缩推迟到下一次轮替 |
ifempty | 即使日志文件是空的也轮替 |
notifempty | 如果日志文件为空,轮循不会进行 |
将轮替后的文件发送到指定E-mail地址 | |
copytruncate | 用于还在打开中的日志文件,把当前日志备份并截断,开始轮替 |
mailfirst/maillast | 向邮件发送轮替文件/轮替后历史文件(默认) |
missingok | 在日志轮循期间,任何错误将被忽略,例如“文件无法找到”之类的错误。 |
monthly | 一个月轮替一次 |
nocompress | 如果在logrotate.conf中启用了压缩,这里是做不用压缩的参数 |
nomail | 不发送邮件到任何地址 |
ifempty | 如果日志时空的就不轮替 |
olddir directory | 轮替后日志文件放入指定的目录,必须和当前日志文件在同一个文件系统 |
postrotate/endscript | 在做完轮替后的命令,两个关键字必须单独成行,使用的操作在2者之间相当于分组“{}”,注意的使用外部指令时要用绝对路径 |
prerotate/endscript | 在做轮替前的命令,同上 |
rotate num | 最多保存几份历史数据,超出的将被删除或邮件接收,设为0则不保存 |
size size | 当日志增长到指定大小的时候开始轮替,它不会考虑 |
start num | 轮替文件名基于这个数字。例如,指定0时,原日志文件轮替的备份文件以.0为扩展名,如果指定9,就直接从.9开始跳过0-8然后再继续向后轮替rotate指定的次数。 |
weekly | 如果当前的星期几比上次轮替的星期几少,或者过了一个多星期,就会发生轮替通常是在每周的第一天轮替,如果logrotate不是每天运行的,会在第一次有机会时进行轮替。 |
yearly | 如果当前年份不同于上次轮替的年份,则进行日志轮替 |
create mode owner group | 在轮替动作之后,postrotate脚本执行之前,立即使用刚轮替的日志文件名创建日志文件。MODE 指定日志文件的权限(0660之类),OWNER 指定日志文件的属主 ,GROUP 指定日志文件的属组 |
extension ext | 日志文件可在轮替后使用指定的EXT扩展名。如果使用压缩,通常EXT后还会加上压缩文件的扩展名,通常是.gz。例如想把mylog.foo轮转为mylog.1.foo.gz而不是mylog.foo.1.gz |
4.3 logrotate轮询mysql.log
添加/root/.my.cnf
文件
[mysqladmin]
password = xxx
user= root
配置/root/.my.cnf
权限
chmod 400 /root/.my.cnf
添加/etc/logrotate.d/mysql-log-rotate
轮询配置,/etc/logrotate.d/中的轮询配置默认每天执行一次,如果要自行执行日志轮询,需要手写crontab
/var/log/mysql.log {
create 0600 mysql mysql
notifempty
weekly
rotate 7
missingok
nocompress
dateext
postrotate
# just if mysqld is really running
if test -x /usr/local/mysql/bin/mysqladmin && \
/usr/local/mysql/bin/mysqladmin ping &>/dev/null
then
/usr/local/mysql/bin/mysqladmin flush-logs
fi
endscript
}
手工logrotate轮询mysql.log验证
logrotate -vf /etc/logrotate.d/mysql-log-rotate
4.4 logrotate轮询nginx日志
vim /etc/logrotate.d/nginx
/usr/local/nginx/logs/*log {
create 0664 nginx nginx
daily
dateext
rotate 10
missingok
notifempty
compress
sharedscripts
postrotate
/bin/kill -USR1 `cat /usr/local/nginx/logs/nginx.pid` 2>/dev/null || true
endscript
}
手工logrotate轮询验证
logrotate -vf /etc/logrotate.d/nginx
5 dmesg
记录系统最后一次的启动信息,每次启动会重置刷新
可以用来查看系统启动时硬件的检测信息,如查看系统网卡信息
[root@node1 ~]$ dmesg | grep eth