New School: find out the ins and outs of Linux log processing

Source: Internet
Author: User
Everyone who uses UNIXLINUX knows the usefulness of logs. do you know the ins and outs of LINUX log processing? We can see that the information logs of LINUX systems are based on

Every UNIX/LINUX user knows the usefulness of logs. do you know the ins and outs of LINUX log processing?

We can see that there are two ways to log information in LINUX:

(1) dmesg view ---- This command is common.

(2) files under/var/log/: Let's start from these two channels and step by step.

(1) first, let's look at what is hidden behind the common dmesg command !!

(1) Let's come to MAN first.

  1. ------------- Man dmesg --------------------------
  2. NAME
  3. Dmesg-print or control the kernel ring buffer
  4. SYNOPSIS
  5. Dmesg [-c] [-n level] [-s bufsize]
  6. DESCRIPTION
  7. Dmesg is used to examine or control the kernel ring buffer.
  8. The program helps users to print out their bootup mes-sages. Instead of copying the messages by hand, the user need only:
  9. Dmesg> boot. messages
  10. And mail the boot. messages file to whoever can debug their
  11. Problem.
  12. OPTIONS
  13. -C Clear the ring buffer contents after printing.
  14. -Sbufsize
  15. Use a buffer of size bufsize to query the kernel ring buffer. this is 16392 by default. (The default kernel syslog buffer size was 4096 at first, 8192 since 1.3.54, 16384 since 2.1.113 .) if you have set the kernel buffer to be larger than the default then this option can be used to view the entire buffer.
  16. -Nlevel
  17. Set the level at which logging of messages is done to the console. for example,-n 1 prevents all messages, CT panic messages, from appearing on the console. all levels of messages are still written to/proc/kmsg, so syslogd (8) can still be used to control exactly where kernel messages appear.
  18. When the-n option is used, dmesg will not print or clear the kernel ring buffer.
  19. When both options are used, only the last option on the command line will have an effect.

From the manual provided by LINUX, we can know that the most important information dmesg reads information from the ring buffer (ring buffer) of the kernel.

(2) What is ring buffer?

In LINUX, all system information (package kernel information) is transmitted to the ring buffer, and the information generated by the kernel is printed by printk. The information displayed when the system starts is printed to the screen by the function. The number <0> <2>... indicates the importance of a message. Higher than a certain priority level will be printed to the screen, otherwise it will only be stored in the system buffer (ring buffer ).

As for how dmesg reads data from the ring buffer, you can see the source code of dmesg. c. It is very short and easy to understand.

(2) you should understand how dmesg works. As for the files under/var/log/, you cannot get familiar with them any more!

(1) Why are there so many files in/var/log?

One sentence: the syslogd Daemon Records logs generated by different services to different files according to/etc/syslog. conf.

I will not elaborate on the/etc/syslog. conf here. there is a lot of information (check it ).

(2) now that you know,/var/log/... is generated by the syslogd daemon. then proceed to this line.

After the LINUX system is started, klogd and syslogd daemon are successively started by/etc/init. d/sysklogd.

Klogd calls the syslog () system or reads the proc file system to obtain the kernel printk () from the system buffer (ring buffer ()

The message is sent. syslogd reads the system kernel information through klogd. so far, I think everyone should feel a series of actions such as log generation and reading.

Summary:

(1) All system information is output to the ring buffer. the content displayed by dmesg is also read from the ring buffer.

(2) in LINUX,/etc/init. d/sysklogd will start two daemon processes: Klogd & Syslogd

(3) klogd is responsible for reading kernel information. There are two methods:

Syslog () system call (this function is fully used. let's take a look at it)

Directly read/proc/kmsg (here,/proc/kmsg is the place that specifically outputs kernel information)

(4) the output result of Klogd is sent to syslogd for processing, and syslogd is based on/etc/syslog. the conf configuration outputs log information to different files under/var/log.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.