Từ Code đến System

Linux

Cách sử dụng dmesg

Tổng quan

Với các hệ điều hành Unix-like | Linux, khi máy tính đang khởi động, trong quá trình nhân hệ điều hành được nạp vào bộ nhớ, tiến trình phân tích chuẩn đoán sẽ được chạy trên tất cả thiết bị phần cứng hiện có trong máy tính (VD: RAM, CPU, Disk, PCI v..) và toàn bộ quá trình phân tích, chuẩn đoán sẽ được ghi nhật ký lại phục vụ cho hoạt động quản trị.

Sau khi máy tính khởi động xong, OS sẽ cung cấp công cụ dmesg cho phép người dùng hoặc người quản trị tra soát lại nhật ký khởi động. Trong thực tế, công cụ dmesg thường được sử dụng để xử lý, khắc phục sự cố khi máy chủ khởi động lỗi hoặc khởi động lại bất thường.

Tips 1. Kết hợp dmesg với tail, more, less, grep, head

Cú pháp

dmesg | tail
dmesg | more
dmesg | less
dmesg | grep '<KEY_WORD>'
dmesg | head

Hiển thị 10 dòng đầu

[root@web1 ~]# dmesg | head -n 10
[    0.000000] Initializing cgroup subsys cpuset
[    0.000000] Initializing cgroup subsys cpu
[    0.000000] Initializing cgroup subsys cpuacct
[    0.000000] Linux version 3.10.0-1127.8.2.el7.x86_64 ([email protected]) (gcc version 4.8.5 20150623 (Red Hat 4.8.5-39) (GCC) ) #1 SMP Tue May 12 16:57:42 UTC 2020
[    0.000000] Command line: BOOT_IMAGE=/vmlinuz-3.10.0-1127.8.2.el7.x86_64 root=/dev/mapper/VolGroup00-LogVol01 ro rd.lvm.lv=VolGroup00/LogVol01 rd.lvm.lv=VolGroup00/LogVol00 biosdevname=0 net.ifnames=0 rhgb quiet LANG=en_US.UTF-8
[    0.000000] e820: BIOS-provided physical RAM map:
[    0.000000] BIOS-e820: [mem 0x0000000000000000-0x000000000009fbff] usable
[    0.000000] BIOS-e820: [mem 0x000000000009fc00-0x000000000009ffff] reserved
[    0.000000] BIOS-e820: [mem 0x00000000000f0000-0x00000000000fffff] reserved
[    0.000000] BIOS-e820: [mem 0x0000000000100000-0x00000000bfff3fff] usable

Hiển thị 10 dòng cuối

[root@web1 ~]# dmesg | tail -n 10
[   45.705098] cni0: port 1(veth0f2e89c2) entered disabled state
[   45.705190] device veth0f2e89c2 entered promiscuous mode
[   45.705248] cni0: port 1(veth0f2e89c2) entered blocking state
[   45.705252] cni0: port 1(veth0f2e89c2) entered forwarding state
[   45.715457] cni0: port 2(vethd41a47f5) entered blocking state
[   45.715462] cni0: port 2(vethd41a47f5) entered disabled state
[   45.715532] device vethd41a47f5 entered promiscuous mode
[   45.715565] cni0: port 2(vethd41a47f5) entered blocking state
[   45.715568] cni0: port 2(vethd41a47f5) entered forwarding state
[16096.261378] hrtimer: interrupt took 1550652 ns

Tips 2. Hiển thị log dmesg với timestamp

Cú pháp

dmesg -T

Kết quả

[root@web1 ~]# dmesg -T | tail
[T6 Th10 30 14:50:42 2020] cni0: port 1(veth0f2e89c2) entered disabled state
[T6 Th10 30 14:50:42 2020] device veth0f2e89c2 entered promiscuous mode
[T6 Th10 30 14:50:42 2020] cni0: port 1(veth0f2e89c2) entered blocking state
[T6 Th10 30 14:50:42 2020] cni0: port 1(veth0f2e89c2) entered forwarding state
[T6 Th10 30 14:50:42 2020] cni0: port 2(vethd41a47f5) entered blocking state
[T6 Th10 30 14:50:42 2020] cni0: port 2(vethd41a47f5) entered disabled state
[T6 Th10 30 14:50:42 2020] device vethd41a47f5 entered promiscuous mode
[T6 Th10 30 14:50:42 2020] cni0: port 2(vethd41a47f5) entered blocking state
[T6 Th10 30 14:50:42 2020] cni0: port 2(vethd41a47f5) entered forwarding state
[T6 Th10 30 19:18:13 2020] hrtimer: interrupt took 1550652 ns

Tips 3. Hiện thị log dmesg có màu sắc

dmesg -L

Tips 4. Lọc log dmesg theo log level

