Photo by Huawei / info.support.huawei.com

华为AirEngine AP | 流日志输出至Rsyslog日志服务器配置实践

实践 | Practices 2025年2月6日

1.First | 序

华为AirEngine系列AP支持配置用户访问网络的流日志输出到日志服务器

网络流日志包括以下信息

  • 用户 MAC 地址
  • 网络数据流的 五元组 信息
    • 源IP
    • 目的IP
    • 源端口号
    • 目的端口号
    • 协议号

此功能开启的前提是启用AP的日志备份和日志抑制功能

所以与此同时输出日志还包括如下内容

  • 用户执行的所有操作
  • 系统运行时的相关信息

1.1.Preconditions | 前提条件

  • 华为 AirEngine 产品
    • AirEngine 5700&6700&8700&9700D 系列
      • AirEngine 6776-56TP
      • AirEngine 5760-10
      • AirEngine 5760-51
      • AirEngine 5760-22W
      • ......
    • AirEngine 9700 系列

示例基于V200R023C00SPC100 版固件

  • Rsyslog Server
    • 安装
    • 保证其正常运行
    • 了解/etc/rsyslog.d 下的 *.frule 文件作用

并不局限于部署设备

1.2.OS&SW Version | 系统&软件 版本

可能不完全列出此篇 OS&SW Version

1.2.1.AirEngine

Huawei Versatile Routing Platform Software
VRP (R) software, Version 5.170 (AirEngine 5700 V200R023C00SPC100)
Copyright (C) 2011-2023 HUAWEI TECH CO., LTD

1.2.2.Rsyslog

2.Setting | 配置

2.1.Setting AirEngine | 配置 AirEngine

首先需要开启AirEngine的两个功能

  • 日志备份
  • 网络流日志输出
  1. 开启日志备份

    Web 形式

    服务器地址根据自身情况填写 后方端口号则默认为 514

  2. 启用网络的日志输出

    Web管理下:此选项在无线网络配置中根据 SSID 列表选择性开启

    1. 依次点击>配置>无线网络配置>导航至SSID列表

    2. 单击需要启用网络的日志输出 的 SSID

    3. 开启用户流日志

    至此 AP 日志导出配置完毕 下一步配置 Rsyslog 接收 AirEngine 导出的日志

2.2.Setting Rsyslog | 配置 Rsyslog

配置 Rsyslog 需要实现以下几个功能

  1. 正确接收 AirEngine 导出日志
  2. 归纳整理导出日志而不是全部塞在 /var/log/messages

2.2.1.接收 AirEngine导出日志

Rsyslog 对于接收或转发远端服务日志有专属的配置文件(/etc/rsyslog.d/remote.conf)

所以我们直接编辑此配置文件而无需修改 /etc/rsyslog.conf

  1. 查看 /etc/rsyslog.d/remote.conf

未经修改过的 remote.conf 如下所示


##
## === When you're using remote logging, enable on-disk queues ===
## === in rsyslog.d/remote.conf.                               ===
##
## Note, that when the MYSQL, PGSQL, GSSAPI, GnuTLS or SNMP modules
## (provided in separate rsyslog-module-* packages) are enabled, the
## configuration can't be used on a system with /usr on a remote
## filesystem, except on newer systems where initrd mounts /usr.
## [The modules are linked against libraries installed bellow of
##  /usr thus also installed in /usr/lib*/rsyslog because of this.]
##

# ######### Enable On-Disk queues for remote logging ##########
#
# 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.
#
#$WorkDirectory /var/spool/rsyslog # where to place spool files
#$ActionQueueFileName uniqName # 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

# ######### Sending Messages to Remote Hosts ##########

# Remote Logging using TCP for reliable delivery
# remote host is: name/ip:port, e.g. 192.168.0.1:514, port optional
#*.* @@remote-host

# Remote Logging using UDP
# remote host is: name/ip:port, e.g. 192.168.0.1:514, port optional
#*.* @remote-host

