diff --git a/src/coalesce.rs b/src/coalesce.rs index 98fb08f..ae388de 100644 --- a/src/coalesce.rs +++ b/src/coalesce.rs @@ -5,10 +5,10 @@ use std::time::{SystemTime, UNIX_EPOCH}; use faster_hex::hex_string; -use serde_json::json; - use linux_audit_parser::*; +use serde::Serialize; + use crate::constants::{ARCH_NAMES, SYSCALL_NAMES}; use crate::label_matcher::LabelMatcher; use crate::proc::{self, ContainerInfo, ProcTable, Process, ProcessKey}; @@ -1011,34 +1011,57 @@ impl<'a, 'ev> Coalesce<'a, 'ev> { } pub fn dump_state(&self, mut w: &mut dyn Write) -> Result<(), Box> { - serde_json::to_writer( + #[derive(Serialize)] + struct Message<'a> { + #[serde(rename = "type")] + typ: &'static str, + inflight: BTreeMap>, + done: Vec, + processes: &'a ProcTable, + next_expire: Option, + } + + #[derive(Serialize)] + struct Out<'a> { + ts: u64, + message: &'a Message<'a>, + } + + crate::json::to_writer( &mut w, - &json!({ - "ts": SystemTime::now().duration_since(UNIX_EPOCH).unwrap().as_secs(), - "message": { - "type": "dump_state", - "label_exe": self.settings.label_exe, - "inflight": self.inflight.iter().map( - |(k,v)| { + &Out { + ts: SystemTime::now() + .duration_since(UNIX_EPOCH) + .unwrap() + .as_secs(), + message: &Message { + typ: "dump_state", + inflight: self + .inflight + .iter() + .map(|(k, v)| { if let Some(node) = &k.0 { (format!("{}::{}", String::from_utf8_lossy(node), k.1), v) } else { (format!("{}", k.1), v) } - } - ).collect::>(), - "done": self.done.iter().map( - |v| if let Some(node ) = &v.0 { - format!("{}::{}", String::from_utf8_lossy(node), v.1) - } else { - format!("{}", v.1) - } - ).collect::>(), - "processes": self.processes, - "userdb": self.userdb, - "next_expire": self.next_expire, + }) + .collect::>(), + done: self + .done + .iter() + .map(|v| { + if let Some(node) = &v.0 { + format!("{}::{}", String::from_utf8_lossy(node), v.1) + } else { + format!("{}", v.1) + } + }) + .collect::>(), + processes: &self.processes, + next_expire: self.next_expire, }, - }), + }, )?; w.write_all(b"\n")?; w.flush()?; diff --git a/src/proc.rs b/src/proc.rs index 24ac527..44c442c 100644 --- a/src/proc.rs +++ b/src/proc.rs @@ -90,8 +90,10 @@ pub struct Process { /// parent's porocess ID pub ppid: u32, /// path to binary + #[serde(serialize_with = "serialize_name")] pub exe: Option>, /// process-settable argv[0] + #[serde(serialize_with = "serialize_name")] pub comm: Option>, /// Labels assigned to process pub labels: HashSet>, @@ -99,6 +101,13 @@ pub struct Process { pub container_info: Option, } +fn serialize_name(t: &Option>, s: S) -> Result +where + S: Serializer, +{ + s.serialize_bytes(&t.clone().unwrap_or_default()) +} + #[cfg(all(feature = "procfs", target_os = "linux"))] impl From for Process { fn from(p: procfs::ProcPidInfo) -> Self {