Linux系统中的日志管理及优化方式

前言

日志的采集是由各个程序来完成的,因为你的程序会将自己的某些执行信息书写到日志中。
而journald和rsyslog是用来采集日志的,并以某种格式保存。在企业7之前只有rsyslog。在企业7之后两个都有。
journald是无法关闭的。
在系统中做任何重大的设置修改,都会产生日志。

  • 实验环境
    rhel7_node1:172.25.254.10
    rhel8_node1:172.25.254.11

journald

服务名称:systemd-journald.service

[root@rhel7_node1 ~]# systemctl status systemd-journald.service
● systemd-journald.service - Journal Service
   Loaded: loaded (/usr/lib/systemd/system/systemd-journald.service; static; vendor preset: disabled)
   Active: active (running) since Mon 2020-03-09 19:10:55 CST; 53min ago
     Docs: man:systemd-journald.service(8)
           man:journald.conf(5)
 Main PID: 3353 (systemd-journal)
   Status: "Processing requests..."
    Tasks: 1
   CGroup: /system.slice/systemd-journald.service
           └─3353 /usr/lib/systemd/systemd-journald

Mar 09 19:10:55 rhel7_node1.westos.com systemd-journal[3353]: Runtime journal…).
Mar 09 19:10:55 rhel7_node1.westos.com systemd-journal[3353]: Journal started
Hint: Some lines were ellipsized, use -l to show in full.

所提供的命令是journalctl
默认日志存放路径:/run/log(每次重启系统会对日志进行清空)

[root@rhel7_node1 ~]# cd /run/log
[root@rhel7_node1 log]# ls
journal
[root@rhel7_node1 log]# bootctl 
System:
   Machine ID: 4f72081b06bf47e99ea36241be61e2e4   #机器码,显示当前机器的日志
      Boot ID: 15a33520fcfe40d48709fc6faa55a7ba

No suitable data is provided by the boot manager. See:
  http://www.freedesktop.org/wiki/Software/systemd/BootLoaderInterface
  http://www.freedesktop.org/wiki/Specifications/BootLoaderSpec
for details.

[root@rhel7_node1 journal]# ls
4f72081b06bf47e99ea36241be61e2e4

