作者:admin,发布日期:2019-12-10
阅读:3596;评论:0

咕了好久才写的文章,博主已经顺利变为年更博主

写在开头

vnStat是用于Linux和BSD的基于控制台的网络流量监视器,它记录所选接口的网络流量日志。它使用内核提供的网络接口统计信息作为信息源。这意味着vnStat实际上不会嗅探任何流量,而且还可以确保不管网络流量速率如何,系统资源的使用都比较少,这篇文章,博主将使用Centos7安装VnStat,并且实现流量的统计。

安装方法

centos下的epel源内已经包含了vnstat的安装包。我们只需要安装epel-release,再安装vnstat,即可完成安装。

yum install -y epel-release # 安装epel源
yum install -y vnstat # 安装VnStat

安装完成后,需要创建vnstat的数据库,用于存放流量的统计信息。

vnStat 1.15 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 top 10 days

         -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 -u -i eth0 # -u 更新数据库 -i 指定网卡

这里指定了eth0网卡,运行此条命令后将为eth0网卡创建数据库(如果网卡名为其他名称,需要进行修改)

创建数据库完成之后,需要给vnstat目录设置所有者!!!(重要,否则无法统计流量,下面写原因)

chown -R vnstat:vnstat /var/lib/vnstat/ # 设置vnstat数据库目录的所有者为vnstat用户

接着启动vnstat统计服务,即可安装完成。

systemctl enable vnstat.service # 启用vnstat服务开机启动
systemctl start vnstat.service # 启动vnstat服务
journalctl -u vnstat.service -f # 查看vnstat服务日志,ctrl+c退出

查看流量统计

vnstat(直接控制台输出流量使用情况)

[root@V2-SHARE vnstat]# vnstat -h # 按小时查看流量使用
 eth0                                                                     22:26 
  ^         t                                                                   
  |        rt                                                                   
  |        rt                                                                   
  |      t rt                                                                   
  |     rt rt                                                                   
  |     rt rt                                                                   
  |     rt rt                                                                   
  |     rt rt                                        rt                         
  |     rt rt          r                             rt                         
  |     rt rt          rt                            rt 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 (MiB)   tx (MiB)      h  rx (MiB)   tx (MiB)      h  rx (MiB)   tx (MiB) 
23      68.10      58.84    07      19.00       6.99    15     239.37     246.43
00     473.20     529.53    08      19.01       7.02    16      82.93      73.40
01     673.20     743.19    09      20.36       8.31    17      25.85      13.92
02      19.05       7.05    10      28.27      16.21    18      19.00       7.04
03      18.99       7.02    11      20.51       8.51    19      18.91       6.93
04      18.95       6.95    12      27.42      15.34    20      19.00       6.99
05     154.30     137.84    13      24.61      12.61    21      33.72      26.74
06      18.98       6.99    14      37.84      27.07    22       8.26       3.06
[root@V2-SHARE vnstat]# vnstat -d # 按天查看流量使用

 eth0  /  daily

         day         rx      |     tx      |    total    |   avg. rate
     ------------------------+-------------+-------------+---------------
     12/04/2019     6.85 GiB |    7.03 GiB |   13.87 GiB |    1.35 Mbit/s
     12/05/2019      112 KiB |      64 KiB |     176 KiB |    0.02 kbit/s
     12/06/2019        0 KiB |       0 KiB |       0 KiB |    0.00 kbit/s
     12/09/2019     3.76 GiB |    2.88 GiB |    6.63 GiB |  644.19 kbit/s
     12/10/2019     1.97 GiB |    1.88 GiB |    3.85 GiB |  400.24 kbit/s
     ------------------------+-------------+-------------+---------------
     estimated      2.11 GiB |    2.01 GiB |    4.12 GiB |
 
[root@V2-SHARE vnstat]# vnstat -m # 按月查看流量使用

 eth0  /  monthly

       month        rx      |     tx      |    total    |   avg. rate
    ------------------------+-------------+-------------+---------------
      Dec '19     12.57 GiB |   11.79 GiB |   24.36 GiB |  246.35 kbit/s
    ------------------------+-------------+-------------+---------------
    estimated     40.60 GiB |   38.06 GiB |   78.65 GiB |
