Skip to content

Commit dfacff2

Browse files
committed
Formated and cleaned up code, added some debugging lines, fixed where plugins look for their config
1 parent c26aa9f commit dfacff2

File tree

12 files changed

+45
-58
lines changed

12 files changed

+45
-58
lines changed

agent/agent_plugins/alive/src/lib.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ use agent_lib::{current_ts, read_cfg, AgentPlugin};
1313

1414
#[derive(Debug, PartialEq, Serialize, Deserialize)]
1515
struct Config {
16-
enabled: bool,
17-
periodicity: i64,
16+
enabled: bool,
17+
periodicity: i64
1818
}
1919

2020

@@ -26,13 +26,13 @@ pub struct Plugin {
2626

2727
impl Plugin {
2828
fn config(&mut self) -> Result<(), String> {
29-
let cfg = read_cfg::<Config>("command_runner.yml")?;
29+
let cfg = read_cfg::<Config>("alive.yml")?;
3030
self.enabled = cfg.enabled;
3131
if !self.enabled {
32-
return Ok(())
32+
return Ok(());
3333
}
3434
self.periodicity = cfg.periodicity;
35-
return Ok(())
35+
Ok(())
3636
}
3737
}
3838

agent/agent_plugins/command_runner/src/lib.rs

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,17 @@ extern crate serde_json;
77
#[macro_use]
88
extern crate serde_derive;
99

10-
use agent_lib::{current_ts, AgentPlugin, read_cfg};
10+
use agent_lib::{current_ts, read_cfg, AgentPlugin};
1111

1212
use std::collections::HashMap;
1313
use std::process::Command;
1414

1515

1616
#[derive(Debug, PartialEq, Serialize, Deserialize)]
1717
pub struct Config {
18-
enabled: bool,
19-
periodicity_arr: Vec<i64>,
20-
commands: Vec<Vec<String>>
18+
enabled: bool,
19+
periodicity_arr: Vec<i64>,
20+
commands: Vec<Vec<String>>
2121
}
2222

2323

@@ -34,15 +34,17 @@ impl Plugin {
3434
let cfg = read_cfg::<Config>("command_runner.yml")?;
3535
plugin.enabled = cfg.enabled;
3636
if !plugin.enabled {
37-
return Ok(())
37+
return Ok(());
3838
}
3939
plugin.commands = cfg.commands;
4040
for i in 0..plugin.commands.len() {
4141
let command_name = plugin.commands[i].join(" ");
42-
plugin.periodicity_map.insert(command_name.clone(), cfg.periodicity_arr[i]);
42+
plugin
43+
.periodicity_map
44+
.insert(command_name.clone(), cfg.periodicity_arr[i]);
4345
plugin.last_call_map.insert(command_name, 0);
4446
}
45-
return Ok(());
47+
Ok(())
4648
}
4749
}
4850

