Can query hypervisor information

This commit is contained in:
2023-09-06 18:54:38 +02:00
parent fbe11af121
commit 57c023b45b
14 changed files with 253 additions and 9 deletions

View File

@ -0,0 +1,79 @@
use crate::app_config::AppConfig;
use actix::{Actor, Context, Handler, Message};
use virt::connect::Connect;
pub struct LibVirtActor {
m: Connect,
}
impl LibVirtActor {
/// Connect to hypervisor
pub async fn connect() -> anyhow::Result<Self> {
let hypervisor_uri = AppConfig::get().hypervisor_uri.as_deref().unwrap_or("");
log::info!(
"Will connect to hypvervisor at address '{}'",
hypervisor_uri
);
let conn = Connect::open(hypervisor_uri)?;
Ok(Self { m: conn })
}
}
impl Actor for LibVirtActor {
type Context = Context<Self>;
}
#[derive(Message)]
#[rtype(result = "anyhow::Result<HypervisorInfo>")]
pub struct GetHypervisorInfo;
#[derive(serde::Serialize)]
pub struct HypervisorInfo {
pub r#type: String,
pub hyp_version: u32,
pub lib_version: u32,
pub capabilities: String,
pub free_memory: u64,
pub hostname: String,
pub node: HypervisorNodeInfo,
}
#[derive(serde::Serialize)]
pub struct HypervisorNodeInfo {
pub cpu_model: String,
/// Memory size in kilobytes
pub memory_size: u64,
pub number_of_active_cpus: u32,
pub cpu_frequency_mhz: u32,
pub number_of_numa_cell: u32,
pub number_of_cpu_socket_per_node: u32,
pub number_of_core_per_sockets: u32,
pub number_of_threads_per_sockets: u32,
}
impl Handler<GetHypervisorInfo> for LibVirtActor {
type Result = anyhow::Result<HypervisorInfo>;
fn handle(&mut self, _msg: GetHypervisorInfo, _ctx: &mut Self::Context) -> Self::Result {
let node = self.m.get_node_info()?;
Ok(HypervisorInfo {
r#type: self.m.get_type()?,
hyp_version: self.m.get_hyp_version()?,
lib_version: self.m.get_lib_version()?,
capabilities: self.m.get_capabilities()?,
free_memory: self.m.get_free_memory()?,
hostname: self.m.get_hostname()?,
node: HypervisorNodeInfo {
cpu_model: node.model,
memory_size: node.memory,
number_of_active_cpus: node.cpus,
cpu_frequency_mhz: node.mhz,
number_of_numa_cell: node.nodes,
number_of_cpu_socket_per_node: node.sockets,
number_of_core_per_sockets: node.cores,
number_of_threads_per_sockets: node.threads,
},
})
}
}