# ######### Receiving Messages from Remote Hosts ##########
# TCP Syslog Server:
# provides TCP syslog reception and GSS-API (if compiled to support it)
# see https://www.rsyslog.com/receiving-messages-from-a-remote-system
# module(load="imtcp")
# input(type="imtcp" port="514" Address="10.10.0.1")
# alternative syntax
#$ModLoad imtcp.so         # load module
#$Address 10.10.0.1        # force to listen on this IP only
#$Port <port>              # Starts a TCP server on selected port
# Legacy configuration parameters that should not be used when crafting new configuration files.
##$UDPServerAddress 10.10.0.1 # force to listen on this IP only
#$InputTCPServerRun <port> # Starts a TCP server on selected port

# UDP Syslog Server:
# module(load="imudp")
# input(type="imudp" port="514" Address="10.10.0.1")
# alternative syntax
#$ModLoad imudp.so         # provides UDP syslog reception
#$Adress 10.10.0.1         # force to listen on this IP only
#$Port 514                 # start a UDP syslog server at standard port 514
# Legacy configuration parameters that should not be used when crafting new configuration files.
#$UDPServerAddress 10.10.0.1 # force to listen on this IP only
#$UDPServerRun 514         # start a UDP syslog server at standard port 514

########### Encrypting Syslog Traffic with TLS ##########
# -- TLS Syslog Server:
## make gtls driver the default
#$DefaultNetstreamDriver gtls
#
## certificate files
#$DefaultNetstreamDriverCAFile /etc/rsyslog.d/ca.pem
#$DefaultNetstreamDriverCertFile /etc/rsyslog.d/server_cert.pem
#$DefaultNetstreamDriverKeyFile /etc/rsyslog.d/server_key.pem
#
#$ModLoad imtcp # load TCP listener
#
#$InputTCPServerStreamDriverMode 1 # run driver in TLS-only mode
#$InputTCPServerStreamDriverAuthMode anon # client is NOT authenticated
#$InputTCPServerRun 10514 # start up listener at port 10514
#
# -- TLS Syslog Client:
## certificate files - just CA for a client
#$DefaultNetstreamDriverCAFile /etc/rsyslog.d/ca.pem
#
## set up the action
#$DefaultNetstreamDriver gtls # use gtls netstream driver
#$ActionSendStreamDriverMode 1 # require TLS for the connection
#$ActionSendStreamDriverAuthMode anon # server is NOT authenticated
#*.* @@(o)server.example.net:10514 # send (all) messages

2.编辑 remote.conf

仔细看的话会发现官方已经写的非常详细

只需比葫芦画瓢写入以下内容即可


## 启用TCP

module(load="imtcp")
input(type="imtcp" port="514" Address="10.7.7.7")


## 启用UDP

module(load="imudp")
input(type="imudp" port="514" Address="10.7.7.7")

3.重启 rsyslog.service

sc-restart rsyslog.service

4.验证

查看 /var/log/messages 文件已可以正常接收 AirEngine 导出的日志

2.2.2.整理日志

默认情况下接收 AirEngine的日志会全部塞在/var/log/messages下

非常不雅观并且难以管理

所以我们还需配置将接收 AirEngine的日志转发至指定文件

Rsyslog 配置文件夹 /etc/rsyslog.d 下的 *.frule 文件负责此事

👩‍🏫
详细了解Rsyslog规则集?
23.2. Rsyslog 的基本配置 | Red Hat Product Documentation
23.2. Rsyslog 的基本配置 | Red Hat Documentation

1.新建 AirEngine.frule 日志过滤规则

vi /etc/rsyslog.d/AirEngine.frule

2.编辑 AirEngine.frule

if      ($programname == 'AirEngine_00') or \
        ($msg contains 'AirEngine_00') \
then {
        -/var/log/AirEngine/AirEngine_00.log
        stop
}

这里的 AirEngine_00 为可选变量(即导入日志的程序名称)

AirEngine导出日志时默认把设备名称填充到程序名称
即 AirEngine_00 为 AirEngine 的设备名(根据自身情况填写)

3.解析

上述 AirEngine.frule 都做了什么?
/var/log/AirEngine/AirEngine_00.log 文件必须已存在

  1. 过滤程序名为 AirEngine_00 或消息中带有 AirEngine_00 日志文件
  2. 将其重定向至 /var/log/AirEngine/AirEngine_00.log 文件
  3. stop 字段终止日志被进一步处理