Các option hỗ trợ phục vụ lọc log dmesg

  • emerg
  • alert
  • crit
  • err
  • warn
  • notice
  • info
  • debug

Hiển thị các log cảnh báo và log lỗi

dmesg --level=err,warn

Ví dụ

[root@web1 ~]# dmesg --level=err,warn | tail
[    2.168304] 	[00] ZERO 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
[    2.168305] 	[00] ZERO 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
[    2.168306] 	[00] ZERO 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
[    2.168307] 	[00] ZERO 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
[    2.168307] 	[00] ZERO 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
[    2.168308] 	[00] ZERO 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
[    2.168309] 	[00] ZERO 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
[    8.961563] TECH PREVIEW: Overlay filesystem may not be fully supported.
Please review provided documentation for limitations.
[16096.261378] hrtimer: interrupt took 1550652 ns

Tips 5. Lọc log dmesg với log level

Cú pháp

dmesg -x

Ví dụ

[root@web1 ~]# dmesg -x | tail
kern  :info  : [   45.705098] cni0: port 1(veth0f2e89c2) entered disabled state
kern  :info  : [   45.705190] device veth0f2e89c2 entered promiscuous mode
kern  :info  : [   45.705248] cni0: port 1(veth0f2e89c2) entered blocking state
kern  :info  : [   45.705252] cni0: port 1(veth0f2e89c2) entered forwarding state
kern  :info  : [   45.715457] cni0: port 2(vethd41a47f5) entered blocking state
kern  :info  : [   45.715462] cni0: port 2(vethd41a47f5) entered disabled state
kern  :info  : [   45.715532] device vethd41a47f5 entered promiscuous mode
kern  :info  : [   45.715565] cni0: port 2(vethd41a47f5) entered blocking state
kern  :info  : [   45.715568] cni0: port 2(vethd41a47f5) entered forwarding state
kern  :warn  : [16096.261378] hrtimer: interrupt took 1550652 ns

Tips 6. Kết hợp các tips trên để sử dụng dmesg hiểu quả nhất

Cú pháp

dmesg -T -L -x --level=err,warn

Kết quả

[root@web1 ~]# dmesg -T -L -x --level=err,warn | tail -n 20
kern  :warn  : [T6 Th10 30 14:49:57 2020] ACPI: All ACPI Tables successfully acquired
kern  :warn  : [T6 Th10 30 14:49:57 2020] ACPI: Enabled 16 GPEs in block 00 to 0F
kern  :warn  : [T6 Th10 30 14:49:57 2020] acpi PNP0A03:00: fail to add MMCONFIG information, can't access extended PCI configuration space under this bridge.
kern  :warn  : [T6 Th10 30 14:49:57 2020] ACPI: PCI Interrupt Link [LNKD] enabled at IRQ 11
kern  :warn  : [T6 Th10 30 14:49:58 2020] Dquot-cache hash table entries: 512 (order 0, 4096 bytes)
kern  :warn  : [T6 Th10 30 14:49:58 2020] ACPI: PCI Interrupt Link [LNKC] enabled at IRQ 10
kern  :warn  : [T6 Th10 30 14:49:58 2020] ACPI: PCI Interrupt Link [LNKA] enabled at IRQ 10
kern  :warn  : [T6 Th10 30 14:49:58 2020] ACPI: PCI Interrupt Link [LNKB] enabled at IRQ 11
kern  :warn  : [T6 Th10 30 14:49:59 2020] bochs-drm 0000:00:02.0: Virtual-1: EDID is invalid:
kern  :warn  : [T6 Th10 30 14:49:59 2020] 	[00] ZERO 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
kern  :warn  : [T6 Th10 30 14:49:59 2020] 	[00] ZERO 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
kern  :warn  : [T6 Th10 30 14:49:59 2020] 	[00] ZERO 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
kern  :warn  : [T6 Th10 30 14:49:59 2020] 	[00] ZERO 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
kern  :warn  : [T6 Th10 30 14:49:59 2020] 	[00] ZERO 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
kern  :warn  : [T6 Th10 30 14:49:59 2020] 	[00] ZERO 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
kern  :warn  : [T6 Th10 30 14:49:59 2020] 	[00] ZERO 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
kern  :warn  : [T6 Th10 30 14:49:59 2020] 	[00] ZERO 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
kern  :warn  : [T6 Th10 30 14:50:05 2020] TECH PREVIEW: Overlay filesystem may not be fully supported.
Please review provided documentation for limitations.
kern  :warn  : [T6 Th10 30 19:18:13 2020] hrtimer: interrupt took 1550652 ns

Tới đây đã kết thúc bài chia sẻ về cách sử dụng dmesg của mình. Hẹn gặp lại các bạn trong các bài viết tiếp theo.

Nguồn

Leave a Reply