简介
rsyslog是Linux系统自带的高性能、高安全性的用来收集、处理日志的程序。rsyslog可以通过TCP/UDP协议转发或接收日志,其可以作为日志服务器接受日志,也可以用作客户端发送日志。
客户端配置
修改 /etc/rsyslog.conf 配置文件:
#################
#### MODULES ####
#################
module(load="imuxsock") # provides support for local system logging
$ModLoad imjournal # provides access to the systemd journal
:<<!
# 这里服务端需要开启端口坚挺,客户端不需要开启
# 启用udp模块,默认端口为514
module(load="imudp")
input(type="imudp" port="514")
# 启用tcp模块
module(load="imtcp")
input(type="imtcp" port="514")
!
# 将日志发送到指定的服务器,例如:
# user.info @100.100.100.100:514 代表将所有user类型的info级别的日志传输到指定服务器
# *.* 代表所有类型所有级别的日志, @代表使用udp传输,@@代表tcp传输
# facility.level @@server_ip:port
# provides kernel logging support and enable non-kernel klog messages
module(load="imklog" permitnonkernelfacility="on")
###########################
#### GLOBAL DIRECTIVES ####
###########################
#
# Use traditional timestamp format.
# To enable high precision timestamps, comment out the following line.
#
$ActionFileDefaultTemplate RSYSLOG_TraditionalFileFormat
# Filter duplicated messages
$RepeatedMsgReduction on
#
# Set the default permissions for all log files.
#
$FileOwner syslog
$FileGroup adm
$FileCreateMode 0640
$DirCreateMode 0755
$Umask 0022
$PrivDropToUser syslog
$PrivDropToGroup syslog
#
# Where to place spool and state files
#
$WorkDirectory /var/spool/rsyslog
#
# Include all config files in /etc/rsyslog.d/
#
$IncludeConfig /etc/rsyslog.d/*.conf
监听Apache日志发送到服务器
在/etc/rsyslog.d/ 目录下新建一个 apache_log.conf 配置文件:
# 加载imfile模块
$ModLoad imfile
$InputFilePollInterval 10
## Apache访问日志文件路径
$InputFileName /var/log/apache2/access.log
# tag 标签,可以作为过滤或分类用
$InputFileTag apache-access
# 状态文件,只需要指定文件名,程序会在工作目录$WorkDirectory下创建指定文件
$InputFileStateFile stat-apache-access
# 日志类型,user代表应用日志
$InputFileFacility user
# 日志级别
$InputFileSeverity info
# 写入状态文件时间间隔
$InputFilePersistStateInterval 25000
## 以上属性可等效写为:input(type="imfile" File="/path" Tag="tag1" StateFile="filename" Severity="info"
## Facility="user" PersistStateInterval="25000")
## Apache错误日志文件路径
$InputFileName /var/log/apache2/error.log
$InputFileTag apache-error
$InputFileStateFile stat-apache-error
$InputFileSeverity error
$InputFilePersistStateInterval 25000
## 日志格式模板:msg代表默认产生的消息
$template apache_temp,"%msg%\n"
## 按照tag分类指定rsyslog日志服务器地址,
if $syslogtag == 'apache-access' then @your_server:port;apache_temp
if $syslogtag == 'apache-access' then ~
if $syslogtag == 'apache-error' then @your_server:port;apache_temp
if $syslogtag == 'apache-error' then ~
服务器端配置
打开 /etc/rsyslog.conf 文件:
# The imjournal module bellow is now used as a message source instead of imuxsock.
$ModLoad imuxsock # provides support for local system logging (e.g. via logger command)
$ModLoad imjournal # provides access to the systemd journal
# 启用udp模块,默认端口为514
module(load="imudp")
input(type="imudp" port="514")
# 启用tcp模块
module(load="imtcp")
input(type="imtcp" port="514")
# 将收到的日志模板化,比如在前面添加ip进行区分
$template access_log, "%FROMHOST-IP% %msg%\n"
$template error_log, "%msg%\n"
$template eophp_log, "%msg%\n"
# 指定保存到服务器的路径
# access-log
$template access_log_path, "/data/apache/logs/%$NOW%/access-log/web1-access_log"
# error-log
$template error_log_path, "/data/apache/logs/%$NOW%/error-log/web1-error_log"
# eophp log
$template eophp_log_path, "/data/eophp_logs/%$NOW%/%FROMHOST-IP%.log"
# if 进行条件匹配比如多台客户端的ip在同一个网络,可以用startwith匹配ip的前缀
# $syslogtag 为日志的tag标签
if $fromhost-ip startswith '119.123.' and $syslogtag == 'apache-access' then ?access_log_path;access_log
& ~
# web1-error-log
if $fromhost-ip startswith '119.123.' and $syslogtag == 'apache-error' then ?error_log_path;error_log
& ~
# eophp
if $fromhost-ip startswith '119.123.' and $syslogtag == 'eplog' then ?eophp_log_path;eophp_log
&~
# omit...
修改完配置文件,重启rsyslog服务:
service rsyslog restart
日志服务器,客户端变搭建完毕了。
附录:
Facility简介:Facility是syslog的模块,通过facility概念来定义日志消息的来源,以方便对日志进行分类。Facility:有以下设备可选,如有某些需要使用可查看相关文档。
kern 内核消息
user 用户级消息
mail 邮件
daemon 系统服务
syslog 日志系统服务
security/authorization messages
line printer subsystem
network news subsystem
UUCP subsystem uucp系统消息
clock daemon
security/authorization messages
FTP daemon
NTP subsystem
log audit
log alert
clock daemon
local0 - local7
Severity:日志等级
0 emerg 系统已经不可用
1 alert 立即处理
2 Critical 严重错误
3 Error 错误
4 Warning 警告
5 Notice 较为重要
6 Informational 正常消息
7 Debug debug消息