-
Notifications
You must be signed in to change notification settings - Fork 40
/
config.rs
432 lines (396 loc) · 14.9 KB
/
config.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
extern crate dotenv;
use clap::{App, Arg, ArgMatches};
use libcrio::ImageCommand;
use log::error;
use serde::Serialize;
use std::env;
use std::ffi::OsString;
use std::path::PathBuf;
use std::str::FromStr;
use tinytemplate::TinyTemplate;
use uuid::Uuid;
#[derive(Serialize)]
pub struct CoreConfig {
pub dot_env_path: PathBuf,
pub base_path: PathBuf,
pub crictl_config_path: PathBuf,
pub log_level: String,
pub log_length: u32,
pub pod_selector_label: String,
pub use_crio_config: bool,
pub ignore_crio: bool,
pub include_proc_info: bool,
pub system_proc_folder_path: String,
pub core_events: bool,
pub timeout: u32,
pub compression: bool,
pub event_location: PathBuf,
pub image_command: ImageCommand,
pub bin_path: String,
pub os_hostname: String,
pub filename_template: String,
pub params: CoreParams,
}
#[derive(Serialize)]
pub struct CoreParams {
pub limit_size: String,
pub exe_name: String,
pub pid: String,
pub signal: String,
pub timestamp: String,
pub directory: String,
pub hostname: String,
pub pathname: String,
pub namespace: Option<String>,
pub podname: Option<String>,
pub uuid: Uuid,
}
static DEFAULT_TEMPLATE: &str = "{uuid}-dump-{timestamp}-{hostname}-{exe_name}-{pid}-{signal}";
impl CoreConfig {
pub fn new() -> Result<CoreConfig, anyhow::Error> {
let matches = try_get_matches()?;
let limit_size = matches.value_of("limit-size").unwrap_or("").to_string();
let exe_name = matches.value_of("exe-name").unwrap_or("").to_string();
let pid = matches.value_of("pid").unwrap_or("").to_string();
let signal = matches.value_of("signal").unwrap_or("").to_string();
let timestamp = matches.value_of("timestamp").unwrap_or("").to_string();
let directory = matches.value_of("directory").unwrap_or("").to_string();
let hostname = matches.value_of("hostname").unwrap_or("").to_string();
let pathname = matches.value_of("pathname").unwrap_or("").to_string();
let uuid = Uuid::new_v4();
let params = CoreParams {
limit_size,
exe_name,
pid,
signal,
timestamp,
directory,
hostname,
pathname,
namespace: None,
podname: None,
uuid,
};
let mut dot_env_path = env::current_exe()?;
dot_env_path.pop();
dot_env_path.push(".env");
match dotenv::from_path(dot_env_path.clone()) {
Ok(v) => v,
Err(e) => error!("error loading .env file {}", e),
}
let mut crictl_config_path = env::current_exe()?;
crictl_config_path.pop();
crictl_config_path.push("crictl.yaml");
let mut base_path = env::current_exe()?;
base_path.pop();
let pod_selector_label = env::var("POD_SELECTOR_LABEL").unwrap_or_default();
let log_level = env::var("LOG_LEVEL").unwrap_or_default();
let ignore_crio = env::var("IGNORE_CRIO")
.unwrap_or_else(|_| "false".to_string())
.to_lowercase()
.parse::<bool>()
.unwrap();
let include_proc_info = env::var("INCLUDE_PROC_INFO")
.unwrap_or_else(|_| "false".to_string().to_lowercase())
.parse::<bool>()
.unwrap();
let system_proc_folder_path =
env::var("OVERRIDE_PROC_FOLDER_PATH").unwrap_or_else(|_| "/proc".to_string());
let log_length = env::var("LOG_LENGTH")
.unwrap_or_else(|_| "500".to_string())
.parse::<u32>()
.unwrap();
let image_command_string = env::var("CRIO_IMAGE_CMD").unwrap_or_else(|_| "img".to_string());
let use_crio_config = env::var("USE_CRIO_CONF")
.unwrap_or_else(|_| "false".to_string().to_lowercase())
.parse::<bool>()
.unwrap();
let compression = env::var("COMPRESSION")
.unwrap_or_else(|_| "true".to_string().to_lowercase())
.parse::<bool>()
.unwrap();
let timeout = env::var("TIMEOUT")
.unwrap_or_else(|_| "600".to_string())
.parse::<u32>()
.unwrap();
let core_events = env::var("CORE_EVENTS")
.unwrap_or_else(|_| "false".to_string())
.to_lowercase()
.parse::<bool>()
.unwrap();
let os_hostname = hostname::get()
.unwrap_or_else(|_| OsString::from_str("unknown").unwrap_or_default())
.into_string()
.unwrap_or_else(|_| "unknown".to_string());
let base_path_str = base_path
.clone()
.into_os_string()
.into_string()
.unwrap_or_else(|_| "/var/mnt/core-dump-handler".to_string());
let bin_path = format!(
"/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/home/kubernetes/bin:{base_path_str}"
);
let image_command =
ImageCommand::from_str(&image_command_string).unwrap_or(ImageCommand::Img);
let filename_template =
env::var("FILENAME_TEMPLATE").unwrap_or_else(|_| String::from(DEFAULT_TEMPLATE));
let event_location = PathBuf::from(
env::var("EVENT_DIRECTORY").unwrap_or_else(|_| format!("{base_path_str}/events")),
);
Ok(CoreConfig {
log_level,
pod_selector_label,
ignore_crio,
include_proc_info,
system_proc_folder_path,
dot_env_path,
image_command,
use_crio_config,
crictl_config_path,
base_path,
bin_path,
os_hostname,
filename_template,
log_length,
params,
compression,
core_events,
event_location,
timeout,
})
}
pub fn get_dump_info(&self) -> String {
format!(
"{{\"uuid\":\"{}\", \"dump_file\":\"{}\", \"timestamp\": \"{}\",
\"hostname\": \"{}\", \"exe\": \"{}\", \"real_pid\": \"{}\", \"signal\": \"{}\", \"node_hostname\": \"{}\", \"path\": \"{}\" }}",
self.params.uuid, self.get_core_filename(), self.params.timestamp, self.params.hostname, self.params.exe_name, self.params.pid, self.params.signal, self.os_hostname, self.params.pathname
)
}
pub fn get_templated_name(&self) -> String {
let mut tt = TinyTemplate::new();
match tt.add_template("name", &self.filename_template) {
Ok(v) => v,
Err(e) => {
error!(
"Adding template failed. Using uuid {} {}",
self.params.uuid.to_string(),
e
);
return self.params.uuid.to_string();
}
}
match tt.render("name", &self.params) {
Ok(v) => v,
Err(e) => {
error!(
"Templating name failed. Using uuid {} {}",
self.params.uuid.to_string(),
e
);
self.params.uuid.to_string()
}
}
}
pub fn set_namespace(&mut self, namespace: String) {
self.params.namespace = Some(namespace)
}
pub fn set_podname(&mut self, podname: String) {
self.params.podname = Some(podname)
}
pub fn get_dump_info_filename(&self) -> String {
format!("{}-dump-info.json", self.get_templated_name())
}
pub fn get_core_filename(&self) -> String {
format!("{}.core", self.get_templated_name())
}
pub fn get_pod_filename(&self) -> String {
format!("{}-pod-info.json", self.get_templated_name())
}
pub fn get_inspect_pod_filename(&self) -> String {
format!("{}-runtime-info.json", self.get_templated_name())
}
pub fn get_ps_filename(&self) -> String {
format!("{}-ps-info.json", self.get_templated_name())
}
pub fn get_image_filename(&self, counter: usize) -> String {
format!("{}-{}-image-info.json", self.get_templated_name(), counter)
}
pub fn get_log_filename(&self, counter: usize) -> String {
format!("{}-{}.log", self.get_templated_name(), counter)
}
pub fn get_zip_full_path(&self) -> String {
format!(
"{}/{}.zip",
self.params.directory,
self.get_templated_name()
)
}
pub fn get_proc_folder_full_path(&self, counter: usize) -> String {
format!("{}-{}-proc", self.get_templated_name(), counter)
}
pub fn get_proc_files_to_gather(&self) -> [&'static str; 5] {
["auxv", "cmdline", "environ", "maps", "status"]
}
}
pub fn try_get_matches() -> clap::Result<ArgMatches> {
App::new("Core Dump Composer")
.version("0.1.0")
.author("Anton Whalley <anton@venshare.com>")
.about("Processes Core Dumps in a K8s System")
.arg(
Arg::new("limit-size")
.short('c')
.long("limit-size")
.required(false)
.takes_value(true)
.help("Core file size soft resource limit of crashing process"),
)
.arg(
Arg::new("exe-name")
.short('e')
.long("exe-name")
.required(false)
.takes_value(true)
.help(
"The process or thread's comm value, which typically is the
same as the executable filename (without path prefix, and
truncated to a maximum of 15 characters)",
),
)
.arg(
Arg::new("pid")
.short('p')
.long("pid")
.required(false)
.takes_value(true)
.help(
"PID of dumped process, as seen in the PID namespace in which
the process resides.",
),
)
.arg(
Arg::new("signal")
.short('s')
.long("signal")
.required(false)
.takes_value(true)
.help("Number of signal causing dump."),
)
.arg(
Arg::new("timestamp")
.short('t')
.long("timestamp")
.required(false)
.takes_value(true)
.help("Time of dump, expressed as seconds since the Epoch."),
)
.arg(
Arg::new("directory")
.short('d')
.long("dir")
.required(false)
.takes_value(true)
.help("Directory to save the core dump to."),
)
.arg(
Arg::new("hostname")
.short('h')
.long("hostname")
.required(false)
.takes_value(true)
.help("Hostname (same as nodename returned by uname(2))"),
)
.arg(
Arg::new("pathname")
.short('E')
.long("pathname")
.required(false)
.takes_value(true)
.help("Hostname (same as nodename returned by uname(2))"),
)
.arg(
Arg::new("timeout")
.short('T')
.long("timeout")
.required(false)
.takes_value(true)
.help("Timeout in seconds to wait for processing of the Coredump"),
)
.arg(
Arg::new("test-threads")
.long("test-threads")
.required(false)
.takes_value(true)
.help("test-threads mapped to support the test scenarios"),
)
.arg(
Arg::new("disable-compression")
.short('D')
.long("disable-compression")
.takes_value(false)
.help("Disables deflate compression in resulting zip file and stores data uncompressed."),
)
.try_get_matches()
}
#[cfg(test)]
mod tests {
use crate::config::CoreConfig;
#[test]
fn namespace_is_rendered() {
let mut config = match CoreConfig::new() {
Ok(v) => v,
Err(e) => panic!("Generation of CoreConfig failed. {}", e),
};
config.set_namespace("anamespace".to_string());
let templated_name = config.get_templated_name();
assert!(templated_name.contains("-dump-----"));
config.filename_template = "{namespace}".to_string();
let just_namespace = config.get_templated_name();
assert_eq!(just_namespace, "anamespace".to_string());
}
#[test]
fn default_template_test() {
// "{uuid}-dump-{timestamp}-{hostname}-{exe_name}-{pid}-{signal}";
let mut config = match CoreConfig::new() {
Ok(v) => v,
Err(e) => panic!("Generation of CoreConfig failed. {}", e),
};
config.params.timestamp = "123123123".to_string();
config.params.hostname = "ahostname".to_string();
config.params.exe_name = "anexe".to_string();
config.params.pid = "2".to_string();
config.params.signal = "9".to_string();
let templated_name = config.get_templated_name();
assert!(templated_name.contains("-dump-123123123-ahostname-anexe-2-9"));
}
#[test]
fn get_files_test() {
let mut config = match CoreConfig::new() {
Ok(v) => v,
Err(e) => panic!("Generation of CoreConfig failed. {}", e),
};
config.params.timestamp = "123123123".to_string();
config.params.hostname = "ahostname".to_string();
config.params.exe_name = "anexe".to_string();
config.params.pid = "2".to_string();
config.params.signal = "9".to_string();
let dump_info_name = config.get_dump_info_filename();
assert!(dump_info_name.contains("-dump-123123123-ahostname-anexe-2-9-dump-info.json"));
let core_file_name = config.get_core_filename();
assert!(core_file_name.contains("-dump-123123123-ahostname-anexe-2-9.core"));
let pod_file_name = config.get_pod_filename();
assert!(pod_file_name.contains("-dump-123123123-ahostname-anexe-2-9-pod-info.json"));
let inspect_file_name = config.get_inspect_pod_filename();
assert!(inspect_file_name.contains("-dump-123123123-ahostname-anexe-2-9-runtime-info.json"));
let inspect_file_name = config.get_inspect_pod_filename();
assert!(inspect_file_name.contains("-dump-123123123-ahostname-anexe-2-9-runtime-info.json"));
let ps_file_name = config.get_ps_filename();
assert!(ps_file_name.contains("-dump-123123123-ahostname-anexe-2-9-ps-info.json"));
let img_file_name = config.get_image_filename(0);
assert!(img_file_name.contains("-dump-123123123-ahostname-anexe-2-9-0-image-info.json"));
let log_file_name = config.get_log_filename(0);
assert!(log_file_name.contains("-dump-123123123-ahostname-anexe-2-9-0.log"));
let zip_file_name = config.get_zip_full_path();
assert!(zip_file_name.contains("-dump-123123123-ahostname-anexe-2-9.zip"));
}
}