Improved Network info
This commit is contained in:
1
.gitignore
vendored
1
.gitignore
vendored
@@ -1 +1,2 @@
|
|||||||
/target
|
/target
|
||||||
|
Rocket.toml
|
||||||
|
|||||||
10
Cargo.lock
generated
10
Cargo.lock
generated
@@ -1160,6 +1160,7 @@ dependencies = [
|
|||||||
"serde",
|
"serde",
|
||||||
"sysinfo",
|
"sysinfo",
|
||||||
"tokio",
|
"tokio",
|
||||||
|
"vnstat_parse",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
@@ -2600,6 +2601,15 @@ version = "0.0.13"
|
|||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "9dcc60c0624df774c82a0ef104151231d37da4962957d691c011c852b2473314"
|
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]]
|
[[package]]
|
||||||
name = "walkdir"
|
name = "walkdir"
|
||||||
version = "2.5.0"
|
version = "2.5.0"
|
||||||
|
|||||||
@@ -12,6 +12,7 @@ sysinfo = "0.30.12"
|
|||||||
readable = "0.16.0"
|
readable = "0.16.0"
|
||||||
reqwest = { version = "0.12", features = ["json", "blocking"] }
|
reqwest = { version = "0.12", features = ["json", "blocking"] }
|
||||||
tokio = { version = "1", features = ["full"] }
|
tokio = { version = "1", features = ["full"] }
|
||||||
|
vnstat_parse = { version = "0.1", features = ["serde"] }
|
||||||
|
|
||||||
[dependencies.rocket_dyn_templates]
|
[dependencies.rocket_dyn_templates]
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
|
|||||||
@@ -1,3 +1,4 @@
|
|||||||
[default]
|
[default]
|
||||||
template_dir = "templates"
|
template_dir = "templates"
|
||||||
#address = "0.0.0.0"
|
address = "0.0.0.0"
|
||||||
|
network_interface = [ "eth0", "eth1" ]
|
||||||
|
|||||||
41
src/main.rs
41
src/main.rs
@@ -1,13 +1,14 @@
|
|||||||
extern crate rocket;
|
extern crate rocket;
|
||||||
use readable::byte::Byte;
|
use readable::byte::Byte;
|
||||||
use readable::up::UptimeFull;
|
use readable::up::UptimeFull;
|
||||||
use rocket::serde::Serialize;
|
use rocket::serde::{Serialize, Deserialize};
|
||||||
use rocket::{get, launch, routes};
|
use rocket::{get, launch, routes, State, fairing::AdHoc};
|
||||||
use rocket_dyn_templates::{context, Template};
|
use rocket_dyn_templates::{context, Template};
|
||||||
use std::path::Path;
|
use std::path::Path;
|
||||||
use reqwest;
|
use reqwest;
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
use sysinfo::{Components, Disks, Networks, System};
|
use sysinfo::{Components, Disks, Networks, System};
|
||||||
|
use vnstat_parse::{Error, Vnstat};
|
||||||
use tokio::task;
|
use tokio::task;
|
||||||
|
|
||||||
mod monero_rpc;
|
mod monero_rpc;
|
||||||
@@ -41,8 +42,9 @@ struct RamInfo {
|
|||||||
#[derive(Serialize, Debug)]
|
#[derive(Serialize, Debug)]
|
||||||
struct NetworkInfo {
|
struct NetworkInfo {
|
||||||
name: String,
|
name: String,
|
||||||
total_sent: String,
|
total_sent_today: String,
|
||||||
total_received: String,
|
total_received_today: String,
|
||||||
|
average_rate_today: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Serialize, Debug)]
|
#[derive(Serialize, Debug)]
|
||||||
@@ -58,7 +60,7 @@ struct SysInfo {
|
|||||||
restart_needed: bool,
|
restart_needed: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
fn get_system_info() -> SysInfo {
|
fn get_system_info(network_if_names: &Vec<String>) -> SysInfo {
|
||||||
let mut sys = System::new_all();
|
let mut sys = System::new_all();
|
||||||
sys.refresh_all();
|
sys.refresh_all();
|
||||||
let cpu_load = System::load_average();
|
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();
|
let mut network_ifs = Vec::new();
|
||||||
for (interface_name, network) in &networks {
|
for network_if_name in network_if_names {
|
||||||
network_ifs.push(NetworkInfo {
|
// TODO: Fail more gracefully
|
||||||
name: interface_name.to_string(),
|
let vnstat_data = Vnstat::get(&network_if_name).unwrap();
|
||||||
total_sent: Byte::from(network.total_transmitted()).to_string(),
|
println!("{:?}", vnstat_data);
|
||||||
total_received: Byte::from(network.total_received()).to_string()
|
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 {
|
SysInfo {
|
||||||
@@ -133,7 +138,6 @@ fn get_node_info() -> NodeInfoInner {
|
|||||||
// TODO: Properly handle this unwrap - half the point of this is to capture
|
// TODO: Properly handle this unwrap - half the point of this is to capture
|
||||||
// when the node is down - don't want it crashing when
|
// 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();
|
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::<NodeInfo>()
|
resp.json::<NodeInfo>()
|
||||||
}).unwrap();
|
}).unwrap();
|
||||||
let mut node_info = node_info.result;
|
let mut node_info = node_info.result;
|
||||||
@@ -145,10 +149,16 @@ fn get_node_info() -> NodeInfoInner {
|
|||||||
node_info
|
node_info
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Deserialize)]
|
||||||
|
#[serde(crate = "rocket::serde")]
|
||||||
|
struct Config {
|
||||||
|
network_interfaces: Vec<String>,
|
||||||
|
}
|
||||||
|
|
||||||
#[get("/")]
|
#[get("/")]
|
||||||
fn index() -> Template {
|
fn index(config: &State<Config>) -> Template {
|
||||||
let node_info = get_node_info();
|
let node_info = get_node_info();
|
||||||
let system_context = get_system_info();
|
let system_context = get_system_info(&config.network_interfaces);
|
||||||
let context = context! {
|
let context = context! {
|
||||||
system: system_context,
|
system: system_context,
|
||||||
node_info: node_info,
|
node_info: node_info,
|
||||||
@@ -161,4 +171,5 @@ fn rocket() -> _ {
|
|||||||
rocket::build()
|
rocket::build()
|
||||||
.mount("/", routes![index])
|
.mount("/", routes![index])
|
||||||
.attach(Template::fairing())
|
.attach(Template::fairing())
|
||||||
|
.attach(AdHoc::config::<Config>())
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -73,8 +73,9 @@
|
|||||||
<h2>Network Interfaces</h2>
|
<h2>Network Interfaces</h2>
|
||||||
{% for network_if in system.network_ifs %}
|
{% for network_if in system.network_ifs %}
|
||||||
<h3 class="node_data">{{ network_if.name }}</h3>
|
<h3 class="node_data">{{ network_if.name }}</h3>
|
||||||
<p>Total Sent: <span class="node_data">{{ network_if.total_sent }}</span></p>
|
<p>Total Sent Today: <span class="node_data">{{ network_if.total_sent_today }}</span></p>
|
||||||
<p>Total Received: <span class="node_data">{{ network_if.total_received }}</span></p>
|
<p>Total Received Today: <span class="node_data">{{ network_if.total_received_today }}</span></p>
|
||||||
|
<p>Average Rate: <span class="node_data">{{ network_if.average_rate_today }}</span></p>
|
||||||
{% endfor %}
|
{% endfor %}
|
||||||
<p>
|
<p>
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
Reference in New Issue
Block a user