Skip to content

Commit 6bc80d8

Browse files
committed
Switched config file path to PathBuf.
Closes George3d6#44. Will likely need another cargo fmt. Rustfmt bug preventing me from running again.
1 parent dbec589 commit 6bc80d8

File tree

13 files changed

+84
-118
lines changed

13 files changed

+84
-118
lines changed

agent/agent_plugins/alive/src/lib.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -22,17 +22,17 @@ struct Config {
2222
pub struct Plugin {
2323
last_call_ts: i64,
2424
periodicity: i64,
25-
enabled: bool,
25+
enabled: bool
2626
}
2727

2828
pub fn new(mut cfg_path: PathBuf) -> Result<Plugin, String> {
2929
cfg_path.push("alive.yml");
30-
let cfg = read_cfg::<Config>(cfg_path)?;
30+
let cfg = read_cfg::<Config>(&cfg_path)?;
3131
if cfg.enabled {
3232
Ok(Plugin {
33-
enabled: true,
33+
enabled: true,
3434
last_call_ts: 0,
35-
periodicity: cfg.periodicity,
35+
periodicity: cfg.periodicity
3636
})
3737
} else {
3838
Err("Alive plugin disabled".into())

agent/agent_plugins/command_runner/src/lib.rs

+7-8
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ extern crate serde_derive;
1010
use inquisitor_lib::{current_ts, read_cfg, AgentPlugin};
1111

1212
use std::collections::HashMap;
13-
use std::process::Command;
1413
use std::path::PathBuf;
14+
use std::process::Command;
1515

1616

1717
#[derive(Debug, PartialEq, Serialize, Deserialize)]
@@ -26,21 +26,20 @@ pub struct Plugin {
2626
last_call_map: HashMap<String, i64>,
2727
periodicity_map: HashMap<String, i64>,
2828
commands: Vec<Vec<String>>,
29-
enabled: bool,
30-
cfg_file: PathBuf
29+
enabled: bool
3130
}
3231

3332
pub fn new(mut cfg_path: PathBuf) -> Result<Plugin, String> {
34-
let cfg = read_cfg::<Config>(cfg_file)?;
33+
cfg_path.push("command_runner.yml");
34+
let cfg = read_cfg::<Config>(&cfg_path)?;
3535
if !cfg.enabled {
3636
return Err("Command runner disabled".into());
3737
}
3838
let mut new_plugin = Plugin {
39-
enabled: cfg.enabled,
40-
last_call_map: HashMap::new(),
39+
enabled: cfg.enabled,
40+
last_call_map: HashMap::new(),
4141
periodicity_map: HashMap::new(),
42-
commands: cfg.commands,
43-
cfg_file
42+
commands: cfg.commands
4443
};
4544
for i in 0..new_plugin.commands.len() {
4645
let command_name = new_plugin.commands[i].join(" ");

agent/agent_plugins/file_checker/src/lib.rs

+7-9
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,12 @@ extern crate serde_derive;
77
extern crate fs_extra;
88
extern crate serde_json;
99

10-
use inquisitor_lib::{current_ts, read_cfg, AgentPlugin};
1110
use fs_extra::dir::get_size;
11+
use inquisitor_lib::{current_ts, read_cfg, AgentPlugin};
1212
use std::collections::HashMap;
1313
use std::fs::File;
1414
use std::io::{BufRead, BufReader};
15+
use std::path::PathBuf;
1516

1617

1718
#[derive(Debug, PartialEq, Serialize, Deserialize)]
@@ -29,27 +30,24 @@ struct FileInfo {
2930
look_for: String
3031
}
3132

32-
3333
pub struct Plugin {
3434
last_call_ts: i64,
3535
periodicity: i64,
3636
file_info_map: HashMap<String, FileInfo>,
37-
enabled: bool,
38-
cfg_file: String
37+
enabled: bool
3938
}
4039

41-
pub fn new(cfg_dir: String) -> Result<Plugin, String> {
42-
let cfg_file = format!("{}/file_checker.yml", cfg_dir);
43-
let cfg = read_cfg::<Config>(cfg_file.clone())?;
40+
pub fn new(mut cfg_path: PathBuf) -> Result<Plugin, String> {
41+
cfg_path.push("file_checker.yml");
42+
let cfg = read_cfg::<Config>(&cfg_path)?;
4443
if !cfg.enabled {
4544
return Err("File checker disabled".into());
4645
}
4746
let mut plugin = Plugin {
4847
enabled: true,
4948
last_call_ts: 0,
5049
periodicity: cfg.periodicity,
51-
file_info_map: HashMap::new(),
52-
cfg_file: cfg_file.clone()
50+
file_info_map: HashMap::new()
5351
};
5452

5553
for i in 0..cfg.files.len() {

agent/agent_plugins/process_counter/src/lib.rs

+18-35
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@ extern crate serde_derive;
99

1010
use inquisitor_lib::{current_ts, read_cfg, AgentPlugin};
1111
use std::collections::HashMap;
12+
use std::path::PathBuf;
1213
use std::process::Command;
1314

14-
1515
#[derive(Debug, PartialEq, Serialize, Deserialize)]
1616
struct Config {
1717
enabled: bool,
@@ -24,43 +24,26 @@ pub struct Plugin {
2424
last_call_map: HashMap<String, i64>,
2525
periodicity_map: HashMap<String, i64>,
2626
processes: Vec<String>,
27-
enabled: bool,
28-
cfg_file: String
27+
enabled: bool
2928
}
3029

31-
impl Plugin {
32-
fn config(&mut self) -> Result<(), String> {
33-
let cfg = read_cfg::<Config>(self.cfg_file.clone())?;
34-
self.enabled = cfg.enabled;
35-
if !self.enabled {
36-
return Ok(());
37-
}
38-
self.processes = cfg.processes;
39-
40-
for i in 0..self.processes.len() {
41-
self.periodicity_map
42-
.insert(self.processes[i].clone(), cfg.periodicity_arr[i]);
43-
self.last_call_map.insert(self.processes[i].clone(), 0);
30+
pub fn new(mut cfg_path: PathBuf) -> Result<Plugin, String> {
31+
cfg_path.push("process_counter.yml");
32+
let cfg = read_cfg::<Config>(&cfg_path)?;
33+
if cfg.enabled {
34+
let mut plugin = Plugin {
35+
enabled: true,
36+
last_call_map: HashMap::new(),
37+
periodicity_map: HashMap::new(),
38+
processes: cfg.processes
39+
};
40+
for i in 0..plugin.processes.len() {
41+
plugin
42+
.periodicity_map
43+
.insert(plugin.processes[i].clone(), cfg.periodicity_arr[i]);
44+
plugin.last_call_map.insert(plugin.processes[i].clone(), 0);
4445
}
45-
Ok(())
46-
}
47-
}
48-
49-
pub fn new(cfg_dir: String) -> Result<Plugin, String> {
50-
let cfg_file = format!("{}/process_counter.yml", cfg_dir);
51-
52-
let mut new_plugin = Plugin {
53-
enabled: false,
54-
last_call_map: HashMap::new(),
55-
periodicity_map: HashMap::new(),
56-
processes: Vec::new(),
57-
cfg_file
58-
};
59-
60-
new_plugin.config()?;
61-
62-
if new_plugin.enabled {
63-
Ok(new_plugin)
46+
Ok(plugin)
6447
} else {
6548
Err("Process counter disabled".into())
6649
}

agent/agent_plugins/system_monitor/src/lib.rs

+13-27
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ extern crate sysinfo;
66

77
use inquisitor_lib::{current_ts, read_cfg, AgentPlugin};
88
use std::collections::HashMap;
9+
use std::path::PathBuf;
910
use sysinfo::{DiskExt, NetworkExt, ProcessorExt, System, SystemExt};
1011

1112

@@ -30,35 +31,20 @@ pub struct Plugin {
3031
sys: System,
3132
last_call_ts: i64,
3233
periodicity: i64,
33-
enabled: bool,
34-
cfg_file: String
34+
enabled: bool
3535
}
3636

37-
impl Plugin {
38-
fn config(&mut self) -> Result<(), String> {
39-
let cfg = read_cfg::<Config>(self.cfg_file.clone())?;
40-
self.enabled = cfg.enabled;
41-
if self.enabled {
42-
self.periodicity = cfg.periodicity;
43-
}
44-
Ok(())
45-
}
46-
}
47-
48-
pub fn new(cfg_dir: String) -> Result<Plugin, String> {
49-
let cfg_file = format!("{}/system_monitor.yml", cfg_dir);
50-
let mut new_plugin = Plugin {
51-
enabled: false,
52-
sys: System::new(),
53-
last_call_ts: 0,
54-
periodicity: 0,
55-
cfg_file
56-
};
57-
58-
new_plugin.config()?;
59-
60-
if new_plugin.enabled {
61-
Ok(new_plugin)
37+
pub fn new(mut cfg_path: PathBuf) -> Result<Plugin, String> {
38+
cfg_path.push("system_monitor.yml");
39+
let cfg = read_cfg::<Config>(&cfg_path)?;
40+
if cfg.enabled {
41+
let plugin = Plugin {
42+
enabled: true,
43+
sys: System::new(),
44+
last_call_ts: 0,
45+
periodicity: cfg.periodicity
46+
};
47+
Ok(plugin)
6248
} else {
6349
Err("System monitor disabled".into())
6450
}

agent/plugins/Cargo.toml

+4-4
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,10 @@ path = "src/lib.rs"
1010

1111
[dependencies]
1212
alive = {path = "../agent_plugins/alive"}
13-
#file_checker = {path = "../agent_plugins/file_checker"}
14-
#process_counter = {path = "../agent_plugins/process_counter"}
15-
#system_monitor = {path = "../agent_plugins/system_monitor"}
16-
#command_runner = {path = "../agent_plugins/command_runner"}
13+
file_checker = {path = "../agent_plugins/file_checker"}
14+
process_counter = {path = "../agent_plugins/process_counter"}
15+
system_monitor = {path = "../agent_plugins/system_monitor"}
16+
command_runner = {path = "../agent_plugins/command_runner"}
1717

1818
inquisitor_lib = {path = "../../inquisitor_lib"}
1919
log = "0.4.1"

agent/src/main.rs

+9-12
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,14 @@ extern crate serde_json;
1010
extern crate serde_derive;
1111
extern crate tokio;
1212

13+
use clap::{App, Arg};
1314
use inquisitor_lib::{current_ts, read_cfg, AgentPlugin, Status};
15+
use std::env::current_exe;
1416
use std::net::SocketAddr;
17+
use std::path::PathBuf;
1518
use std::{thread, time};
1619
use tokio::net::TcpStream;
1720
use tokio::prelude::Future;
18-
use clap::{App, Arg};
19-
use std::env::current_exe;
20-
use std::path::PathBuf;
2121

2222

2323
#[derive(Debug, PartialEq, Serialize, Deserialize)]
@@ -28,8 +28,8 @@ struct ReceptorAddr {
2828

2929
#[derive(Debug, PartialEq, Serialize, Deserialize)]
3030
struct Config {
31-
machine_identifier: Option<String>,
32-
receptor: ReceptorAddr
31+
machine_identifier: Option<String>,
32+
receptor: ReceptorAddr
3333
}
3434

3535

@@ -62,18 +62,15 @@ fn main() {
6262
let mut agent_config = config_dir.clone();
6363
agent_config.push("agent_config.yml");
6464

65-
let config = read_cfg::<Config>(format!("{}/{}", config_dir, "agent_config.yml")).unwrap();
65+
let config = read_cfg::<Config>(&agent_config).unwrap();
6666

6767
let mut plugins = plugins::init(config_dir);
6868

69-
let hostname = config.machine_identifier
69+
let hostname = config
70+
.machine_identifier
7071
.unwrap_or_else(|| hostname::get_hostname().unwrap());
7172

72-
let addr = format!(
73-
"{}:{}",
74-
config.receptor.host,
75-
config.receptor.port
76-
);
73+
let addr = format!("{}:{}", config.receptor.host, config.receptor.port);
7774

7875
let mut sender = StatusSender::new(hostname, addr.parse().expect("Couldn't convert IP address"));
7976

inquisitor_lib/Cargo.toml

-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ serde = "1.0.37"
88
serde_derive = "1.0.37"
99
serde_yaml = "0.7.3"
1010
fs_extra = "1.1.0"
11-
yaml-rust = "0.4.0"
1211
url= "1.7.0"
1312
log = "0.4.1"
1413
env_logger = "0.5.6"

inquisitor_lib/src/lib.rs

+1-3
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,13 @@
22
extern crate serde_derive;
33
extern crate serde;
44
extern crate serde_yaml;
5-
extern crate yaml_rust;
65
#[macro_use]
76
extern crate log;
87
extern crate env_logger;
98
extern crate hyper;
109
extern crate rusqlite;
1110
extern crate url;
1211

13-
use self::yaml_rust::{Yaml, YamlLoader};
1412
extern crate fs_extra;
1513
use self::fs_extra::file::read_to_string;
1614
use self::hyper::server::Request;
@@ -37,7 +35,7 @@ pub fn current_ts() -> i64 {
3735
.as_secs() as i64
3836
}
3937

40-
pub fn read_cfg<ConfigT>(cfg_file_path: String) -> Result<ConfigT, String>
38+
pub fn read_cfg<ConfigT>(cfg_file_path: &PathBuf) -> Result<ConfigT, String>
4139
where
4240
ConfigT: DeserializeOwned
4341
{

receptor/plugins/src/plugin_initialization.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ macro_rules! plugins {
22
( $( $x:ident ),* ) => {
33
$(extern crate $x;)*
44

5-
pub fn init(config_dir: String) -> Vec<Box<ReceptorPlugin>> {
5+
pub fn init(config_dir: std::path::PathBuf) -> Vec<Box<ReceptorPlugin>> {
66
let mut v: Vec<Box<ReceptorPlugin>> = vec!();
77
$(
88
match $x::new(config_dir.clone()) {

receptor/receptor_plugins/comparator/src/lib.rs

+7-5
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ extern crate env_logger;
1010
use inquisitor_lib::{current_ts, read_cfg, ReceptorPlugin};
1111
use rusqlite::Connection;
1212
use std::collections::HashMap;
13+
use std::path::PathBuf;
1314

1415

1516
#[derive(Debug, PartialEq, Serialize, Deserialize)]
@@ -27,12 +28,12 @@ pub struct Plugin {
2728
enabled: bool,
2829
checks: Vec<Vec<String>>,
2930
keys: Vec<Vec<String>>,
30-
cfg_file: String
31+
cfg_path: PathBuf
3132
}
3233

3334
impl Plugin {
3435
fn config(&mut self) -> Result<(), String> {
35-
let cfg = read_cfg::<Config>(self.cfg_file.clone())?;
36+
let cfg = read_cfg::<Config>(&self.cfg_path)?;
3637
self.enabled = cfg.enabled;
3738
if !self.enabled {
3839
return Ok(());
@@ -48,14 +49,15 @@ impl Plugin {
4849
}
4950
}
5051

51-
pub fn new(cfg_dir: String) -> Result<Plugin, String> {
52+
pub fn new(mut cfg_path: PathBuf) -> Result<Plugin, String> {
53+
cfg_path.push("comparator.yml");
5254
let mut new_plugin = Plugin {
53-
enabled: true,
55+
enabled: false,
5456
last_call_ts: current_ts(),
5557
periodicity: 0,
5658
keys: vec![],
5759
checks: vec![],
58-
cfg_file: format!("{}/comparator.yml", cfg_dir)
60+
cfg_path
5961
};
6062

6163
new_plugin.config()?;

0 commit comments

Comments
 (0)