Skip to content

Commit 3161432

Browse files
committed
add the code necessary to gather proc files and store in the zip file
Signed-off-by: Ermin Hrkalovic <[email protected]>
1 parent 700070c commit 3161432

File tree

2 files changed

+88
-5
lines changed

2 files changed

+88
-5
lines changed

core-dump-composer/src/config.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ pub struct CoreConfig {
2222
pub use_crio_config: bool,
2323
pub ignore_crio: bool,
2424
pub include_proc_info: bool,
25+
pub system_proc_folder_path: String,
2526
pub core_events: bool,
2627
pub timeout: u32,
2728
pub compression: bool,
@@ -103,6 +104,8 @@ impl CoreConfig {
103104
.unwrap_or_else(|_| "false".to_string().to_lowercase())
104105
.parse::<bool>()
105106
.unwrap();
107+
let system_proc_folder_path =
108+
env::var("OVERRIDE_PROC_FOLDER_PATH").unwrap_or_else(|_| "/proc".to_string());
106109
let log_length = env::var("LOG_LENGTH")
107110
.unwrap_or_else(|_| "500".to_string())
108111
.parse::<u32>()
@@ -151,6 +154,7 @@ impl CoreConfig {
151154
pod_selector_label,
152155
ignore_crio,
153156
include_proc_info,
157+
system_proc_folder_path,
154158
dot_env_path,
155159
image_command,
156160
use_crio_config,
@@ -236,13 +240,22 @@ impl CoreConfig {
236240
pub fn get_log_filename(&self, counter: usize) -> String {
237241
format!("{}-{}.log", self.get_templated_name(), counter)
238242
}
243+
239244
pub fn get_zip_full_path(&self) -> String {
240245
format!(
241246
"{}/{}.zip",
242247
self.params.directory,
243248
self.get_templated_name()
244249
)
245250
}
251+
252+
pub fn get_proc_folder_full_path(&self, counter: usize) -> String {
253+
format!("{}-{}-proc", self.get_templated_name(), counter)
254+
}
255+
256+
pub fn get_proc_files_to_gather(&self) -> [&'static str; 5] {
257+
["auxv", "cmdline", "environ", "maps", "status"]
258+
}
246259
}
247260

248261
pub fn try_get_matches() -> clap::Result<ArgMatches> {

core-dump-composer/src/main.rs

Lines changed: 75 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use crate::events::CoreEvent;
44

55
use advisory_lock::{AdvisoryFileLock, FileLockMode};
66
use libcrio::Cli;
7-
use log::{debug, error, info};
7+
use log::{debug, error, info, warn};
88
use serde_json::json;
99
use serde_json::Value;
1010
use std::env;
@@ -297,6 +297,9 @@ fn handle(mut cc: config::CoreConfig) -> Result<(), anyhow::Error> {
297297
break;
298298
}
299299
};
300+
301+
let container_id = container["id"].as_str().unwrap_or_default();
302+
debug!("Getting logs for container id {}", container_id);
300303
let log =
301304
match cli.tail_logs(container["id"].as_str().unwrap_or_default(), cc.log_length) {
302305
Ok(v) => v,
@@ -357,10 +360,77 @@ fn handle(mut cc: config::CoreConfig) -> Result<(), anyhow::Error> {
357360
process::exit(1);
358361
}
359362
};
360-
debug!(
361-
"Getting logs for container id {}",
362-
container["id"].as_str().unwrap_or_default()
363-
);
363+
364+
if cc.include_proc_info {
365+
debug!("Getting pid info for container id {}", container_id);
366+
367+
let inspect = match cli.inspect_container(container_id) {
368+
Ok(v) => v,
369+
Err(e) => {
370+
error!("Error inspecting container \n{}", e);
371+
// We continue here since we do not want to interrupt the whole gathering
372+
// flow because we could not inspect a container
373+
continue;
374+
}
375+
};
376+
377+
let pid = match inspect["info"]["pid"].as_u64() {
378+
Some(p) => p,
379+
None => {
380+
// We continue here since we do not want to interrupt the whole gathering
381+
// flow because we could not extract pid from the container inspection
382+
warn!("Failed to parse pid from inspect container, skipping");
383+
continue;
384+
}
385+
};
386+
387+
debug!("Got pid {} for container", pid);
388+
389+
debug!("Add proc files to the zip");
390+
391+
// Gathering proc files means reading from the /proc/$pid folder to capture files
392+
// needed fore core2md to do its conversion. If for some reason a file is missing,
393+
// then that means that the container was evicted and has no longer a pid folder.
394+
// When this happens we do not want to abort the rest of data gathering as it can
395+
// contain useful information regardless of that containers process data.
396+
// So if any files failed to open, then log the error and continue on to the next container(s)
397+
let proc_folder_full_path = cc.get_proc_folder_full_path(counter);
398+
for filename in cc.get_proc_files_to_gather() {
399+
let mut file = match File::open(format!(
400+
"{}/{}/{}",
401+
cc.system_proc_folder_path, pid, filename
402+
)) {
403+
Ok(f) => f,
404+
Err(e) => {
405+
warn!(
406+
"Failed to open {}. Has the pod been ejected?\n{}",
407+
filename, e
408+
);
409+
break;
410+
}
411+
};
412+
413+
let mut buffer = Vec::new();
414+
if let Err(e) = file.read_to_end(&mut buffer) {
415+
warn!("Failed read contents of the {} file \n{}", filename, e);
416+
break;
417+
}
418+
419+
if let Err(e) =
420+
zip.start_file(format!("{}/{}", proc_folder_full_path, filename), options)
421+
{
422+
warn!("Error starting {} file in zip \n{}", filename, e);
423+
break;
424+
}
425+
426+
if let Err(e) = zip.write_all(buffer.as_slice()) {
427+
warn!("Error writing {} file in zip \n{}", filename, e);
428+
break;
429+
}
430+
}
431+
432+
debug!("Finished adding proc files to the zip");
433+
}
364434
}
365435
};
366436

0 commit comments

Comments
 (0)