Skip to content

Commit

Permalink
tappd: Add API SysInfo
Browse files Browse the repository at this point in the history
  • Loading branch information
kvinwang committed Dec 10, 2024
1 parent bdfffbd commit 538ac84
Show file tree
Hide file tree
Showing 7 changed files with 317 additions and 10 deletions.
124 changes: 120 additions & 4 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions tappd/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,4 @@ ra-rpc = { workspace = true, features = ["rocket"] }
tappd-rpc = { workspace = true }
ra-tls = { workspace = true }
tdx-attest = { workspace = true }
sysinfo = "0.33.0"
49 changes: 49 additions & 0 deletions tappd/rpc/proto/tappd_rpc.proto
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ message TdxQuoteResponse {
service Worker {
// Get worker info
rpc Info(google.protobuf.Empty) returns (WorkerInfo) {}
// Get system info
rpc SysInfo(google.protobuf.Empty) returns (SystemInfo) {}
// Get worker containers
rpc ListContainers(google.protobuf.Empty) returns (ListContainersResponse) {}
}
Expand Down Expand Up @@ -104,3 +106,50 @@ message WorkerInfo {
// TCB info
string tcb_info = 4;
}

// The system info
message SystemInfo {
// Operating system
string os_name = 1;
// Operating system version
string os_version = 2;
// Kernel version
string kernel_version = 3;
// Cpu model
string cpu_model = 4;
// Number of logical CPUs
uint32 num_cpus = 5;
// Total memory
uint64 total_memory = 6;
// Available memory
uint64 available_memory = 7;
// Used memory
uint64 used_memory = 8;
// Free memory
uint64 free_memory = 9;
// Total swap memory
uint64 total_swap = 10;
// Used swap memory
uint64 used_swap = 11;
// Free swap memory
uint64 free_swap = 12;
// Uptime
uint64 uptime = 13;
// Load average
uint32 loadavg_one = 14;
uint32 loadavg_five = 15;
uint32 loadavg_fifteen = 16;
// Disks
repeated DiskInfo disks = 17;
}

message DiskInfo {
// Device name
string name = 1;
// Mount point
string mount_point = 2;
// Total size
uint64 total_size = 3;
// Free size
uint64 free_size = 5;
}
5 changes: 5 additions & 0 deletions tappd/src/http_routes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,13 +74,18 @@ async fn index(state: &State<AppState>) -> Result<RawHtml<String>, String> {
.await
.map_err(|e| format!("Failed to get worker info: {}", e))?;

let handler = ExternalRpcHandler::construct(state, None)
.map_err(|e| format!("Failed to construct RPC handler: {}", e))?;
let system_info = handler.sys_info().await.unwrap_or_default();

let containers = list_containers().await.unwrap_or_default().containers;
let model = crate::models::Dashboard {
app_id,
instance_id,
app_cert,
tcb_info,
containers,
system_info,
};
match model.render() {
Ok(html) => Ok(RawHtml(html)),
Expand Down
19 changes: 18 additions & 1 deletion tappd/src/models.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use rinja::Template;
use tappd_rpc::Container;
use tappd_rpc::{Container, SystemInfo};

mod filters {
use anyhow::Result;
Expand All @@ -8,6 +8,22 @@ mod filters {
let name = s.map(|s| s.as_str()).unwrap_or_default();
Ok(name.strip_prefix("/").unwrap_or(name))
}

pub fn hsize(s: &u64) -> Result<String, rinja::Error> {
// convert bytes to human readable size
let mut size = *s as f64;
let units = ["B", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"];
let mut unit_index = 0;
while size >= 1024.0 && unit_index < units.len() - 1 {
size /= 1024.0;
unit_index += 1;
}
Ok(format!(
"{:.2} {}",
size,
units.get(unit_index).unwrap_or(&"?")
))
}
}

#[derive(Template)]
Expand All @@ -18,4 +34,5 @@ pub struct Dashboard {
pub app_cert: String,
pub tcb_info: String,
pub containers: Vec<Container>,
pub system_info: SystemInfo,
}
48 changes: 45 additions & 3 deletions tappd/src/rpc_service.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::sync::Arc;
use std::{path::Path, sync::Arc};

use anyhow::{bail, Context, Result};
use bollard::{container::ListContainersOptions, Docker};
Expand All @@ -13,8 +13,8 @@ use serde_json::json;
use tappd_rpc::{
tappd_server::{TappdRpc, TappdServer},
worker_server::{WorkerRpc, WorkerServer},
Container, DeriveKeyArgs, DeriveKeyResponse, ListContainersResponse, TdxQuoteArgs,
TdxQuoteResponse, WorkerInfo,
Container, DeriveKeyArgs, DeriveKeyResponse, DiskInfo, ListContainersResponse, SystemInfo,
TdxQuoteArgs, TdxQuoteResponse, WorkerInfo,
};
use tdx_attest::eventlog::read_event_logs;

Expand Down Expand Up @@ -149,6 +149,48 @@ impl WorkerRpc for ExternalRpcHandler {
})
}

async fn sys_info(self) -> Result<SystemInfo> {
use sysinfo::System;

let system = System::new_all();
let cpus = system.cpus();

let disks = sysinfo::Disks::new_with_refreshed_list();
let disks = disks
.list()
.iter()
.filter(|d| d.mount_point() == Path::new("/"))
.map(|d| DiskInfo {
name: d.name().to_string_lossy().to_string(),
mount_point: d.mount_point().to_string_lossy().to_string(),
total_size: d.total_space(),
free_size: d.available_space(),
})
.collect::<Vec<_>>();
let avg = System::load_average();
Ok(SystemInfo {
os_name: System::name().unwrap_or_default(),
os_version: System::os_version().unwrap_or_default(),
kernel_version: System::kernel_version().unwrap_or_default(),
cpu_model: cpus.get(0).map_or("".into(), |cpu| {
format!("{} @{} MHz", cpu.name(), cpu.frequency())
}),
num_cpus: cpus.len() as _,
total_memory: system.total_memory(),
available_memory: system.available_memory(),
used_memory: system.used_memory(),
free_memory: system.free_memory(),
total_swap: system.total_swap(),
used_swap: system.used_swap(),
free_swap: system.free_swap(),
uptime: System::uptime(),
loadavg_one: (avg.one * 100.0) as u32,
loadavg_five: (avg.five * 100.0) as u32,
loadavg_fifteen: (avg.fifteen * 100.0) as u32,
disks,
})
}

async fn list_containers(self) -> Result<ListContainersResponse> {
list_containers().await
}
Expand Down
Loading

0 comments on commit 538ac84

Please sign in to comment.