搞了个XXX服务器以备不时之需,服务器流量是有限额的,所以花了点时间研究了研究记录使用量功能。现在不仅可以记录每个用户用了多少流量,还可以记录下来登录时间登陆IP(PPTP有效,L2TP不知为何采集不到)
步骤很简单,在/etc/ppp/ip-up文件最下边添加下边的脚本:
echo "****************************************************" >> /var/log/XXX-${1}.log echo "username: $PEERNAME" >> /var/log/XXX-${1}.log echo "clientIP: $6" >> /var/log/XXX-${1}.log echo "device: $1" >> /var/log/XXX-${1}.log echo "xxxIP: $4" >> /var/log/XXX-${1}.log echo "assignIP: $5" >> /var/log/XXX-${1}.log echo "logintime: `date -d today +%F_%T`" >> /var/log/XXX-${1}.log
在/etc/ppp/ip-down文件最下边添加下边的脚本:
echo "downtime: `date -d today +%F_%T`" >> /var/log/XXX-${1}.log echo "bytes sent: $BYTES_SENT B" >> /var/log/XXX-${1}.log echo "bytes received: $BYTES_RCVD B" >> /var/log/XXX-${1}.log sum_bytes=$(($BYTES_SENT+$BYTES_RCVD)) sum=`echo "scale=2;$sum_bytes/1024/1024"|bc` echo "bytes sum: $sum MB" >> /var/log/XXX-${1}.log ave=`echo "scale=2;$sum_bytes/1024/$CONNECT_TIME"|bc` echo "average speed: $ave KB/s" >> /var/log/XXX-${1}.log echo "connect time: $CONNECT_TIME S" >> /var/log/XXX-${1}.log echo "****************************************************" >> /var/log/XXX-${1}.log
注意这个代码为了计算平局网速用到了dc工具,可能需要提前安装:
sudo apt install bc
然后就能在/var/log/XXX-ppp0.log中看到xxx连接记录类似这样:
**************************************************** username: XXX clientIP: XXX.XXX.XXX.XXX device: ppp0 xxxIP: 172.16.0.1 assignIP: 172.16.0.10 logintime: 2018-06-10_17:11:20 downtime: 2018-06-10_17:11:21 bytes sent: 3272 B bytes received: 3626 B bytes sum: 0 MB average speed: 6.73 KB/s connect time: 1 S ****************************************************
如果有多个用户同时连接,那么可能会生成多个log文件,通过ppp*这个星号数字作区分。如果是L2TP连接的话clientIP可能为空,其他的正常,我也没搞清楚为啥,不重要不管了。
我这个系列的第二篇,网页监控XXX情况:
http://blog.shuspieler.com/761/
参考:
http://ju.outofmemory.cn/entry/197132