Linux Log Analysis

Source: Internet
Author: User

One of the keys to successful management of any system is to know what is happening in the system. Exception logs are provided in Linux and the log details are configurable.
 
Linux logs are stored in plain text, so you can search and read them without special tools. You can also write scripts to scan these logs and automatically execute certain functions based on their content.
 
Linux logs are stored in the/var/log directory. There are several log files maintained by the system, but other services and programs may also put their logs here. Most logs can only be read by the root user. However, you only need to modify the file access permission to allow others to read the logs.
 
/Var/log/messages
 
Messages logs are core system log files. It contains boot messages when the system is started and other status messages when the system is running. Io errors, network errors, and other system errors are recorded in this file. Other information, such as switching a person's identity to root, is also listed here. If the service is running, such as a DHCP server, you can observe its activity in the messages file. In general,/var/log/messages is the file you need to view before troubleshooting.
 
/Var/log/xfree86.0.log
 
This log records the last execution result of xfree86 xwindows server. If you encounter a problem when starting to the graphic mode, you can find the cause of the failure from this file.
 
Other logs
 
The/var/log directory contains other log files based on your Linux release version and the services and applications you run. For example, there may be logs related to the email server, resource sharing, and automatic tasks.
 
Start the round robin after preparation. 
 
You will see that some files in the/var/log directory end with a number and these are round-robin archive files. Log files become very large and bulky. Linux provides a command to repeat these logs so that your current log information will not be drowned in the old irrelevant information. Logrotate usually runs automatically on a regular basis, but can also be run manually. After the execution, logrotate will get the log file of the current version, and then append a ". 1" at the end of the file name ". Other earlier rounds of documents are ". 2", ". 3", and so on. The larger the number after the file name, the older the log.
 
You can edit the/etc/logrotate. conf file to configure the automatic behavior of logrotate. Use man logrotate to learn all the details of logrotate.
 
Log tools 
 
Any text tool can be used to process log files. The following are some useful tools.
 
Dmesg
You can use the dmesg command to quickly view the boot log of the last system boot. Usually it has a lot of content, so you often want to transmit it to a reader through a pipeline.
 
Dmesg | more
 
The preceding command displays the boot information by page.
 
Tail
Sometimes, when some behavior occurs, you want to pay close attention to a log file. The tail command is designed to display the last few lines of a text file. When you use the-F switch to add new content to the log on the current day, tail will continue to display new output.
 
Tail-F/var/log/messages
 
The above command will display the last 10 lines of the/var/log/messages file, then continue to monitor that file and output new behaviors. To stop the tail-F command, run Ctrl + C to stop the process.
 
More
More works in the same way as the DOS version. You can point it to a file, or use it to output information in an MPS queue and view the information by page. For example, the xfree86 startup log file is displayed by page:
 
More/var/log/xfree86.0.log
 
Use "Q" or [CTRL]-C to stop viewing files.
 
Less
Less is another text reader, but it also allows you to scroll through and retrieve information in files.
 
Less/var/log/messages
 
The above command displays the content of the/var/log/messages file. Use "Q" to stop viewing files. Use "H" to get less help.
 
Logger
You may want to record your messages to log files. You can only append the log message to an appropriate text file, but you must apply the log information format. Similarly, if the log system is customized, you will have to modify your code. The llogger command allows you to send your own messages to the log tool. Use it in the script to provide messages about execution and errors.
 
Custom logs 
 
There are two services, or backend programs. In the control log, they are klogd and syslogd. Klogd only processes kernel messages. syslogd processes messages from other systems, such as applications. You can configure/ET/syslog. conf and/etc/sysconfig/syslog. The custom log is out of the scope of this article, but detailed information can be found in the reference list at the end of this article. You can also view the man page of/etc/syslogd. conf to learn more.
 
Essentially, each message generated by the software provides information to determine where the message comes from and what it is. The/etc/syslog. conf file allows you to specify how messages of that type are processed. You can dump it to a message file or a custom file. You can send it to a remote host to process it according to its own syslogd configuration. Remote logs are a very good security feature. By placing your logs in a remote system, you can prevent others from hiding their traces by changing the log file.
 
The following is an example of a custom log from the man/etc/syslog. conf page:
 
# Kernel messages are first, stored in Thekernel
# File, critical messages and higher ones alsogo
# To another host and to the console
#
Kern. */var/adm/kernel
Kern. crit @ Finlandia
Kern. crit/dev/console
Kern.info; Kern .! Err/var/adm/kernel-Info
 
The first rule directs all kernel messages to the/var/adm/kernel file.
 
The second statement directs all kernel messages with a priority of crit or higher to a remote host named Finlandia. This is useful because if the host crashes and disk errors cannot be restored, you cannot read the stored messages. If the messages also exist on the remote host, you can still find out the cause of the crash.
 
The third rule directs these messages to the actual console so that people working on this machine can see them.
 
The fourth line tells syslogd to save all kernel messages whose priority is info to the/var/adm/kernel-info file. All messages with priority of err and higher are not included.
 
This custom log capability provides great flexibility and controllability for the Linux environment.
 
Log configuration in Webmin
 
Webmin has a module used to operate log files.
 
 
 
Figure 1. Webmin system log view
 
All configuration log files are displayed. Click a log file to edit its configuration.
 
 
 
Figure 2. Webmin log editing screen
 
You can also click View to view the log file content.
 
View log files in the console
 
Because log files in Linux are recorded in plain text, special tools are not required to interpret them. Any text file reader can display Linux Log Files. Browsers, such as Mozilla, can display log files and provide the search function. Linux also has console tools to view text files. More, display the file in paging mode like the ms dos version. The less command will display the file in the read-only reader, which has two-way scrolling and searching functions. Enter less/var/log/messages in the command line to understand the command.
 
The Webmin module is associated with the/etc/syslog. conf file. Therefore, the modifications you make to one of them will affect the other.
 
Logs in your life
 
Log Files in Linux are critical for System Fault Diagnosis and Maintenance. Linux logs are recorded in text files, so no special tools are required to view these files. Text files are also easy to use to customize scripts and programs.
 
Logs are rotated to avoid being too large, and the current information can be separated from the old data. Log cycle is configurable.
 
Logs are highly configurable. For security and backup, logs can even be stored in a separate system. You can have your scripts and programs generate system log messages, which will be identified and processed by syslogd background processes.

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.