rsyslog传输type

本文涉及的产品
日志服务 SLS,月写入数据量 50GB 1个月
简介: Rsyslog远程传输的几种方式 基本介绍 Rsyslog是一个syslogd的多线程增强版,rsyslog vs. syslog-ng 链接是rsyslog官方和syslog特性和性能上的一些对比,目前大部分Linux发行版本默认也是使用rsyslog记录日志。

Rsyslog远程传输的几种方式

基本介绍

Rsyslog是一个syslogd的多线程增强版,rsyslog vs. syslog-ng 链接是rsyslog官方和syslog特性和性能上的一些对比,目前大部分Linux发行版本默认也是使用rsyslog记录日志。这里介绍rsyslog远程传输的几种方式,对远程日志传输可以有一个了解。

rsyslog提供三个远程日志传输方式:

  • UDP: 数据包传输可信度不高
  • TCP: 数据包传输可信度比较高
  • RELP: 数据包传输可信度最高,避免数据丢失,比较新的协议,目前应用较少

以下为man手册对RELP协议的一个介绍:

RELP can be used instead of UDP or plain TCP syslog to provide reliable delivery of syslog messages. Please note that plain TCP syslog does NOT provide truly reliable delivery, with it messages may be lost when there is a connection problem or the server shuts down. RELP prevents message loss in hose cases.

关于RELP的更进一步了解可以参考 Using TLS with RELP RELP Input Module RELP Output Module (omrelp)

相关配置

To forward messages to another host via UDP, prepend the hostname with the at sign (“@”). To forward it via plain tcp, prepend two at signs (“@@”). To forward via RELP, prepend the string “:omrelp:” in front of the hostname.

UDP传输

Server端配置
/etc/rsyslog.conf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
# Provides UDP syslog reception
$ModLoad imudp
$UDPServerRun 514
$AllowedSender UDP, 192.168.80.0/24
# This one is the template to generate the log filename dynamically, depending on the client's IP address. 
# 根据客户端的IP单独存放主机日志在不同目录,syslog需要手动创建             
$template Remote,"/var/log/syslog/%fromhost-ip%/%fromhost-ip%_%$YEAR%-%$MONTH%-%$DAY%.log"
# Log all messages to the dynamically formed file.
:fromhost-ip, !isequal, "127.0.0.1" ?Remote
# 排除本地主机IP日志记录,只记录远程主机日志
# 注意此规则需要在其它规则之前,否则配置没有意义,远程主机的日志也会记录到Server的日志文件中
& ~ # 忽略之前所有的日志,远程主机日志记录完之后不再继续往下记录

或者把以上配置单独存放在/etc/rsyslog.d/中的xxx.conf配置文件中,尽量避免修改主配置文件,当然如果要独立文件主配置文件中必须含有以下配置