2.3.Verify | 验证

经过以上配置现已实现如下功能

  • AirEngine 导出日志
  • Rsyslog 接收日志并将其存入指定文件

此节 加以验证

  1. 查看用户执行的所有操作

    2025-02-06T20:22:48.441812+08:00 Feb 06 2025 20:13:40.340.1+08:00 AirEngine_00 %%01HTTP/6/WEBSERVER_OPERATION(l)[2239]:Operated on the web service.(User=xzllll, IP=10.7.7.211, CodeId=159, Description=HOUP_CHECK_UPDATE_INFO, Results=success)
    

    解析如下 (即xzllll用户打开了Web管理控制台)

    • User=xzllll
    • IP=10.7.7.211
    • Operated on the web service
  2. 网络数据流的五元组信息

    2025-02-06T20:14:22.174675+08:00 Feb 06 2025 20:05:14.70.2+08:00 AirEngine_00 %%01WSRV/5/STA_EVENT_FLOW(l)[2112]:WLAN/6/USER_FLOW_LOG/A new flow was generated on the device. (StaMac=[**:**:**:89:46:2a], SrcIp=[10.7.7.212], DstIp=[**.**.**.71], SrcPort=[45268], DstPort=[80], Protocol=[TCP], ApMac=[**:**:**:**:**:60])#015
    

    解析如下

    • 2025-02-06T20:14:22.174675+08:00 Feb 06 2025 20:05:14.70.2+08:00
    • AirEngine_00
    • %%01WSRV/5/STA_EVENT_FLOW(l)[2112]:WLAN/6/USER_FLOW_LOG/A
    • StaMac=[::**:89:46:2a]
    • SrcIp=[10.7.7.212]
    • DstIp=[..**.71]
    • SrcPort=[45268]
    • DstPort=[80]
    • Protocol=[TCP]
    • ApMac=[::::**:60]

3.Ex | 拓展

以上配置现已实现如下功能

  • AirEngine 导出日志
  • Rsyslog 接收日志并将其存入指定文件

但似乎对于 {Rsyslog 接收日志并将其存入指定文件} 这一步还有可优化的空间

那就是日志分割

大可以根据日期归纳日志而非全部保存至 /var/log/AirEngine/AirEngine_00.log

3.1.Cron

老土法屡试不爽

  1. 编辑 crontab

    crontab -e
    
  2. 写入定时规则

    59 23 * * * cd /var/log/AirEngine/ && cat /var/log/AirEngine/AirEngine_00.log > $(date +\%Y-\%m-\%d).log && echo "" > /var/log/AirEngine/AirEngine_00.log
    

    每天的23:59分执行上述命令行并以日期形式命名存放在/var/log/AirEngine/下

    完事后清空/var/log/AirEngine/AirEngine_00.log

3.2.logrotate

自动化嘎嘎滴

  1. 新建并编辑 /etc/logrotate.d/airengine 文件

    
    vi /etc/logrotate.d/airengine
    
    
  2. 写入如下规则

    /var/log/AirEngine/AirEngine_00.log {
    daily
    missingok
    rotate 7
    compress
    delaycompress
    notifempty
    create 0644 root root
    dateext
    postrotate
    endscript
    }
    

    详析如下

    • daily:表示每天轮换一次日志。
    • rotate 7:表示保留最近 7 个轮换的日志文件。
    • compress:表示旧的日志文件会被压缩。
    • delaycompress:推迟压缩直到下次轮换。
    • notifempty:如果日志为空则不进行轮换。
    • create 0640 root root:为新创建的日志文件指定权限和所有者。
    • dateext:使用日期作为备份文件的后缀(例如:AirEngine_00.log-2025-02-06)
  3. 手动执行测试(可选):

    如果你想测试配置是否有效,可以使用以下命令手动运行 logrotate:

    sudo logrotate -f /etc/logrotate.d/airengine
    

Thanks & Copy


标签

xzllll

三分钟热度三分钟收获