zdz / ServerStatus-Rust

✨ Rust 版 ServerStatus 探针、威力加强版
https://ssr.rs
Apache License 2.0
1.76k stars 320 forks source link

关于无法触发tg警告 #60

Closed lls275 closed 2 years ago

lls275 commented 2 years ago

我的配置文件上,配置了tg的信息,并开启了推送功能,但无法推送!

notify_interval = 30
# https://core.telegram.org/bots/api
# https://jinja.palletsprojects.com/en/3.0.x/templates/#if
[tgbot]
enabled = true
bot_token = "2xxxx67783:AAF4eCz9XMm-TSnI0xxxxxxxx7WYOEAgxqw"
chat_id = "88xxxx680"
# 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 %}
"""

但我通过 curl 方式区测试是正常的,能否告知我出了什么问题?

curl -X POST \
     -H 'Content-Type: application/json' \
     -d '{"chat_id": "88xxxx680", "text": "This is a test from curl", "disable_notification": true}' \
     https://api.telegram.org/bot2xxxx67783:AAF4eCz9XMm-TSnI0xxxxxxxx7WYOEAgxqw/sendMessage
zdz commented 2 years ago

用下面命令测试一下,貌似 chat_id 一般为负数

export RUST_LOG=trace
export RUST_BACKTRACE=1
# 根据配置发送测试消息,验证通知是否生效
./stat_server -c config.toml --notify-test
lls275 commented 2 years ago

用下面命令测试一下,貌似 chat_id 一般为负数

export RUST_LOG=trace
export RUST_BACKTRACE=1
# 根据配置发送测试消息,验证通知是否生效
./stat_server -c config.toml --notify-test

❗ServerStatus test msg

按照您所说,可以看到一条消息,不过主机下线就没法警告了

lls275 commented 2 years ago

用下面命令测试一下,貌似 chat_id 一般为负数

export RUST_LOG=trace
export RUST_BACKTRACE=1
# 根据配置发送测试消息,验证通知是否生效
./stat_server -c config.toml --notify-test

按照您这些命令测试,是可以收到一条 ❗ServerStatus test msg的消息的。不过当主机下线时,面板可以看到主机是红色的,但是就是没法推送到tg上。

zdz commented 2 years ago

我晚点瞧瞧

lls275 commented 2 years ago

我晚点瞧瞧

好的,我是根据源码进行编译的。不知是不是源码有问题。我拉取的是最新的源码来编译的

zdz commented 2 years ago

跑一下 systemctl status stat_server -l 帮忙把输出日志贴一下

lls275 commented 2 years ago

systemctl status stat_server -l

● stat_server.service - ServerStatus-Rust Server Loaded: loaded (/etc/systemd/system/stat_server.service; disabled; vendor preset: enabled) Active: active (running) since Fri 2022-07-01 03:20:21 UTC; 56s ago Main PID: 1265376 (stat_server) Tasks: 5 (limit: 1066) Memory: 2.3M CGroup: /system.slice/stat_server.service └─1265376 /opt/ServerStatus/stat_server -c /opt/ServerStatus/config.toml

Jul 01 03:20:21 vultr systemd[1]: Started ServerStatus-Rust Server. Jul 01 03:20:21 vultr stat_server[1265376]: ✨ stat_server v1.5.2 GIT:a6aa0f3, BUILD:2022-07-01 02:38:03 UTC Jul 01 03:20:21 vultr stat_server[1265376]: ✨ run in normal mode, load conf from local file `/opt/ServerStatus/config.toml Jul 01 03:20:21 vultr stat_server[1265376]: ✨ admin_user: admin Jul 01 03:20:21 vultr stat_server[1265376]: ✨ admin_pass: xxxxxxxxxxxxxxxxxxxxxxxxxxxxx Jul 01 03:20:21 vultr stat_server[1265376]: 🚀 listening on http://0.0.0.0:8080 Jul 01 03:20:21 vultr stat_server[1265376]: 🚀 listening on grpc://0.0.0.0:9394

zdz commented 2 years ago

试试 https://github.com/zdz/ServerStatus-Rust/releases/tag/v1.5.3

lls275 commented 2 years ago

试试 https://github.com/zdz/ServerStatus-Rust/releases/tag/v1.5.3

非常棒!c彻底解决了。

不过我想请教一个问题,如果将url指向 /admin/status 代码怎么修改? 我似乎修改了/root/ServerStatus-Rust/server/src/main.rs的req_path,但是前端的css、js跟随者/admin/status一起加上,导致无法加载,这个该怎么修改?

async fn main_service_func(req: Request<Body>) -> Result<Response<Body>> {
    let req_path = req.uri().path();
    match (req.method(), req_path) {
        (&Method::POST, "/report") => stats_report(req).await,
        (&Method::GET, "/json/stats.json") => get_stats_json(req).await,
        (&Method::GET, "/detail") => http::get_detail(req).await,
        (&Method::GET, "/detail_ht") => http::render_jinja_ht_tpl("detail_ht", req).await,
        (&Method::GET, "/map") => http::render_jinja_ht_tpl("map", req).await,
        (&Method::GET, "/i") => http::init_client(req).await,
        (&Method::GET, "/admin/status") | (&Method::GET, "/index.html") => {            << == 修改了req_path 
            if !http::is_admin(&req) {
                return Ok(Response::builder()
                    .header(header::WWW_AUTHENTICATE, "Basic realm=\"Restricted\"")
                    .status(StatusCode::UNAUTHORIZED)
                    .body(UNAUTHORIZED.into())?);
            }
            let body = Body::from(Asset::get("/index.html").unwrap().data);
            Ok(Response::builder()
                .header(header::CONTENT_TYPE, "text/html; charset=utf-8")
                .body(body)?)
        }
        _ => {
            if req.method() == Method::GET
                && (req_path.starts_with("/js/")
                    || req_path.starts_with("/css/")
                    || req_path.starts_with("/img/")
                    || req_path.eq("/config.js")
                    || req_path.eq("/favicon.ico"))
            {
                if let Some(data) = Asset::get(req_path) {
                    let ct = mime_guess::from_path(req_path);
                    return Ok(Response::builder()
                        .header(header::CONTENT_TYPE, ct.first_raw().unwrap())
                        .body(Body::from(data.data))?);
                } else {
                    error!("can't get => {:?}", req_path);
                }
            }

            // Return 404 not found response.
            Ok(Response::builder()
                .status(StatusCode::NOT_FOUND)
                .body(NOTFOUND.into())?)
        }

前端的css请求地址却变成了:https://www.xxx.com/admin/css/semantic.min.css,导致无法获取到css。 我对rust不熟悉,不知怎么修改

zdz commented 2 years ago

不用修改源码,用 nginx 配置重写一下 route 指向就行,或者 参考 https://github.com/zdz/ServerStatus-Rust/discussions/37

lls275 commented 2 years ago

不用修改源码,用 nginx 配置重写一下 route 指向就行,或者 参考 #37

好的,已经配置好了。感谢作者