Write shell script execution to Rsyslog Log Server
In the O & M work, it is inevitable to write some scripts for scheduled tasks (cron) to regularly execute some daily work to automate the O & M work. For example, backing up data in my daily work is an important task. You need to regularly back up the data to the backup server and some other backup media. Most of the backup work is written to the script, and the/etc/crontab is edited to regularly complete the backup task. This saves a lot of daily work pressure.
To monitor the execution of the script, you can append the execution to the specified local log file when writing the script, and regularly view these files to ensure that the task is correctly executed.
However, as the number of servers to be backed up increases, it becomes a burden to view logs. Generally, You need to log on to each server one by one to check the log files.
To solve the above problem, I plan to write all the backup logs to a backup server, so that the logs can be centrally collected, managed, and viewed. I set up a log server a few days ago to collect the log information of each server (see setting up an Rsyslog log server in CentOS 6.7 ).
The log server has been set up and the problem has also occurred. How can I write the execution in the script to the log server? In fact, because the log server has been set up, you only need to write the information to the local log system, then the local rsyslog service will write the log information to the remote log server database at the same time.
To write custom information to the local log system, use the: logger command.
Logger is a shell command interface that allows you to use the Syslog System Log Module and write a line of information directly to the system log file from the command line.
Example:
logger -it error -p local5.info "hello world"
-I. Process IDs are recorded on each line.
-T add an error label to each line in the log
-P specifies the custom log device and log level. For more information, see the appendix.
The text in the double quotation marks ("") in the last parameter is the custom log information. Command Execution result (echo $?) in the script ?) Determine if and use logger to output different log information. For example:
umount -l /mntif [ `echo $?` -eq 0 ]; then logger -p local3.info "Umount /dev/sdd1 is OK!"else logger -p local3.err "Umount /dev/sdd1 is Failure!"fi
The above script is used to unmount the USB flash drive or move the hard drive. According to echo $? The returned value of the log. The user-defined log information at the info or err level is written to the system log. With Rsyslog server, you can centrally collect and manage shell script execution.
Appendix: Log Type and Log Level
Log Type
Log Type |
Description |
Auth |
Logs generated by pam |
Authpriv |
Authentication Information for logon information such as ssh and ftp |
Cron |
Time task Problems |
Kern |
Kernel |
Lpr |
Print |
Mail |
Email |
Mark (syslog) |
Rsyslog service internal information, time ID |
News |
Newsgroup |
User |
Information generated by user programs |
Uucp |
Unix to unix copy: communication between unix hosts |
Local1 ~ 7 |
Custom Log Device |
Log Level
From top to bottom, the level is low to high, and the record information is getting less and less detailed. You can refer to the Manual: man 3 syslog
Level |
Description |
Level value |
Debug |
If there is mode information, the maximum number of logs is displayed. |
7 |
Info |
General information logs, the most common |
|
Notice |
Information of the most important common condition |
|
Warning |
Warning Level |
4 |
Err |
Error level to prevent a function or module from working properly |
|
Crit |
Severe information that prevents the entire system or the entire software from working properly |
|
Alert |
Information to be modified immediately |
|
Emerg |
Critical information such as kernel crash |
|
None |
Nothing is recorded |
-------------------------------------- Split line --------------------------------------
Configure the rsyslog client on CentOS to remotely record logs.
Deploy a log server using Rsyslog + LogAnalyzer + MySQL in CentOS 6.3
Log servers using rsyslog mysql and logAnalyzer
Rsyslog configuration and usage tutorial
RHEL5.4 deployment of central log server rsyslog + loganalyzer
-------------------------------------- Split line --------------------------------------
Rsyslog details: click here
Rsyslog: click here
This article permanently updates the link address: