diff --git a/Cargo.lock b/Cargo.lock index 8db328af8..b5131aba4 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1609,6 +1609,7 @@ dependencies = [ "serde_json", "sha3", "strum 0.25.0", + "systemstat", "tarpc", "tasm-lib", "test-strategy", @@ -2640,6 +2641,20 @@ version = "0.1.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "2047c6ded9c721764247e62cd3b03c09ffc529b2ba5b10ec482ae507a4a70160" +[[package]] +name = "systemstat" +version = "0.2.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a24aec24a9312c83999a28e3ef9db7e2afd5c64bf47725b758cdc1cafd5b0bd2" +dependencies = [ + "bytesize", + "lazy_static", + "libc", + "nom", + "time", + "winapi", +] + [[package]] name = "tarpc" version = "0.34.0" diff --git a/Cargo.toml b/Cargo.toml index 0fa8372ee..b2b99757a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -66,6 +66,7 @@ leveldb-sys = "2.0.9" async-trait = "0.1.77" async-stream = "0.3.5" sha3 = "0.10.8" +systemstat = "0.2.3" readonly = "0.2.12" [dev-dependencies] diff --git a/src/rpc_server.rs b/src/rpc_server.rs index 71ebadddf..a7b0aed0a 100644 --- a/src/rpc_server.rs +++ b/src/rpc_server.rs @@ -10,6 +10,7 @@ use std::collections::HashMap; use std::net::IpAddr; use std::net::SocketAddr; use std::str::FromStr; +use systemstat::{Platform, System}; use tarpc::context; use tokio::sync::mpsc::error::SendError; use tracing::{error, info}; @@ -223,6 +224,9 @@ pub trait RPC { /// Gracious shutdown. async fn shutdown() -> bool; + + /// Get CPU temperature. + async fn cpu_temp() -> Option; } #[derive(Clone)] @@ -747,6 +751,14 @@ impl RPC for NeptuneRPCServer { .get_all_own_coins_with_possible_timelocks() .await } + + async fn cpu_temp(self, _context: tarpc::context::Context) -> Option { + let current_system = System::new(); + match current_system.cpu_temp() { + Ok(temp) => Some(temp.into()), + Err(_) => None, + } + } } #[cfg(test)] @@ -1126,6 +1138,20 @@ mod rpc_server_tests { Ok(()) } + #[traced_test] + #[tokio::test] + async fn test_can_get_server_temperature() { + let (rpc_server, _state_lock) = + test_rpc_server(Network::Alpha, WalletSecret::new_random(), 2).await; + let current_server_temperature = rpc_server.cpu_temp(context::current()).await; + // Silicon Macs do not provide CPU temperature using systemstat + if std::env::consts::OS == "macos" { + assert!(current_server_temperature.is_none()); + } else { + assert!(current_server_temperature.is_some()); + } + } + #[traced_test] #[tokio::test] async fn utxo_digest_test() {