Skip to content

Commit

Permalink
wip: update sysinfo structs
Browse files Browse the repository at this point in the history
  • Loading branch information
cilki committed Jan 6, 2025
1 parent 0e9f5e2 commit 26272a1
Show file tree
Hide file tree
Showing 22 changed files with 262 additions and 167 deletions.
2 changes: 1 addition & 1 deletion sandpolis/src/agent/layer/shell.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ impl Drop for ShellSession {
debug!("Killing child process");
self.process.kill(); // TODO await

self.data.ended = todo!();
// self.data.update(ShellSessionDelta::ended);
}
}

Expand Down
6 changes: 6 additions & 0 deletions sandpolis/src/agent/layer/sysinfo/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@ pub struct SysinfoLayer {
pub memory: MemoryMonitor,
}

impl SysinfoLayer {
pub fn new() -> Self {
todo!()
}
}

pub fn router() -> Router {
Router::new()
}
4 changes: 2 additions & 2 deletions sandpolis/src/agent/layer/sysinfo/os/user.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ use crate::agent::Monitor;
use crate::core::database::{Collection, Oid};
use crate::core::layer::sysinfo::os::user::UserData;
use anyhow::Result;
use sysinfo::{User, Users};
use sysinfo::Users;

impl Monitor for Collection<UserData> {
fn refresh(&self) -> Result<()> {
fn refresh(&mut self) -> Result<()> {
for user in Users::new_with_refreshed_list().list() {
self.document(**user.id())?.mutate(|data| {
data.uid = **user.id() as u64;
Expand Down
9 changes: 8 additions & 1 deletion sandpolis/src/agent/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -221,10 +221,17 @@ pub async fn main(args: CommandLine) -> Result<()> {
let _ = tokio::fs::remove_file(&args.agent_args.agent_socket).await;

let db = Database::new(args.storage.join("agent.db"))?;
let state = AgentState {
local: Arc::new(AgentInstance {
#[cfg(feature = "layer-sysinfo")]
sysinfo: crate::agent::layer::sysinfo::SysinfoLayer::new(),
}),
db,
};

let uds = UnixListener::bind(&args.agent_args.agent_socket)?;
tokio::spawn(async move {
let app = Router::new().with_state(AgentState { db });
let app = Router::new().with_state(state);

#[cfg(feature = "layer-shell")]
let app = app.nest("/layer/shell", crate::agent::layer::shell::router());
Expand Down
96 changes: 68 additions & 28 deletions sandpolis/src/core/database.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,39 @@ use serde::de::DeserializeOwned;
use serde::{Deserialize, Serialize};
use std::fmt::{Display, Write};
use std::marker::PhantomData;
use std::str::FromStr;
use std::{path::Path, sync::Arc};
use tracing::debug;

use super::InstanceId;
use super::{Instance, InstanceData, InstanceType};

#[derive(Clone)]
pub struct Database(sled::Db);

impl Database {
/// Initialize a new database from the given directory.
pub fn new<P>(path: P) -> Result<Self>
where
P: AsRef<Path>,
{
Ok(Self(sled::Config::new().path(path.as_ref()).open()?))
let path = path.as_ref();

debug!(path = %path.display(), "Initializing database");
Ok(Self(sled::Config::new().path(path).open()?))
}

pub fn document<T>(&self, oid: impl TryInto<Oid>) -> Result<Document<T>>
where
T: Serialize + DeserializeOwned + Clone,
T: Serialize + DeserializeOwned,
{
let oid = oid.try_into()?;
todo!()
}

pub fn collection<T>(&self, oid: impl TryInto<Oid>) -> Result<Collection<T>>
where
T: Serialize + DeserializeOwned,
{
let oid = oid.try_into()?;
todo!()
}

Expand All @@ -45,27 +56,49 @@ impl Display for Oid {
let mut iter = self.0.iter();
loop {
if let Some(b) = iter.next() {
f.write_char(*b as char);
f.write_char(*b as char)?;

if ':' as u8 == *b {
// TODO timestamp
// The next 8 bytes are the timestamp
let mut timestamp = 0u64;
for i in 0..8 {
if let Some(byte) = iter.next() {
timestamp |= (*byte as u64) << (i * 8);
} else {
return Err(std::fmt::Error);
}
}
f.write_str(&format!("{}", timestamp))?;
}
} else {
break;
}
}
todo!()
Ok(())
}
}

#[cfg(test)]
mod test_display {
use super::Oid;

#[test]
fn test_format_good() {
assert_eq!(Oid("/a".as_bytes().to_vec()).to_string(), "/a");
}
}

impl Oid {
pub fn extend(mut self, extension: &str) -> Result<Oid> {
self.path.push('/' as u8);
self.path.extend_from_slice(extension.as_bytes());
Ok(self)
pub fn extend(&self, oid: impl TryInto<Oid>) -> Result<Oid> {
let mut path = self.0.clone();

path.push('/' as u8);
path.extend_from_slice(&oid.try_into()?.0);
Ok(Oid(path))
}

pub fn timestamp(mut self, timestamp: u64) -> Result<Oid<T>> {
/// Add or replace the timestamp.
pub fn timestamp(mut self, timestamp: u64) -> Result<Oid> {
// TODO overwrite timestamp if one exists
for byte in self.path.iter().rev() {
if *byte == ':' as u8 {
Expand Down Expand Up @@ -108,19 +141,16 @@ impl AsRef<[u8]> for Oid {
#[derive(Clone)]
pub struct Collection<T>
where
T: Serialize + DeserializeOwned + Clone,
T: Serialize + DeserializeOwned,
{
db: sled::Tree,
oid: Oid,
data: PhantomData<T>,
}

impl<T: Serialize + DeserializeOwned + Clone> Collection<T> {
pub fn get_document<I>(&self, id: I) -> Result<Option<Document<T>>>
where
I: Into<Oid>,
{
let oid = self.oid.extend_id(id.into())?;
impl<T: Serialize + DeserializeOwned> Collection<T> {
pub fn get_document(&self, oid: impl TryInto<Oid>) -> Result<Option<Document<T>>> {
let oid = self.oid.extend(oid)?;

Ok(if let Some(data) = self.db.get(&oid)? {
Some(Document {
Expand All @@ -133,20 +163,26 @@ impl<T: Serialize + DeserializeOwned + Clone> Collection<T> {
})
}

pub fn collection<U>(&self) -> Result<Collection<U>>
pub fn documents(&self) {
self.db
.range::<&Oid, std::ops::Range<&Oid>>(&self.oid..&self.oid);
}

pub fn collection<U>(&self, oid: impl TryInto<Oid>) -> Result<Collection<U>>
where
U: Serialize + DeserializeOwned + Clone,
{
todo!()
Ok(Collection {
db: self.db.clone(),
oid: self.oid.extend(oid)?,
data: PhantomData {},
})
}
}

impl<T: Serialize + DeserializeOwned + Clone + Default> Collection<T> {
pub fn document<I>(&self, id: I) -> Result<Document<T>>
where
I: Into<Oid>,
{
let oid = self.oid.extend_id(id.into())?;
impl<T: Serialize + DeserializeOwned + Default> Collection<T> {
pub fn document(&self, oid: impl TryInto<Oid>) -> Result<Document<T>> {
let oid = self.oid.extend(oid)?;

Ok(Document {
db: self.db.clone(),
Expand All @@ -163,13 +199,17 @@ impl<T: Serialize + DeserializeOwned + Clone + Default> Collection<T> {
#[derive(Clone)]
pub struct Document<T>
where
T: Serialize + DeserializeOwned + Clone,
T: Serialize + DeserializeOwned,
{
pub db: sled::Tree,
pub oid: Oid,
pub data: T,
}

impl<T: Serialize + DeserializeOwned> Document<T> {
// pub fn update<U>(&mut self, update: )
}

impl<T: Serialize + DeserializeOwned + Clone> Document<T> {
pub fn mutate<F>(&mut self, mutator: F) -> Result<()>
where
Expand Down
21 changes: 10 additions & 11 deletions sandpolis/src/core/layer/sysinfo/hardware/cpu.rs
Original file line number Diff line number Diff line change
@@ -1,23 +1,22 @@
pub struct CpuData {
/// null
/// Product model
pub model: Option<String>,
/// null
/// Vendor name
pub vendor: Option<String>,
/// The specified frequency in Hertz
pub frequency_spec: u64,
/// Actual frequency in Hertz
pub frequency: u64,
/// The size of the L1 cache in bytes
pub l1_cache: u64,
pub l1_cache: Option<u64>,
/// The size of the L2 cache in bytes
pub l2_cache: u64,
pub l2_cache: Option<u64>,
/// The size of the L3 cache in bytes
pub l3_cache: u64,
pub l3_cache: Option<u64>,
/// The size of the L4 cache in bytes
pub l4_cache: u64,
}

pub struct CpuCoreData {
pub l4_cache: Option<u64>,
/// The core's usage between 0.0 and 1.0
pub usage: Double,
pub usage: f64,
/// The core's temperature in Celsius
pub temperature: Double,
pub temperature: Option<f64>,
}
56 changes: 30 additions & 26 deletions sandpolis/src/core/layer/sysinfo/hardware/disk/mod.rs
Original file line number Diff line number Diff line change
@@ -1,26 +1,30 @@
/// null
pub name: java.lang.String,
/// null
pub model: java.lang.String,
/// null
pub serial: java.lang.String,
/// The disk's total size in bytes
pub size: java.lang.Long,
/// null
pub reads: java.lang.Long,
/// null
pub read_bytes: java.lang.Long,
/// null
pub writes: java.lang.Long,
/// null
pub write_bytes: java.lang.Long,
/// null
pub queue_length: java.lang.Long,
/// null
pub transfer_time: java.lang.Long,
/// null
pub model_family: java.lang.String,
/// null
pub firmware_version: java.lang.String,
/// null
pub read_error_rate: java.lang.Long,
pub mod smart;

pub struct DiskData {
/// null
pub name: String,
/// null
pub model: String,
/// null
pub serial: String,
/// Total size in bytes
pub size: u64,
/// null
pub reads: u64,
/// null
pub read_bytes: u64,
/// null
pub writes: u64,
/// null
pub write_bytes: u64,
/// null
pub queue_length: u64,
/// null
pub transfer_time: u64,
/// null
pub model_family: String,
/// null
pub firmware_version: String,
/// null
pub read_error_rate: u64,
}
34 changes: 18 additions & 16 deletions sandpolis/src/core/layer/sysinfo/hardware/disk/partition.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
/// null
pub identification: java.lang.String,
/// null
pub name: java.lang.String,
/// null
pub description: java.lang.String,
/// The partition's UUID
pub uuid: java.lang.String,
/// The partition's total size in bytes
pub size: java.lang.Long,
/// null
pub major: java.lang.Integer,
/// null
pub minor: java.lang.Integer,
/// The partition's mount point
pub mount: java.lang.String,
pub struct PartitionData {
/// null
pub identification: String,
/// null
pub name: String,
/// null
pub description: String,
/// The partition's UUID
pub uuid: String,
/// The partition's total size in bytes
pub size: u64,
/// null
pub major: u32,
/// null
pub minor: u32,
/// The partition's mount point
pub mount: String,
}
Loading

0 comments on commit 26272a1

Please sign in to comment.