type
status
date
slug
summary
tags
category
icon
password
需求背景
原先的日志存在问题:
- 固件侧日志在设备断电后会丢失
- 需要持久化固件侧日志以供分析
- 便于快速定位故障设备关键信息
通过接入日志系统,可以在意外掉电后保证系统日志不丢失。
我们将原先由syslogd管理的日志系统改为了rsyslogd,并通过日志文件和管道将日志输出到服务器。此外,还使用logrotate进行日志轮转管理,确保日志文件不会无限制增长。
此功能涉及云 AC 侧支持,相关文档:http://wiki.bigo.sg:8090/pages/viewpage.action?pageId=1033438725
工作流

画板
- 日志记录:rsyslogd记录系统和内核日志,按照配置将所有日志输出到/var/log/all.log和管道/tmp/agent.pipe。
- 日志采集与上报:bigo_agent进程通过管道/tmp/agent.pipe采集日志并上报到服务器。
- 日志轮转:每天凌晨,logrotate按照配置轮转/var/log/all.log日志文件,保留最近三天的日志,并压缩前两天的日志文件。
- 服务重启:日志轮转后,通过postrotate脚本重新启动syslog和rsyslog服务,确保日志记录不中断。
日志样例
2024-06-21T02:17:48.282646+00:00 58 kern.err kernel:[47937.638872] wlan: [20633:E:SPECTRAL] ucfg_spectral_is_mode_specific_request: Invalid spectral cp request id 142024-06-21T02:26:21.736542+00:00 58 user.debug repacd.wifimon: Onboarding 1 onboarding_done 1 sta_iface sta_iface_backup 2024-06-21T02:26:23.872055+00:00 58 user.info repacd.gwmon: Enable wsplcd 2024-06-21T02:26:26.224315+00:00 58 user.info wpa_supplicant: nl80211: Ignored event 103 (NL80211_CMD_VENDOR) for foreign interface (ifindex 27 wdev 0x0) 2024-06-21T02:26:26.224340+00:00 58 user.info wpa_supplicant: nl80211: Ignored event 103 (NL80211_CMD_VENDOR) for foreign interface (ifindex 27 wdev 0x0) 2024-06-21T02:26:26.224476+00:00 58 user.notice hostapd: nl80211: Ignored event 103 (NL80211_CMD_VENDOR) for foreign interface (ifindex 27 wdev 0x0) 2024-06-21T02:26:26.224501+00:00 58 user.notice hostapd: nl80211: Ignored event 103 (NL80211_CMD_VENDOR) for foreign interface (ifindex 27 wdev 0x0) 2024-06-21T02:26:26.224517+00:00 58 user.notice hostapd: nl80211: Drv Event 103 (NL80211_CMD_VENDOR) received for ath1 2024-06-21T02:26:26.224532+00:00 58 user.notice hostapd: nl80211: Vendor event: wiphy=2 vendor_id=0x1374 subcmd=752024-06-21T02:26:26.224546+00:00 58 user.notice hostapd: nl80211: Ignore unsupported QCA vendor event 752024-06-21T02:26:26.224565+00:00 58 user.notice hostapd: nl80211: BSS Event 59 (NL80211_CMD_FRAME) received for ath1 2024-06-21T02:26:26.224582+00:00 58 user.notice hostapd: nl80211: RX frame da=00:4d:98:e3:5c:15 sa=e4:34:93:6e:9b:81 bssid=00:4d:98:e3:5c:15 freq=5745 ssi_signal=0 fc=0xd0 seq_ctrl=0x5c70 stype=13 (WLAN_FC_STYPE_ACTION) len=472024-06-21T02:26:26.224597+00:00 58 user.notice hostapd: ath1: Event RX_MGMT (18) received

