ehlxr / ServerStatus-Rust

✨ Rust 版 ServerStatus 云探针、云监控、支持 Railway 部署、支持 vnstat、tg、wx、email

Home Page:https://tz-rust.vercel.app

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

✨ Rust 版 ServerStatus 云探针

Docker Release GitHub release (latest SemVer) GitHub all releases

image

image

1. 介绍

cppla/ServerStatus 的威力加强版,保持轻量和简化部署,增加主要特性如下:

  • 使用 rust 完全重写 serverclient,单个执行文件部署
  • 支持上下线和简单自定义规则告警 (telegramwechatemail)
  • 支持 http 协议上报,可配合 cf 等优化上报链路
  • 支持 vnstat 统计月流量,重启不丢流量数据
  • 支持 railway 快速部署
  • 支持 systemd 开机自启
  • 其它功能,如 🗺️ 见 wiki

演示:tz-rust.vercel.app | 下载:Releases | 反馈:Discussions

🍀 主题

如果你觉得你修改的主题还不错,欢迎分享出来

Hotaru 主题

Hotaru 主题 @HinataKato 修改提供,主题地址

image

2. 安装部署

2.1 快速体验

# for CentOS/Debian/Ubuntu x86_64
mkdir -p /opt/ServerStatus && cd /opt/ServerStatus
# apt install -y unzip / yum install -y unzip
wget --no-check-certificate -qO one-touch.sh 'https://raw.githubusercontent.com/zdz/ServerStatus-Rust/master/one-touch.sh'
bash -ex one-touch.sh
# 部署完毕,打开 http://127.0.0.1:8080/ 或 http://<你的IP>:8080/
# 自定义部署可参照 one-touch.sh 脚本

2.2 服务管理脚本部署,感谢 @Colsro 提供

管理脚本使用说明
# 下载脚本
wget --no-check-certificate -qO status.sh 'https://raw.githubusercontent.com/zdz/ServerStatus-Rust/master/status.sh'

# 安装 服务端
bash status.sh -i -s

# 安装 客户端
bash status.sh -i -c
# or
bash status.sh -i -c protocol://username:password@master:port
# eg:
bash status.sh -i -c grpc://h1:p1@127.0.0.1:9394
bash status.sh -i -c http://h1:p1@127.0.0.1:8080

# 更多用法:
❯ bash status.sh

help:
    -i,--install    安装 Status
        -i -s           安装 Server
        -i -c           安装 Client
        -i -c conf      自动安装 Client
    -u,--uninstall  卸载 Status
        -u -s           卸载 Server
        -u -c           卸载 Client
    -r,--reset      更改 Status 配置
        -r          更改 Client 配置
        -r conf         自动更改 Client配置
    -s,--server     管理 Status 运行状态
        -s {start|stop|restart}
    -c,--client     管理 Client 运行状态
        -c {start|stop|restart}

若无法访问 Github:
    CN=true bash status.sh args
# 可能有点用

2.3 Railway 部署

懒得配置 NginxSSL 证书?试试 在 Railway 部署 Server 教程

Deploy on Railway

2.4 前后端分离部署

前后端分离部署
# 1. 参照前面先把 Server 部署好
# 2. 复制 web 目录,新建一个代码库,工程目录结构如下

~$ tree .
.
├── README.md
├── css
│   ├── bootstrap-theme.min.css
│   ├── bootstrap-theme.min.css.map
│   ├── bootstrap.min.css
│   ├── bootstrap.min.css.map
│   └── dark.css
├── favicon.ico
├── img
│   └── dark.png
├── index.html
├── js
│   ├── bootstrap.min.js
│   ├── jquery.min.js
│   └── serverstatus.js
├── netlify.toml
└── vercel.json

# 3. 在工程目录添加 vercel.json 文件来指定路由,指向你的 Server,内容如下

