Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make Coalesce::dump_state more useful for debugging process tracking issues #235

Merged
merged 1 commit into from
Dec 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 46 additions & 23 deletions src/coalesce.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -1011,34 +1011,57 @@ impl<'a, 'ev> Coalesce<'a, 'ev> {
}

pub fn dump_state(&self, mut w: &mut dyn Write) -> Result<(), Box<dyn Error>> {
serde_json::to_writer(
#[derive(Serialize)]
struct Message<'a> {
#[serde(rename = "type")]
typ: &'static str,
inflight: BTreeMap<String, &'a Event<'a>>,
done: Vec<String>,
processes: &'a ProcTable,
next_expire: Option<u64>,
}

#[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::<BTreeMap<_,_>>(),
"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::<Vec<_>>(),
"processes": self.processes,
"userdb": self.userdb,
"next_expire": self.next_expire,
})
.collect::<BTreeMap<_, _>>(),
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::<Vec<_>>(),
processes: &self.processes,
next_expire: self.next_expire,
},
}),
},
)?;
w.write_all(b"\n")?;
w.flush()?;
Expand Down
9 changes: 9 additions & 0 deletions src/proc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,15 +90,24 @@ pub struct Process {
/// parent's porocess ID
pub ppid: u32,
/// path to binary
#[serde(serialize_with = "serialize_name")]
pub exe: Option<Vec<u8>>,
/// process-settable argv[0]
#[serde(serialize_with = "serialize_name")]
pub comm: Option<Vec<u8>>,
/// Labels assigned to process
pub labels: HashSet<Vec<u8>>,
#[cfg(all(feature = "procfs", target_os = "linux"))]
pub container_info: Option<ContainerInfo>,
}

fn serialize_name<S>(t: &Option<Vec<u8>>, s: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
s.serialize_bytes(&t.clone().unwrap_or_default())
}

#[cfg(all(feature = "procfs", target_os = "linux"))]
impl From<procfs::ProcPidInfo> for Process {
fn from(p: procfs::ProcPidInfo) -> Self {
Expand Down
Loading