forked from hongwenjun/vps_setup
-
Notifications
You must be signed in to change notification settings - Fork 0
/
netlog.sh
executable file
·87 lines (65 loc) · 2.26 KB
/
netlog.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#!/usr/bin/env bash
####### 安装使用原理 本脚本实现自动安装################
# apt-get install vnstat
# ip addr 查看网卡名称是否是 eth0,如果是网卡名是 ens3 或者 venet0
# 编辑/etc/vnstat.conf 替换,重启vnstat服务(本脚本自动能自动修改网卡名)
# sed -i "s/eth0/ens3/g" /etc/vnstat.conf
# systemctl restart vnstat
# crontab -e 修改定时任务, 添加运行脚本
# # netlog.sh 定时执行转html脚本,每3小时一次,为了修改方便和多台机器用,直接到github更新
# * */3 * * * wget -qO- git.io/fxxlb | bash
######################################################
# 输出网络流量日志到html
output_html(){
# html 写文件位置
if [ ! -e '/etc/redhat-release' ]; then
INDEX_HTML=/var/www/html/index.html
mkdir -p /var/www/html/
else
INDEX_HTML=/usr/share/nginx/html/index.html
fi
echo '<!DOCTYPE html><meta charset=utf-8><pre>' > ${INDEX_HTML}
top -b | head -5 >> ${INDEX_HTML}
vnstat -u
vnstat -m >> ${INDEX_HTML}
vnstat -d >> ${INDEX_HTML}
vnstat -h >> ${INDEX_HTML}
echo ' ' >> ${INDEX_HTML}
echo ' netlog.sh Source code: https://git.io/fxxlb' >> ${INDEX_HTML}
}
# 安装 vnstat 添加定期运行
vnstat_install(){
# 判断系统 安装软件
if [ ! -e '/etc/redhat-release' ]; then
# debian 系安装
apt -y install vnstat nginx
else
# centos 系安装 vnstat nginx,如果web没法访问,需要关防火墙
yum -y install vnstat nginx
systemctl enable nginx
systemctl restart nginx
fi
# vps网卡如果不是eth0,修改成实际网卡
ni=$(ls /sys/class/net | awk {print} | grep -e eth. -e ens. -e venet.)
if [ $ni != "eth0" ]; then
sed -i "s/eth0/${ni}/g" /etc/vnstat.conf
fi
systemctl restart vnstat
# 设置定时运行脚本
crontab -l >> crontab.txt
echo "* */3 * * * wget -qO- git.io/fxxlb | bash" >> crontab.txt
crontab crontab.txt
sleep 2
if [ ! -e '/etc/redhat-release' ]; then
systemctl restart cron
else
systemctl restart crond
fi
rm -f crontab.txt
}
# 首次运行脚本需要安装
if [ ! -f '/usr/bin/vnstat' ]; then
vnstat_install
fi
# 输出网络流量信息到html文件
output_html