{
  "routes": [
    {
      "src": "/json/stats.json",
      "dest": "http://tz.xxx.com:8080/json/stats.json"
    },
    {
      "src": "/detail",
      "dest": "http://tz.xxx.com:8080/detail"
    },
    {
      "src": "/map",
      "dest": "http://tz.xxx.com:8080/map"
    }
  ]
}

# 4. 最后将这个静态站点部署到 vercel.com
# 部署教程参照 =》 google 搜索 "vercel.com 部署静态站点"

3. 服务端说明

3.1 配置文件 config.toml

# 侦听地址, ipv6 使用 [::]:9394
grpc_addr = "0.0.0.0:9394"
http_addr = "0.0.0.0:8080"
# 默认30s无上报判定下线
offline_threshold = 30

# 管理员账号,不设置默认随机生成,用于查看 /detail, /map
admin_user = ""
admin_pass = ""

# name 主机唯一标识,不可重复,alias 为展示名
# 使用 ansible 批量部署时可以用主机 hostname 作为 name,统一密码
# notify = false 单独禁止单台机器的告警,一般针对网络差,频繁上下线
# monthstart = 1 没启用 vnstat 时,表示月流量从每月哪天开始统计
# disabled = true 单机禁用,跟删除这条配置的效果一样
hosts = [
  {name = "h1", password = "p1", alias = "n1", location = "🏠", type = "kvm"},
  {name = "h2", password = "p2", alias = "n2", location = "🏢", type = "kvm", notify = true},
  {name = "h3", password = "p3", alias = "n3", location = "🏝️", type = "kvm", monthstart = 1},
  {name = "h4", password = "p4", alias = "n4", location = "🏢", type = "kvm", disabled = false},
]

# 不开启告警,可忽略后面配置,或者删除不需要的通知方式
# 告警间隔默认为30s
notify_interval = 30
# https://core.telegram.org/bots/api
# https://jinja.palletsprojects.com/en/3.0.x/templates/#if
[tgbot]
enabled = false
bot_token = "<tg bot token>"
chat_id = "<chat id>"
# host 可用字段参见 payload.rs 文件 HostStat 结构, {{host.xxx}} 为占位变量
# 例如 host.name 可替换为 host.alias,大家根据喜好来编写通知消息
title = "❗<b>Server Status</b>"
online_tpl  = "{{config.title}} \n😆 {{host.location}} 的 {{host.name}} 主机恢复上线啦"
offline_tpl = "{{config.title}} \n😱 {{host.location}} 的 {{host.name}} 主机已经掉线啦"
# custom 模板置空则停用自定义告警,只保留上下线通知
custom_tpl = """
{% if host.memory_used / host.memory_total > 0.5  %}
<pre>😲 {{host.name}} 主机内存使用率超50%, 当前{{ (100 * host.memory_used / host.memory_total) | round }}%  </pre>
{% endif %}

{% if host.hdd_used / host.hdd_total  > 0.5  %}
<pre>😲 {{host.name}} 主机硬盘使用率超50%, 当前{{ (100 * host.hdd_used / host.hdd_total) | round }}% </pre>
{% endif %}
"""

# wechat, email 等其它通知方式 配置详细见 config.toml

3.2 服务端运行

# systemd 方式, 参照 one-touch.sh 脚本 (推荐)

# 💪 手动方式
# help
./stat_server -h
# 手动运行
./stat_server -c config.toml
#
RUST_BACKTRACE=1 RUST_LOG=trace ./stat_server -c config.toml

# 测试配置文件是否有效
./stat_server -c config.toml -t
# 根据配置发送测试消息,验证通知是否生效
./stat_server -c config.toml --notify-test

# 🐳 docker 方式
wget --no-check-certificate -qO docker-compose.yml 'https://raw.githubusercontent.com/zdz/ServerStatus-Rust/master/docker-compose.yml'
wget --no-check-certificate -qO config.toml 'https://raw.githubusercontent.com/zdz/ServerStatus-Rust/master/config.toml'
touch stats.json
docker network create traefik_gw
docker-compose up -d

4. 客户端说明

4.1 Linux (CentOS, Ubuntu, Debian)

