码迷,mamicode.com
首页 > 其他好文 > 详细

RSYSLOG

时间:2014-11-27 06:58:04      阅读:265      评论:0      收藏:0      [点我收藏+]

标签:rsyslog   linux   

RSYSLOG is the rocket-fast system for log processing.这是官方的定义,详见http://www.rsyslog.com/

以下主要内容有:

一、查看rsyslog状态

二、解读rsyslog配置文件

三、全局指令中的配置日志的格式

四、3种记录日志方式(默认本地,远程,mysql库)是如何实现的


rsyslog 服务 

查看rsyslog状态

[root@www ~]# service rsyslog status

rsyslogd (pid  1079) is running…     -->默认是运行的

 

rsyslog的配置文件(/etc/rsyslog.conf)

vim /etc/rsyslog.conf

# rsyslog v5 configuration file             -->rsyslog版本
 
# For more
information see /usr/share/doc/rsyslog-*/rsyslog_conf.html
# If you experience
problems, see http://www.rsyslog.com/doc/troubleshoot.html
 
#### MODULES ####                                 加载的模块
 
$ModLoad imuxsock # provides support for local system logging (e.g.
via logger command)  
#支持logger命令的日志
$ModLoad imklog   # provides
kernel logging support (previously done by rklogd)       
#支持内核日志
#$ModLoad
immark  # provides --MARK-- message capability
$ModLoad ommysql                                        
#支持mysql的模块(默认是没有的,需要自己添加)
# Provides UDP syslog reception       允许514端口使用UDP协议远程接收日志信息,默认关闭
$ModLoad imudp
$UDPServerRun 514
 
# Provides TCP syslog reception      允许514端口使用TCP协议远程接收日志信息,默认关闭
#$ModLoad imtcp
#$InputTCPServerRun
514
 
 
#### GLOBAL DIRECTIVES ####   全局指令,定义日志格式默认模板
 
# Use default
timestamp format
$ActionFileDefaultTemplate
RSYSLOG_TraditionalFileFormat
 
# File syncing
capability is disabled by default. This feature is usually not required,
# not useful and an
extreme performance hit
#$ActionFileEnableSync
on
 
