← 回到技术随笔
· 9574 字

H3C交换机Syslog日志服务器配置实例

沿用提供 NTP 服务的 Ubuntu Server 24.04 LTS,在同一台服务器上使用 rsyslog 接收 H3C 交换机日志,并完成来源限制、日志验证和文件轮转。

本文以 H3C S5570S 系列和 Comware V7 为主要示例,只适用于本人拥有或已经明确授权管理的交换机。文中的地址、端口和设备名称都是示例,使用时要换成现场实际规划。

一、现场场景

交换机本机只能保存有限数量的日志。设备重启、日志被覆盖或者交换机发生故障后,只看本机记录往往无法还原问题发生的过程。

我通常会把交换机日志发送到一台长期在线的内网服务器。中小企业没有必要单独准备两台服务器,本例沿用上一篇提供 NTP 服务的 Ubuntu Server 24.04 LTS,在同一台服务器上运行 Chrony 和 rsyslog,并把 HQ-Access-01 的日志单独保存到一个文件中。

本次使用下面的示例:

项目 示例值
交换机 HQ-Access-01
管理 VLAN 接口 Vlan-interface100
交换机 IPv4 192.168.100.2
交换机 IPv6 2001:db8:100::2
日志服务器系统 Ubuntu Server 24.04 LTS
内网运维服务器 IPv4 192.168.100.30
内网运维服务器 IPv6 2001:db8:100::30
时间同步程序 Chrony
日志接收程序 rsyslog
接收协议和端口 UDP 5514
日志文件 /var/log/h3c/HQ-Access-01.log

示例中的 IP 地址不能直接用于生产网络。这里使用非默认端口 5514,交换机和服务器两端必须保持一致。

二、配置目标

  1. 让 Ubuntu 服务器接收交换机发来的日志;
  2. 只允许示例交换机访问 UDP 5514
  3. 把交换机日志保存到单独的文件;
  4. 使用管理 VLAN 地址发送日志;
  5. 发送 informational 及更重要的日志,不发送 debugging 日志;
  6. 确认服务器能收到新产生的端口状态和登录日志。

三、配置前检查

先确认上一篇配置的时间同步仍然正常。交换机和日志服务器时间不一致,会给后续排障带来麻烦。

# 查看交换机当前时间和时区。
<HQ-Access-01> display clock

# 确认交换机已经与 NTP 时间源同步。
<HQ-Access-01> display ntp-service status

再检查交换机与日志服务器的 IPv4、IPv6 连通性:

# 测试日志服务器的 IPv4 地址。
<HQ-Access-01> ping 192.168.100.30

# 测试日志服务器的 IPv6 地址。
<HQ-Access-01> ping ipv6 2001:db8:100::30

# 确认管理 VLAN 接口处于 UP 状态,并且地址正确。
<HQ-Access-01> display interface Vlan-interface100

# 查看现有日志中心配置,避免覆盖正在使用的日志主机。
<HQ-Access-01> display info-center

如果已经配置其他日志主机,要先记录地址、端口、日志级别和用途。新增日志主机通常不会替换旧配置,但可能让同一份日志同时发送到多个平台。

四、配置 Ubuntu 日志服务器

1. 安装并准备 rsyslog

Ubuntu Server 通常已经安装 rsyslog,仍可以用下面的命令确认:

# 更新软件包索引并安装 rsyslog。
ubuntu@ops-server:~$ sudo apt update
ubuntu@ops-server:~$ sudo apt install -y rsyslog

# 设置 rsyslog 开机启动,并立即启动服务。
ubuntu@ops-server:~$ sudo systemctl enable --now rsyslog

# 创建保存 H3C 日志的目录,并允许 rsyslog 写入。
ubuntu@ops-server:~$ sudo install -o syslog -g adm -m 0750 -d /var/log/h3c

2. 配置 UDP 接收和日志文件

新建 /etc/rsyslog.d/30-h3c-switches.conf