实验步骤(journalctl命令的用法)

  • journalctl
    -n 3(日志的最新3条)

       [root@rhel7_node1 journal]# systemctl restart sshd
      [root@rhel7_node1 journal]# journalctl -n 3
      -- Logs begin at Mon 2020-03-09 19:09:58 CST, end at Mon 2020-03-09 20:12:01 CST. --
      Mar 09 20:12:00 rhel7_node1.westos.com sshd[10817]: Server listening on :: port 22.
      Mar 09 20:12:01 rhel7_node1.westos.com polkitd[7090]: Unregistered Authentication Agent for unix-pr
      Mar 09 20:12:00 rhel7_node1.westos.com systemd[1]: Started OpenSSH server daemon.
    

    since “2020-02-13 17:00”(显示17:00后的日志)

        [root@rhel7_node1 ~]# journalctl --since "2020-03-10 16:25"
      -- Logs begin at Tue 2020-03-10 16:22:32 CST, end at Tue 2020-03-10 16:30:01 CST. --
      Mar 10 16:25:24 rhel7_node1.westos.com gnome-shell[9919]: JS WARNING: [resource:///org/gnome/shell/
      Mar 10 16:25:24 rhel7_node1.westos.com gnome-shell[9919]: JS WARNING: [resource:///org/gnome/shell/
      Mar 10 16:25:26 rhel7_node1.westos.com dbus[7066]: [system] Activating via systemd: service name='n
      ...
    

    until"2020-02-13 17:00"(显示日志到17:00)

      [root@rhel7_node1 ~]# journalctl --until "2020-03-10 16:22:33"
      -- Logs begin at Tue 2020-03-10 16:22:32 CST, end at Tue 2020-03-10 16:31:28 CST. --
      Mar 10 16:22:32 localhost.localdomain systemd-journal[85]: Runtime journal is using 8.0M (max allow
      Mar 10 16:22:32 localhost.localdomain kernel: Initializing cgroup subsys cpuset
      Mar 10 16:22:32 localhost.localdomain kernel: Initializing cgroup subsys cpu
      Mar 10 16:22:32 localhost.localdomain kernel: Initializing cgroup subsys cpuacct
      ...
    

    -o(设置日志的显示方式)
    short(经典模式显示日志)

      [root@rhel7_node1 ~]# journalctl -o short
      -- Logs begin at Tue 2020-03-10 16:22:32 CST, end at Tue 2020-03-10 16:33:31 CST. --
      Mar 10 16:22:32 localhost.localdomain systemd-journal[85]: Runtime journal is using 8.0M (max allow
      Mar 10 16:22:32 localhost.localdomain kernel: Initializing cgroup subsys cpuset
      Mar 10 16:22:32 localhost.localdomain kernel: Initializing cgroup subsys cpu
      Mar 10 16:22:32 localhost.localdomain kernel: Initializing cgroup subsys cpuacct
    

    verbose(显示日志的全部字节)

      [root@rhel7_node1 ~]# journalctl -o verbose
      -- Logs begin at Tue 2020-03-10 16:22:32 CST, end at Tue 2020-03-10 16:36:30 CST. --
      Tue 2020-03-10 16:22:32.608315 CST [s=f137873616914c5dae3d6ba6f1bfd010;i=1;b=ea79dda9bc13465f9f8acb
          PRIORITY=6
          _TRANSPORT=driver
          MESSAGE=Runtime journal is using 8.0M (max allowed 90.9M, trying to leave 136.4M free of 901.7M
          MESSAGE_ID=ec387f577b844b8fa948f33cad9a75e6
          _PID=85
          _UID=0
          _GID=0
          _COMM=systemd-journal
          _EXE=/usr/lib/systemd/systemd-journald
          _CMDLINE=/usr/lib/systemd/systemd-journald
          _CAP_EFFECTIVE=5402800cf
          _SYSTEMD_CGROUP=/system.slice/systemd-journald.service
          _SYSTEMD_UNIT=systemd-journald.service
          _SYSTEMD_SLICE=system.slice
          _BOOT_ID=ea79dda9bc13465f9f8acb8ed43f07b1
          _MACHINE_ID=4f72081b06bf47e99ea36241be61e2e4
          _HOSTNAME=localhost.localdomain
      Tue 2020-03-10 16:22:32.608396 CST [s=f137873616914c5dae3d6ba6f1bfd010;i=2;b=ea79dda9bc13465f9f8acb
          PRIORITY=6
          _BOOT_ID=ea79dda9bc13465f9f8acb8ed43f07b1
          _MACHINE_ID=4f72081b06bf47e99ea36241be61e2e4
    

    export(适合传输和备份的二进制格式)

      [root@rhel7_node1 ~]# journalctl -o export
      __CURSOR=s=f137873616914c5dae3d6ba6f1bfd010;i=1;b=ea79dda9bc13465f9f8acb8ed43f07b1;m=170534;t=5a07b
      __REALTIME_TIMESTAMP=1583828552608315
      __MONOTONIC_TIMESTAMP=1508660
      _BOOT_ID=ea79dda9bc13465f9f8acb8ed43f07b1
      PRIORITY=6
      _TRANSPORT=driver
      MESSAGE=Runtime journal is using 8.0M (max allowed 90.9M, trying to leave 136.4M free of 901.7M ava
      MESSAGE_ID=ec387f577b844b8fa948f33cad9a75e6
      _PID=85
      ......
    

    json(js格式显示输出)

      [root@rhel7_node1 ~]# journalctl -o json
      { "__CURSOR" : "s=f137873616914c5dae3d6ba6f1bfd010;i=1;b=ea79dda9bc13465f9f8acb8ed43f07b1;m=170534;
      { "__CURSOR" : "s=f137873616914c5dae3d6ba6f1bfd010;i=2;b=ea79dda9bc13465f9f8acb8ed43f07b1;m=170585;
      { "__CURSOR" : "s=f137873616914c5dae3d6ba6f1bfd010;i=3;b=ea79dda9bc13465f9f8acb8ed43f07b1;m=17059c;
      { "__CURSOR" : "s=f137873616914c5dae3d6ba6f1bfd010;i=4;b=ea79dda9bc13465f9f8acb8ed43f07b1;m=1705ad;
      { "__CURSOR" : "s=f137873616914c5dae3d6ba6f1bfd010;i=5;b=ea79dda9bc13465f9f8acb8ed43f07b1;m=1705b9;
      { "__CURSOR" : "s=f137873616914c5dae3d6ba6f1bfd010;i=6;b=ea79dda9bc13465f9f8acb8ed43f07b1;m=1705c7;
      { "__CURSOR" : "s=f137873616914c5dae3d6ba6f1bfd010;i=7;b=ea79dda9bc13465f9f8acb8ed43f07b1;m=1705d5;
      { "__CURSOR" : "s=f137873616914c5dae3d6ba6f1bfd010;i=8;b=ea79dda9bc13465f9f8acb8ed43f07b1;m=1705e3;
      { "__CURSOR" : "s=f137873616914c5dae3d6ba6f1bfd010;i=9;b=ea79dda9bc13465f9f8acb8ed43f07b1;m=1705f5;
    

    -p(显示指定级别的日志)
    0 emerg(系统的严重问题日志)
    1 alert(系统中立即要更改的信息)
    2 crit(严重级别会导致系统软件不能正常工作)
    3 err(程序报错)
    4 warning(程序警告)
    5 notice(重要信息的普通日志)
    6 info(普通信息)
    7 debug(程序排错信息)

      [root@rhel7_node1 ~]# journalctl -p err
      -- Logs begin at Tue 2020-03-10 16:22:32 CST, end at Tue 2020-03-10 16:41:07 CST. --
      Mar 10 16:22:54 rhel7_node1.westos.com kernel: piix4_smbus 0000:00:07.3: SMBus Host Controller not 
      Mar 10 16:23:14 rhel7_node1.westos.com smartd[7088]: DEVICESCAN failed: glob(3) aborted matching pa
      Mar 10 16:23:14 rhel7_node1.westos.com smartd[7088]: In the system's table of devices NO devices fo
      Mar 10 16:24:10 rhel7_node1.westos.com pulseaudio[10476]: [alsa-source-ES1371/1] alsa-source.c: ALS
      Mar 10 16:24:10 rhel7_node1.westos.com pulseaudio[10476]: [alsa-source-ES1371/1] alsa-source.c: Mos
      Mar 10 16:24:10 rhel7_node1.westos.com pulseaudio[10476]: [alsa-source-ES1371/1] alsa-source.c: We 
      Mar 10 16:24:24 rhel7_node1.westos.com spice-vdagent[11460]: Cannot access vdagent virtio channel /
      ...
    

    -F PRIORITY(查看可控日志级别是234567因为01是内核级别的)

      [root@rhel7_node1 ~]# journalctl -F PRIORITY
      2
      3
      4
      7
      5
      6
    

    -u sshd(指定查看服务)

      [root@rhel7_node1 ~]# journalctl -u sshd
      -- Logs begin at Tue 2020-03-10 16:22:32 CST, end at Tue 2020-03-10 16:47:43 CST. --
      Mar 10 16:23:33 rhel7_node1.westos.com systemd[1]: Starting OpenSSH server daemon...
      Mar 10 16:23:35 rhel7_node1.westos.com sshd[7725]: Server listening on 0.0.0.0 port 22.
      Mar 10 16:23:35 rhel7_node1.westos.com sshd[7725]: Server listening on :: port 22.
      Mar 10 16:23:35 rhel7_node1.westos.com systemd[1]: Started OpenSSH server daemon.
    

    disk-usage(查看日志大小)

      [root@rhel7_node1 ~]# journalctl --disk-usage
      Archived and active journals take up 8.0M on disk.
    

    vacuum-size=1G(设定日志存放大小:临时)

      [root@rhel7_node1 ~]# journalctl --vacuum-size=1G
      Vacuuming done, freed 0B of archived journals on disk.
    

    vacuum-time=1W(设定日志最长存放时间:临时)

      [root@rhel7_node1 ~]# journalctl --vacuum-time=1W
      Failed to parse vacuum time: 1W
    

    -f(监控日志)
    注意:需要查看某个服务的日志的时候,先查找到这个服务的ID(systemctl -o verbose),然后显示日志的全部内容,查找这个服务的_SYSTEMD_UNIT,接下来输入以下命令:journalctl _PID=10924 _SYSTEMD_UNIT=sshd.service

      [root@rhel7_node1 ~]# journalctl _PID=19655 _SYSTEMD_UNIT=sshd.service 
      -- Logs begin at Tue 2020-03-10 16:22:32 CST, end at Tue 2020-03-10 17:01:01 CST. --
      Mar 10 16:59:50 rhel7_node1.westos.com sshd[19655]: Server listening on 0.0.0.0 port 22.
      Mar 10 16:59:50 rhel7_node1.westos.com sshd[19655]: Server listening on :: port 22.
    

实验步骤(用journald服务永久存放日志)

系统中默认日志在:/run/log/journal中
默认方式在系统重启后日志会被清理要永久保持日志请完成一下操作:

mkdir   /var/log/journal
chgrp   systemd-journal    /var/log/journal
chmod 2775     /var/log/journal
systemctl   restart  systemd-journald.service

当服务器重启,日志存放路径会被制定到:/var/log/journal

测试:
1.在操作以上步骤之前查看日志
2.重启系统
3.再次查看日志
4.可以看出之前的日志并没有被保存,只能看到重启后的新日志
5.完成上述操作后再次重启系统可以看到日志是被保存下来的

rsyslog

  • 服务名称:rsyslog.service
  • 日志存放:
    /var/log/messages #系统服务日志,常规信息,服务报错
    /var/log/secure #系统认证信息日志
    /var/log/maillog #系统邮件日志信息
    /var/log/cron #系统定时任务信息
    /var/log/boot.log #系统启动日志信息
  • 配置文件:/etc/rsyslog.conf

实验步骤(自定义日志采集路径)

1.修改配置文件
vim /etc/rsyslog.conf

日志类型.日志级别 日志存放路径

*.*                                                     /var/log/westos     #把系统中所有级别的日志存放到westos中


*.*;authpriv.none     							/var/log/westos      #把系统中所有级别的日志存放到westos中,但是除了authpriv不存放到westos中

日志类型:
auth #用户认证
authpriv #服务认证
cron #时间任务
kern #内核类型
mail #邮件
news #系统更新信息
user #用户

日志级别:
debug
info
notice
waring
err
crit
alert
emerg
none

2.执行和测试

实验步骤(如何更改日志采集格式)

1.定义日志采集格式

在配置文件中:
30 $template WESTOS, "%FROMHOST-IP% %timegenerated% %FROMHOST-IP% %syslogtag% %msg%\n"  #定义一个名为WESTOS的格式
				#WESTOS:   格式名称
				#%FORMHOST-IP%:日志来源主机IP
				#%timegenerated%: 日志生成时间
				#%syslogtag%: 日志生成服务
				#%msg%: 日志内容
				#\n: 换行

2.设定日志采集格式应用
通配符.通配符authpriv.none /var/log/ westos;WESTOS

54 *.info;mail.none;authpriv.none;cron.none                /var/log/messages;WESTOS   #将此格式应用到某个记录日志的文件

3.清空原来的日志文件

[root@rhel7_node1 log]# > /var/log/messages 

4.重启日志服务

[root@rhel7_node1 log]# systemctl restart rsyslog

5.查看新的日志格式

[root@rhel7_node1 log]# cat /var/log/messages 
127.0.0.1 Mar 10 19:45:03 systemd: Stopping System Logging Service...
127.0.0.1 Mar 10 19:45:03 rsyslogd:  [origin software="rsyslogd" swVersion="8.24.0-34.el7" x-pid="23061" x-info="http://www.rsyslog.com"] exiting on signal 15.
127.0.0.1 Mar 10 19:45:03 systemd: Stopped System Logging Service.
127.0.0.1 Mar 10 19:45:03 systemd: Starting System Logging Service...
127.0.0.1 Mar 10 19:45:03 rsyslogd:  [origin software="rsyslogd" swVersion="8.24.0-34.el7" x-pid="23120" x-info="http://www.rsyslog.com"] start
127.0.0.1 Mar 10 19:45:03 systemd: Started System Logging Service.

实验步骤(日志的远程同步)

在企业中,为了方便管理,我们需要集中查看日志,而不是一台一台的去查看。所以需要将所有人的日志存放到一台主机。

实验环境:
rhel7_node1:172.25.254.10 #存放日志作为日志接收端,所有人日志都存放到此台主机
rhel8_node1:172.25.254.11 #发送日志到rhel7_node主机中

1.在rhel7中设定接收所有人日志
先关闭火墙

systemctl stop firewalld 

选择udp协议是因为此实验及其操作环境均在内网,且对效率要求较高

vim /etc/rsyslog.conf
15 $ModLoad imudp  ##打开日志接受插件
16 ¥UDPServerRun  514 ##指定插件使用接口

重启服务:

root@rhel7_node1 ~]# systemctl restart rsyslog

查询端口:

root@rhel7_node1 ~]# netstat -antlupe | grep rsyslog
udp  0  0 0.0.0.0:514  0.0.0.0:*    0   67600   11115/rsyslogd
udp  0  0 :::514  :::*    0   67601   11115/rsyslogd
此时企业7的udp接受端口已经打开

2.rhel8中设定发送日志到rhel7中
设置配置文件:

vim /etc/rsyslog.conf
*.*    @172.25.254.10
										#  @  表示使用udp传输日志
										#  @@  表示使用tcp传输日志
										#  @172.25.254.11  把本机日志用udp方式发送到172.25.254.11主机

重启服务:

systemctl restart rsyslog	

测试:

在rhel7和8中

"> /var/log/messages"   #清空日志文件以便观察

在rhel8中

logger westos test message  #制造日志文件

在rhel7中可以看到8中生成的日志

[root@rhel7_node1 log]# cat /var/log/messages
Mar 10 20:41:48 rhel8_node1 root[6352]: westos haha

timedatectl

查看当前系统时间:
[root@rhel7_node1 log]# timedatectl
Local time: Wed 2020-03-11 00:39:37 CST
Universal time: Tue 2020-03-10 16:39:37 UTC #格林威治时间,伦敦时间
RTC time: Tue 2020-03-10 16:39:38 #硬件时间
Time zone: Asia/Shanghai (CST, +0800)
NTP enabled: no
NTP synchronized: no
RTC in local TZ: no
DST active: n/a
设定系统时间

timedatectl set-time "2020-02-13 10:41:55"   

显示系统的所有时区

timedatectl list-timezones  

设定系统时区

timedatectl set-timezone "Asis/Shanghai" 

设定系统时间计算方法

timedatectl set-local-rtc 0|1  #0表示使用utc时间计算方法(即硬件时间+时区)
#1表示使用local时间计算方式

时间同步服务

产生原因:需要更改大量主机系统时间时,不能一台一台改
服务名称: chronyd.service
配置文件: /etc/chrony.conf

实验步骤(同步两个主机的时间)

要求:在rhel7作为时间源
rhel8同步rhel7时间
1.在rhel7中

编辑配置文件:
vim /etc/chrony.conf
26 allow 192.168.0.0/24   #允许172.25.254.0.网段主机同步时间
29 local stratum 10    #开启时间同步服务功能并设定级别为10
重启时间同步服务并关闭火墙:
systemctl restart chronyd.service
systemctl stop firewalld

在rhel8中

vim /etc/chrony.conf
3 pool  172.25.254.11 iburst

重启服务

systemctl restart chronyd

查看:
在rhel8中查看时间:

[root@rhel8_node1 ~]# date
2020年 03月 10日 星期二 17:00:50 CST

使用chronyc命令查看时间效果:

[root@rhel8_node1 ~]# chronyc sources -v
210 Number of sources = 1

  .-- Source mode  '^' = server, '=' = peer, '#' = local clock.
 / .- Source state '*' = current synced, '+' = combined , '-' = not combined,
| /   '?' = unreachable, 'x' = time may be in error, '~' = time too variable.
||                                                 .- xxxx [ yyyy ] +/- zzzz
||      Reachability register (octal) -.           |  xxxx = adjusted offset,
||      Log2(Polling interval) --.      |          |  yyyy = measured offset,
||                                \     |          |  zzzz = estimated error.
||                                 |    |           \
MS Name/IP address         Stratum Poll Reach LastRx Last sample               
===============================================================================
^* 192.168.0.10                  3   6   177    56   -862us[ -565us] +/-   23ms    #证明已经从企业7上同步过来了时间

后记

journald响应比较快,处理灵活
rsyslog比较精准
本节实验比较多!

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章