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

2,837次阅读
58 条评论

共计 4154 个字符,预计需要花费 11 分钟才能阅读完成。

步骤

  • SSH 登录到 VPS

  • 获取权限

sudo -i

  • ### 更新和升级系统

apt update && apt upgrade -y

  • 安装 caddy

  • Caddy 的 GPG 密钥添加到 trusted.gpg.d 目录中

curl -1sLf 'https://dl.cloudsmith.io/public/caddy/stable/gpg.key' | sudo tee /etc/apt/trusted.gpg.d/caddy-stable.asc >/dev/null
  • #### 添加 Caddy 的 apt 仓库
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
  • #### apt 软件源列表并安装 Caddy
apt update

apt install caddy
  • ### 编写服务

vim /etc/systemd/system/traffic.service

将下面内容粘贴进去后保存

[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

备注:默认的是网卡名是 eth0,如果是其它网卡名,将 ExecStart=/root/traffic.sh这里改写为:ExecStart=/root/traffic.sh ethxx ethxx为你的网卡名

  • ### 编写运行程序

vim /root/traffice.sh

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

#!/bin/sh

if ["$1" == "--help"];then
  cat << 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)" > ./all
  else
    echo "1" > ./all
  fi
  if [-n "$(cat ./tx)" ]; then
    expr "$(cat ./tx)" + "$(cat ./tx-now)" > ./tx
  else
    echo "1" > ./tx
  fi
  if [-n "$(cat ./rx)" ]; then
    expr "$(cat ./rx)" + "$(cat ./rx-now)" > ./rx
  else
    echo "1" > ./rx
  fi
else
  if [-z "$(cat ./all)" ]; then
    echo "1" > ./all
  fi
  if [-z "$(cat ./tx)" ]; then
    echo "1" > ./tx
  fi
  if [-z "$(cat ./rx)" ]; then
    echo "1" > ./rx
  fi
fi

nohup caddy file-server --browse --listen :49155 &