# Include all config
files in /etc/rsyslog.d/
$IncludeConfig
/etc/rsyslog.d/*.conf
 
 
#### 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
#记录所有日志类型的info级别以及大于info级别的信息到/var/log/messages,但是mail邮件信息,authpriv验证方面的信息和cron时间任务相关的信息除外
 
*.info                                                 :ommysql:192.168.13.130,Syslog,rsysloguser,rsyslog
#记录所有信息到那个地址的mysql数据库中(mysql库名:Syslog,用户:rsysloguser,密码:rsyslog)(默认没有需要自己添加)
# The authpriv file
has restricted access.
authpriv.*                                             
/var/log/secure
#authpriv验证相关的所有信息存放在/var/log/secure
 
# Log all the mail
messages in one place.
mail.*                                                 
-/var/log/maillog
#邮件的所有信息存放在/var/log/maillog;这里有一个-符号, 表示是使用异步的方式记录, 因为日志一般会比较大
 
# Log cron stuff
cron.*                                                 
/var/log/cron   
#计划任务有关的信息存放在/var/log/cron
 
# Everybody gets
emergency messages
*.emerg                                                
*   
#记录所有的大于等于emerg级别信息, 以wall方式发送给每个登录到系统的人
 
# Save news errors
of level crit and higher in a special file.
uucp,news.crit                                         
/var/log/spooler    
#记录uucp,news.crit等存放在/var/log/spooler
 
#
Save boot messages also to boot.log    
local7.*                                               
/var/log/boot.log 
#启动相关信息
 
 
# ### begin forwarding rule ###                                转发规则
# The statement
between the begin ... end define a SINGLE forwarding
# rule. They belong
together, do NOT split them. If you create multiple
# forwarding rules,
duplicate the whole block!
# Remote Logging (we
use TCP for reliable delivery)
#
# An on-disk queue
is created for this action. If the remote host is
# down, messages are
spooled to disk and sent when it is up again.
#$WorkDirectory
/var/lib/rsyslog # where to place spool files
#$ActionQueueFileName
fwdRule1 # unique name prefix for spool files
#$ActionQueueMaxDiskSpace
1g   # 1gb space limit (use as much as
possible)
#$ActionQueueSaveOnShutdown
on # save messages to disk on shutdown
#$ActionQueueType
LinkedList   # run asynchronously
#$ActionResumeRetryCount
-1    # infinite retries if host is down
# remote host is:
name/ip:port, e.g. 192.168.0.1:514, port optional
#*.*
@@remote-host:514
# ### end of the
forwarding rule ###

 

全局指令中的配置日志的格式

日志设备(类型).(连接符号)日志级别  日志处理方式(action) 

日志设备(可以理解为日志类型): 

        auth        –pam产生的日志 

        authpriv    –ssh,ftp等登录信息的验证信息 

        cron        –时间任务相关 

        kern        –内核 

        lpr         –打印 

        mail        –邮件 

        mark(syslog)–rsyslog服务内部的信息,时间标识 

        news        –新闻组 

        user        –用户程序产生的相关信息 

        uucp        –unix to unix copy, unix主机之间相关的通讯 

        local 1~7   –自定义的日志设备 

日志级别: 

        debug       –有调式信息的,日志信息最多 

        info        –一般信息的日志,最常用 

        notice      –最具有重要性的普通条件的信息 

        warning     –警告级别 

        err         –错误级别,阻止某个功能或者模块不能正常工作的信息 

        crit        –严重级别,阻止整个系统或者整个软件不能正常工作的信息 

        alert       –需要立刻修改的信息 

        emerg       –内核崩溃等严重信息 

        none        –什么都不记录 

        从上到下,级别从低到高,记录的信息越来越少 

        详细的可以查看手册: man 3 syslog 

连接符号 

                .xxx: 表示大于等于xxx级别的信息 

                .=xxx:表示等于xxx级别的信息 

                .!xxx:表示在xxx之外的等级的信息 

Actions 

                1. 记录到普通文件或设备文件:: 

                *.*     /var/log/file.log   # 绝对路径 

                *.*     /dev/pts/0 

                测试: logger -p info ‘hello‘                            #logger 命令用于产生日志 

                2. 转发到远程:: 

                *.* @192.168.0.1            # 使用UDP协议转发到192.168.0.1514(默认)端口 

                *.* @@192.168.0.1:10514     # 使用TCP协议转发到192.168.0.110514(默认)端口 

                3. 发送给用户(需要在线才能收到):: 

                *.*   root 

                *.*   root,user1,user2     # 使用,号分隔多个用户 

                *.*   *     # *号表示所有在线用户 

                4. 忽略,丢弃:: 

                local3.*   ~    # 忽略所有local3类型的所有级别的日志 

                5. 执行脚本:: 

                local3.*    ^/tmp/a.sh      # ^号后跟可执行脚本或程序的绝对路径 

 

如何查看rsyslog保存在本地的日志信息

tail /var/log/messages

测试:

产生日志信息:logger -p info ‘hello‘

查看:tail -1 /var/log/messages

[root@www ~]# tail -1 /var/log/messages

Nov 27 01:53:53 www root: hello

 

如何把日志记录到日志服务器

假设把A服务器上的日志信息保存到B服务器

1、A服务器需要修改配置文件:/etc/rsysylog.conf

在全局指令里添加:

*.*                                                @B服务器IP地址

重启服务器:service rsyslog restart

2、B服务器需要修改配置文件:

开启远程接收日志信息模块和端口

$ModLoad imudp

$UDPServerRun 514

重启服务器:service rsyslog restart

 

验证:

在A服务器记录一条日志信息,logger -p info “hello”

到B服务器看是否有这条日志信息:tail  /var/log/messages

 

如何把日志记录到mysql数据库

1、首先需要安装mysql

yum -y install mysql

2、安装rsyslog-mysql

yum -y install rsyslog-mysql

3、创建rsyslog数据库的脚本

cd /usr/share/doc/rsyslog-mysql-5.8.10

这里有这么个createDB.sql,通过他可以在mysql数据库中创建一个rsyslog的数据库

mysql < createDB.sql

Mysql

use Syslog      -->进入Syslog

SHOW tabels; -->查看表名

mysql>
SHOW tables;
+------------------------+
|
Tables_in_Syslog       |
+------------------------+
|
SystemEvents           |
|
SystemEventsProperties |
+------------------------+
2
rows in set (0.01 sec)

它有2个表,日志存放在第一张表里即SystemEvents,第二张表未知 - -

4、在mysql中,为rsyslog添加个用户,并授权

grant all on Syslog.* to ‘rsysloguser‘@‘127.0.0.1‘ identified by "password"

grant all on Syslog.* to ‘rsysloguser‘@‘localhost‘ identified by "password"

5、编辑rsyslog配置文件/etc/rsyslog.conf

添加模块

$ModLoad ommysql

添加全局指令

*.info                                                                           :ommysql:127.0.0.1,Syslog,rsysloguser,password

 

     6、验证:logger -p info “hello”

mysqlSyslog库中找到SystemEvents表,里面记录了日志信息

mysql                                      -->登入mysql

use Syslog                              -->进入Syslog

select * from SystemEvents\G;      -->查看SystemEvents表信息

 

   

      ID: 864
        CustomerID: NULL
        ReceivedAt: 2014-11-27 02:29:42
DeviceReportedTime:
2014-11-27 02:29:42
          Facility: 1
          Priority: 6
          FromHost: www
           Message:  hello                        
-->这就是刚才logger命令产生的日志
        NTSeverity: NULL
        Importance: NULL
       EventSource: NULL
         EventUser: NULL
     EventCategory: NULL
           EventID: NULL
   EventBinaryData: NULL
      MaxAvailable: NULL
         CurrUsage: NULL
          MinUsage: NULL
          MaxUsage: NULL
        InfoUnitID: 1
         SysLogTag: root:
      EventLogType: NULL
   GenericFileName: NULL
          SystemID: NULL


参考资料:

http://blog.csdn.net/hxh129/article/details/8089474


RSYSLOG

标签:rsyslog   linux   

原文地址:http://aflylinux.blog.51cto.com/8056774/1582960

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!