Skip to content

CaoZ/RouterStat

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

11 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Part I: 使用 MQTT / Python / Grafana 实现小米路由器网速查看与统计

Python

小米 AX6000 路由器虽然身形巨大无比,但自带的网速查看功能却十分简陋,约等于没有。幸好系统是基于 OpenWrt 的,可以自己动手实现一个。

Step 1: 寻找数据

命令:

ubus call trafficd hw '{"debug": true}'

trafficd

简单的命令却寻觅最久,机缘巧合之下才发现... 使用时加入 debug 参数以获得更多信息。


Step 2:加工处理

有了数据源就好做了,定时采样加工处理再展示即可。小米路由器自带了一部分 MQTT 工具,因此使用 MQTT 将数据源转发给电脑处理并入库,再使用 Grafana 做展示。即使电脑不一直开机也没关系,通过配置,MQTT broker 可持久化暂存未处理消息。

2.1 安装 Mosquitto broker 2.0

小强系统自带了 MQTT client,只需安装支持 MQTT v5 的 broker 即可。手动下载 ipk 文件,解压后获得可执行文件(.\data.tar.gz\.\usr\sbin\mosquitto),将其上传到路由器中并注册为服务。如果 opkg 可用更佳。

Cortex-A53: mosquitto-nossl_2.0.15-1_aarch64_cortex-a53.ipk

更多https://mirrors.aliyun.com/openwrt/releases/22.03-SNAPSHOT/packages/

创建配置文件:

/etc/mosquitto.conf:

# log_dest file /tmp/log/mosquitto.log
user root
bind_address 0.0.0.0
allow_anonymous true

/etc/init.d/mosquitto-2:

#!/bin/sh /etc/rc.common

START=50

start() {
    /data/cz/bin/mosquitto-2 -c /etc/mosquitto.conf -d
    return 0
}

2.2 定时采样

创建 cron job 实现定时采样,可结合 sleep 实现分钟内采样。

* * * * * ubus -S call trafficd hw '{"debug": true}' | mosquitto_pub -t 'home/router-stat' -q 2 -i client_pub -l -D publish user-property timestamp $(date +%s) >/dev/null 2>&1

使用 Python 编写 MQTT 消费者程序,解析数据并写入数据库。如果是 Windows 可以使用 nssm 将其注册为服务

配置在 config.py 中,表的结构在 data.py 中。

2.4 安装 Grafana 并创建监控面板

有了数据就可以用 Grafana 自由的创建监控面板了~

比如创建一个 Time series 面板来查看上传下载总速度:

select sum(rx_rate) as rx_rate, sum(tx_rate) as tx_rate, unix_timestamp(timestamp) as time from home.router_stat where $__timeFilter(timestamp) group by timestamp



Part II: Hyper-V 虚拟机网络 / 硬盘使用统计

后来网络结构变了,有设备(虚拟机)直连光猫,不能通过路由器进行网速监控了。经过一番尝试,发现可以使用 Windows 性能计数器 来收集关注的信息。

性能计数器

关注的数据:

\Hyper-V Virtual Network Adapter(*)\Bytes Received/sec
\Hyper-V Virtual Network Adapter(*)\Bytes Sent/sec
\Hyper-V Virtual Storage Device(*)\Read Bytes/sec
\Hyper-V Virtual Storage Device(*)\Write Bytes/sec`

Screenshot:

网络监控

存储监控

About

使用 MQTT / Python / Grafana 实现小米路由器网速查看与统计 & Hyper-V 虚拟机网络 / 硬盘使用统计

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages