New FAMILUG

The PyMiers

Tuesday 14 February 2017

[systemd] journalctl cho người mới tập chơi

Lên Ubuntu 16.04, hệ thống init Upstart đã được thay thế bằng hệ thống systemd, kèm theo nhiều thay đổi khác, trong đó đáng chú ý nhất là cách xem log trên server mãi mãi thay đổi từ đây.
Không còn syslog, rsyslog ... nữa mà tất cả log đổ vào một chương trình duy nhất, có tên journald

Tìm hiểu câu lệnh journalctl

sử dụng các câu lệnh tìm hiểu câu lệnh
$ for cmd in whatis whereis; do $cmd journalctl; done
journalctl (1)       - Query the systemd journal
journalctl: /bin/journalctl /usr/share/man/man1/journalctl.1.gz
$ dpkg -S $(which journalctl)
systemd: /bin/journalctl
Vậy journalctl được cung cấp bởi package ``systemd``, nó là một cộng cụ để query (truy vấn) "the systemd journal" (journald).

systemd journal

là một daemon , một bộ phận của systemd, là một system service thực hiện thu thập và lưu trữ dữ liệu logging. Nó nhận dữ liệu từ nhiều nguồn như kernel log message (kmsg), system log messages (syslog), stdout/stderr của các system service, audit record thông qua hệ thống audit.

Mặc định log sẽ được chứa trong /run/log/journal/, bởi dữ liệu trong /run sẽ bị mất sau khi reboot, log cũng sẽ bị mất theo (có thể config để thay đổi điều này).

Các câu lệnh journalctl thường dùng

In ra tất cả các log có thể truy cập:
journalctl
Có thể grep những log mong muốn bằng:
journalctl | grep SOMETHING
Theo dõi file log như lệnh tail -F :
journalctl -f # f là follow
Hiển thị 1000 dòng cuối, không cần dùng thêm lệnh "less":
journalctl -e # e là pager-end. Sử dụng pager (less) và cuộn xuống cuối (end).
Hiển thị log lỗi về 1 service với các thông tin trợ giúp mà systemd đã thu thập:
journalctl -x
Hiển thị log cho một unit cụ thể (service unit):
journalctl -u ssh
-- Logs begin at Tue 2017-02-14 11:57:44 UTC, end at Tue 2017-02-14 12:28:52 UTC. --
Feb 14 11:58:25 k8s-03 sshd[19950]: Connection closed by 123.59.134.76 port 53765 [preauth]
Feb 14 12:01:26 k8s-03 sshd[20015]: Bad protocol version identification 'GET / HTTP/1.1' from 143.255.180.139 port 39753
Hiển thị một lượng event nhất định (2 event):
journalctl -n SO_DONG # mặc định nếu không điền là 10 dòng
journalctl -n2
-- Logs begin at Tue 2017-02-14 11:57:44 UTC, end at Tue 2017-02-14 12:31:48 UTC. --
Feb 14 12:31:48 k8s-03 kubelet[16701]: E0214 12:31:48.263417   16790 kubelet_node_status.go:98] Unable to register node "k8s-03" with API server: Post https://loca...
Feb 14 12:31:48 k8s-03 kubelet[16701]: E0214 12:31:48.434034   16790 reflector.go:188] pkg/kubelet/config/apiserver.go:44: Failed to list *api.Pod: Get https://loc...
Hiển thị 10 log lỗi mới nhất:
 journalctl -xn
-- Logs begin at Tue 2017-02-14 11:57:44 UTC, end at Tue 2017-02-14 12:32:16 UTC. --
Feb 14 12:32:15 k8s-03 kubelet[16701]: E0214 12:32:15.129040   16790 reflector.go:188] pkg/kubelet/kubelet.go:386: Failed to list *api.Node: Get https://localhost:
Feb 14 12:32:15 k8s-03 kubelet[16701]: E0214 12:32:15.653546   16790 reflector.go:188] pkg/kubelet/kubelet.go:378: Failed to list *api.Service: Get https://localho
Feb 14 12:32:15 k8s-03 kubelet[16701]: E0214 12:32:15.817165   16790 reflector.go:188] pkg/kubelet/config/apiserver.go:44: Failed to list *api.Pod: Get https://loc
Feb 14 12:32:16 k8s-03 kubelet[16701]: E0214 12:32:16.131977   16790 reflector.go:188] pkg/kubelet/kubelet.go:386: Failed to list *api.Node: Get https://localhost:
Feb 14 12:32:16 k8s-03 kubelet[16701]: I0214 12:32:16.327079   16790 kubelet_node_status.go:204] Setting node annotation to enable volume controller attach/detach
Feb 14 12:32:16 k8s-03 kubelet[16701]: I0214 12:32:16.335960   16790 kubelet_node_status.go:358] Recording NodeHasSufficientDisk event message for node k8s-03

syslog đã là quá khứ trên các hệ thống Linux sử dụng systemd (Debian 8+, Ubuntu 16.04+, ArchLinux ...)  dù nhớ nhung hay tiếc nuối gì thì cũng phải đứng dậy mà troubleshoot tiếp 😎
Bài viết thực hiện trên:
# journalctl --version
systemd 229
+PAM +AUDIT +SELINUX +IMA +APPARMOR +SMACK +SYSVINIT +UTMP +LIBCRYPTSETUP +GCRYPT +GNUTLS +ACL +XZ -LZ4 +SECCOMP +BLKID +ELFUTILS +KMOD -IDN
# lsb_release -d
Description:    Ubuntu 16.04.1 LTS

Hết!
HVN at http://www.familug.org/ and http://pymi.vn

Tham khảo: man 1 journalctl  

No comments:

Post a Comment