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

一.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 stuff
cron.*                                                  /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
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值