Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

vnstat 配置 MonthRotate 为非 1 的天数后,当前月份流量特定时间内会错误显示为 0.00G | 0.00G #99

Closed
daseinem opened this issue Nov 6, 2022 · 5 comments

Comments

@daseinem
Copy link

daseinem commented Nov 6, 2022

有一些机器上的流量不是按自然月刷新的,比如在15日刷新并且将 vnstatMonthRotate 也配置为15,那么在15日之前 vnstat --json m 返回的数据会存在不包含当前自然月流量信息的情况,导致 ServerStatus-Rust 显示的月流量为 0.00G | 0.00G.

vnstat json 输出样例 (省略中间部分月份)

{
    "vnstatversion": "2.6",
    "jsonversion": "2",
    "interfaces": [
        {
            "name": "eth0",
            "alias": "",
            "created": {
                "date": {
                    "year": 2022,
                    "month": 3,
                    "day": 1
                }
            },
            "updated": {
                "date": {
                    "year": 2022,
                    "month": 11,
                    "day": 6
                },
                "time": {
                    "hour": 6,
                    "minute": 25
                }
            },
            "traffic": {
                "total": {
                    "rx": 34838225310,
                    "tx": 537051064160
                },
                "month": [
                    {
                        "id": 1,
                        "date": {
                            "year": 2022,
                            "month": 3
                        },
                        "rx": 120663351707,
                        "tx": 107577662878
                    },
                    {
                        "id": 7,
                        "date": {
                            "year": 2022,
                            "month": 9
                        },
                        "rx": 37335903948,
                        "tx": 23122453659
                    },
                    {
                        "id": 8,
                        "date": {
                            "year": 2022,
                            "month": 10
                        },
                        "rx": 21771011198,
                        "tx": 9919763673
                    }
                ]
            }
        }
    ]
}

stat_server 版本:

❯ /usr/local/ServerStatus/server/stat_server --version
stat_server v1.5.6 GIT:51f17c7, BUILD:2022-11-02 16:17:06 UTC

stat_client 版本:

❯ /usr/local/ServerStatus/client/stat_client --version
stat_client v1.5.6 GIT:51f17c7, BUILD:2022-11-02 16:17:07 UTC
@zdz
Copy link
Owner

zdz commented Nov 6, 2022

vnstat --json m 会返回记录以来所有月份的数据,stat_client 只是提取当前月份的数据来上报,可以看看 vnstat 当前月的数据对不对,和对对 https://xxx.com/json/stats.json 返回的几个字段 network_rx, network_tx, network_in, network_out, last_network_in, last_network_out

@daseinem
Copy link
Author

daseinem commented Nov 6, 2022

vnstat 分别按月和按天的控制台输出, 可以看到按天是统计了11月以后的数据,但是按月的数据仍然截至10月.

❯ vnstat -m

 eth0  /  monthly

        month        rx      |     tx      |    total    |   avg. rate
     ------------------------+-------------+-------------+---------------
       2022-03    112.38 GiB |  100.19 GiB |  212.57 GiB |  681.72 kbit/s
       2022-04     94.12 GiB |  109.97 GiB |  204.09 GiB |  676.37 kbit/s
       2022-05     71.68 GiB |   84.98 GiB |  156.66 GiB |  502.41 kbit/s
       2022-06     85.67 GiB |   68.26 GiB |  153.93 GiB |  510.13 kbit/s
       2022-07     58.07 GiB |   33.68 GiB |   91.75 GiB |  294.25 kbit/s
       2022-08     35.11 GiB |   25.75 GiB |   60.86 GiB |  195.19 kbit/s
       2022-09     34.77 GiB |   21.53 GiB |   56.31 GiB |  186.60 kbit/s
       2022-10     20.39 GiB |    9.35 GiB |   29.73 GiB |   95.36 kbit/s
     ------------------------+-------------+-------------+---------------
     estimated     31.59 GiB |   14.48 GiB |   46.08 GiB |