calculate() {
str=<code>expr $str + 2</code>
str=<code>expr $str / 4 </code>
if [$str = 0]; then
value="$info"B
elif [$str = 1]; then
value=<code>expr $info / 1024</code>KB
elif [$str = 2]; then
value=<code>expr $info / 1024 / 1024</code>MB
elif [$str = 3]; then
value=<code>expr $info / 1024 / 1024 / 1024</code>GB
elif [$str = 4]; then
value=<code>expr $info / 1024 / 1024 / 1024 / 1024</code>TB
elif [$str = 5]; then
value=<code>expr $info / 1024 / 1024 / 1024 / 1024 / 1024</code>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} > ./all-now
  echo ${NIC_TX} > ./tx-now
  echo ${NIC_RX} > ./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} && info=${NIC_RX_ALL} && calculate && NIC_RX_ALL=$value
  str=${#NIC_TX_ALL} && info=${NIC_TX_ALL} && calculate && NIC_TX_ALL=$value
  str=${#NIC_ALL} && info=${NIC_ALL} && calculate && 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 "{" > ./traffic
  echo "\"in\": \"${NIC_RX_ALL}\","  >> ./traffic
  echo "\"out\": \"${NIC_TX_ALL}\"," >> ./traffic
  echo "\"all\": \"${NIC_ALL}\"," >> ./traffic
  echo "\"cpu\": \"${CPU}%\"," >> ./traffic
  echo "\"mem\": \"${MEM}%\"," >> ./traffic
  echo "\"last_exec_time\": \"$(date '+%Y-%m-%d %H:%M:%S')\"" >> ./traffic
  echo "}" >> ./traffic

  # 休眠 10 秒钟
  sleep 10
done

说明:此代码是通过 VPS 的 49155 端口进行监控,请确保该端口的开放,如果你熟悉代码,也可以根据自己需要进行修改

  • 运行

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

systemctl enable --now traffic

可以通过 bash traffic.sh 来直接运行
通过 systemctl status traffic 来查看服务状态
如果发现出来的时间不对,可以通过 timedatectl set-timezone Asia/Shanghai 来将 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&title=Cat VPS&icon=bolt.horizontal.icloud.fill&low=#06D6A0&mid=#FFD166&high=#EF476F&ddl=2100-01-01&total=10TB

将其中的 http://127.0.0.1:49155/traffic部分根据自己上面教程部分改为自己的 VPS IP 和端口即可使用。

 

更多资源请 点击这里 关注 TG 频道

正文完
 
lucky
版权声明:本站原创文章,由 lucky 2023-07-02发表,共计4154字。
转载说明:转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。
评论(58 条评论)
2024-04-19 18:18:53 回复

Your point of view caught my eye and was very interesting. Thanks. I have a question for you.

 Windows  Edge  美国加利福尼亚旧金山
2024-07-19 19:23:21 回复

Thanks for sharing. I read many of your blog posts, cool, your blog is very good.

 Windows  Edge  美国加利福尼亚旧金山
2024-08-05 11:56:59 回复

purchase lasuna pill – cheap diarex generic buy generic himcolin over the counter

 Windows  Chrome  美国加利福尼亚旧金山
2024-08-19 15:05:58 回复

besifloxacin generic – besifloxacin cost how to buy sildamax

 Windows  Chrome  美国佛罗里达迈阿密
2024-08-21 16:10:25 回复

buy neurontin online cheap – buy azulfidine generic azulfidine 500mg cost

 Windows  Chrome  美国加利福尼亚旧金山
2024-08-24 08:56:35 回复

order probalan for sale – buy generic etodolac 600 mg tegretol 400mg pills

 Windows  Chrome  美国加利福尼亚旧金山
2024-08-27 08:06:33 回复

buy celebrex 200mg online cheap – order indocin 75mg online cheap order indocin for sale

 Windows  Chrome  俄罗斯莫斯科莫斯科
2024-08-28 17:15:40 回复

buy generic colospa – etoricoxib over the counter order cilostazol 100 mg

 Windows  Chrome  美国加利福尼亚旧金山
2024-08-31 09:56:33 回复

Ratko TA, Detrisac CJ, Rao KV priligy uk

 Windows  Chrome  美国加利福尼亚旧金山
2024-09-01 05:46:16 回复

buy diclofenac without a prescription – order voltaren for sale aspirin 75 mg over the counter

 Windows  Chrome  美国加利福尼亚旧金山
2024-09-06 11:43:07 回复

purchase rumalaya online cheap – oral endep buy elavil generic

 Windows  Chrome  美国加利福尼亚旧金山
2024-09-09 02:40:16 回复

buy pyridostigmine pills – imuran 50mg canada imuran 25mg price

 Windows  Chrome  美国加利福尼亚旧金山
2024-09-13 08:39:32 回复

buy voveran tablets – nimotop pills nimodipine cheap

 Windows  Chrome  巴西圣保罗圣保罗
2024-09-14 23:41:22 回复

baclofen 10mg ca – order ozobax online oral piroxicam 20mg

 Windows  Chrome  俄罗斯列宁格勒圣彼得堡
2024-09-18 13:52:20 回复

casibom 158 giris: casibom 158 giris – casibom giris
casibom guncel giris adresi

 Windows  Chrome  埃及
2024-09-19 09:00:08 回复

purchase meloxicam for sale – buy mobic cheap buy toradol for sale

 Windows  Chrome  美国佛罗里达迈阿密
2024-09-20 09:48:58 回复

periactin order – periactin 4mg uk zanaflex sale

 Windows  Chrome  俄罗斯列宁格勒圣彼得堡
NormanERAKS
2024-09-21 23:04:38 回复

farmacia barata: farmacia online envio gratis valencia – farmacia barata

 Windows  Chrome  美国加利福尼亚旧金山
NormanERAKS
2024-09-22 13:53:48 回复

farmacia online envГ­o gratis: Cialis sin receta – farmacias online seguras en espaГ±a

 Windows  Chrome  意大利米兰米兰
NormanERAKS
2024-09-23 04:41:57 回复

farmacia online barata y fiable: Cialis sin receta – farmacias online baratas

 Windows  Chrome  美国加利福尼亚旧金山
Edisonnus
2024-09-24 00:11:49 回复

Farmacia online piГ№ conveniente: Farmacia online migliore – farmacia online piГ№ conveniente

 Windows  Chrome  美国加利福尼亚旧金山
Edisonnus
2024-09-24 07:12:25 回复

Farmacia online piГ№ conveniente: Cialis generico recensioni – migliori farmacie online 2024

 Windows  Chrome  埃及
Edisonnus
2024-09-24 14:26:42 回复

viagra acquisto in contrassegno in italia: acquisto viagra – viagra generico prezzo piГ№ basso

 Windows  Chrome  埃及
2024-09-25 00:58:00 回复

artane over the counter – order trihexyphenidyl without prescription order cheap voltaren gel

 Windows  Chrome  巴西圣保罗圣保罗
PatrickGroms
2024-09-25 06:47:50 回复

п»їFarmacia online migliore: BRUFEN 600 bustine prezzo – farmacie online autorizzate elenco

 Windows  Chrome  埃及
PatrickGroms
2024-09-26 07:12:28 回复

farmacie online sicure: Ibuprofene 600 generico prezzo – farmacia online

 Windows  Chrome  美国加利福尼亚旧金山
2024-09-26 12:23:54 回复

how to get cefdinir without a prescription – cleocin cheap order cleocin

 Windows  Chrome  美国加利福尼亚旧金山
PatrickGroms
2024-09-27 00:31:00 回复

farmacie online affidabili: Farmacie on line spedizione gratuita – Farmacie on line spedizione gratuita

 Windows  Chrome  荷兰阿姆斯特丹阿姆斯特丹
2024-09-27 09:28:17 回复

I don’t think the title of your article matches the content lol. Just kidding, mainly because I had some doubts after reading the article.

 Windows  Edge  美国加利福尼亚旧金山
ArnoldnaH
2024-09-28 10:36:04 回复

prednisone 15 mg daily: 50 mg prednisone canada pharmacy – buy prednisone no prescription

 Windows  Chrome  美国加利福尼亚旧金山
Josephtiz
2024-09-29 06:04:23 回复

buy lasix online: lasix medication – lasix furosemide 40 mg

 Windows  Chrome  美国加利福尼亚旧金山
Josephtiz
2024-09-29 17:49:26 回复

ventolin 200 mcg: Ventolin inhaler – ventolin brand

 Windows  Chrome  意大利米兰米兰
2024-09-30 08:58:33 回复

isotretinoin uk – isotretinoin 20mg uk buy deltasone 10mg online

 Windows  Chrome  美国加利福尼亚旧金山
TimothyTrode
2024-09-30 13:00:07 回复

п»їbest mexican online pharmacies: medication from mexico – best online pharmacies in mexico

 Windows  Chrome  埃及
TimothyTrode
2024-10-01 03:44:26 回复

Online medicine home delivery: Indian pharmacy international shipping – top online pharmacy india

 Windows  Chrome  美国加利福尼亚旧金山
2024-10-02 20:12:32 回复

deltasone order – buy generic omnacortil permethrin online

 Windows  Chrome  德国法兰克福法兰克福
2024-10-05 18:06:33 回复

order permethrin online – order acticin sale buy retin for sale

 Windows  Chrome  美国加利福尼亚旧金山
2024-10-08 11:30:12 回复

betnovate 20gm without prescription – buy monobenzone generic purchase monobenzone sale

 Windows  Chrome  俄罗斯列宁格勒圣彼得堡
2024-10-10 14:57:26 回复

buy metronidazole 400mg for sale – flagyl pill buy cenforce 100mg sale

 Windows  Chrome  俄罗斯列宁格勒圣彼得堡
2024-10-14 03:59:27 回复

order amoxiclav online – augmentin 1000mg without prescription levothyroxine order online

 Windows  Chrome  美国加利福尼亚旧金山
2024-10-17 14:45:16 回复

clindamycin over the counter – order generic indocin 50mg indomethacin without prescription

 Windows  Chrome  俄罗斯列宁格勒圣彼得堡
2024-10-19 02:39:12 回复

hyzaar cheap – losartan price buy generic keflex 250mg

 Windows  Chrome  美国佛罗里达迈阿密
2024-10-22 12:01:09 回复

order eurax – aczone order buy aczone without a prescription

 Windows  Chrome  巴西圣保罗圣保罗
2024-10-24 13:06:43 回复

generic modafinil – order phenergan pills melatonin brand

 Windows  Chrome  美国加利福尼亚旧金山
2024-10-27 02:14:35 回复

zyban 150 mg cheap – order xenical 120mg online cheap buy cheap generic shuddha guggulu

 Windows  Chrome  美国加利福尼亚旧金山
2024-10-30 13:40:49 回复

xeloda 500 mg usa – buy ponstel generic danazol 100mg brand

 Windows  Chrome  美国加利福尼亚旧金山
2024-10-31 21:05:19 回复

progesterone ca – order prometrium 200mg without prescription buy cheap clomiphene

 Windows  Chrome  美国加利福尼亚旧金山
2024-11-04 11:32:18 回复

Your point of view caught my eye and was very interesting. Thanks. I have a question for you.

 Windows  Edge  美国加利福尼亚旧金山
2024-11-05 01:16:55 回复

order fosamax 70mg generic – order medroxyprogesterone 5mg without prescription provera 5mg without prescription

 Windows  Chrome  美国加利福尼亚旧金山
2024-11-06 10:27:29 回复

aygestin drug – lumigan allergy spray yasmin tablet

 Windows  Chrome  美国加利福尼亚旧金山

Your point of view caught my eye and was very interesting. Thanks. I have a question for you.

 Windows  Edge  美国加利福尼亚旧金山
2024-11-09 18:06:25 回复

Above average consumption of foods high in vitamins C and E, beta carotene and zinc was associated with a 35 percent lower risk of the disease, compared with below average consumption of at least one of these antioxidants buy priligy generic Anti apoptotic Bcl x L, Bcl 2, and Bcl w are collectively dispensable for sustained in vitro survival of AML

 Windows  Chrome  美国加利福尼亚旧金山
2024-11-12 06:44:21 回复

estradiol without prescription – order letrozole pills where to buy anastrozole without a prescription

 Windows  Chrome  美国加利福尼亚旧金山
2024-11-12 11:40:38 回复

oral dostinex – alesse buy online purchase alesse without prescription

 Windows  Chrome  美国新泽西
2024-11-16 18:41:25 回复

They tell you to remove the patch in 5 hours priligy side effects We conducted a phase 2 double blind randomized controlled outpatient trial of favipiravir in asymptomatic or mildly symptomatic adults with a positive SARS CoV2 RT PCR within 72 hours of enrollment

 Windows  Chrome  美国宾夕法尼亚
2024-11-18 17:23:15 回复

バイアグラの購入 – バイアグラ гЃЇйЂљиІ©гЃ§гЃ®иіј タダラフィル処方

 Windows  Chrome  美国加利福尼亚旧金山
2024-11-19 03:07:32 回复

гѓ—гѓ¬гѓ‰гѓ‹гѓі и–¬е±ЂгЃ§иІ·гЃ€г‚‹ – г‚ўгѓўг‚­г‚·гѓ«йЊ  250mg еј·гЃ• アジスロマイシンジェネリック йЂљиІ©

 Windows  Chrome  英国伦敦伦敦

Can you be more specific about the content of your article? After reading it, I still have some doubts. Hope you can help me.

 Windows  Edge  美国加利福尼亚旧金山