Skip to content

Commit

Permalink
.
Browse files Browse the repository at this point in the history
  • Loading branch information
zdz committed Mar 5, 2022
1 parent 7bd33bd commit 8737a9b
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 26 deletions.
13 changes: 7 additions & 6 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,21 +1,22 @@
FROM rust:1-alpine3.15 as builder
# This is important, see https://github.com/rust-lang/docker-rust/issues/85
ENV RUSTFLAGS="-C target-feature=-crt-static" \
APP_TARGET=/app/target/release/stat_server

RUN apk add --no-cache musl-dev git
ENV RUSTFLAGS="-C target-feature=-crt-static"

WORKDIR /app
COPY ./ /app

RUN apk add --no-cache musl-dev git
RUN cargo build --release --bin stat_server
RUN strip /app/target/release/stat_server
# RUN strip /app/target/release/stat_client

FROM alpine:3.15 as production
LABEL version="1.0.0" \
description="A simple server monitoring tool" \
by="Doge" \
maintainer="doge.py@gmail.com"

RUN apk add --no-cache libgcc
COPY --from=builder /app/target/release/stat_server /stat_server
# COPY --from=builder /app/target/release/stat_client /stat_client

WORKDIR /
EXPOSE 8080 34512
Expand Down
17 changes: 9 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

- `rust` 版本 `server`, `client`,单个执行文件部署
- 支持上下线和简单自定义规则告警 (`telegram`, `wechat`)
- 支持 `vnstat` 更精准统计月流量
- 支持 `vnstat` 统计月流量,重启不丢流量数据
- 支持 `tcp`, `http` 协议上报
- 支持 `systemd`, 开机自启
- 更小 `docker` 镜像
Expand Down Expand Up @@ -59,15 +59,15 @@ hosts = [
]

# 不开启告警,可省略后面配置
# 告警间隔 s
# 告警间隔默认为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文件HostStat结构,模板置空则停用自定义告警
# host 可用字段参见 payload.rs 文件 HostStat 结构,模板置空则停用自定义告警,只保留上下线通知
custom_tpl = """
{% if host.memory_used / host.memory_total > 0.5 %}
<pre>❗{{ host.name }} 主机内存使用率超50%, 当前{{ (100 * host.memory_used / host.memory_total) | round }}% </pre>
Expand Down Expand Up @@ -105,7 +105,7 @@ wget --no-check-certificate -qO docker-compose.yml 'https://raw.githubuserconten
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
# 默认使用 watchtower 自动更新,不需要可以去除
# 默认使用 watchtower 自动更新,不需要可以去掉
docker-compose up -d

# 源码编译
Expand All @@ -124,6 +124,10 @@ RUST_BACKTRACE=1 RUST_LOG=trace ./stat_server -c config.toml
```bash
# 公网环境建议 nebula 组网或走 https, 使用 nginx 对 server 套 ssl 和自定义 location /report

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

# Rust 版本 Client
./stat_client -h
./stat_client -a "tcp://127.0.0.1:34512" -u h1 -p p1
Expand All @@ -145,13 +149,10 @@ python3 client-linux.py -h
python3 client-linux.py -a "tcp://127.0.0.1:34512" -u h1 -p p1
python3 client-linux.py -a "http://127.0.0.1:8080/report" -u h1 -p p1

## systemd 方式, 参照 one-touch.sh 脚本 (推荐)
systemctl enable stat_client
systemctl start stat_client
```

## 5.开启 `vnstat` 支持
[vnstat](https://zh.wikipedia.org/wiki/VnStat) 是Linux下一个流量统计工具,开启 `vnstat` 后,`server` 完全依赖客户机的 `vnstat` 数据来显示月流量和总流量,优点是重启不丢流量数据,数据更准确
[vnstat](https://zh.wikipedia.org/wiki/VnStat) 是Linux下一个流量统计工具,开启 `vnstat` 后,`server` 完全依赖客户机的 `vnstat` 数据来显示月流量和总流量,优点是重启不丢流量数据。
```bash
# 在client端安装 vnstat
## Centos
Expand Down
10 changes: 4 additions & 6 deletions client/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,10 +120,11 @@ async fn do_tcp_report(
// dbg!(&stat_base);

loop {
thread::sleep(Duration::from_millis(INTERVAL_MS));

let result = TcpStream::connect(&tcp_addr).await;
if result.is_err() {
error!("{:?}", result);
thread::sleep(Duration::from_millis(INTERVAL_MS));
continue;
}
let mut socket = result.unwrap();
Expand All @@ -140,7 +141,6 @@ async fn do_tcp_report(
if result.is_err() {
error!("{:?}", result);
drop(socket);
thread::sleep(Duration::from_millis(INTERVAL_MS));
continue;
}

Expand All @@ -154,7 +154,6 @@ async fn do_tcp_report(
if result.is_err() {
error!("{:?}", result);
drop(socket);
thread::sleep(Duration::from_millis(INTERVAL_MS));
continue;
}

Expand All @@ -167,7 +166,6 @@ async fn do_tcp_report(
dbg!(&result);
error!("Authentication failed!");
drop(socket);
thread::sleep(Duration::from_millis(INTERVAL_MS));
continue;
}

Expand Down Expand Up @@ -281,8 +279,8 @@ async fn main() -> Result<()> {
let args = Args::parse();
dbg!(&args);

status::start_net_speed_t();
status::start_all_ping_t();
status::start_net_speed_collect_t();
status::start_all_ping_collect_t();
status::start_cpu_percent_collect_t();
let (ipv4, ipv6) = status::get_network();

Expand Down
12 changes: 6 additions & 6 deletions client/src/status.rs
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ lazy_static! {
pub static ref G_NET_SPEED: Arc<Mutex<NetSpeed>> = Arc::new(Default::default());
}

pub fn start_net_speed_t() {
pub fn start_net_speed_collect_t() {
thread::spawn(|| loop {
let _ = File::open("/proc/net/dev").map(|file| {
let buf_reader = BufReader::new(file);
Expand Down Expand Up @@ -336,7 +336,7 @@ lazy_static! {
}));
}

fn start_ping_thread_t(data: &Arc<Mutex<PingData>>) {
fn start_ping_collect_t(data: &Arc<Mutex<PingData>>) {
let mut package_list: LinkedList<i32> = LinkedList::new();
let mut package_lost: u32 = 0;
let pt = &*data.lock().unwrap();
Expand Down Expand Up @@ -390,8 +390,8 @@ fn start_ping_thread_t(data: &Arc<Mutex<PingData>>) {
});
}

pub fn start_all_ping_t() {
start_ping_thread_t(&G_PING_10010);
start_ping_thread_t(&G_PING_189);
start_ping_thread_t(&G_PING_10086);
pub fn start_all_ping_collect_t() {
start_ping_collect_t(&G_PING_10010);
start_ping_collect_t(&G_PING_189);
start_ping_collect_t(&G_PING_10086);
}

0 comments on commit 8737a9b

Please sign in to comment.