adds ip gathering
Signed-off-by: Jochen maes <jochen@sejo-it.be>
This commit is contained in:
parent
c325024f80
commit
09757baf4a
3
.gitignore
vendored
3
.gitignore
vendored
@ -1 +1,4 @@
|
||||
/target
|
||||
.vscode/
|
||||
*.un~
|
||||
*.swp
|
||||
|
@ -1,4 +1,4 @@
|
||||
use crate::types::{fact::{Fact, FactData}};
|
||||
use crate::types::fact::{FactData, Fact};
|
||||
use std::env;
|
||||
|
||||
pub struct EnvironmentData {}
|
||||
|
91
src/gatherers/ip.rs
Normal file
91
src/gatherers/ip.rs
Normal file
@ -0,0 +1,91 @@
|
||||
use std::process::Command;
|
||||
|
||||
use crate::types::fact::{Fact, FactData};
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
pub struct IPData {}
|
||||
|
||||
impl IPData {}
|
||||
|
||||
#[derive(Serialize, Deserialize)]
|
||||
struct IpAddrInfo {
|
||||
family: String,
|
||||
local: String,
|
||||
prefixlen: i32,
|
||||
broadcast: Option<String>,
|
||||
scope: String,
|
||||
dynamic: Option<bool>,
|
||||
noprefixroute: Option<bool>,
|
||||
label: Option<String>,
|
||||
valid_life_time: i64,
|
||||
preferred_life_time: i64,
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize)]
|
||||
struct IpAddr {
|
||||
ifindex: i32,
|
||||
ifname: String,
|
||||
flags: Vec<String>,
|
||||
mtu: i32,
|
||||
qdisc: String,
|
||||
operstate: String,
|
||||
group: String,
|
||||
txqlen: i32,
|
||||
link_type: String,
|
||||
address: Option<String>,
|
||||
broadcast: Option<String>,
|
||||
addr_info: Vec<IpAddrInfo>,
|
||||
}
|
||||
|
||||
impl Fact for IPData {
|
||||
fn gather(&self) -> Vec<FactData> {
|
||||
let mut vfd: Vec<FactData> = vec![];
|
||||
let time_set = self.get_epoch_ms();
|
||||
let output = Command::new("ip")
|
||||
.arg("-j")
|
||||
.arg("addr")
|
||||
.output()
|
||||
.expect("Failed to execute ip");
|
||||
let stdout = String::from_utf8(output.stdout).unwrap();
|
||||
let ips: Vec<IpAddr> = serde_json::from_str(stdout.as_str()).unwrap();
|
||||
for ip in ips {
|
||||
if ip.ifname != "lo" {
|
||||
for addr_info in ip.addr_info {
|
||||
let fd = FactData {
|
||||
name: format!("ip_addr_{}_{}_addr", ip.ifname, addr_info.family),
|
||||
value: addr_info.local,
|
||||
time_set: time_set,
|
||||
};
|
||||
vfd.push(fd);
|
||||
vfd.push(FactData {
|
||||
name: format!("ip_addr_{}_{}_prefixlen", ip.ifname, addr_info.family),
|
||||
value: addr_info.prefixlen.to_string(),
|
||||
time_set: time_set,
|
||||
});
|
||||
if addr_info.label.is_some() {
|
||||
vfd.push(FactData {
|
||||
name: format!("ip_addr_{}_{}_label", ip.ifname, addr_info.family),
|
||||
value: addr_info.label.unwrap(),
|
||||
time_set: time_set,
|
||||
});
|
||||
}
|
||||
if addr_info.broadcast.is_some() {
|
||||
vfd.push(FactData {
|
||||
name: format!("ip_addr_{}_{}_broadcast", ip.ifname, addr_info.family),
|
||||
value: addr_info.broadcast.unwrap(),
|
||||
time_set: time_set,
|
||||
});
|
||||
}
|
||||
if ip.address.is_some() {
|
||||
vfd.push(FactData {
|
||||
name: format!("ip_addr_{}_macaddr", ip.ifname),
|
||||
value: ip.address.clone().unwrap(),
|
||||
time_set: time_set,
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return vfd;
|
||||
}
|
||||
}
|
@ -1 +1,2 @@
|
||||
pub mod environment;
|
||||
pub mod ip;
|
12
src/main.rs
12
src/main.rs
@ -4,8 +4,9 @@ mod gatherers;
|
||||
use std::collections::HashSet;
|
||||
use crate::types::fact::{Fact, FactData};
|
||||
use crate::gatherers::environment::EnvironmentData;
|
||||
use crate::gatherers::ip::IPData;
|
||||
use clap::Parser;
|
||||
use csv::{ Writer};
|
||||
use csv::Writer;
|
||||
|
||||
|
||||
#[derive(Parser)]
|
||||
@ -22,6 +23,7 @@ struct Args {
|
||||
fn gather_all() -> Vec<FactData>{
|
||||
let mut data: Vec<FactData> = vec![];
|
||||
data.append(&mut gather(&EnvironmentData{}));
|
||||
data.append(&mut gather(&IPData{}));
|
||||
return data;
|
||||
}
|
||||
|
||||
@ -35,6 +37,9 @@ fn gather_list(gatherers: HashSet<String>) -> Vec<FactData> {
|
||||
"env" => for d in gather(&EnvironmentData{}) {
|
||||
data.push(d);
|
||||
},
|
||||
"ip" => for d in gather(&IPData{}) {
|
||||
data.push(d);
|
||||
}
|
||||
_ => for d in gather_all() {
|
||||
data.push(d);
|
||||
}
|
||||
@ -49,7 +54,7 @@ fn main() {
|
||||
let args = Args::parse();
|
||||
let mut data:Vec<FactData> = vec![];
|
||||
|
||||
if !args.gatherer.is_some() {
|
||||
if args.gatherer.is_none() {
|
||||
data.append(&mut gather_all());
|
||||
} else {
|
||||
// sanitize the gatherer list
|
||||
@ -59,9 +64,6 @@ fn main() {
|
||||
};
|
||||
data.append(&mut gather_list(ugatherers));
|
||||
}
|
||||
println!("got here: {}", data.len());
|
||||
|
||||
|
||||
match args.output.as_str() {
|
||||
"json" => {
|
||||
let serialized_json = serde_json::to_string(&data).unwrap();
|
||||
|
Loading…
Reference in New Issue
Block a user