# 打开 H3C 交换机日志接收配置。
ubuntu@ops-server:~$ sudo nano /etc/rsyslog.d/30-h3c-switches.conf

写入下面的内容:

# 加载 UDP 日志接收模块。
module(load="imudp")

# 把这个监听端口收到的日志单独写入 H3C 日志文件。
ruleset(name="h3c-switches") {
    action(type="omfile" file="/var/log/h3c/HQ-Access-01.log")
}

# 在 UDP 5514 端口接收日志,只接受示例交换机的 IPv4/IPv6 地址。
input(
    type="imudp"
    port="5514"
    allowedSender=["192.168.100.2/32", "[2001:db8:100::2]/128"]
    ruleset="h3c-switches"
)

如果现有 rsyslog 配置已经包含 module(load="imudp"),只保留一处模块加载命令,避免重复加载。allowedSender 只限制本例的 5514 监听端口,UFW 还会再限制一次。UDP Syslog 不会验证设备身份,来源地址也可能被伪造,因此只适合放在受控的管理网络内使用。

保存后先检查语法,再重启服务:

# 只检查配置语法,不重启服务。
ubuntu@ops-server:~$ sudo rsyslogd -N1

# 语法检查通过后再重启 rsyslog。
ubuntu@ops-server:~$ sudo systemctl restart rsyslog

# 确认服务正在运行。
ubuntu@ops-server:~$ sudo systemctl status rsyslog --no-pager

# 确认 UDP 5514 已经处于监听状态。
ubuntu@ops-server:~$ sudo ss -lunp | grep ':5514'

如果 rsyslogd -N1 报错,要先按提示检查文件行号,不能带着错误配置重启服务。

3. 配置 UFW

先查看 UFW 状态和 IPv6 开关:

# 查看现有防火墙规则。
ubuntu@ops-server:~$ sudo ufw status verbose

# 双栈环境应显示 IPV6=yes。
ubuntu@ops-server:~$ grep '^IPV6=' /etc/default/ufw

UFW 已启用时,只放行交换机管理地址:

# 只允许交换机 IPv4 地址向 UDP 5514 发送日志。
ubuntu@ops-server:~$ sudo ufw allow proto udp from 192.168.100.2 to any port 5514 comment 'H3C Syslog IPv4'

# 只允许交换机 IPv6 地址向 UDP 5514 发送日志。
ubuntu@ops-server:~$ sudo ufw allow proto udp from 2001:db8:100::2 to any port 5514 comment 'H3C Syslog IPv6'

# 按编号查看规则,确认没有向整个网段开放端口。
ubuntu@ops-server:~$ sudo ufw status numbered

如果 UFW 处于关闭状态,不要在远程 SSH 会话中直接执行 ufw enable。启用前要先放行实际使用的 SSH 管理端口,并核对服务器原有安全策略。

4. 设置日志文件轮转

日志文件长期不处理会持续增大。新建 /etc/logrotate.d/h3c-syslog

# 打开 H3C 日志轮转配置。
ubuntu@ops-server:~$ sudo nano /etc/logrotate.d/h3c-syslog

写入下面的内容:

