n/chit-chatu/stevenlee · 2024-07-04
注册 登录
n/chit-chat · 2024-07-04 · 470 次浏览
u/stevenlee白银

自己部署一个VPS流量监控Surge面板

步骤

SSH 登录到 VPS

获取权限
~~</s>sudo -i<e>

###~~ 更新和升级系统

~~</s>apt update &amp;&amp; apt upgrade -y<e>

安装 caddy

Caddy 的 GPG 密钥添加到 trusted.gpg.d 目录中
</s>curl -1sLf 'https://dl.cloudsmith.io/public/caddy/stable/gpg.key' | sudo tee /etc/apt/trusted.gpg.d/caddy-stable.asc &gt;/dev/null<e>

####~~ 添加 Caddy 的 apt 仓库

~~</s>echo "deb [trusted=yes] https://dl.cloudsmith.io/public/caddy/stable/deb/debian any-version main" | sudo tee /etc/apt/sources.list.d/caddy-stable.list<e>

####~~ apt 软件源列表并安装 Caddy

```
apt update
apt install caddy 
```

### 编写服务

~~</s>vim /etc/systemd/system/traffic.service<e>
将下面内容粘贴进去后保存

[Unit]
Description=traffic
After=network.target
 
[Service]
Type=simple
User=root
WorkingDirectory=/root/
ExecStart=/root/traffic.sh
Restart=on-failure
RestartSec=30s
 
[Install]
WantedBy=multi-user.target
<e>```</e>
备注:默认的是网卡名是 eth0,如果是其它网卡名,将 ExecStart=/root/traffic.sh这里改写为:ExecStart=/root/traffic.sh ethxx ethxx为你的网卡名


~~###~~ 编写运行程序

<C>~~```</s>vim /root/traffice.sh<e>```</e></C>


将以下内容粘贴进去并保存

```#!/bin/sh~~
 
if ["$1" == "--help"];then
cat &lt;&lt; EOF
$0 网卡名称
--help 打印帮助菜单
EOF
fi
 
