《初入linux》--第九部分-linux的日志管理

来源:互联网 发布:网络审计设备 编辑:程序博客网 时间:2024/05/22 05:12

一.linux日志服务

其配置文件在 /etc/rsyslog.conf

何为日志:顾名思义就是针对自己的工作,每天记录工作的内容、所花费的时间以及在工作过程中遇到的问题,解决问题的思路和方法,
                     linux系统中,日志是查看工作状态,防患差错于未然的重要服务。

1.日志文件

日志文件为分别有:
1)/var/log/messages          //记录系统服务日志
</pre><pre name="code" class="plain">Oct 18 07:07:34 localhost dbus[776]: [system] Activating service name='org.freedesktop.problems' (using servicehelper)Oct 18 07:07:34 localhost dbus-daemon: dbus[776]: [system] Activating service name='org.freedesktop.problems' (using servicehelper)Oct 18 07:07:34 localhost dbus[776]: [system] Successfully activated service 'org.freedesktop.problems'Oct 18 07:07:34 localhost dbus-daemon: dbus[776]: [system] Successfully activated service 'org.freedesktop.problems'Oct 18 07:10:01 localhost systemd: Started Session 9 of user root.Oct 18 07:10:01 localhost systemd: Starting Session 9 of user root.Oct 18 07:10:24 localhost dbus[776]: [system] Activating service name='org.freedesktop.problems' (using servicehelper)

2)/var/log/maillog                   //记录系统邮件日志

3)/var/log/cron                         //记录系统定时任务


4)/var/log/boot.log                   //记录启动信息


2.日志管理服务      rsyslog  


                rsyslog                                 负责采集日志和分类


                配置文件:vim /etc/rsyslog.conf

在配置文件的rules部分,我们可以对日志文件,采集格式等进行配置

#### RULES ##### Log all kernel messages to the console.# Logging much else clutters up the screen.#kern.*                                                 /dev/console# Log anything (except mail) of level info or higher.# Don't log private authentication messages!*.info;mail.none;authpriv.none;cron.none                /var/log/messages# The authpriv file has restricted access.authpriv.*                                              /var/log/secure# Log all the mail messages in one place.mail.*                                                  -/var/log/maillog# Log cron stuffcron.*                                                  /var/log/cron# Everybody gets emergency messages*.emerg                                                 :omusrmsg:*

3.日志采集格式设置方法

1)进入   /etc/rsyslog.conf

2)在配置文件的RULES部分上方添加自己需要的格式宏定义
如:$template WESTOS,"%timegenerated% %FROMHOST-IP% %syslogtag% %msg%\n"
        $ActionfileDefaultTemplate WESTOS                         //这表示设置一个名为WESTOS的采集格式
    

3)在需要用此格式采集的日志文件后加上   ;<WESTOS
$template WESTOS,"%timegenerated% %FROMHOST-IP% %syslogtag% %msg%\n"$ActionfileDefaultTemplate WESTOS#### RULES ##### Log all kernel messages to the console.# Logging much else clutters up the screen.#kern.*                                                 /dev/console# Log anything (except mail) of level info or higher.# Don't log private authentication messages!*.info;mail.none;authpriv.none;cron.none                /var/log/messages;<WESTOS# The authpriv file has restricted access.authpriv.*                                              /var/log/secure


4)测试修改是否成功

>/var/log/messages               //清空日志文件


logger test message             //发送测试信息



二.远程同步日志:

在实际工作环境下,当一台服务器宕机后,最快查出问题的方法,就是实时同步它的日志信息,在同步的服务器上查看日志。

设置方法:
注:在虚拟机的实验环境下必须关闭防火墙服务,否则无法进行数据同步:
关闭方法:systemctl stop firewalld      

1.配置日志发送方:

1)vim /etc/rsyslog.conf           

2)*.* @172.25.5.11                配置日志接受方ip,@表示用UDP协议发送

2.配置日志接受方:

1)vim /etc/rsyslog.conf

2)在配置文件的udp设置中添加:
       $ModLoad imudp

       $UDPServerRun 514

# Provides UDP syslog reception$ModLoad imudp              //使用udp协议接受$UDPServerRun 514           //开放udp端口514

设置完成后可以使用

>/var/log/messages               //发送,接受都要清空日志文件


logger test message             //发送测试信息

来测试是否成功

0 0