diff --git a/.gitignore b/.gitignore index ea8c4bf..c1cdd55 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ /target +Rocket.toml diff --git a/Cargo.lock b/Cargo.lock index 6680e0a..41117ea 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1160,6 +1160,7 @@ dependencies = [ "serde", "sysinfo", "tokio", + "vnstat_parse", ] [[package]] @@ -2600,6 +2601,15 @@ version = "0.0.13" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9dcc60c0624df774c82a0ef104151231d37da4962957d691c011c852b2473314" +[[package]] +name = "vnstat_parse" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "75334377a2918b45b5b8da023080375a4ec0aa04b0bc88f896ea93cf4b32feff" +dependencies = [ + "serde", +] + [[package]] name = "walkdir" version = "2.5.0" diff --git a/Cargo.toml b/Cargo.toml index 8196f02..bd617fc 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -12,6 +12,7 @@ sysinfo = "0.30.12" readable = "0.16.0" reqwest = { version = "0.12", features = ["json", "blocking"] } tokio = { version = "1", features = ["full"] } +vnstat_parse = { version = "0.1", features = ["serde"] } [dependencies.rocket_dyn_templates] version = "0.1.0" diff --git a/Rocket.toml.example b/Rocket.toml.example index 8688b82..d4c0305 100644 --- a/Rocket.toml.example +++ b/Rocket.toml.example @@ -1,3 +1,4 @@ [default] template_dir = "templates" -#address = "0.0.0.0" +address = "0.0.0.0" +network_interface = [ "eth0", "eth1" ] diff --git a/src/main.rs b/src/main.rs index 5e5f0b1..5b7b17f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,13 +1,14 @@ extern crate rocket; use readable::byte::Byte; use readable::up::UptimeFull; -use rocket::serde::Serialize; -use rocket::{get, launch, routes}; +use rocket::serde::{Serialize, Deserialize}; +use rocket::{get, launch, routes, State, fairing::AdHoc}; use rocket_dyn_templates::{context, Template}; use std::path::Path; use reqwest; use std::collections::HashMap; use sysinfo::{Components, Disks, Networks, System}; +use vnstat_parse::{Error, Vnstat}; use tokio::task; mod monero_rpc; @@ -41,8 +42,9 @@ struct RamInfo { #[derive(Serialize, Debug)] struct NetworkInfo { name: String, - total_sent: String, - total_received: String, + total_sent_today: String, + total_received_today: String, + average_rate_today: String, } #[derive(Serialize, Debug)] @@ -58,7 +60,7 @@ struct SysInfo { restart_needed: bool, } -fn get_system_info() -> SysInfo { +fn get_system_info(network_if_names: &Vec) -> SysInfo { let mut sys = System::new_all(); sys.refresh_all(); let cpu_load = System::load_average(); @@ -93,14 +95,17 @@ fn get_system_info() -> SysInfo { }) } - let networks = Networks::new_with_refreshed_list(); let mut network_ifs = Vec::new(); - for (interface_name, network) in &networks { - network_ifs.push(NetworkInfo { - name: interface_name.to_string(), - total_sent: Byte::from(network.total_transmitted()).to_string(), - total_received: Byte::from(network.total_received()).to_string() - }); + for network_if_name in network_if_names { + // TODO: Fail more gracefully + let vnstat_data = Vnstat::get(&network_if_name).unwrap(); + println!("{:?}", vnstat_data); + network_ifs.push(NetworkInfo{ + name: network_if_name.to_string(), + total_sent_today: format!("{} {}", vnstat_data.day_tx, vnstat_data.day_tx_unit), + total_received_today: format!("{} {}", vnstat_data.day_rx, vnstat_data.day_rx_unit), + average_rate_today: format!("{} {}", vnstat_data.day_avg_rate, vnstat_data.day_avg_rate_unit), + }) } SysInfo { @@ -133,7 +138,6 @@ fn get_node_info() -> NodeInfoInner { // TODO: Properly handle this unwrap - half the point of this is to capture // when the node is down - don't want it crashing when let resp = client.post("http://127.0.0.1:18081/json_rpc").json(&map).send().unwrap(); - // TODO: Figure out _why_ this part needs to be inside this closure resp.json::() }).unwrap(); let mut node_info = node_info.result; @@ -145,10 +149,16 @@ fn get_node_info() -> NodeInfoInner { node_info } +#[derive(Deserialize)] +#[serde(crate = "rocket::serde")] +struct Config { + network_interfaces: Vec, +} + #[get("/")] -fn index() -> Template { +fn index(config: &State) -> Template { let node_info = get_node_info(); - let system_context = get_system_info(); + let system_context = get_system_info(&config.network_interfaces); let context = context! { system: system_context, node_info: node_info, @@ -161,4 +171,5 @@ fn rocket() -> _ { rocket::build() .mount("/", routes![index]) .attach(Template::fairing()) + .attach(AdHoc::config::()) } diff --git a/templates/index.html.tera b/templates/index.html.tera index 9566766..9ea76b3 100644 --- a/templates/index.html.tera +++ b/templates/index.html.tera @@ -73,8 +73,9 @@

Network Interfaces

{% for network_if in system.network_ifs %}

{{ network_if.name }}

-

Total Sent: {{ network_if.total_sent }}

-

Total Received: {{ network_if.total_received }}

+

Total Sent Today: {{ network_if.total_sent_today }}

+

Total Received Today: {{ network_if.total_received_today }}

+

Average Rate: {{ network_if.average_rate_today }}

{% endfor %}