Centos 7.x rsyslog服务器端与客户端配置进行日志转发与接收

注意1:不管服务端还是客户端,首先确保已经装了rsyslog服务并且守护进程开启了。

systemctl status rsyslog.service    #检查进程是否开启
#如果没有装该服务,装一下
yum install rsyslog

注意:2:不管是服务端还是客户端,在编辑完rsyslog.conf后一定要重启rsyslog服务,才能生效。

服务端

进入root用户,编辑/etc/rsyslog.conf文件如下。
(1)主要是开启udp和tcp接收(可同时开启)
$ModLoad imudp
$UDPServerRun 514

$ModLoad imtcp
$InputTCPServerRun 514

(2)定义模板
$template Remote,"/data/log/waf_log/%$YEAR%-%$MONTH%-%$DAY%-%fromhost-ip%.log" # 设置远程日志存放路径和文件格式
:fromhost-ip, !isequal, “127.0.0.1” ?Remote # 如果是本机日志则不记录

Eg.

# rsyslog configuration file

# For more information see /usr/share/doc/rsyslog-*/rsyslog_conf.html
# If you experience problems, see http://www.rsyslog.com/doc/troubleshoot.html

#### MODULES ####

# 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
#$ModLoad imklog # reads kernel messages (the same are read from journald)
#$ModLoad immark  # provides --MARK-- message capability

# Provides UDP syslog reception
$ModLoad imudp
$UDPServerRun 514

# Provides TCP syslog reception
$ModLoad imtcp
$InputTCPServerRun 514


#### GLOBAL DIRECTIVES ####

# Where to place auxiliary files
$WorkDirectory /var/lib/rsyslog

# Use default timestamp format
$ActionFileDefaultTemplate RSYSLOG_TraditionalFileFormat

$template Remote,"/data/log/waf_log/%$YEAR%-%$MONTH%-%$DAY%-%fromhost-ip%.log" # 设置远程日志存放路径和文件格式
:fromhost-ip, !isequal, "127.0.0.1" ?Remote  # 如果是本机日志则不记录

# 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

# Turn off message reception via local log socket;
# local messages are retrieved through imjournal now.
$OmitLocalLogging on

# File to store the position in the journal
$IMJournalStateFile imjournal.state


#### 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                /data/log/messages

# The authpriv file has restricted access.  将authpirv设备的任何级别的信息记录到/data/log/secure中
authpriv.*                                              /data/log/secure
# Log all the mail messages in one place. 将mail设备中的任何级别信息记录到/data/log/mailog文件中
mail.*                                                  /data/log/mail
# # Log cron stuff 将cron设备的任何级别的信息记录到/var/log/cron文件中
cron.*                                                  /data/log/cron
# Everybody gets emergency messages 将任何设备的emerg级别或者更高的消息发送给所有正在系统上用户
*.emerg                                                 :omusrmsg:*
# Save news errors of level crit and higher in a special file. 将uucp和news设备的crint级别或者更高级别消息记录到/var/log/spooler文件中
uucp,news.crit                                          /data/log/spooler
# Save boot messages also to boot.log. 将和本地系统启动相关的信息记录到/var/log/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.
#$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 ###

(3)重启守护进程
配置完成后,重启rsyslog服务,可以用如下命令查看514端口是否成功监听

netstat -tnulp | grep rsyslog

注意:服务端要关闭防火墙(更好的方法是定义防火墙进出策略),如果关闭了防火墙还发现接收不到日志,需要查看是不是开启了SELINUX,如果是的话,关掉.

vim /etc/selinux/config
#将selinux关掉,如果不想重启系统,使用命令setenforce 0(可能不是永久生效) 
SELINUX=disabled
客户端

同样编辑/etc/rsyslog.conf,在末尾添加如下,即转发所有info级别以上的消息到rsyslog的服务端,@表示以udp协议,@@表示以tcp协议。(以下配置只用来测试,具体的客户端期望转发的日志形式需要根据自己需求配置)

*.info @@服务端ip

利用looger命令发送一条邮件日志测试一下,如果成功的话,在服务端配置的日志接收目录下会产生相应的日志文件

logger -p mail.info "test mail log from syslogClient."
  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值