设备上如何查看日志?
输出所有日志
log
输出最近 1000 行
log 1000
实时输出日志
logf
实时输出某个模块日志
logf hostapd
系统配置详情
rsyslogd 配置
在本系统中,rsyslogd用于记录和管理系统日志。其配置文件如下:
module(load="imuxsock") module(load="imklog") module(load="imudp") input(type="imudp" port="514") #module(load="imtcp")#input(type="imtcp" port="514")module(load="ompipe") template(name="SkynetFormat" type="string" string="%timestamp:::date-rfc3339% %hostname% %pri-text% %syslogtag%%msg%\n") #$ActionFileDefaultTemplate RSYSLOG_TraditionalFileFormat$ActionFileDefaultTemplate SkynetFormat *.* action(type="ompipe" Pipe="/tmp/agent.pipe" Template="SkynetFormat") *.* /var/log/all.log kernel.* /var/log/kernel.log authpriv.* /var/log/secure cron.* /var/log/cron local7.* /var/log/boot.log
配置解析
- module(load=“imuxsock”) 和 module(load=“imklog”) 用于加载系统日志和内核日志模块。
- module(load=“imudp”) 和 input(type=“imudp” port=“514”) 用于启用UDP日志输入,监听514端口。
- module(load=“ompipe”) 用于启用日志输出到管道的功能。
- template(name=“SkynetFormat”…) 定义了日志输出的格式模板,使用RFC 3339格式的时间戳。
- action(type=“ompipe” Pipe=“/tmp/agent.pipe” Template=“SkynetFormat”) 将所有日志按照SkynetFormat格式输出到管道/tmp/agent.pipe。
- . /var/log/all.log 将所有日志记录到/var/log/all.log文件中。
- 其他配置项将特定类别的日志输出到相应的日志文件中。
logrotate 配置
为了防止日志文件无限制增长,我们使用logrotate进行日志轮转。其配置文件如下:
# rotate log files weekly#weeklydaily # keep 4 weeks worth of backlogsrotate 3# create new (empty) log files after rotating old onescreate notifempty nomail #olddir /var/log/backup/missingok #dateext# uncomment this if you want your log files compressed#compress# packages can drop log rotation information into this directoryinclude /etc/logrotate.d # no packages own lastlog or wtmp -- we'll rotate them here#/var/log/wtmp {# monthly# create 0664 root utmp# rotate 1#}/var/log/all.log { daily rotate 3 compress delaycompress missingok notifempty create 644 root root postrotate /etc/init.d/syslog restart > /dev/null /etc/init.d/rsyslog restart > /dev/null endscript }
配置解析
- daily 指定日志轮转的频率为每天。
- rotate 3 保留最近三天的日志。
- create 在轮转后创建新的日志文件。
- compress 启用日志压缩。
- delaycompress 延迟压缩,直到下一个轮转周期。
- missingok 忽略缺失的日志文件,不产生错误。
- notifempty 忽略空日志文件。
- /var/log/all.log 配置了/var/log/all.log文件的轮转规则。
- postrotate 脚本在轮转后重新启动syslog和rsyslog服务,确保日志记录不中断。
启动与初始化
rsyslogd 启动脚本
rsyslogd的启动脚本位于/etc/init.d/rsyslog,内容如下:
#!/bin/sh /etc/rc.common # Copyright (C) 2014 OpenWrt.org START=20 USE_PROCD=1 start_service() { mkfifo /tmp/agent.pipe procd_open_instance procd_set_param command /usr/sbin/rsyslogd -n procd_close_instance }
配置解析
- START=20 指定启动顺序。
- USE_PROCD=1 使用procd进行进程管理。
- start_service 函数在启动时创建管道/tmp/agent.pipe并启动rsyslogd。
Crontab 配置
在设备首次启动时,会自动执行reconfig脚本,配置crontab以每日执行logrotate。相关配置如下:
# Set Crontab to logrotate /var/log/all.log if ! crontab -l 2>/dev/null | grep -q 'logrotate'; then (crontab -l 2>/dev/null; echo "0 0 * * * /usr/sbin/logrotate /etc/logrotate.conf") | crontab - fi
配置解析
- 每天凌晨0点执行logrotate,轮转/var/log/all.log日志文件。
其他模块日志接入
为日后方便定位关联认证异常问题,将 hostapd 与 wpa_supplicant 的日志也接入 rsyslog,如需更详细的日志,可将如下脚本中的 ‘-d’ 改为 ‘-ddd’。
hostapd
start_service() { local cert_enabled="$(is_cert_enabled)" procd_open_instance $PKG_NAME procd_set_param command sh -c "/usr/sbin/hostapd -g $WPAD_VARRUN/hostapd/global -P $WPAD_VARRUN/hostapd-global.pid -d 2>&1 | logger -t hostapd" ...... } ...... boot_hostapd() { ...... nohup sh -c "hostapd -g $WPAD_VARRUN/hostapd/global -P $WPAD_VARRUN/hostapd-global.pid -d 2>&1 | logger -t hostapd"& }
wpa_supplicant
start_service() { local cert_enabled="$(is_cert_enabled)" procd_open_instance $PKG_NAME procd_set_param command sh -c "/usr/sbin/wpa_supplicant -g $WPAD_VARRUN/wpa_supplicantglobal -P $WPAD_VARRUN/wpa_supplicant-global.pid -d 2>&1 | logger - ...... } ...... boot_supplicant() { ...... nohup sh -c "wpa_supplicant -g $WPAD_VARRUN/wpa_supplicantglobal -P $WPAD_VARRUN/wpa_supplicant-global.pid -d 2>&1 | logger -t wpa_supplicant -p user.inf }
- Author:Ethan
- URL:https://www.zheyu.ink/article/system-log
- Copyright:All articles in this blog, except for special statements, adopt BY-NC-SA agreement. Please indicate the source!
Relate Posts