2024-10-21 17:36:18 +11:00
|
|
|
use crate::RustbotError;
|
|
|
|
use super::PoiseContext;
|
2024-01-07 14:43:10 +11:00
|
|
|
|
|
|
|
use sysinfo::System;
|
|
|
|
use uptime_lib::get;
|
2024-08-09 18:19:59 -04:00
|
|
|
use std::{
|
2024-10-21 17:36:18 +11:00
|
|
|
env::var,
|
2024-08-09 18:19:59 -04:00
|
|
|
fs::File,
|
|
|
|
path::Path,
|
|
|
|
time::{
|
|
|
|
Duration,
|
|
|
|
SystemTime,
|
|
|
|
UNIX_EPOCH
|
|
|
|
},
|
|
|
|
io::{
|
|
|
|
BufRead,
|
|
|
|
BufReader
|
|
|
|
}
|
2024-01-07 14:43:10 +11:00
|
|
|
};
|
2024-10-21 17:36:18 +11:00
|
|
|
use rustbot_lib::utils::{
|
|
|
|
BOT_VERSION,
|
|
|
|
GIT_COMMIT_HASH,
|
|
|
|
GIT_COMMIT_BRANCH,
|
|
|
|
format_duration
|
|
|
|
};
|
2024-01-07 14:43:10 +11:00
|
|
|
|
2024-08-09 18:19:59 -04:00
|
|
|
fn get_os_info() -> String {
|
|
|
|
let path = Path::new("/etc/os-release");
|
|
|
|
let mut name = "BoringOS".to_string();
|
|
|
|
let mut version = "v0.0".to_string();
|
|
|
|
|
2024-10-21 17:36:18 +11:00
|
|
|
if let Ok(file) = File::open(path) {
|
2024-08-09 18:19:59 -04:00
|
|
|
let reader = BufReader::new(file);
|
2024-10-21 17:36:18 +11:00
|
|
|
let set_value = |s: String| s.split('=').nth(1).unwrap_or_default().trim_matches('"').to_string();
|
|
|
|
reader.lines().map_while(Result::ok).for_each(|line| {
|
|
|
|
match line {
|
|
|
|
l if l.starts_with("NAME=") => name = set_value(l),
|
|
|
|
l if l.starts_with("VERSION=") => version = set_value(l),
|
|
|
|
l if l.starts_with("VERSION_ID=") => version = set_value(l),
|
|
|
|
_ => {}
|
2024-08-09 18:19:59 -04:00
|
|
|
}
|
2024-10-21 17:36:18 +11:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
format!("{name} {version}")
|
|
|
|
}
|
|
|
|
|
|
|
|
fn fmt_mem(bytes: u64) -> String {
|
|
|
|
let units = ["B", "KB", "MB", "GB"];
|
|
|
|
let mut bytes = bytes as f64;
|
|
|
|
let mut unit = units[0];
|
|
|
|
|
|
|
|
for &u in &units {
|
|
|
|
if bytes < 1024.0 {
|
|
|
|
unit = u;
|
|
|
|
break;
|
2024-08-09 18:19:59 -04:00
|
|
|
}
|
2024-10-21 17:36:18 +11:00
|
|
|
bytes /= 1024.0;
|
2024-08-09 18:19:59 -04:00
|
|
|
}
|
|
|
|
|
2024-10-21 17:36:18 +11:00
|
|
|
format!("{bytes:.2} {unit}")
|
2024-08-09 18:19:59 -04:00
|
|
|
}
|
|
|
|
|
2024-01-07 14:43:10 +11:00
|
|
|
/// Retrieve host and bot uptimes
|
|
|
|
#[poise::command(slash_command)]
|
2024-10-21 17:36:18 +11:00
|
|
|
pub async fn uptime(ctx: PoiseContext<'_>) -> Result<(), RustbotError> {
|
|
|
|
let bot = ctx.http().get_current_user().await.unwrap();
|
2024-01-07 14:43:10 +11:00
|
|
|
let mut sys = System::new_all();
|
|
|
|
sys.refresh_all();
|
|
|
|
|
|
|
|
// Fetch system's uptime
|
|
|
|
let sys_uptime = get().unwrap().as_secs();
|
|
|
|
|
2024-08-09 18:19:59 -04:00
|
|
|
// Fetch system's processor
|
|
|
|
let cpu = sys.cpus();
|
|
|
|
|
2024-10-21 17:36:18 +11:00
|
|
|
// Fetch system memory usage
|
|
|
|
let sram = fmt_mem(sys.used_memory());
|
|
|
|
let sram_total = fmt_mem(sys.total_memory());
|
|
|
|
|
|
|
|
// Fetch process memory usage
|
|
|
|
let pram = match sys.process(sysinfo::get_current_pid().unwrap()) {
|
|
|
|
Some(proc) => fmt_mem(proc.memory()),
|
|
|
|
None => String::from("Unavailable")
|
|
|
|
};
|
|
|
|
|
|
|
|
// Fetch process uptime
|
2024-01-07 14:43:10 +11:00
|
|
|
let curr_pid = sysinfo::get_current_pid().unwrap();
|
|
|
|
let now = SystemTime::now();
|
|
|
|
let mut proc_uptime = 0;
|
|
|
|
if let Some(process) = sys.process(curr_pid) {
|
|
|
|
let time_started = UNIX_EPOCH + Duration::from_secs(process.start_time());
|
|
|
|
proc_uptime = now.duration_since(time_started).unwrap().as_secs();
|
|
|
|
}
|
|
|
|
|
2024-10-21 17:36:18 +11:00
|
|
|
// Fetch the node hostname from envvar
|
|
|
|
let docker_node = match var("DOCKER_HOSTNAME") {
|
|
|
|
Ok(h) => h.to_string(),
|
|
|
|
Err(_) => "DOCKER_HOSTNAME is empty!".to_string()
|
|
|
|
};
|
|
|
|
|
|
|
|
let stat_msg = [
|
2024-10-22 08:28:44 +11:00
|
|
|
format!("**{} v{}** `{GIT_COMMIT_HASH}:{GIT_COMMIT_BRANCH}`", bot.name, *BOT_VERSION),
|
2024-03-13 20:27:16 +11:00
|
|
|
format!(">>> System: `{}`", format_duration(sys_uptime)),
|
2024-08-09 18:19:59 -04:00
|
|
|
format!("Process: `{}`", format_duration(proc_uptime)),
|
2024-10-21 17:36:18 +11:00
|
|
|
format!("Node: `{docker_node}`"),
|
|
|
|
format!("CPU: `{}`", cpu[0].brand()),
|
|
|
|
format!("RAM: `{pram}` (`{sram}/{sram_total}`)"),
|
2024-08-09 18:19:59 -04:00
|
|
|
format!("OS: `{}`", get_os_info())
|
2024-03-13 20:27:16 +11:00
|
|
|
];
|
2024-10-21 17:36:18 +11:00
|
|
|
ctx.reply(stat_msg.join("\n")).await?;
|
2024-01-07 14:43:10 +11:00
|
|
|
|
2024-03-13 20:27:16 +11:00
|
|
|
Ok(())
|
2024-01-07 14:43:10 +11:00
|
|
|
}
|