3

Linux 网络流量统计工具 vnstat

 1 year ago
source link: https://xujinzh.github.io/2023/03/04/linux-network-traffic-stats-vnstat/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Linux 网络流量统计工具 vnstat

2023-03-04technologylinux

3 1.6k 1 分钟

Linux 服务器网络流量监测工具除了常见的 ifconfig, nload, iftop 等,但只有总流量信息,且当服务器重启后,监测数据会重新统计。本篇介绍 vnstat,它能够把监测数据保存到数据库中,可以根据需要显示流量统计信息。

sudo apt update
sudo apt install vnstat vnstati

安装完成后有配置文件:/etc/vnstat.conf,里面配置参数,如 Interface 该参数默认会自动监测需要监控的网卡,MonthRotat 为每月流量结算日期,即每月流量重新计算的日期,默认为每月1号。

对网卡生产数据库文件:

# 假设网卡是 eth0
# --dbdir 为我们保存数据路径,如果不指定,则默认保存到 /var/lib/vnstat 下
# 数据名 vnstat.db
vnstat -i eth0 --dbdir /disk0/vnstat

常用命令:

# 实时总流量
vnstat

# 查看网卡 eth0 的实时流量
vnstat -l -i eth0

# 按没5分钟查看
vnstat -5

# 按每小时查看
vnstat -h
# 按每小时查看并画图
vnstat -hg

# 按每天查看
vnstat -d

# 按每月查看
vnstat -m

# 把每小时的结果输出为 xml
vnstat -h --xml

# 把每小时的结果输出为 json
vnstat -h --json

# 查看指定时间段的数据,数据格式:YYYY-MM-DD HH:MM and YYYY-MM-DD
# 如查看 2023.03.01 到 2023.03.04 时间段按每小时显示流量信息
vnstat -h -b 2023-03-01 -e 2023-03-04

# 把结果保存为 PNG 图片,需要使用 vnstati 命令
vnstati -i eth0 --months --output month-network-traffic-stats.png

输出结果解释:

  • rx:接收(receive)
  • tx:发送(transport)
  • total:总计
  • estimated:估计

更多请使用命令 vnstat --help 查看,或者 man vnstat

vnStat 2.6 by Teemu Toivola <tst at iki dot fi>

-5, --fiveminutes [limit] show 5 minutes
-h, --hours [limit] show hours
-hg, --hoursgraph show hours graph
-d, --days [limit] show days
-m, --months [limit] show months
-y, --years [limit] show years
-t, --top [limit] show top days

-b, --begin <date> set list begin date
-e, --end <date> set list end date

--oneline [mode] show simple parsable format
--json [mode] [limit] show database in json format
--xml [mode] [limit] show database in xml format

-tr, --traffic [time] calculate traffic
-l, --live [mode] show transfer rate in real time
-i, --iface <interface> select interface

Use "--longhelp" or "man vnstat" for complete list of options.

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK