mirror of
https://git.alemi.dev/server-monitor.git
synced 2024-11-23 07:54:48 +01:00
feat: initial implementation keeps track of network per session
This commit is contained in:
parent
aefe2153e9
commit
0064f0daa9
2 changed files with 118 additions and 0 deletions
9
Cargo.toml
Normal file
9
Cargo.toml
Normal file
|
@ -0,0 +1,9 @@
|
|||
[package]
|
||||
name = "server-monitor"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
[dependencies]
|
||||
rocket = { version = "0.5.0-rc.2", features = ["json"] }
|
||||
systemstat = "0.2"
|
||||
serde = "1"
|
109
src/main.rs
Normal file
109
src/main.rs
Normal file
|
@ -0,0 +1,109 @@
|
|||
#[macro_use] extern crate rocket;
|
||||
|
||||
use std::collections::HashMap;
|
||||
|
||||
use rocket::State;
|
||||
use rocket::serde::{Serialize, json::Json};
|
||||
use rocket::tokio::sync::Mutex;
|
||||
use systemstat::{System, Platform};
|
||||
|
||||
#[derive(Serialize)]
|
||||
struct SystemInfoView {
|
||||
session: String,
|
||||
mem: f32,
|
||||
cpu: f32,
|
||||
tx: u64,
|
||||
rx: u64,
|
||||
disk: u64,
|
||||
}
|
||||
|
||||
struct NetDeltaTracker {
|
||||
tx: u64,
|
||||
rx: u64,
|
||||
}
|
||||
|
||||
impl Default for NetDeltaTracker {
|
||||
fn default() -> Self {
|
||||
NetDeltaTracker { tx: 0, rx: 0 }
|
||||
}
|
||||
}
|
||||
|
||||
impl NetDeltaTracker {
|
||||
pub fn delta(&mut self, tx: u64, rx: u64) -> (u64, u64) {
|
||||
let out = (tx - self.tx, rx - self.rx);
|
||||
self.tx = tx;
|
||||
self.rx = rx;
|
||||
return out;
|
||||
}
|
||||
}
|
||||
|
||||
struct NetDeltaState {
|
||||
pub store: Mutex<HashMap<String, NetDeltaTracker>>,
|
||||
}
|
||||
|
||||
#[get("/<session>")]
|
||||
async fn stats_delta(session: String, state: &State<NetDeltaState>) -> Json<SystemInfoView> {
|
||||
let sys = System::new();
|
||||
let (delta_tx, delta_rx);
|
||||
|
||||
let (net_tx, net_rx) = match sys.network_stats(&"enp5s0".to_string()) {
|
||||
Ok(stats) => (stats.tx_bytes.as_u64(), stats.rx_bytes.as_u64()),
|
||||
Err(x) => (0, 0),
|
||||
};
|
||||
|
||||
{
|
||||
let mut store = state.inner().store.lock().await;
|
||||
if !store.contains_key(&session) {
|
||||
store.insert(session.clone(), NetDeltaTracker::default());
|
||||
}
|
||||
|
||||
let net_tracker = store.get_mut(&session).unwrap();
|
||||
|
||||
(delta_tx, delta_rx) = net_tracker.delta(net_tx, net_rx);
|
||||
}
|
||||
|
||||
Json(
|
||||
SystemInfoView {
|
||||
session,
|
||||
mem: match sys.memory() {
|
||||
Ok(mem) => { 1.0 - ((mem.free.as_u64() as f32) / (mem.total.as_u64() as f32)) },
|
||||
Err(e) => { -1.0 },
|
||||
},
|
||||
cpu: 0.0,
|
||||
tx: delta_tx,
|
||||
rx: delta_rx,
|
||||
disk: 0,
|
||||
}
|
||||
)
|
||||
}
|
||||
|
||||
#[get("/")]
|
||||
async fn stats_total() -> Json<SystemInfoView> {
|
||||
let sys = System::new();
|
||||
|
||||
let (net_tx, net_rx) = match sys.network_stats(&"enp5s0".to_string()) {
|
||||
Ok(stats) => (stats.tx_bytes.as_u64(), stats.rx_bytes.as_u64()),
|
||||
Err(x) => (0, 0),
|
||||
};
|
||||
|
||||
Json(
|
||||
SystemInfoView {
|
||||
session: "<TOTAL>".to_string(),
|
||||
mem: match sys.memory() {
|
||||
Ok(mem) => { 1.0 - ((mem.free.as_u64() as f32) / (mem.total.as_u64() as f32)) },
|
||||
Err(e) => { -1.0 },
|
||||
},
|
||||
cpu: 0.0,
|
||||
tx: net_tx,
|
||||
rx: net_rx,
|
||||
disk: 0,
|
||||
}
|
||||
)
|
||||
}
|
||||
|
||||
#[launch]
|
||||
fn rocket() -> _ {
|
||||
rocket::build()
|
||||
.manage(NetDeltaState { store: Mutex::new(HashMap::new()) })
|
||||
.mount("/", routes![stats_delta, stats_total])
|
||||
}
|
Loading…
Reference in a new issue