1
0

netlog.sh 2.4 KB

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