Vnstat是一个开源的网络流量统计工具,可以按分钟、天、月查看流量。
vnstat-dashboard.png

安装Vnstat

安装非常简单,CentOS中使用以下命令即可:

yum install epel-release -y
yum install vnstat -y
systemctl enable vnstat
systemctl start vnstat

使用Vnstat

vnstat -l  # 或者 `--live` 实时流量
vnstat -h  # 显示小时流量
vnstat -d  # 显示日流量信息
vnstat -w  # 显示周流量信息
vnstat -m  # 显示月流量信息
vnstat -t  # 显示流量最高top10天

安装Web界面

  • 首先需要安装PHP,可以使用lnmp组件来完成这一操作。
  • 再安装composer.
  • 新建一个vhost,此处假设根目录为/home/wwwroot/vnstat

    cd /home/wwwroot
    wget https://github.com/tomangert/vnstat-dashboard/archive/refs/heads/master.zip
    unzip master.zip
    mkdir vnstat
    mv vnstat-dashboard-master/app/* vnstat
    rm vnstat-dashboard-master -rf
    cd vnstat
    composer install
    nano includes/config.php
  • 编辑配置文件:

    // Set the default system Timezone,修改时区,东八区可以按以下设置,更多时区见https://www.php.net/manual/zh/timezones.php
    date_default_timezone_set('Asia/Hong_Kong'icon_wink.png;
    // Path of vnstat,设置vnstat可执行文件的路径
    $vnstat_bin_dir = '/usr/bin/vnstat';
    // Set to true to set your own interfaces,设置是否仅显示指定网络接口,false表示显示所有
    $use_predefined_interfaces = false;
    if ($use_predefined_interfaces == true) {
    //如果你的机器有多个网络接口,而你只想让其中某一些显示出来,就可以在此处设置
      $interface_list = ["eth0", "eth1"];
      $interface_name['eth0'] = "Internal #1";
      $interface_name['eth1'] = "Internal #2";
    }
  • 允许popen函数
    此程序使用了popen函数来运行vnstat,而LNMP默认禁用了这个函数,所以需要更改PHP配置文件。否则Web界面是没有数据的。

    nano /usr/local/php/etc/php.ini

    找到disable_functions

    disable_functions = passthru,system,chroot,chgrp,chown,shell_exec,proc_open,popen,proc_get_status,ini_alter,ini_restore,dl,openlog,syslog,readlink,symlink,popepassthru

    将其中的popen去掉,然后重启PHPsystemctl restart php-fpm
    这样就可以啦。

文章目录