#教程# – 安装Vnstat流量统计,查看AWS Lightsail流量使用情况(可分小时/周/日/月统计)

前言

这几天把所有站点都迁移到亚马逊光帆AWS LightSail了,但是要查看 AWS LightSail 的流量统计非常麻烦,要在 CloudWatch 里建监控,也不知道收费不收费,光帆听说流量万一超了,房子都没了,博主这里就赶紧整个流量统计 vnstat 来方便检查流量。

图片[1] - #教程# – 安装Vnstat流量统计,查看AWS Lightsail流量使用情况(可分小时/周/日/月统计) - 云线路

vnstat 非常的轻量,运行也不占用服务器资源,安装非常的便捷,查看流量也非常简单!

vnStat 官网

安装及使用

注意:安装Vnstat需要 sqlite3 等依赖,否则无法安装

yum install gd gd-devel sqlite-devel #CentOS/Fedora

apt-get install build-essential gd gd-devel libsqlite3-dev  #Debian/Ubuntu

安装方法

直接 yum 或者 apt 安装的版本有些老旧,但没关系,又不是跑程序!

yum install vnstat -y  #centos7+

apt install vnstat  #Debian

跳过可选,安装完后直接到下面[配置]

可选:升级最新版,或者最新安装

下载官方文件,解压缩,进入安装目录

cd /usr/local
wget http://humdi.net/vnstat/vnstat-2.10.tar.gz
wget http://humdi.net/vnstat/vnstat-2.10.tar.gz --no-check-certificate #无法下载请加参数再下
tar -zxvf vnstat-2.10.tar.gz
cd vnstat-2.10
./configure --prefix=/usr --sysconfdir=/etc #可选安装路径

然后安装

make
make install

make: *** No targets specified and no makefile found. Stop.

如出现上面提示请yum update 更新最新版本系统软件;

然后编译缺失关联软件yum install gcc build-essential

安装 sqllite:yum install gd gd-devel sqlite-devel;

编译执行完毕之后,再执行./configure && make命令就可以解决问题;

./configure && make && make install

安装路径
configure:
configure: host: x86_64-unknown-linux-gnu
configure: build: x86_64-unknown-linux-gnu
configure:
configure: prefix: /usr
configure: exec_prefix: ${prefix}
configure: bindir: ${exec_prefix}/bin
configure: sbindir: ${exec_prefix}/sbin
configure: sysconfdir: /etc
configure:
configure: Install paths
configure:
configure: vnstat: /usr/bin/vnstat
configure: vnstati: /usr/bin/vnstati
configure: vnstatd: /usr/sbin/vnstatd
configure: config: /etc/vnstat.conf
configure:
configure: Tests: unavailable
configure: Image output: enabled

安装完成后,需要复制Vnstat服务文件

VnStat Systemd

cp -v examples/systemd/vnstat.service /etc/systemd/system/

检查配置文件”/etc/systemd/system/vnstat.service“路径是否正确

ExecStart=/usr/local/sbin/vnstatd -n

查看版本

vnstat -v

命令以在开机时启动

systemctl enable vnstat

新建观察网卡/启动后台监控

vnstatd -d #或者 vnstat -i eth0,默认 eth0,可修改为机器网卡;
systemctl start vnstat

 要启动/停止/重新启动和控制 vnstart 服务,运行基于 systemd 的发行版(CentOS/RHEL 版本 7.x+)使用 systemctl 命令:

systemctl start vnstat
systemctl stop vnstat
systemctl restart vnstat
systemctl status vnstat

配置

初始化指定统计网卡 eth0,具体哪个可通过 ifconfig 查看,2.1+版没有-u参数

vnstat -i -u eth0

这里如果出现错误,找不到文件,是因为初始化数据库,忽略

Error: Unable to read database “/var/lib/vnstat/eth0”: No such file or directory
Info: -> A new database has been created.

编辑配置文件 “/etc/vnstat.conf”;vnstat 可以不使用 root 用户运行,亚马逊光帆默认的 centos 或者 admin 都可以指定为运行用户

Interface "eth0"  #设置默认网络接口 
DaemonUser "root"  #指定后台运行的用户和组
DaemonGroup "root" 

编辑服务文件 “/usr/lib/systemd/system/vnstat.service”;或者“/etc/systemd/system/vnstat.service

[Service]
User=root #修改指定运行用户

SSH 启动后台监控

vnstatd -d

SSH 设置开机运行

service vnstat start
chkconfig vnstat on

使用方法

查看数据:小时、天、周、月、实时数据、帮助

vnstat -h
vnstat -d
vnstat -w
vnstat -m 
vnstat -l
vnstat -?
图片[2] - #教程# – 安装Vnstat流量统计,查看AWS Lightsail流量使用情况(可分小时/周/日/月统计) - 云线路

rx 表示服务器下行总流量, tx 表示服务器上行总流量,total 是合计流量,avg.rate 是平均流量,estimated 是预估流量/月;

查错

journalctl -xe

[/etc/systemd/system/vnstat.service:27] Failed to parse protect system value, ignoring: strict

如果遇到的错误信息是关于vnstat.service文件中的一个警告。警告信息指出解析protect system值时出现错误,但系统将忽略该错误并继续执行。这个警告通常不会对vnstat服务的正常运行产生重大影响,您可以继续使用vnstat服务而不受影响。

解决办法为修改vnstat.service文件:如果更新软件包后仍然存在警告,您可以尝试手动修改vnstat.service文件。打开该文件并找到提到protect system的行,在该行之前添加一个#符号以注释掉该行

#ProtectSystem=strict

保存文件并退出后,重新加载vnstat服务,警告应该不再出现:

systemctl daemon-reload
systemctl restart vnstat

其他

vnstat 还提供基于 PHP 的 Web 界面来显示图形统计信息,需要 nginx 支持。

下载 vnstat php 代码

wget http://www.sqweek.com/sqweek/files/vnstat_php_frontend-1.5.1.tar.gz

在 Web 访问目录中解压缩下载的文件

tar xzf vnstat_php_frontend-1.5.1.tar.gz
mv vnstat_php_frontend-1.5.1 /home/wwwroot/www/

编辑 config.php 文件并设置参数

$language = \"en\";
$iface_list = array(\"eth0\", \"sixxs\");
$iface_title[\"eth0\"] = \"Public Interface\";
$vnstat_bin = \"/usr/bin/vnstat\";

这里还可以配和VPS 限定时间内超流量自动关机脚本来防止剑皇,省的被暴力了还不知道

结语

博主只是测试了下 web 页面并不推荐安装,查看个流量要神马页面,直接登录 SSH 输入个命令就能查看整那么麻烦干啥呢,是吧!

© 本站文章随意转载,但请注明出处!
THE END
点赞8 分享
评论 抢沙发
头像
务必使用真实的邮箱地址评论,虚假邮箱的评论将不通过审核及无回复。
提交
头像

昵称

取消
昵称表情代码图片

    暂无评论内容