/var/log/h3c/*.log {
    # 每天检查一次,最多保留 30 份旧日志。
    daily
    rotate 30

    # 压缩旧日志,第一份旧日志延迟到下次再压缩。
    compress
    delaycompress

    # 文件不存在或内容为空时不报错、不轮转。
    missingok
    notifempty

    # 复制后清空原文件,rsyslog 不需要重新打开文件。
    copytruncate
}

这里保留 30 份只是示例。正式环境要按公司的日志保存期限和磁盘容量调整。

# 只检查轮转配置,不真正改动日志文件。
ubuntu@ops-server:~$ sudo logrotate -d /etc/logrotate.d/h3c-syslog

五、Comware V7 配置实例

1. 设置日志级别和源接口

informational 会发送该级别以及更重要的日志,常见的端口 Up/Down 和登录记录可以包含在内,同时排除数量很大的 debugging 日志。

<HQ-Access-01> system-view

# 开启信息中心。该功能通常默认开启,明确配置便于后续核对。
[HQ-Access-01] info-center enable

# 发送 informational 及更重要的普通日志,不发送 debugging 日志。
[HQ-Access-01] info-center source default loghost level informational

# 使用管理 VLAN 接口的地址作为日志来源。
[HQ-Access-01] info-center loghost source Vlan-interface 100

# 日志时间使用 ISO 格式并带上时区,便于服务器和人工核对。
[HQ-Access-01] info-center timestamp loghost iso with-timezone

2. 配置日志服务器

同一台双栈服务器只选择一种地址发送即可。如果同时配置它的 IPv4 和 IPv6 地址,同一条日志可能在服务器上出现两次。本例使用 IPv4:

# 把日志发送到 192.168.100.30 的 UDP 5514,并使用 local5 标记来源。
[HQ-Access-01] info-center loghost 192.168.100.30 port 5514 facility local5
[HQ-Access-01] return

如果现场决定只使用 IPv6,应把上面的 IPv4 日志主机换成下面这条,而不是两条一起保留:

# IPv6 替代配置,不要与同一服务器的 IPv4 配置同时使用。
[HQ-Access-01] info-center loghost ipv6 2001:db8:100::30 port 5514 facility local5

六、Comware V5 配置

H3C S5500 Comware V5 Release 2208 也支持 IPv4、IPv6 日志主机和自定义端口,但日志输出规则的写法与 V7 不同。下面仍以 IPv4 为实际发送地址:

<HQ-Access-01> system-view

# 开启信息中心。
[HQ-Access-01] info-center enable

# 使用管理 VLAN 接口作为日志来源。
[HQ-Access-01] info-center loghost source Vlan-interface 100

# 使用 ISO 时间格式。
[HQ-Access-01] info-center timestamp loghost iso

# 配置 IPv4 日志服务器、UDP 5514 和 local5。
[HQ-Access-01] info-center loghost 192.168.100.30 port 5514 channel loghost facility local5

# 先关闭 loghost 通道原有的调试、日志和告警输出,避免混入不需要的信息。
[HQ-Access-01] info-center source default channel loghost debug state off log state off trap state off

# 再开启所有模块 informational 及更重要的普通日志。
[HQ-Access-01] info-center source default channel loghost log level informational state on
[HQ-Access-01] return

V5 改用 IPv6 时,日志主机命令写成:

# IPv6 替代配置,不要与同一服务器的 IPv4 配置同时使用。
[HQ-Access-01] info-center loghost ipv6 2001:db8:100::30 port 5514 channel loghost facility local5

V5 的 info-center timestamp loghost iso 不带 V7 示例中的 with-timezone 参数,因此更要先确认交换机和服务器的时区设置一致。

七、验证方法

1. 检查交换机配置

# 查看信息中心、日志主机、源接口和输出级别。
<HQ-Access-01> display info-center

# 查看交换机本机最近产生的日志,方便与服务器记录对照。
<HQ-Access-01> display logbuffer reverse

2. 产生一条真实日志

我一般从已经授权的运维电脑重新登录一次 SSH,再正常退出。这样可以产生登录相关日志,不需要故意关闭生产端口。

也可以等待正常的端口 Up/Down 事件。不要为了测试 Syslog 随意插拔上联线或关闭正在使用的接口。

3. 在服务器上查看结果

# 持续查看交换机日志文件,按 Ctrl+C 退出。
ubuntu@ops-server:~$ sudo tail -f /var/log/h3c/HQ-Access-01.log

# 如果文件还没有生成,先确认 rsyslog 服务和监听端口。
ubuntu@ops-server:~$ sudo systemctl status rsyslog --no-pager
ubuntu@ops-server:~$ sudo ss -lunp | grep ':5514'

# 查看最近的 rsyslog 服务日志,排查配置或写入错误。
ubuntu@ops-server:~$ sudo journalctl -u rsyslog -n 50 --no-pager

服务器收到新日志后,要核对设备名称、时间和事件内容。只有日志文件出现了刚刚产生的新记录,才能说明交换机发送、网络放行和服务器写入全部正常。

八、常见问题与处理

1. Ping 正常,但服务器没有日志

Ping 正常只说明基本网络可达。继续检查两端是否都使用 UDP 5514、UFW 是否放行、rsyslog 是否监听,以及中间设备是否拦截该端口。

2. 服务器监听正常,文件仍未生成

先执行 sudo rsyslogd -N1 检查语法,再查看 /var/log/h3c 的属主和权限。服务器还会拒绝不在 allowedSender 列表中的来源地址,因此也要核对交换机实际使用的源地址。

3. 同一条日志出现两次

检查 display info-center 中是否同时配置了同一台服务器的 IPv4 和 IPv6 地址。双地址并不等于主备,交换机会向两个日志主机都发送日志。

4. 日志时间不正确

先检查交换机 display clock 和 NTP 状态,再检查 Ubuntu 服务器的 timedatectl status。旧日志不会在时间同步后自动改正,应重点查看同步完成后新产生的日志。

5. 日志太多

先确认是否误发了 debugging 日志。本例使用 informational,如果现场只需要更重要的事件,可以根据公司要求提高日志级别,但要评估是否会漏掉登录或端口状态记录。

九、回退方法

1. H3C 交换机回退

下面只删除本文新增的 V7 IPv4 日志主机,并恢复日志主机输出规则和源接口的默认状态:

<HQ-Access-01> system-view

# 删除本文配置的 IPv4 日志主机。
[HQ-Access-01] undo info-center loghost 192.168.100.30

# 如果实际使用的是 IPv6,则删除这一条,不要重复执行 IPv4 删除命令。
[HQ-Access-01] undo info-center loghost ipv6 2001:db8:100::30

# 恢复默认日志主机输出规则、源接口和时间格式。
[HQ-Access-01] undo info-center source default loghost
[HQ-Access-01] undo info-center loghost source
[HQ-Access-01] undo info-center timestamp loghost
[HQ-Access-01] return

如果变更前已有同类配置,应恢复事先记录的日志级别、源接口和时间格式,不能直接恢复默认值。

2. Ubuntu 服务器回退

确认没有其他交换机使用这套接收配置后,再删除本文新增的文件和防火墙规则:

# 删除 H3C 日志接收和轮转配置。
ubuntu@ops-server:~$ sudo rm /etc/rsyslog.d/30-h3c-switches.conf
ubuntu@ops-server:~$ sudo rm /etc/logrotate.d/h3c-syslog

# 删除本文添加的 IPv4/IPv6 UFW 规则。
ubuntu@ops-server:~$ sudo ufw delete allow proto udp from 192.168.100.2 to any port 5514 comment 'H3C Syslog IPv4'
ubuntu@ops-server:~$ sudo ufw delete allow proto udp from 2001:db8:100::2 to any port 5514 comment 'H3C Syslog IPv6'

# 检查剩余配置并重启 rsyslog。
ubuntu@ops-server:~$ sudo rsyslogd -N1
ubuntu@ops-server:~$ sudo systemctl restart rsyslog

历史日志是否删除要按公司的保存要求决定。回退接收配置时不需要删除 /var/log/h3c/HQ-Access-01.log

十、小结

集中日志的关键不是只把 info-center loghost 命令敲进去,而是确认服务器确实收到了新日志。我会先把 Ubuntu 的 rsyslog、目录权限和 UFW 配好,再配置交换机的日志级别、源接口和服务器地址。

同一台双栈服务器选择 IPv4 或 IPv6 其中一种地址即可,两个地址同时配置会产生重复日志。最后通过一次正常的 SSH 登录或真实端口事件验证,并给日志文件设置轮转,才能让这套配置长期使用。

十一、完整配置汇总

下面汇总 Ubuntu Server 24.04 LTS 和 Comware V7 的 IPv4 发送方案。示例地址、端口、接口和日志保存期限都要按现场替换。

1. Ubuntu 日志服务器

/etc/rsyslog.d/30-h3c-switches.conf

# 加载 UDP 日志接收模块。
module(load="imudp")

# 把收到的日志写入单独文件。
ruleset(name="h3c-switches") {
    action(type="omfile" file="/var/log/h3c/HQ-Access-01.log")
}

# 监听 UDP 5514,只接受示例交换机的 IPv4/IPv6 地址。
input(
    type="imudp"
    port="5514"
    allowedSender=["192.168.100.2/32", "[2001:db8:100::2]/128"]
    ruleset="h3c-switches"
)

/etc/logrotate.d/h3c-syslog

/var/log/h3c/*.log {
    daily
    rotate 30
    compress
    delaycompress
    missingok
    notifempty
    copytruncate
}

服务器端执行:

# 安装并启动 rsyslog。
ubuntu@ops-server:~$ sudo apt update
ubuntu@ops-server:~$ sudo apt install -y rsyslog
ubuntu@ops-server:~$ sudo systemctl enable --now rsyslog

# 创建日志目录,然后编辑上面的两个配置文件。
ubuntu@ops-server:~$ sudo install -o syslog -g adm -m 0750 -d /var/log/h3c
ubuntu@ops-server:~$ sudo nano /etc/rsyslog.d/30-h3c-switches.conf
ubuntu@ops-server:~$ sudo nano /etc/logrotate.d/h3c-syslog

# 检查配置,通过后再重启服务。
ubuntu@ops-server:~$ sudo rsyslogd -N1
ubuntu@ops-server:~$ sudo systemctl restart rsyslog

# UFW 已启用时,只放行示例交换机。
ubuntu@ops-server:~$ sudo ufw status verbose
ubuntu@ops-server:~$ grep '^IPV6=' /etc/default/ufw
ubuntu@ops-server:~$ sudo ufw allow proto udp from 192.168.100.2 to any port 5514 comment 'H3C Syslog IPv4'
ubuntu@ops-server:~$ sudo ufw allow proto udp from 2001:db8:100::2 to any port 5514 comment 'H3C Syslog IPv6'

# 检查监听端口、日志轮转配置和新收到的日志。
ubuntu@ops-server:~$ sudo ss -lunp | grep ':5514'
ubuntu@ops-server:~$ sudo logrotate -d /etc/logrotate.d/h3c-syslog
ubuntu@ops-server:~$ sudo tail -f /var/log/h3c/HQ-Access-01.log

2. H3C 交换机

<HQ-Access-01> system-view

# 开启信息中心,发送 informational 及更重要的普通日志。
[HQ-Access-01] info-center enable
[HQ-Access-01] info-center source default loghost level informational

# 使用管理 VLAN 地址作为来源,并给日志增加带时区的 ISO 时间。
[HQ-Access-01] info-center loghost source Vlan-interface 100
[HQ-Access-01] info-center timestamp loghost iso with-timezone

# 使用 IPv4 向 Ubuntu 服务器的 UDP 5514 发送日志。
[HQ-Access-01] info-center loghost 192.168.100.30 port 5514 facility local5
[HQ-Access-01] return

# 查看配置和本机近期日志。
<HQ-Access-01> display info-center
<HQ-Access-01> display logbuffer reverse

# 服务器确认收到新日志后保存交换机配置。
<HQ-Access-01> save force

如果现场选择 IPv6,只把上面的 info-center loghost 命令替换为:

[HQ-Access-01] info-center loghost ipv6 2001:db8:100::30 port 5514 facility local5

参考资料