一、背景
最近在研究如何统计网络流量的问题,发现了 Vnstat 这款小软件,它的特点是简单实用,配置方便,另外,它还自带了 vnstati 命令,通过它可以直接可以输出流量统计图。
vnStat 是一个用于 Linux 和 BSD 的命令行网络流量监控器,用于记录指定接口的网络流量日志,它使用由内核提供的网络接口统计信息作为信息源,这就意味着vnStat 不会嗅探任何流量,并且占用的系统资源极小。
二、安装
项目主页:vnstat
1、CentOS
# yum install vnstat -y
2、Ubuntu
# apt-get install vnstat vnstati -y
三、用法
1、初始化
指定统计网卡 eth0,具体哪个可通过 ifconfig 查看:
# vnstat -i eth0 -u
2、后台监控
一般都不需要修改,个别客制系统需要:
# vim /etc/vnstat.conf
...
Interface "eth0" #设置默认网络接口
...
DaemonUser "root" #指定后台运行的用户和组,如果发现无法自动更新流量统计,可以尝试将默认的 vnstat 用户及组改为 root
DaemonGroup "root"
# vnstatd -d #启动后台监控
3、查看方法
按小时、天、周、月
# vnstat -h
# vnstat -d
# vnstat -w
# vnstat -m
不加参数则默认显示月、天
# vnstat
4、图形输出
如:将本月流量统计信息输出到 vnstat.png:
# vnstati -m -o vnstat.png
四、问题
执行 vnstat
时提示 eth0: Not enough data available yet.
出现此问题的原因主要有三个,一个是没有初始化,初始化:
# vnstat
No database found, nothing to do. Use --help for help.
A new database can be created with the following command:
vnstat --create -i eth0
Replace 'eth0' with the interface that should be monitored.
The following interfaces are currently available:
lo eth0 (10000 Mbit)
# vnstat -i eth0 -u
# vnstatd -d
# vnstat -u
另一个就是调整了内核,后一种情况必须先运行一次下面这条命令:
# vnstat --testkernel
还有就是没有启动后台监控:
# vnstatd -d
五、附:nload
nload
是一个实时监控网络流量和带宽使用的控制台应用程序,使用两个图表可视化地展示接收和发送的流量,并提供诸如数据交换总量、最小/最大网络带宽使用量等附加信息。
nolad
与 vnstat
区别在于,nload
可以显示实时网速,vnstat
显示的则是平均网速。
1、安装
I、CentOS
# yum install nload
II、Ubuntu
# apt install nload
2、使用
# nload
Device eth0 [172.21.99.28] (1/2):
============================================================================================================================
Incoming:
Curr: 44.77 kBit/s
Avg: 65.45 kBit/s
Min: 7.31 kBit/s
Max: 816.14 kBit/s
Ttl: 8.15 GByte
Outgoing:
Curr: 125.04 kBit/s
Avg: 163.53 kBit/s
Min: 11.85 kBit/s
Max: 1.93 MBit/s
. Ttl: 14.61 GByte
3、参数
更新间隔
# nload -t 500
默认每 100 毫秒刷新一次显示数值,上面的例子将时间间隔设置成 500 毫秒。
显示单位
# nload -u h|H|b|B|k|K|m|M|g|G
# nload -U h|H|b|B|k|K|m|M|g|G
小写选项 -u: h 意为自动格式化为人类易读的单位,b 意为 Bit/s,k 意为 kBit/s,m 意为 MBit/s,g 意为 GBit/s。大写字母意为使用 Byte 替代 Bit。默认为 k。
大写选项 -U 与小写选项 -u 非常相似,不同之处在于它展示的是数据量,比如 Bit, kByte, GBit 等等。(没有 "/s")。默认值是 M。
快捷键
nload 命令一旦执行就会开始监控网络设备,你可以使用下列快捷键操控 nload 应用程序:
- 按键盘上的 ← → 或者 Enter/Tab 键在设备间切换
- 按 F2 显示选项窗口
- 按 F5 将当前设置保存到用户配置文件
- 按 F6 从配置文件重新加载设置
- 按 q 或者 Ctrl+C 退出 nload
你可能还需要:《Linux 下如何监控进程的实时网速 - NetHogs》