❯ vnstat -d 21

 eth0  /  daily

          day        rx      |     tx      |    total    |   avg. rate
     ------------------------+-------------+-------------+---------------
     2022-10-17   731.98 MiB |  135.46 MiB |  867.44 MiB |   84.22 kbit/s
     2022-10-18   691.62 MiB |  135.83 MiB |  827.45 MiB |   80.34 kbit/s
     2022-10-19   698.00 MiB |  161.92 MiB |  859.92 MiB |   83.49 kbit/s
     2022-10-20   688.36 MiB |  139.73 MiB |  828.09 MiB |   80.40 kbit/s
     2022-10-21   747.63 MiB |  139.54 MiB |  887.17 MiB |   86.14 kbit/s
     2022-10-22   749.18 MiB |  139.24 MiB |  888.42 MiB |   86.26 kbit/s
     2022-10-23   737.18 MiB |  167.20 MiB |  904.38 MiB |   87.81 kbit/s
     2022-10-24   727.15 MiB |  140.01 MiB |  867.16 MiB |   84.19 kbit/s
     2022-10-25   759.93 MiB |  140.37 MiB |  900.30 MiB |   87.41 kbit/s
     2022-10-26     1.25 GiB |  321.85 MiB |    1.57 GiB |  155.77 kbit/s
     2022-10-27     1.28 GiB |  480.07 MiB |    1.75 GiB |  174.05 kbit/s
     2022-10-28   748.75 MiB |  325.86 MiB |    1.05 GiB |  104.33 kbit/s
     2022-10-29     1.61 GiB |    1.40 GiB |    3.01 GiB |  299.46 kbit/s
     2022-10-30   801.00 MiB |  229.50 MiB |    1.01 GiB |  100.05 kbit/s
     2022-10-31   724.66 MiB |  140.12 MiB |  864.79 MiB |   83.96 kbit/s
     2022-11-01   758.46 MiB |  202.03 MiB |  960.49 MiB |   93.25 kbit/s
     2022-11-02     2.15 GiB |    1.57 GiB |    3.72 GiB |  369.90 kbit/s
     2022-11-03     0.98 GiB |  424.06 MiB |    1.40 GiB |  138.99 kbit/s
     2022-11-04     1.17 GiB |  595.53 MiB |    1.75 GiB |  173.82 kbit/s
     2022-11-05     1.60 GiB |    1.04 GiB |    2.64 GiB |  262.23 kbit/s
     2022-11-06     1.73 GiB |    1.54 GiB |    3.27 GiB |  899.15 kbit/s
     ------------------------+-------------+-------------+---------------
     estimated      4.79 GiB |    4.25 GiB |    9.04 GiB |

这台机器配置的 MonthRotate18, 手动加算下来从10月18日到今天总数据量确实和 vnstat -m 输出的 2022-10 的数据吻合.

以下是https://xxx.com/json/stats.json 的相关部分(页面显示的总流量是 512.22G | 453.72G):

{
    "vnstat": true,
    "network_rx": 7224,
    "network_tx": 2379,
    "network_in": 549957633026,
    "network_out": 487166824728,
    "last_network_in": 549957633026,
    "last_network_out": 487166824728,
    "latest_ts": 1667723944
}

我的理解是 vnstatMonthRotate 和月份数的处理是,如果自然月内,日期天数没有到这个设置的值(18),就仍然把流量统计到上一个自然月的月份数(10)里,从而导致 client 获取到的数据里,当前自然月对应的月份数值(11)不存在相关数据。


我查到 vnstat --oneline 可以输出一个一行数据,会包含当前的统计月份数值,不知道是否方便利用:
1;eth0;2022-11-06;1.76 GiB;1.55 GiB;3.30 GiB;844.47 kbit/s;2022-10;20.42 GiB;9.36 GiB;29.78 GiB;152.68 kbit/s;512.22 GiB;453.72 GiB;965.94 GiB

vnstat 的 Manpage 是这样描述的:

--oneline [mode]
Show traffic summary for selected interface using one line with a parsable format. The output contains 15 fields with ; used as field delimiter. The 1st field contains the API version information of the output that will only be changed in future versions if the field content or structure changes. The following fields in order 2) interface name, 3) timestamp for today, 4) rx for today, 5) tx for today, 6) total for today, 7) average traffic rate for today, 8) timestamp for current month, 9) rx for current month, 10) tx for current month, 11) total for current month, 12) average traffic rate for current month, 13) all time total rx, 14) all time total tx, 15) all time total traffic. An optional mode parameter can be used to force all fields to output in bytes without the unit itself shown.

@zdz
Copy link
Owner

zdz commented Nov 6, 2022

扫了一下 vnstat 的源码, 确实是这么算,MonthRotate 本质定义了上一个月的结束和下一个月的开始
手册也写了

$ man 5 vnstat.conf

MonthRotate
Day of month that months are expected to change. Usually set to 1 but can be set to alternative values for example for tracking monthly billed traffic where the billing period doesn't start on the  first  day.
For example, if set to 7, days of February up to and including the 6th will count for January. Changing this option will not cause existing data to be recalculated. Value range: 1..28

--oneline 多个网口测试貌似没法选择和过滤网口

相关讨论: vergoh/vnstat#87 (comment)

@zdz
Copy link
Owner

zdz commented Nov 12, 2022

v1.5.7 版本,适配了 vnstat month rotate 逻辑,用法

# 可选范围 1..28,要跟 vnstat.conf 配置 MonthRotate 对齐
./stat_client -n --vnstat-mr 7

@daseinem
Copy link
Author

更新服务端到 v1.5.7 后,使用一键部署更新客户端并应用 vnstat-mr=18 参数,现在已经可以正确显示月流量了:

  • 网页显示:27.80G | 13.80G
  • vnstat -m 1 控制台输出:
        month        rx      |     tx      |    total    |   avg. rate
     ------------------------+-------------+-------------+---------------
       2022-10     27.81 GiB |   13.80 GiB |   41.62 GiB |  133.46 kbit/s
     ------------------------+-------------+-------------+---------------

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants