红帽系统管理一 11章节 分析和存储日志

系统日志架构

进程和操作系统内核发生的时间将记录到日志。红帽linux建立了基于syslog协议的标准日志记录系统。
systemd-journald服务是操作系统事件日志架构的核心。他收集许多来源的事件消息,包括内核,引导过程早期阶段的输出,守护进程启动和系统运行时的标准输出和标准错误,还包括syslog事件。收集之后,他会将这些消息重构成一种标准格式,并写入一种带索引的结构化系统日志中。
rsyslog服务会进一步的处理systemd-journald收集的syslog消息:rsyslog根据一定的规则,对syslog数据进行排序,然后写入到指定的日志文件中或者按要求发送给其服务。

除了syslog消息外,/var/log还包含了很多其他服务的日志文件。

日志文件存储的消息类型
/var/log/messages大多数系统日志消息存储在此。比如电子邮件处理,调度作业执行等
/var/log/secure安全性和身份验证相关的syslog消息
/var/log/maillog与邮件服务器相关的syslog消息
/var/log/cron与调度作业执行相关的
/var/log/boot.log与系统启动相关的非syslog控制台消息

查看系统日志文件

将事件记录到系统

系统对时间的优先级进行了区分

代码优先级严重性
0emerg系统不可用
1alert必须立即采取措施
2crit临界情况
3err非严重错误
4warring警告
5notice正常但重要的事件
6info信息性事件
7debug调试级别消息

rsyslog服务根据消息的优先级以及消息的发送设备来按照一定的配置规则确定如何处理日志消息。配置规则位于/etc/rsyslog.conf文件或位于/etc/rsyslog.d目录中,拓展名为.conf的任何文件。
配置文件中的以下语句,意义为:将设备authpriv发出的优先级为 * (*即代表所有的优先级)的消息记录到文件/var/log/secure中。

# The authpriv file has restricted access.
authpriv.*                                              /var/log/secure

一条日志消息可能会匹配到多条规则,因此将有肯呢个被存储到多个文件中。为了限制存储消息,在优先级字段中使用关键字none,可以禁止规则左侧指定的设备将日志存储在规则右侧指定的文件中。如下,设备authpriv的消息无论如何将不会存储到/var/log/secure
文件中。

# The authpriv file has restricted access.
authpriv.none                                              /var/log/secure

rsyslog规则示例

# 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的消息
*.emerg                                                 :omusrmsg:*

# Save news errors of level crit and higher in a special file.
uucp,news.crit                                          /var/log/spooler

# Save boot messages also to boot.log
local7.*                                                /var/log/boot.log
日志文件轮转

logrotate工具会轮转日志文件,以防止他们在含有/var/log目录的文件系统中占用过多的空间。轮转文件时,使用轮状日期作为拓展名对其进行重命名。例如,在2023年10月1 号轮转,原来名为/var/log/messages的文件可能会变成/var/log/messages-20231001。轮转后,会创建新的日志文件,并通知对它执行写操作的服务。
轮转若干次后(通常在四周之后),丢弃最旧的日志文件。调度作业每日运行一次logrotate程序,以查看是否有任何日志需要轮转。

分析syslog条目

日志文件的开头记录最旧的消息,末尾记录最新消息。以下是日志文件中消息的标准格式。

  67	Oct 24 14:17:56 #test sshd[6586]: pam_unix(sshd:session): session opened for user root by (uid=0)
监控日志
tail -f /path/to/log

[root@#test ~]# tail -f /var/log/secure #持续监控登录情况
手动发送syslog消息

使用logger命令可以发送消息到rsyslog服务。默认情况下,命令将发送优先级为notice的消息发送给设备user(即在配置rsyslog配置文件格式为:user.notice),除非通过 -p 选项选定其他。这个命令可用于测试rsyslog的配置情况。
向rsyslog服务发送消息并记录在/var/log/boot.log文件中。

[root@#test ~]# logger -p local7.notice "logger test with /var/log/boot.log"
[root@#test ~]# 
[root@#test ~]# tail -n 3 /var/log/boot.log
         Starting Wait for Plymouth Boot Screen to Quit...
         Starting Terminate Plymouth Boot Screen...
Oct 26 08:55:00 #test root: logger test with /var/log/boot.log

需求:将所有的debug级别的信息重定向到/var/log/message-debug文件。
更改rsyslog配置文件,重启rsyslog服务:

[root@#test ~]# vim /etc/rsyslog.conf
#添加以下内容
#Log all the debug messgaes in one place
*.debug                                        /var/log/messages-debug

[root@#test ~]# systemctl restart rsyslog

使用logger命令测试以上配置是否成功:手动发送debug级别消息。tail 查看messages-debug文件中是否收到手动发送的消息。

[root@#test ~]# logger -p user.debug "test:log debug messages in /var/log/messages-debug"
[root@#test ~]# 
[root@#test ~]# tail -n 3 /var/log/messages-debug 
Oct 26 09:30:01 #test systemd: Started Session 9 of user root.
Oct 26 09:30:01 #test CROND[7001]: (root) CMD (/usr/lib64/sa/sa1 1 1)
Oct 26 09:32:30 #test root: test:log debug messages in /var/log/messages-debug

查看系统日志条目

linux8中默认在/run/log目录存储系统日志。默认情况下,/run/log中的内容将在系统重启后被删除。
使用journalctl命令来检索系统日志中的日志消息。

[root@#test ~]# journalctl   #显示本次启动以来的全部日志
[root@#test ~]# journalctl -n 5   #显示最后5条日志
[root@#test ~]# journalctl -f   #持续监控
[root@#test ~]# journalctl -p err   #只显示err或者更高级别的日志
[root@#test ~]# journalctl --since today   #时间格式可以是“YYYY-MM-DDhh:mm:ss”"-1 hour"
保留系统日志

永久保存系统日志,默认情况下,/run/log中的内容将在系统重启后被删除。可以使用/etc/systemd/journald.conf文件中更改systemd-journald服务的配置设置,使系统日志保存下来。
/etc/systemd/journald.conf文件中的storage参数将决定系统日志的存储方式:
persistent,将日志存储在/var/log/journal目录中,这可在系统重启后持久保留。如过系统中没有这个目录,那么系统将会自动创建这个目录。
volatile,将日志存储在易失性/run/log/journal目录中,系统重启后不会被保留。
auto,系统存在/var/log/journal目录时,存储持久性保留目录/var/log/journal目录中,不存在此目录时间,存储在易失性/run/log/journal目录中。
注意:auto选项为默认选项。即便是持久日志,也不是永久保留,日志每月会进行轮转,日志大小占用的文件系统不能超过预设的比例…

维护准确的时间

在多个系统间分析日志文件,正确同步系统间

[root@#test ~]# timedatectl    #列出当前系统时间时区,以及相关配置信息
[root@#test ~]# timedatectl list-timezones   #列出时区
[root@#test ~]# timedatectl set-timezone America/Phoenix   #设置时区
[root@#test ~]# timedatectl set-time "YYYY-MM-DD hh:mm:ss"   #设置具体时间
[root@#test ~]# timedatectl set-ntp true   #启用ntp服务(自动同步时间)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值