if [-z "$1"];then
  if ip a ; then
  interface=$(ip a | grep mtu | awk -F ':' '{print $2}' | head -n 2 | tail -n +2 | awk -F '''{print $1}')
  else
  interface=eth0
  fi
else
  interface=$1
fi
 
if ["$(cat /proc/uptime | awk'{print $1}'| sed's/\..*//g')" -lt "120" ]; then
  if [-n "$(cat ./all)" ]; then
    expr "$(cat ./all)" + "$(cat ./all-now)" &gt; ./all
  else
    echo "1" &gt; ./all
  fi
  if [-n "$(cat ./tx)" ]; then
    expr "$(cat ./tx)" + "$(cat ./tx-now)" &gt; ./tx
  else
    echo "1" &gt; ./tx
  fi
  if [-n "$(cat ./rx)" ]; then
    expr "$(cat ./rx)" + "$(cat ./rx-now)" &gt; ./rx
  else
    echo "1" &gt; ./rx
  fi
else
  if [-z "$(cat ./all)" ]; then
    echo "1" &gt; ./all
  fi
  if [-z "$(cat ./tx)" ]; then
    echo "1" &gt; ./tx
  fi
  if [-z "$(cat ./rx)" ]; then
    echo "1" &gt; ./rx
  fi
fi
 
nohup caddy file-server --browse --listen :49155 &amp;
 
calculate() {
str=&lt;code&gt;expr $str + 2&lt;/code&gt;
str=&lt;code&gt;expr $str / 4 &lt;/code&gt;
if [$str = 0]; then
value="$info"B
elif [$str = 1]; then
value=&lt;code&gt;expr $info / 1024&lt;/code&gt;KB
elif [$str = 2]; then
value=&lt;code&gt;expr $info / 1024 / 1024&lt;/code&gt;MB
elif [$str = 3]; then
value=&lt;code&gt;expr $info / 1024 / 1024 / 1024&lt;/code&gt;GB
elif [$str = 4]; then
value=&lt;code&gt;expr $info / 1024 / 1024 / 1024 / 1024&lt;/code&gt;TB
elif [$str = 5]; then
value=&lt;code&gt;expr $info / 1024 / 1024 / 1024 / 1024 / 1024&lt;/code&gt;PB
fi
}
 
START_TIME=$(date +%s)
 
while true; do
  # 记录执行时间
  CURRENT_TIME=$(date +%s)
  TIME_PASSED=$((CURRENT_TIME - START_TIME))
 
  NIC_RX=$(cat "/sys/class/net/${interface}/statistics/rx_bytes")
  NIC_TX=$(cat "/sys/class/net/${interface}/statistics/tx_bytes")
  NIC=$(expr $NIC_RX + $NIC_TX)
  echo ${NIC} &gt; ./all-now
  echo ${NIC_TX} &gt; ./tx-now
  echo ${NIC_RX} &gt; ./rx-now
  rx=$(cat ./rx)
  tx=$(cat ./tx)
  all=$(cat ./all)
  NIC_RX_ALL=$(expr ${NIC_RX} + ${rx})
  NIC_TX_ALL=$(expr ${NIC_TX} + ${tx})
  NIC_ALL=$(expr ${NIC} + ${all})
  str=${#NIC_RX_ALL} &amp;&amp; info=${NIC_RX_ALL} &amp;&amp; calculate &amp;&amp; NIC_RX_ALL=$value
  str=${#NIC_TX_ALL} &amp;&amp; info=${NIC_TX_ALL} &amp;&amp; calculate &amp;&amp; NIC_TX_ALL=$value
  str=${#NIC_ALL} &amp;&amp; info=${NIC_ALL} &amp;&amp; calculate &amp;&amp; NIC_ALL=$value
 
  CPU_USAGE=$(top -b -n 1 | grep Cpu | awk '{print $2}' | cut -f 1 -d "%" | sed 's/\..*//g')
  CPU_SYS=$(top -b -n 1 | grep Cpu | awk '{print $4}' | cut -f 1 -d "%" | sed 's/\..*//g')
  CPU=$(expr $CPU_USAGE + $CPU_SYS)
 
  MEM_TOTAL=$(free -m | awk -F '[ :]+' 'NR==2{print $2}')
  MEM_USER=$(free -m | awk -F '[ :]+' 'NR==2{print $3}')
  MEM=$(expr $MEM_USER \* 100 / $MEM_TOTAL)
 
  clear
  echo "网卡流量监控"
  echo "----------------------------------------"
  echo "网卡: $interface"
  echo "发送: ${NIC_TX_ALL}  接收: ${NIC_RX_ALL}  总流量: ${NIC_ALL}"
  echo "CPU 使用率:${CPU}%  内存使用率: ${MEM}%"
  echo "{" &gt; ./traffic
  echo "\"in\": \"${NIC_RX_ALL}\","  &gt;&gt; ./traffic
  echo "\"out\": \"${NIC_TX_ALL}\"," &gt;&gt; ./traffic
  echo "\"all\": \"${NIC_ALL}\"," &gt;&gt; ./traffic
  echo "\"cpu\": \"${CPU}%\"," &gt;&gt; ./traffic
  echo "\"mem\": \"${MEM}%\"," &gt;&gt; ./traffic
  echo "\"last_exec_time\": \"$(date '+%Y-%m-%d %H:%M:%S')\"" &gt;&gt; ./traffic
  echo "}" &gt;&gt; ./traffic
 
  # 休眠 10 秒钟
  sleep 10
done
<e>```</e>
说明:此代码是通过 VPS 的 49155 端口进行监控,请确保该端口的开放,如果你熟悉代码,也可以根据自己需要进行修改


运行


进行完上述步骤后,执行下面指令运行


<C>~~`</s>systemctl enable --now traffic<e>`</e></C>


可以通过 <C>`</s>bash traffic.sh<e>`</e></C> 来直接运行
通过 <C>`</s>systemctl status traffic<e>`</e></C> 来查看服务状态
如果发现出来的时间不对,可以通过 <C>`</s>timedatectl set-timezone Asia/Shanghai<e>`</e></C> 来将 vps 时区调整为东八区。


Surge 模块安装


将下面内容复制到本地模块中:

```~~
#!name=CatVPS
#!desc= 监控 VPS 流量信息和处理器、内存占用情况
#!author= 面板和脚本部分 @wuhu_zzz VPS 端部分 @ATRI0828 由 @整点猫咪 进行整理
#!howto= 将模块内容复制到本地后根据自己 VPS IP 地址及端口修改 http://127.0.0.1:49155/traffic 部分进行使用 ddl= 后面接你的 VPS 到期时间,total= 输入你的 VPS 每月流量数目
 
[Panel]
Cat VPS = script-name=CatVPS
 
[Script]
CatVPS = type=generic,script-path=https://raw.githubusercontent.com/getsomecat/GetSomeCats/Surge/script/CatVPS.js, argument = url=http://127.0.0.1:49155/traffic&amp;title=Cat VPS&amp;icon=bolt.horizontal.icloud.fill&amp;low=#06D6A0&amp;mid=#FFD166&amp;high=#EF476F&amp;ddl=2100-01-01&amp;total=10TB
<e>```</e>
将其中的 <URL url="http://127.0.0.1:49155/traffic%E9%83%A8%E5%88%86%E6%A0%B9%E6%8D%AE%E8%87%AA%E5%B7%B1%E4%B8%8A%E9%9D%A2%E6%95%99%E7%A8%8B%E9%83%A8%E5%88%86%E6%94%B9%E4%B8%BA%E8%87%AA%E5%B7%B1%E7%9A%84">http://127.0.0.1:49155/traffic部分根据自己上面教程部分改为自己的</URL> VPS IP 和端口即可使用。


&#128226; 群聊:@stevenlee1036
&#127880; 订阅:@stevenlee1009
&#129302; 互推:@stevenlee_bot


不定期推送有用有趣的内容,包括但不限于:软件 脚本 工具 网站 科技新闻。
</r>
0
13
登录后参与讨论。

13 条回复

排序
  1. fengzai 荣誉版主 王者 2024-07-04

    代码块可以用</s>'''<e>括起来会更好阅读哦

    0
  2. stevenlee 白银 楼主 2024-07-04

    @"丰仔"#p42524 我不会,下次发研究下,都是玩TG频道,第一次玩论坛!

    0
  3. fengzai 荣誉版主 王者 2024-07-04

    @"stevenlee"#p42527 没事帮你改了下

    0
  4. stevenlee 白银 楼主 2024-07-04

    @"丰仔"#p42533 多谢啦,学习啦!

    0
  5. stevenlee 白银 楼主 2024-07-04

    @"stevenlee"#p42534 谢谢佬!

    0
  6. TUGOSAN 白银 2024-07-04

    ● traffic.service - traffic
    Loaded: loaded (/etc/systemd/system/traffic.service; enabled; vendor preset: enabled)
    Active: activating (auto-restart) (Result: exit-code) since Thu 2024-07-04 13:58:48 HKT; 27s ago
    Process: 18922 ExecStart=/root/traffic.sh (code=exited, status=203/EXEC)
    Main PID: 18922 (code=exited, status=203/EXEC)
    CPU: 1ms
    一直报错
    bash: traffic.sh: No such file or directory
    都是按流程来的 这个traffic.sh也有

    0
  7. TUGOSAN 白银 2024-07-04

    知道 多了个e

    0
  8. webcloud 白银 2024-07-04

    效果图怎么样

    0
  9. Googol 白银 2024-07-06

    有没有免费的 VPS?

    0
  10. stevenlee 白银 楼主 2024-07-06

    @"Googol"#p43117 谷歌云,亚马逊,甲骨文,都可以撸啊!

    0
  11. xjfkkk 白银 2024-07-09

    佬发个图

    0
  12. sakujo 荣誉会员 钻石 2024-07-09

    多谢!看上去不错

    0
  13. Googol 白银 2024-07-09

    @"stevenlee"#p43128 都要信用卡,我没有

    0