1
2
3
# grep 'rsyslog.d' /etc/rsyslog.conf 
# Include all config files in /etc/rsyslog.d/
$IncludeConfig /etc/rsyslog.d/*.conf
Client端配置
/etc/rsyslog.conf
1
*.*                     @192.168.80.130

以上配置完成之后/etc/init.d/rsyslog restart

TCP传输

TCP配置和UDP类似,如下

Server端配置
/etc/rsyslog.conf
1
2
3
4
5
6
7
8
9
10
11
# Provides TCP syslog reception
$ModLoad imtcp
$InputTCPServerRun 514
$AllowedSender TCP, 192.168.80.0/24
# This one is the template to generate the log filename dynamically, depending on the client's IP address.          
$template Remote,"/var/log/syslog/%fromhost-ip%/%fromhost-ip%_%$YEAR%-%$MONTH%-%$DAY%.log"
# Log all messages to the dynamically formed file.
:fromhost-ip, !isequal, "127.0.0.1" ?Remote
& ~
Client端配置
/etc/rsyslog.conf
1
*.*                     @@192.168.80.130

客户端和服务端重启相关服务即可

关于TCP和UDP的传输方式,rsyslog官方推荐使用TCP传输方式

In general, we suggest to use TCP syslog. It is way more reliable than UDP syslog and still pretty fast. The main reason is, that UDP might suffer of message loss. This happens when the syslog server must receive large bursts of messages. If the system buffer for UDP is full, all other messages will be dropped. With TCP, this will not happen. But sometimes it might be good to have a UDP server configured as well. That is, because some devices (like routers) are not able to send TCP syslog by design. In that case, you would need both syslog server types to have everything covered. If you need both syslog server types configured, please make sure they run on proper ports. By default UDP syslog is received on port 514. TCP syslog needs a different port because often the RPC service is using this port as well.

RELP传输

RELP需要安装rsyslog-relp相应模块

1
# yum install rsyslog-relp -y
Server端配置
/etc/rsyslog.conf
1
2
3
4
5
6
7
8
$ModLoad imrelp # 加载相应模块
$InputRELPServerRun 20514 # 监听端口
# This one is the template to generate the log filename dynamically, depending on the client's IP address.          
$template Remote,"/var/log/syslog/%fromhost-ip%/%fromhost-ip%_%$YEAR%-%$MONTH%-%$DAY%.log"
# Log all messages to the dynamically formed file.
:fromhost-ip, !isequal, "127.0.0.1" ?Remote
Client端配置
/etc/rsyslog.conf
1
2
3
4
5
$ActionQueueType LinkedList     # use asynchronous processing
$ActionQueueFileName srvrfwd    # set file name, also enables disk mode
$ActionResumeRetryCount -1      # infinite retries on insert failure
$ActionQueueSaveOnShutdown on   # save in-memory data if rsyslog shuts down
*.* :omrelp:192.168.80.130:20514

客户端和服务端重启相关服务即可

参考和拓展资料

–EOF–

相关实践学习
通过日志服务实现云资源OSS的安全审计
本实验介绍如何通过日志服务实现云资源OSS的安全审计。
目录
相关文章
|
11月前
|
安全 Linux 网络安全
centos7中firewall防火墙的常用命令总结
以上命令集覆盖了 `firewalld`的基本操作,是维护CentOS 7系统安全不可或缺的工具。对于更高级的配置需求或遇到特定问题
241 3
|
12月前
|
数据库 索引
联合索引和单独列有什么区别
【10月更文挑战第15天】联合索引和单独列有什么区别
585 2
|
索引 Python
|
机器学习/深度学习 自然语言处理 搜索推荐
【Python机器学习专栏】迁移学习在机器学习中的应用
【4月更文挑战第30天】迁移学习是利用已有知识解决新问题的机器学习方法,尤其在数据稀缺或资源有限时展现优势。本文介绍了迁移学习的基本概念,包括源域和目标域,并探讨了其在图像识别、自然语言处理和推荐系统的应用。在Python中,可使用Keras或TensorFlow实现迁移学习,如示例所示,通过预训练的VGG16模型进行图像识别。迁移学习提高了学习效率和性能,随着技术发展,其应用前景广阔。
234 0
|
JSON 前端开发 JavaScript
关于做好React项目,你需要知道的几点最佳实践
关于做好React项目,你需要知道的几点最佳实践
303 0
|
人工智能 大数据 云计算
Hi,液冷 | 928,锁定「磐久液冷线上小课堂」第三期
当液冷遇上算力,奏响智算低碳旋“绿”
Hi,液冷 | 928,锁定「磐久液冷线上小课堂」第三期
|
XML SQL Java
【SpringBoot + Mybatis系列】Mapper接口与Sql绑定几种姿势
通常我们在使用Mybatis进行开发时,会选择xml文件来写对应的sql,然后将Mapper接口与sql的xml文件建立绑定关系,然后在项目中调用mapper接口就可以执行对应的sql 那么如何将Mapper接口与sql进行绑定呢?本文将介绍四种常见的姿势
1554 0
【SpringBoot + Mybatis系列】Mapper接口与Sql绑定几种姿势