@@ -85,9 +87,9 @@ impl AgentPlugin for Plugin {
8587
let output = cmd.output().map_err(|e| e.to_string())?;
8688

8789
let str_output = if output.status.success() {
88-
String::from_utf8(output.stdout).map_err(|e| e.to_string())?;
90+
String::from_utf8(output.stdout).map_err(|e| e.to_string())?
8991
} else {
90-
String::from_utf8(output.stderr).map_err(|e| e.to_string())?;
92+
String::from_utf8(output.stderr).map_err(|e| e.to_string())?
9193
};
9294

9395
results.insert(command_name, str_output);

agent/agent_plugins/file_checker/src/lib.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
extern crate agent_lib;
55
#[macro_use]
66
extern crate serde_derive;
7-
extern crate serde_json;
87
extern crate fs_extra;
8+
extern crate serde_json;
99

1010
use agent_lib::{current_ts, read_cfg, AgentPlugin};
1111
use fs_extra::dir::get_size;
@@ -16,10 +16,10 @@ use std::io::{BufRead, BufReader};
1616

1717
#[derive(Debug, PartialEq, Serialize, Deserialize)]
1818
struct Config {
19-
enabled: bool,
20-
periodicity: i64,
21-
files: Vec<String>,
22-
keyphrase: Vec<String>,
19+
enabled: bool,
20+
periodicity: i64,
21+
files: Vec<String>,
22+
keyphrase: Vec<String>
2323
}
2424

2525

@@ -38,11 +38,11 @@ pub struct Plugin {
3838
}
3939

4040
impl Plugin {
41-
fn config(plugin: &mut Plugin) -> Result<(), String> {
42-
let cfg = read_cfg::<Config>("command_runner.yml")?;
41+
fn config(plugin: &mut Plugin) -> Result<(), String> {
42+
let cfg = read_cfg::<Config>("file_checker.yml")?;
4343
plugin.enabled = cfg.enabled;
4444
if !plugin.enabled {
45-
return Ok(())
45+
return Ok(());
4646
}
4747
plugin.periodicity = cfg.periodicity;
4848

agent/agent_plugins/process_counter/src/lib.rs

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@ use std::process::Command;
1414

1515
#[derive(Debug, PartialEq, Serialize, Deserialize)]
1616
struct Config {
17-
enabled: bool,
18-
periodicity_arr: Vec<i64>,
19-
processes: Vec<String>,
17+
enabled: bool,
18+
periodicity_arr: Vec<i64>,
19+
processes: Vec<String>
2020
}
2121

2222

@@ -29,19 +29,16 @@ pub struct Plugin {
2929

3030
impl Plugin {
3131
fn config(&mut self) -> Result<(), String> {
32-
let cfg = read_cfg::<Config>("command_runner.yml")?;
32+
let cfg = read_cfg::<Config>("process_counter.yml")?;
3333
self.enabled = cfg.enabled;
3434
if !self.enabled {
35-
return Ok(())
35+
return Ok(());
3636
}
37-
self.periodicity = cfg.periodicity;
38-
3937
self.processes = cfg.processes;
4038

41-
42-
4339
for i in 0..self.processes.len() {
44-
self.periodicity_map.insert(self.processes[i].clone(), cfg.periodicity_arr[i]);
40+
self.periodicity_map
41+
.insert(self.processes[i].clone(), cfg.periodicity_arr[i]);
4542
self.last_call_map.insert(self.processes[i].clone(), 0);
4643
}
4744
return Ok(())
@@ -56,7 +53,7 @@ pub fn new() -> Result<Plugin, String> {
5653
processes: Vec::new()
5754
};
5855

59-
Plugin::config(&mut new_plugin);
56+
Plugin::config(&mut new_plugin)?;
6057

6158
if new_plugin.enabled {
6259
Ok(new_plugin)

agent/agent_plugins/system_monitor/src/lib.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ use std::collections::HashMap;
1111

1212
#[derive(Debug, PartialEq, Serialize, Deserialize)]
1313
struct Config {
14-
enabled: bool,
15-
periodicity: i64,
14+
enabled: bool,
15+
periodicity: i64
1616
}
1717

1818

@@ -35,10 +35,10 @@ pub struct Plugin {
3535

3636
impl Plugin {
3737
fn config(&mut self) -> Result<(), String> {
38-
let cfg = read_cfg::<Config>("command_runner.yml")?;
38+
let cfg = read_cfg::<Config>("system_monitor.yml")?;
3939
self.enabled = cfg.enabled;
4040
if !self.enabled {
41-
return Ok(())
41+
return Ok(());
4242
}
4343
self.periodicity = cfg.periodicity;
4444
return Ok(())

agent/plugins/src/plugin_initialization.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ macro_rules! plugins {
77
$(
88
match $x::new() {
99
Ok(x) => {info!("{} successfully loaded", x.name()); v.push(Box::new(x)) }
10-
Err(x) => {error!("{}", x)}
10+
Err(err) => {error!("Plugin: {}, error: {}", stringify!($x), err)}
1111
}
1212
)*
1313
v

receptor/plugins/src/plugin_initialization.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ macro_rules! plugins {
77
$(
88
match $x::new() {
99
Ok(x) => {info!("{} successfully loaded", x.name()); v.push(Box::new(x)) }
10-
Err(err) => {error!("{}", err)}
10+
Err(err) => {error!("Plugin: {}, error: {}", stringify!($x), err)}
1111
}
1212
)*
1313
v

receptor/receptor_lib/src/utils.rs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,10 @@ extern crate hyper;
33
extern crate url;
44
extern crate yaml_rust;
55

6-
use self::fs_extra::file::read_to_string;
76
use self::hyper::server::Request;
87
use self::url::Url;
9-
use self::yaml_rust::{Yaml, YamlLoader};
108

119
use std::collections::HashMap;
12-
use std::env::current_exe;
13-
use std::time::{SystemTime, UNIX_EPOCH};
1410

1511
pub fn get_url_params(req: &Request) -> HashMap<String, String> {
1612
let parsed_url = Url::parse(&format!("http://badhyper.io/{}", req.uri().as_ref())).unwrap();

receptor/receptor_plugins/sync_check/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ pub struct Plugin {
2525

2626
impl Plugin {
2727
fn config(&mut self) -> Result<(), String> {
28-
let cfg = read_cfg::<Config>("command_runner.yml")?;
28+
let cfg = read_cfg::<Config>("sync_check.yml")?;
2929
self.enabled = cfg.enabled;
3030
if !self.enabled {
3131
return Ok(());

receptor/src/database.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,5 @@ pub fn initialize_database() {
4848
}
4949

5050
pub fn get_connection() -> Connection {
51-
let conn = Connection::open("database.sqlite").expect("Can't open database connection");
52-
53-
conn
51+
Connection::open("database.sqlite").expect("Can't open database connection")
5452
}

receptor/src/main.rs

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,12 @@ extern crate fs_extra;
55
extern crate futures;
66
extern crate hyper;
77
extern crate hyper_staticfile;
8-
98
#[macro_use]
10-
119
extern crate log;
12-
1310
extern crate plugins;
1411
extern crate receptor_lib;
1512
extern crate rusqlite;
16-
extern crate serde_derive;
17-
18-
#[macro_use]
19-
2013
extern crate serde_json;
21-
2214
extern crate tokio;
2315
extern crate tokio_core;
2416

@@ -131,7 +123,7 @@ impl Service for DataServer {
131123
}
132124

133125
_ => response.set_status(StatusCode::NotFound)
134-
}
126+
};
135127

136128
Box::new(futures::future::ok(response))
137129
} else if req.path() == "/plugin_list" {
@@ -177,7 +169,7 @@ fn proces_status(stream: TcpStream, db_conn: Connection) {
177169
let (reader, _) = stream.split();
178170

179171
let conn = tokio::io::read_to_end(reader, Vec::new()).then(move |res| {
180-
let payload = Vec::from(res.expect("Can't read input from agent").1);
172+
let payload = res.expect("Can't read input from agent").1;
181173

182174
let statuses: Vec<Status> = serde_json::from_slice(&payload).expect("Can't deserialize status");
183175

@@ -315,7 +307,7 @@ fn main() {
315307
let handler = handle.clone();
316308

317309
let handleds = handle.clone();
318-
310+
debug!("Serving web_UI from: {}", format!("{}{}", root.to_str().unwrap(), "/web_ui/"));
319311
let serve = Http::new()
320312
.serve_addr_handle(&server_addr, &handle, move || {
321313
Ok(DataServer {
@@ -328,6 +320,7 @@ fn main() {
328320
})
329321
.expect("Can't start HTTP server");
330322

323+
debug!("Spawning server !");
331324
handler.spawn(
332325
serve
333326
.for_each(move |conn| {

shared_lib/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ pub fn get_yml_config_string(name: &str) -> Result<String, String> {
5252
let mut cfg_file_path = current_exe().unwrap();
5353
cfg_file_path.pop();
5454
cfg_file_path.push(name);
55+
println!("{:?}", cfg_file_path);
5556
return Ok(read_to_string(&cfg_file_path).map_err(|e| e.to_string())?)
5657
}
5758

0 commit comments

Comments
 (0)