[root@V2-SHARE vnstat]# vnstat -w # 按周查看流量使用

 eth0  /  weekly

                      rx      |     tx      |    total    |   avg. rate
   ---------------------------+-------------+-------------+---------------
    last 7 days     12.58 GiB |   11.79 GiB |   24.36 GiB |  340.94 kbit/s
      last week      6.85 GiB |    7.03 GiB |   13.87 GiB |  192.43 kbit/s
   current week      5.73 GiB |    4.76 GiB |   10.49 GiB |  525.49 kbit/s
   ---------------------------+-------------+-------------+---------------
      estimated     20.93 GiB |   17.38 GiB |   38.31 GiB |
      
 [root@V2-SHARE vnstat]# vnstat -t # 显示流量使用前十的日期

 eth0  /  top 10

    #      day          rx      |     tx      |    total    |   avg. rate
   -----------------------------+-------------+-------------+---------------
    1   12/04/2019     6.85 GiB |    7.03 GiB |   13.87 GiB |    1.35 Mbit/s
    2   12/09/2019     3.76 GiB |    2.88 GiB |    6.63 GiB |  644.19 kbit/s
    3   12/05/2019      112 KiB |      64 KiB |     176 KiB |    0.02 kbit/s
   -----------------------------+-------------+-------------+---------------

vnstati (输出流量使用图片)

[root@V2-SHARE vnstat]# vnstati 
 vnStat image output 1.15 by Teemu Toivola <tst at iki dot fi>

         -h,  --hours          output hours
         -d,  --days           output days
         -m,  --months         output months
         -t,  --top10          output top10
         -s,  --summary        output summary
         -hs, --hsummary       output horizontal summary with hours
         -vs, --vsummary       output vertical summary with hours
         -nh, --noheader       remove header from output
         -ne, --noedge         remove edge from output
         -nl, --nolegend       remove legend from output
         -ru, --rateunit       swap configured rate unit
         -o,  --output         select output filename
         -c,  --cache          update output only when too old
         -i,  --iface          used interface (default: eth0)
         -?,  --help           this help
         -D,  --debug          show some additional debug information
         -v,  --version        show version
         --dbdir               select database directory
         --style               select output style (0-3)
         --locale              set locale
         --config              select config file
         --altdate             use alternative date location
         --headertext          specify header text string
         --transparent         toggle background transparency

See also "man vnstati".

[root@V2-SHARE vnstat]# vnstati -h -o output.png # 按小时查看流量使用情况,输出到当前目录的output.png文件内

(其余命令可参考上面的vnstat使用)

a.png

无法在后台统计流量的解决

这也是偶尔一次的发现,centos7安装完成vnstat后,按照正常方法,启用vnstat服务,发现不能统计流量,只有vnstat -u之后,才能看到一些零星的数据,查阅资料已经翻看日志后,才发现,因为权限问题,vnstat用户无法往所有者为root用户的数据库文件内写入流量统计信息,此时数据只能记录在缓存中。

下面是日志显示的内容,可以自行排查是否出现下面日志:

-- Logs begin at Mon 2019-12-09 17:09:29 CST. --
Dec 10 19:46:04 V2-SHARE vnstatd[990]: Error: Unable to create database backup "/var/lib/vnstat/.eth0".
Dec 10 19:46:04 V2-SHARE vnstatd[990]: Error: Unable to create database backup "/var/lib/vnstat/.eth0".
Dec 10 19:46:04 V2-SHARE vnstatd[990]: Error: Unable to create database backup "/var/lib/vnstat/.eth0".
Dec 10 19:46:04 V2-SHARE vnstatd[990]: Error: Unable to create database backup "/var/lib/vnstat/.eth0".
Dec 10 19:46:04 V2-SHARE vnstatd[990]: Error: Unable to create database backup "/var/lib/vnstat/.eth0".
Dec 10 19:46:04 V2-SHARE vnstatd[990]: Error: Unable to create database backup "/var/lib/vnstat/.eth0".
Dec 10 19:46:04 V2-SHARE vnstatd[990]: Error: Unable to create database backup "/var/lib/vnstat/.eth0".
Dec 10 19:46:04 V2-SHARE vnstatd[990]: Error: Unable to create database backup "/var/lib/vnstat/.eth0".
Dec 10 19:46:04 V2-SHARE vnstatd[990]: Error: Unable to create database backup "/var/lib/vnstat/.eth0".
Dec 10 19:46:04 V2-SHARE vnstatd[990]: Error: Unable to create database backup "/var/lib/vnstat/.eth0".
Dec 09 17:30:58 V2-SHARE vnstatd[990]: Error: Unable to write database, continuing with cached data.

解决方法就是给vnstat整个目录修改所有者,然后重启服务,就能看到正常的流量统计

chown -R vnstat:vnstat /var/lib/vnstat/ # 设置vnstat数据库目录的所有者为vnstat用户
systemctl restart vnstat.service # 重启vnstat


你可能感兴趣的文章

评论区

发表评论

必填

选填

选填

必填

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。