# 公网环境建议 nebula 组网或走 https, 使用 nginx 对 server 套 ssl 和自定义 location /report
# Rust 版只在 CentOS, Ubuntu, Debian 测试过
# 如果 Rust 版客户端在你的系统无法使用,请切换到下面 4.2 跨平台版本

# systemd 方式, 参照 one-touch.sh 脚本 (推荐)

# 💪 手动方式
# Rust 版本 Client
./stat_client -h
./stat_client -a "http://127.0.0.1:8080/report" -u h1 -p p1
#
./stat_client -a "grpc://127.0.0.1:9394" -u h1 -p p1

# rust client 可用参数
./stat_client -h
OPTIONS:
    -6, --ipv6             ipv6 only, default:false
    -a, --addr <ADDR>      [default: http://127.0.0.1:8080/report]
        --cm <CM_ADDR>     China Mobile probe addr [default: cm.tz.cloudcpp.com:80]
        --ct <CT_ADDR>     China Telecom probe addr [default: ct.tz.cloudcpp.com:80]
        --cu <CU_ADDR>     China Unicom probe addr [default: cu.tz.cloudcpp.com:80]
        --disable-extra    disable extra info report, default:false
        --disable-ping     disable ping, default:false
        --disable-tupd     disable t/u/p/d, default:false
    -h, --help             Print help information
        --ip-info          show ip info, default:false
        --json             use json protocol, default:false
    -n, --vnstat           enable vnstat, default:false
    -p, --pass <PASS>      password [default: p1]
    -u, --user <USER>      username [default: h1]
    -V, --version          Print version information

# 一些参数说明
--ip-info       # 显示本机ip信息后立即退出,目前使用 ip-api.com 数据
--disable-extra # 不上报系统信息和IP信息
--disable-ping  # 停用三网延时和丢包率探测
--disable-tupd  # 不上报 tcp/udp/进程数/线程数,减少CPU占用

4.2 跨平台版本 (Window, Linux, ...)

跨平台版本说明
# Python 版本 Client 依赖安装
## Centos
yum -y install epel-release
yum -y install python3-pip gcc python3-devel
python3 -m pip install psutil requests py-cpuinfo

## Ubuntu/Debian
apt -y install python3-pip
python3 -m pip install psutil requests py-cpuinfo

## Alpine linux
apk add wget python3 py3-pip gcc python3-dev musl-dev linux-headers
python3 -m pip install psutil requests py-cpuinfo

wget --no-check-certificate -qO stat_client.py 'https://raw.githubusercontent.com/zdz/ServerStatus-Rust/master/client/stat_client.py'

## Windows
# 安装 python 3.10 版本,并设置环境变量
# 命令行执行 pip install psutil requests
# 下载 https://raw.githubusercontent.com/zdz/ServerStatus-Rust/master/client/stat_client.py
pip install psutil requests py-cpuinfo

python3 stat_client.py -h
python3 stat_client.py -a "http://127.0.0.1:8080/report" -u h1 -p p1

5. 开启 vnstat 支持

vnstat 是Linux下一个流量统计工具,开启 vnstat 后,server 完全依赖客户机的 vnstat 数据来显示月流量和总流量,优点是重启不丢流量数据。

开启 vnstat 设置
# 在client端安装 vnstat
## Centos
sudo yum install epel-release -y
sudo yum install -y vnstat
## Ubuntu/Debian
sudo apt install -y vnstat

# 修改 /etc/vnstat.conf
# BandwidthDetection 0
# MaxBandwidth 0
# 默认不是 eth0 网口的需要置空 Interface 来自动选择网口
# 没报错一般不需要改
# Interface ""
systemctl restart vnstat

# 确保 version >= 2.6
vnstat --version
# 测试查看月流量 (刚安装可能需等一小段时间来采集数据)
vnstat -m
vnstat --json m

# server config.toml 开启 vnstat
# 从 v1.3.6 不再需要在 server 配置开启,client 自由选择启用与否,client 可部分打开,部分关闭
vnstat = true

# client 使用 -n 参数开启 vnstat 统计
./stat_client -a "grpc://127.0.0.1:9394" -u h1 -p p1 -n
#
python3 stat_client.py -a "http://127.0.0.1:8080/report" -u h1 -p p1 -n

6. FAQ

如何使用自定义主题
server {
  # ssl,domain 等其它配置

  # 反代 /report 请求
  location = /report {
    proxy_set_header Host              $host;
    proxy_set_header X-Real-IP         $remote_addr;
    proxy_set_header X-Forwarded-For   $proxy_add_x_forwarded_for;
    proxy_set_header X-Forwarded-Proto $scheme;
    proxy_set_header X-Forwarded-Host  $host;
    proxy_set_header X-Forwarded-Port  $server_port;

    proxy_pass http://127.0.0.1:8080/report;
  }
  # 反代 json 数据请求
  location = /json/stats.json {
    proxy_set_header Host              $host;
    proxy_set_header X-Real-IP         $remote_addr;
    proxy_set_header X-Forwarded-For   $proxy_add_x_forwarded_for;
    proxy_set_header X-Forwarded-Proto $scheme;
    proxy_set_header X-Forwarded-Host  $host;
    proxy_set_header X-Forwarded-Port  $server_port;

    proxy_pass http://127.0.0.1:8080/json/stats.json;
  }
  # v1.4.0后,同样需要反代  /detail, /map

  # 其它 html,js,css 等,走本地文本
  location / {
    root   /opt/ServerStatus/web; # 你自己修改的主题目录
    index  index.html index.htm;
  }
}
如何源码编译
# 按提示安装 rust 编译器
curl https://sh.rustup.rs -sSf | sh
yum install -y openssl-devel
git clone https://github.com/zdz/ServerStatus-Rust.git
cd ServerStatus-Rust
cargo build --release
# 编译好的文件目录 target/release
如何自定义 ping 地址
# 例如自定义移动探测地址,用 --cm 指定地址
./stat_client -a "grpc://127.0.0.1:9394" -u h1 -p p1 --cm=cm.tz.cloudcpp.com:80

# 电信联通参数可以使用 -h 命令查看
./stat_client -h
OPTIONS:
    --cm <CM_ADDR>    China Mobile probe addr [default: cm.tz.cloudcpp.com:80]
    --ct <CT_ADDR>    China Telecom probe addr [default: ct.tz.cloudcpp.com:80]
    --cu <CU_ADDR>    China Unicom probe addr [default: cu.tz.cloudcpp.com:80]
关于这个轮子

之前一直在使用 Prometheus + Grafana + Alertmanager + node_exporter 做VPS监控,这也是业界比较成熟的监控方案,用过一段时间后,发现非生产环境的话,很多监控指标都用不上,反而显得有些重。 而 ServerStatus 很好,足够简单和轻量,一眼可以看尽大好山河,只是 c++ 版本很久没迭代过,自己的一些需求在原版上不是很好修改,如自带 tcp 上报对跨区机器不是很友好,也不方便对上报的链路优化 等等。过年的时候正值疫情闲来无事,学习 Rust 正好需要个小项目练手,于是撸了个 ServerStatus 来练手,项目后面会继续维护但不会增加复杂的功能,保持小而美,简单部署,配合 Uptime Kuma 基本上可以满足个人大部分监控需求。

7. 相关项目

8. 最后

很高兴我的代码能跑在你的服务器上,如果对你有帮助的话,欢迎留下你的 star ⭐ 支持一下

About

✨ Rust 版 ServerStatus 云探针、云监控、支持 Railway 部署、支持 vnstat、tg、wx、email

https://tz-rust.vercel.app

License:Apache License 2.0


Languages

Language:Rust 59.4%Language:JavaScript 12.5%Language:Python 10.7%Language:Shell 7.2%Language:HTML 6.5%Language:CSS 3.3%Language:Dockerfile 0.5%