服务器上需要监控网络流量,以前是用snmp方式监控,发现snmp有时候不太稳定,客户端连接不上snmp获取不到CPU 内存的信息。
现在换成vnstat用一下,软件可以到官方网站上下载 http://humdi.net/vnstat/
我这里下载 vnstat-1.9.tar.gz 版本。
wget http://humdi.net/vnstat/vnstat-1.9.tar.gz
tar xvzf vnstat-1.9.tar.gz
进入目录:
cd vnstat-1.9
编译安装:
make
发现找不到make, 安装一下 yum --disableplugin=fastestmirror install make gcc
make install
成功。
通过ifconfig查看网卡名称,eth0
然后通过 vnstat -u -i eth0 来生成数据库
可以通过命令查看流量了,
vnstat
Database updated: Wed Apr 14 15:04:39 2010eth0 since 04/14/10
rx: 4.09 MiB tx: 4.12 MiB total: 8.21 MiB
monthly
rx | tx | total | avg. rate
------------------------+-------------+-------------+---------------
Apr '10 4.09 MiB | 4.12 MiB | 8.21 MiB | 0.06 kbit/s
------------------------+-------------+-------------+---------------
estimated 8 MiB | 8 MiB | 16 MiB |daily
rx | tx | total | avg. rate
------------------------+-------------+-------------+---------------
today 4.09 MiB | 4.12 MiB | 8.21 MiB | 1.24 kbit/s
------------------------+-------------+-------------+---------------
estimated 6 MiB | 6 MiB | 12 MiB |
我们可以通过vnstat 查看流量,我们看看都有哪些指令。
-bash-3.2# vnstat --help
vnStat 1.9 by Teemu Toivola <tst at iki dot fi>
-q, --query query database
-h, --hours show hours
-d, --days show days
-m, --months show months
-w, --weeks show weeks
-t, --top10 show top10
-s, --short use short output
-u, --update update database
-i, --iface select interface (default: eth0)
-?, --help short help
-v, --version show version
-tr, --traffic calculate traffic
-ru, --rateunit swap configured rate unit
-l, --live show transfer rate in real time
See also "--longhelp" for complete options list and "man vnstat".
vnstat -h 可以看到一个小时的流量
^ r
| r
| t r
| t t r
| t t r
| t t r
| t t r
| t t r
| t t r
| t t r
-+--------------------------------------------------------------------------->
| 23 00 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22
h rx (KiB) tx (KiB) h rx (KiB) tx (KiB) h rx (KiB) tx (KiB)
23 0 0 07 0 0 15 451 21286
00 0 0 08 0 0 16 886 26065
01 0 0 09 0 0 17 223 141
02 0 0 10 0 0 18 186 110
03 0 0 11 0 0 19 196 114
04 0 0 12 0 0 20 195 116
05 0 0 13 0 0 21 2864 250
06 0 0 14 0 0 22 29403 589
-bash-3.2#
-bash-3.2# vnstat -d
eth0 daily
day rx | tx | total | avg. rate
------------------------+-------------+-------------+---------------
04/14/10 30.09 MiB | 28.53 MiB | 58.62 MiB | 5.56 kbit/s
04/15/10 0 KiB | 0 KiB | 0 KiB | 0.00 kbit/s
04/16/10 0 KiB | 0 KiB | 0 KiB | 0.00 kbit/s
04/21/10 0 KiB | 0 KiB | 0 KiB | 0.00 kbit/s
05/10/10 33.60 MiB | 47.53 MiB | 81.13 MiB | 8.32 kbit/s
------------------------+-------------+-------------+---------------
estimated 35 MiB | 50 MiB | 85 MiB |
-bash-3.2#
好了,安装完成,可以睡觉啦。
注: 可以通过vnStat PHP frontend 1.4.1 程序,访问vnStat的流量, 我这里展示安装好的效果 http://155.pipc.net/ ,上面是1分钟更新一次服务器流量。