赞
踩
过去只有 rsyslogd 的年代中,由于 rsyslogd 必须要开机完成并且执行了 rsyslogd 这个 daemon 之后,登录文件才会开始记录。所以,核心还得要自己产生一个 klogd 的服务, 才能将系统在开机过程、启动服务的过程中的信息记录下来,然后等 rsyslogd 启动后才传送给它来处理~
现在有了 systemd 之后,由于这玩意儿是核心唤醒的,然后又是第一支执行的软件,它可以主动调用 systemd-journald 来协助记载登录文件~ 因此在开机过程中的所有信息,包括启动服务与服务若启动失败的情况等等,都可以直接被记录到 systemd-journald 里头去!
不过 systemd-journald 由于是使用于内存的登录文件记录方式,因此重新开机过后,开机前的登录文件信息当然就不会被记载了。 为此,我们还是建议启动 rsyslogd 来协助分类记录!也就是说, systemd-journald 用来管理与查询这次开机后的登录信息,而 rsyslogd 可以用来记录以前及现在的所以数据到磁盘文件中,方便未来进行查询喔!
虽然 systemd-journald 所记录的数据其实是在内存中,但是系统还是利用文件的型态将它记录到 /run/log/ 下面! 不过我们从前面几章也知道, /run 在 CentOS 7 其实是内存内的数据,所以重新开机过后,这个 /run/log 下面的数据当然就被刷新,旧的当然就不再存在了!
systemd-journald 是一个收集并存储各类日志数据的系统服务。 它创建并维护一个带有索引的、结构化的日志数据库, 并可以收集来自各种不同渠道的日志:
1>通过 kmsg 收集内核日志
2>通过 libc 的 syslog(3) 接口收集系统日志
3>通过 本地日志接口 sd_journal_print(3) 收集结构化的系统日志
4>捕获服务单元的标准输出(STDOUT)与标准错误(STDERR)
5>通过内核审计子系统收集审计记录
systemd-journald.service 的数据要如何叫出来查阅呢?很简单!就通过 journalctl 即可。
journalctl 手册页 man 1 journalctl 中介绍了所有开关。
# journalctl -h journalctl [OPTIONS...] [MATCHES...] Query the journal. Flags: --system Show the system journal --user Show the user journal for the current user -M --machine=CONTAINER Operate on local container -S --since=DATE Show entries not older than the specified date -U --until=DATE Show entries not newer than the specified date -c --cursor=CURSOR Show entries starting at the specified cursor --after-cursor=CURSOR Show entries after the specified cursor --show-cursor Print the cursor after all the entries -b --boot[=ID] Show current boot or the specified boot --list-boots Show terse information about recorded boots -k --dmesg Show kernel message log from the current boot -u --unit=UNIT Show logs from the specified unit -t --identifier=STRING Show entries with the specified syslog identifier -p --priority=RANGE Show entries with the specified priority -e --pager-end Immediately jump to the end in the pager -f --follow Follow the journal -n --lines[=INTEGER] Number of journal entries to show --no-tail Show all lines, even in follow mode -r --reverse Show the newest entries first -o --output=STRING Change journal output mode (short, short-iso, short-precise, short-monotonic, verbose, export, json, json-pretty, json-sse, cat) --utc Express time in Coordinated Universal Time (UTC) -x --catalog Add message explanations where available --no-full Ellipsize fields -a --all Show all fields, including long and unprintable -q --quiet Do not show privilege warning --no-pager Do not pipe output into a pager -m --merge Show entries from all available journals -D --directory=PATH Show journal files from directory --file=PATH Show journal file --root=ROOT Operate on catalog files underneath the root ROOT --interval=TIME Time interval for changing the FSS sealing key --verify-key=KEY Specify FSS verification key --force Override of the FSS key pair with --setup-keys Commands: -h --help Show this help text --version Show package version -F --field=FIELD List all values that a specified field takes --new-id128 Generate a new 128-bit ID --disk-usage Show total disk usage of all journal files --vacuum-size=BYTES Reduce disk usage below specified size --vacuum-time=TIME Remove journal files older than specified date --flush Flush all journal data from /run into /var --header Show journal header information --list-catalog Show all message IDs in the catalog --dump-catalog Show entries in the message catalog --update-catalog Update the message catalog database --setup-keys Generate a new FSS key pair --verify Verify journal file consistency
# journalctl --disk-usage
Archived and active journals take up 4.0G on disk.
journalctl --vacuum-size=500M
rm -rf /run/log/journal/*
or
rm -rf /var/log/journal/*
systemctl restart systemd-journald.service
如果不结合任何开关调用 journalctl,它将显示日记的整个内容,最旧的项列在最前面。您可按特定的开关和字段过滤输出。
journalctl 可以根据特定的系统引导过滤讯息。要列出所有可用引导,请运行:
# journalctl --list-boots
-1 097ed2cd99124a2391d2cffab1b566f0 Mon 2014-05-26 08:36:56 EDT—Fri 2014-05-30 05:33:44 EDT
0 156019a44a774a0bb0148a92df4af81b Fri 2014-05-30 05:34:09 EDT—Fri 2014-05-30 06:15:01 EDT
第一列列出引导偏移:0 表示当前引导,-1 表示上一次引导,-2 表示再上一次引导,以此类推。第二列包含引导 ID,其后是特定引导的限制时间戳。
# journalctl -b
# journalctl -b -1
以按特定的字段过滤日记输出。要匹配的字段语法为 FIELD_NAME=MATCHED_VALUE,例如 _SYSTEMD_UNIT=httpd.service。您可以在单个查询中指定多个匹配条件,以进一步过滤输出讯息。有关默认字段的列表,请参见 man 7 systemd.journal-fields。
(1)显示特定进程 ID 生成的讯息:
journalctl _PID=1039
(2)显示属于特定用户 ID 的讯息:
journalctl _UID=1000
(3)显示来自内核环缓冲区的讯息(与 dmesg 的生成结果相同):
journalctl _TRANSPORT=kernel
(4)显示来自服务的标准输出或错误输出的讯息:
journalctl _TRANSPORT=stdout
(5)仅显示指定服务生成的讯息:
journalctl _SYSTEMD_UNIT=avahi-daemon.service
(6)指定了两个不同的字段,则仅显示同时与两个表达式匹配的项:
journalctl _SYSTEMD_UNIT=avahi-daemon.service _PID=1488
(7)如果两个匹配条件引用了相同的字段,则显示与两个表达式中任意一个匹配的所有项:
journalctl _SYSTEMD_UNIT=avahi-daemon.service _SYSTEMD_UNIT=dbus.service
可通过指定开始日期和/或结束日期来过滤 journalctl 的输出。日期规范应采用类似于“2014-06-30 9:17:16”的格式。也可以不采用数字表示,而是指定关键字“yesterday”、“today”或“tomorrow”。它们表示当日前一天、当日或者当日后一天的午夜。如果指定“now”,则表示当前时间。您还可以指定以 - 或 + 为前缀的相对时间,分别表示当前时间之前或之后的特定时间。
(1)仅显示出 2015/08/18 整天
[root@study ~]# journalctl --since "2015-08-18 00:00:00" --until "2015-08-19 00:00:00"
(2)仅今天的日志
[root@study ~]# journalctl --since today
(3)仅昨天的日志数据内容
[root@study ~]# journalctl --since yesterday --until today
默认情况下,日记在 /run/log/journal/ 中储存日志数据。由于 /run/ 目录具有易失本性,因此,在重启时会丢失日志数据。要永久保存日志数据,/var/log/journal/ 目录必须存在且具有正确的所有权和权限,这样systemd-journald 服务便可在其中储存其数据。
如果您执行以下操作,它将会切换到永久日志记录:
(1) 以 root 身份打开 /etc/systemd/journald.conf 进行编辑;
将包含 Storage= 的行取消注释,并将它更改为:
[...]
[Journal]
Storage=persistent
#Compress=yes
[...]
(2) 保存该文件,然后重启动 systemd-journald:systemctl restart systemd-journald
先处理所需要的目录与相关权限设置
[root@study ~]# mkdir /var/log/journal
[root@study ~]# chown root:systemd-journal /var/log/journal
[root@study ~]# chmod 2775 /var/log/journal
重新启动 systemd-journald 并且观察备份的日志数据!
[root@study ~]# systemctl restart systemd-journald.service
[root@study ~]# ll /var/log/journal/
drwxr-sr-x. 2 root systemd-journal 27 Aug 20 02:37 309eb890d09f440681f596543d95ec7a
基本上,系统由 systemd 所管理,那所有经由 systemd 启动的服务,如果再启动或结束的过程中发生一些问题或者是正常的讯息, 就会将该讯息由 systemd-journald.service 以二进制的方式记录下来,之后再将这个讯息发送给 rsyslog.service 作进一步的记载。
将介绍一个简单的示例,演示如何找出并修复在 apache2 启动期间 systemd 报告的错误。
# systemctl start apache2
Job for apache2.service failed. See 'systemctl status apache2' and 'journalctl -xn' for details.
# systemctl status apache2
apache2.service - The Apache Webserver
Loaded: loaded (/usr/lib/systemd/system/apache2.service; disabled)
Active: failed (Result: exit-code) since Tue 2014-06-03 11:08:13 CEST; 7min ago
Process: 11026 ExecStop=/usr/sbin/start_apache2 -D SYSTEMD -DFOREGROUND \
-k graceful-stop (code=exited, status=1/FAILURE)
# journalctl -o verbose _PID=11026
[...]
MESSAGE=AH00526: Syntax error on line 6 of /etc/apache2/default-server.conf:
[...]
MESSAGE=Invalid command 'DocumenttRoot', perhaps misspelled or defined by a module
[...]
# systemctl start apache2 && systemctl status apache2 apache2.service - The Apache Webserver Loaded: loaded (/usr/lib/systemd/system/apache2.service; disabled) Active: active (running) since Tue 2014-06-03 11:26:24 CEST; 4ms ago Process: 11026 ExecStop=/usr/sbin/start_apache2 -D SYSTEMD -DFOREGROUND -k graceful-stop (code=exited, status=1/FAILURE) Main PID: 11263 (httpd2-prefork) Status: "Processing requests..." CGroup: /system.slice/apache2.service ├─11263 /usr/sbin/httpd2-prefork -f /etc/apache2/httpd.conf -D [...] ├─11280 /usr/sbin/httpd2-prefork -f /etc/apache2/httpd.conf -D [...] ├─11281 /usr/sbin/httpd2-prefork -f /etc/apache2/httpd.conf -D [...] ├─11282 /usr/sbin/httpd2-prefork -f /etc/apache2/httpd.conf -D [...] ├─11283 /usr/sbin/httpd2-prefork -f /etc/apache2/httpd.conf -D [...] └─11285 /usr/sbin/httpd2-prefork -f /etc/apache2/httpd.conf -D [...]
通过修改 /etc/systemd/journald.conf 可调整 systemd-journald 服务的行为。有关完整的文件描述,请参见 man 5 journald.conf。请注意,要使更改生效,需要使用以下命令重启动日记:
systemctl restart systemd-journald
如果将日记日志数据保存到永久位置(参见第 15.1 节 “将日记设为永久”),这些数据最多会占用 /var/log/journal 所在文件系统空间的 10%。
例如,如果 /var/log/journal 位于一个 30 GB /var 的分区中,则日记最多会占用 3 GB 磁盘空间。要更改此限制,请更改(并取消注释)SystemMaxUse 选项:
SystemMaxUse=50M
您可以将日记转发到终端设备,以便在首选的终端屏幕(例如 /dev/tty12)上了解相关的系统讯息。将以下 journald 选项更改为:
ForwardToConsole=yes
TTYPath=/dev/tty12
Journald 与传统的 syslog 实施(例如 rsyslog)向后兼容。请确保满足以下条件:
1> 已安装 rsyslog。
# rpm -q rsyslog
2> 已启用 rsyslog 服务。
# systemctl is-enabled rsyslog
3> 已在 /etc/systemd/journald.conf 中启用转发到 syslog 设置。
ForwardToSyslog=yes
https://documentation.suse.com/zh-cn/sles/12-SP4/html/SLES-all/cha-journalctl.html#journalctl-persistent
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。