侧边栏壁纸
博主头像
liveJQ博主等级

沒有乐趣,何来开始

  • 累计撰写 146 篇文章
  • 累计创建 60 个标签
  • 累计收到 2 条评论

MikroTik RouterOS 配置 Rsyslog 远程存储日志

liveJQ
2022-06-03 / 0 评论 / 1 点赞 / 1,182 阅读 / 1,770 字 / 正在检测是否收录...
广告 广告

我的环境

ROS 6.43.2、CentOS7、rsyslogd 8.24.0

前几天这台 ROS 突然无法连接上 L2TP,重启后才恢复正常(有点 DoS 的味道了🙂)。本想查看日志,谁知都是 memory 级别,啥也看不到了。

ROS 配置

在这边可以将日志配置为存储到远程的服务器上,这样就不用担心 ROS 空间不够或在内存上重启失效的问题了,下面通过终端命令进行演示。

首先需要配置远程服务器:

/system logging action set name="remote" target=remote remote=日志服务器的IP地址 remote-port=514 src-address=0.0.0.0 bsd-syslog=no syslog-time-format=bsd-syslog syslog-facility=daemon syslog-severity=auto

最后配置需要收集的日志类型(topics):

/system logging add topics=write action=remote

topics 有很多,常用的有 info、warning和error

Rsyslog 配置

安装

yum install -y rsyslog

查看版本

rsyslogd -v

这里我们用的的主要配置文件为/etc/rsyslog.conf,需要配置如下内容:

#### MODULES ####
$ModLoad imuxsock
$ModLoad imjournal

# Provides UDP syslog reception
$ModLoad imudp
$UDPServerRun 514

如果开启了防火墙,需要确保放行 UDP 端口 514

还需要新建一个配置文件/etc/rsyslog.d/ros-remote-logs.conf,作为特殊配置,写入如下内容:

# define template for remote loggin
# remote logs will be stored at /var/log/remotelogs directory
# each host will have specific directory based on the system %HOSTNAME%
# name of the log file is %PROGRAMNAME%.log such as sshd.log, su.log
# both %HOSTNAME% and %PROGRAMNAME% is the Rsyslog message properties
template (
    name="ros"
    type="string"
    string="/var/log/ros/%hostname%/%$YEAR%-%$MONTH%-%$DAY%/%syslogtag%/%programname%.log"
)

# gather all log messages from all facilities
# at all severity levels to the RemoteLogs template
*.* -?ros

# stop the process once the file is written
stop

配置写入完成后,还需要新建一个日志存放的目录/var/log/ros/。上面的配置模板相关的详细说明,我已经在文末给出了官方文档链接。主要的作用是根据配置参数匹配所有日志来源,不同类型的日志根据它们的来源主机IP地址、日志产生的时间来保存到适合的目录位置。

通过以下命令可以验证配置文件是否有效

[root@livejq ~]# rsyslogd -f /etc/rsyslog.d/ros-remote-logs.conf -N1
rsyslogd: version 8.24.0-57.el7_9.3, config validation run (level 1), master config /etc/rsyslog.d/ros-remote-logs.conf
rsyslogd: End of config validation run. Bye.

看到这样的输出则证明文件的配置没问题。最后重启服务即可生效:

systemctl restart rsyslog

相关资料

  1. Configure format conversion
  2. Templates (v8-stable)
  3. Property Replacer (v8-stable)
1

评论区