sovereign-intelligence
Front door to Drake Stapleton's sovereign stack.
Brought by drakestapleton
sovereign-intelligence / docs / archive / drakestapleton.com-old / crates / site-server / src / telemetry.rs
| 1 | use serde::{Deserialize, Serialize}; |
| 2 | use std::fs; |
| 3 | use std::process::Command; |
| 4 | use std::time::Instant; |
| 5 | |
| 6 | #[derive(Clone, Debug, Serialize, Deserialize)] |
| 7 | pub struct TelemetryPacket { |
| 8 | pub timestamp: String, |
| 9 | pub gpu_utilization_pct: u32, |
| 10 | pub gpu_temperature_c: u32, |
| 11 | pub gpu_power_watts: f32, |
| 12 | pub system_memory_used_mb: u64, |
| 13 | pub system_memory_total_mb: u64, |
| 14 | pub aien_agent_status: String, |
| 15 | pub active_goal: String, |
| 16 | pub latest_milestone: String, |
| 17 | pub uptime_seconds: u64, |
| 18 | } |
| 19 | |
| 20 | impl Default for TelemetryPacket { |
| 21 | fn default() -> Self { |
| 22 | Self { |
| 23 | timestamp: chrono::Utc::now().to_rfc3339(), |
| 24 | gpu_utilization_pct: 0, |
| 25 | gpu_temperature_c: 0, |
| 26 | gpu_power_watts: 0.0, |
| 27 | system_memory_used_mb: 0, |
| 28 | system_memory_total_mb: 124608, |
| 29 | aien_agent_status: "ONLINE (GB10 ACTIVE)".to_string(), |
| 30 | active_goal: "Sovereign Autonomous Systems".to_string(), |
| 31 | latest_milestone: "Full-Stack Rust Server on Spark".to_string(), |
| 32 | uptime_seconds: 0, |
| 33 | } |
| 34 | } |
| 35 | } |
| 36 | |
| 37 | pub fn collect_gpu_telemetry() -> (u32, u32, f32) { |
| 38 | let output = Command::new("nvidia-smi") |
| 39 | .args([ |
| 40 | "--query-gpu=utilization.gpu,temperature.gpu,power.draw", |
| 41 | "--format=csv,noheader,nounits", |
| 42 | ]) |
| 43 | .output(); |
| 44 | |
| 45 | match output { |
| 46 | Ok(out) if out.status.success() => { |
| 47 | let text = String::from_utf8_lossy(&out.stdout); |
| 48 | let parts: Vec<&str> = text.trim().split(',').map(|s| s.trim()).collect(); |
| 49 | if parts.len() >= 3 { |
| 50 | let util = parts[0].parse::<u32>().unwrap_or(0); |
| 51 | let temp = parts[1].parse::<u32>().unwrap_or(0); |
| 52 | let power = parts[2].parse::<f32>().unwrap_or(0.0); |
| 53 | return (util, temp, power); |
| 54 | } |
| 55 | } |
| 56 | _ => {} |
| 57 | } |
| 58 | (0, 0, 0.0) |
| 59 | } |
| 60 | |
| 61 | pub fn collect_memory_telemetry() -> (u64, u64) { |
| 62 | if let Ok(meminfo) = fs::read_to_string("/proc/meminfo") { |
| 63 | let mut total_kb = 0u64; |
| 64 | let mut avail_kb = 0u64; |
| 65 | for line in meminfo.lines() { |
| 66 | if line.starts_with("MemTotal:") { |
| 67 | if let Some(val) = line.split_whitespace().nth(1) { |
| 68 | total_kb = val.parse::<u64>().unwrap_or(0); |
| 69 | } |
| 70 | } else if line.starts_with("MemAvailable:") { |
| 71 | if let Some(val) = line.split_whitespace().nth(1) { |
| 72 | avail_kb = val.parse::<u64>().unwrap_or(0); |
| 73 | } |
| 74 | } |
| 75 | } |
| 76 | if total_kb > 0 { |
| 77 | let used_mb = (total_kb.saturating_sub(avail_kb)) / 1024; |
| 78 | let total_mb = total_kb / 1024; |
| 79 | return (used_mb, total_mb); |
| 80 | } |
| 81 | } |
| 82 | (0, 124608) |
| 83 | } |
| 84 | |
| 85 | pub fn sample_telemetry(start_time: Instant, active_goal: &str, latest_milestone: &str) -> TelemetryPacket { |
| 86 | let (gpu_util, gpu_temp, gpu_power) = collect_gpu_telemetry(); |
| 87 | let (mem_used, mem_total) = collect_memory_telemetry(); |
| 88 | |
| 89 | TelemetryPacket { |
| 90 | timestamp: chrono::Utc::now().to_rfc3339(), |
| 91 | gpu_utilization_pct: gpu_util, |
| 92 | gpu_temperature_c: gpu_temp, |
| 93 | gpu_power_watts: gpu_power, |
| 94 | system_memory_used_mb: mem_used, |
| 95 | system_memory_total_mb: mem_total, |
| 96 | aien_agent_status: "ONLINE (GB10 ACTIVE)".to_string(), |
| 97 | active_goal: active_goal.to_string(), |
| 98 | latest_milestone: latest_milestone.to_string(), |
| 99 | uptime_seconds: start_time.elapsed().as_secs(), |
| 100 | } |
| 101 | } |