From 6f7ea17a43e710388507a85a9d621587188a2575 Mon Sep 17 00:00:00 2001 From: Jochen Maes Date: Wed, 1 Nov 2023 10:30:46 +0100 Subject: [PATCH] Adds system gatherer, and ... fixes version.sh for the ADC version fixes .drone.yml for some reason it installed the gitea apk instead of the git. (typo) --- .drone.yml | 2 +- Cargo.lock | 152 ++++++++++++++++++++++++++++++++++++++++ Cargo.toml | 1 + src/gatherers/mod.rs | 1 + src/gatherers/system.rs | 110 +++++------------------------ src/main.rs | 8 +++ version.sh | 2 +- 7 files changed, 180 insertions(+), 96 deletions(-) diff --git a/.drone.yml b/.drone.yml index 99a2fd6..e0116c8 100644 --- a/.drone.yml +++ b/.drone.yml @@ -21,7 +21,7 @@ steps: - name: build amd64 image: rust:1.73-alpine commands: - - apk add --no-cache musl-dev gcc-aarch64-none-elf bash gitea + - apk add --no-cache musl-dev gcc-aarch64-none-elf bash git - git fetch --all --tags - pwd && ls -al && sh ./version.sh - mkdir /artifacts/mouse-amd64 diff --git a/Cargo.lock b/Cargo.lock index b6fefd9..82ca963 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -50,6 +50,18 @@ dependencies = [ "windows-sys", ] +[[package]] +name = "autocfg" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d468802bab17cbc0cc575e9b053f41e72aa36bfa6b7f55e3529ffa43161b97fa" + +[[package]] +name = "cfg-if" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" + [[package]] name = "clap" version = "4.4.7" @@ -96,6 +108,45 @@ version = "1.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "acbf1af155f9b9ef647e42cdc158db4b64a1b61f743629225fde6f3e0be2a7c7" +[[package]] +name = "core-foundation-sys" +version = "0.8.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e496a50fda8aacccc86d7529e2c1e0892dbd0f898a6b5645b5561b89c3210efa" + +[[package]] +name = "crossbeam-deque" +version = "0.8.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ce6fd6f855243022dcecf8702fef0c297d4338e226845fe067f6341ad9fa0cef" +dependencies = [ + "cfg-if", + "crossbeam-epoch", + "crossbeam-utils", +] + +[[package]] +name = "crossbeam-epoch" +version = "0.9.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ae211234986c545741a7dc064309f67ee1e5ad243d0e48335adc0484d960bcc7" +dependencies = [ + "autocfg", + "cfg-if", + "crossbeam-utils", + "memoffset", + "scopeguard", +] + +[[package]] +name = "crossbeam-utils" +version = "0.8.16" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5a22b2d63d4d1dc0b7f1b6b2747dd0088008a9be28b6ddf0b1e7d335e3037294" +dependencies = [ + "cfg-if", +] + [[package]] name = "csv" version = "1.3.0" @@ -117,6 +168,12 @@ dependencies = [ "memchr", ] +[[package]] +name = "either" +version = "1.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a26ae43d7bcc3b814de94796a5e736d4029efb0ee900c12e2d54c993ad1a1e07" + [[package]] name = "equivalent" version = "1.0.1" @@ -151,6 +208,12 @@ version = "1.0.9" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "af150ab688ff2122fcef229be89cb50dd66af9e01a4ff320cc137eecc9bacc38" +[[package]] +name = "libc" +version = "0.2.149" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a08173bc88b7955d1b3145aa561539096c421ac8debde8cbc3612ec635fee29b" + [[package]] name = "linked-hash-map" version = "0.5.6" @@ -163,6 +226,15 @@ version = "2.6.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f665ee40bc4a3c5590afb1e9677db74a508659dfd71e126420da8274909a0167" +[[package]] +name = "memoffset" +version = "0.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5a634b1c61a95585bd15607c6ab0c4e5b226e695ff2800ba0cdccddf208c406c" +dependencies = [ + "autocfg", +] + [[package]] name = "mouse" version = "0.0.0" @@ -172,9 +244,25 @@ dependencies = [ "serde", "serde_json", "serde_yaml", + "sysinfo", "yaml-rust", ] +[[package]] +name = "ntapi" +version = "0.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e8a3895c6391c39d7fe7ebc444a87eb2991b2a0bc718fdabd071eec617fc68e4" +dependencies = [ + "winapi", +] + +[[package]] +name = "once_cell" +version = "1.18.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dd8b5dd2ae5ed71462c540258bedcb51965123ad7e7ccf4b9a8cafaa4a63576d" + [[package]] name = "proc-macro2" version = "1.0.69" @@ -193,12 +281,38 @@ dependencies = [ "proc-macro2", ] +[[package]] +name = "rayon" +version = "1.8.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9c27db03db7734835b3f53954b534c91069375ce6ccaa2e065441e07d9b6cdb1" +dependencies = [ + "either", + "rayon-core", +] + +[[package]] +name = "rayon-core" +version = "1.12.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5ce3fb6ad83f861aac485e76e1985cd109d9a3713802152be56c3b1f0e0658ed" +dependencies = [ + "crossbeam-deque", + "crossbeam-utils", +] + [[package]] name = "ryu" version = "1.0.15" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1ad4cc8da4ef723ed60bced201181d83791ad433213d8c24efffda1eec85d741" +[[package]] +name = "scopeguard" +version = "1.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "94143f37725109f92c262ed2cf5e59bce7498c01bcc1502d7b9afe439a4e9f49" + [[package]] name = "serde" version = "1.0.190" @@ -260,6 +374,22 @@ dependencies = [ "unicode-ident", ] +[[package]] +name = "sysinfo" +version = "0.29.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0a18d114d420ada3a891e6bc8e96a2023402203296a47cdd65083377dad18ba5" +dependencies = [ + "cfg-if", + "core-foundation-sys", + "libc", + "ntapi", + "once_cell", + "rayon", + "serde", + "winapi", +] + [[package]] name = "unicode-ident" version = "1.0.12" @@ -278,6 +408,28 @@ version = "0.2.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "711b9620af191e0cdc7468a8d14e709c3dcdb115b36f838e601583af800a370a" +[[package]] +name = "winapi" +version = "0.3.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419" +dependencies = [ + "winapi-i686-pc-windows-gnu", + "winapi-x86_64-pc-windows-gnu", +] + +[[package]] +name = "winapi-i686-pc-windows-gnu" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" + +[[package]] +name = "winapi-x86_64-pc-windows-gnu" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" + [[package]] name = "windows-sys" version = "0.48.0" diff --git a/Cargo.toml b/Cargo.toml index 167ddff..904983a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -9,6 +9,7 @@ edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] +sysinfo = { version = "0.29.10", features = ["serde"] } yaml-rust = "0.4.5" clap = { version = "4.4.7", features = ["derive"] } csv = "1.3.0" diff --git a/src/gatherers/mod.rs b/src/gatherers/mod.rs index 01f2c67..948a5c4 100644 --- a/src/gatherers/mod.rs +++ b/src/gatherers/mod.rs @@ -4,3 +4,4 @@ pub mod environment; pub mod ip; +pub mod system; diff --git a/src/gatherers/system.rs b/src/gatherers/system.rs index 4b4eba6..4181243 100644 --- a/src/gatherers/system.rs +++ b/src/gatherers/system.rs @@ -2,106 +2,28 @@ // License, v. 2.0. If a copy of the MPL was not distributed with this // file, You can obtain one at http://mozilla.org/MPL/2.0/. -use std::process::Command; +use sysinfo::{System, SystemExt}; -use crate::types::fact::{Fact, FactData}; -use serde::{Deserialize, Serialize}; +use crate::types::fact::Fact; pub struct SystemData {} - impl SystemData {} -#[derive(Serialize, Deserialize)] -struct UnameData { - kernel_name: String, - node_name: String, - kernel_release: String, - kernel_version: String, - machine: String, - processor: String, - hardware_platform: String, - operating_system: String, -} - -#[derive(Serialize, Deserialize)] -struct IpAddrInfo { - family: String, - local: String, - prefixlen: i32, - broadcast: Option, - scope: String, - dynamic: Option, - noprefixroute: Option, - label: Option, - valid_life_time: i64, - preferred_life_time: i64, -} - -#[derive(Serialize, Deserialize)] -struct IpAddr { - ifindex: i32, - ifname: String, - flags: Vec, - mtu: i32, - qdisc: String, - operstate: String, - group: String, - txqlen: i32, - link_type: String, - address: Option, - broadcast: Option, - addr_info: Vec, -} - impl Fact for SystemData { - fn gather(&self) -> Vec { - let mut vfd: Vec = 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 = 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, - }) - } - } - } + fn gather(&self) -> String { + if System::IS_SUPPORTED { + let mut sys = System::new_all(); + // we only need certain values. + sys.refresh_cpu(); + sys.refresh_disks(); + sys.refresh_memory(); + sys.refresh_system(); + sys.refresh_components(); + sys.refresh_disks_list(); + sys.refresh_users_list(); + serde_json::to_string(&sys).unwrap() + } else { + "{\"error\": \"Not Supported\"}".to_string() } - return vfd; } } diff --git a/src/main.rs b/src/main.rs index 4ac0800..5ffaa0a 100644 --- a/src/main.rs +++ b/src/main.rs @@ -13,6 +13,7 @@ use crate::gatherers::environment::EnvironmentData; use crate::gatherers::ip::{IPData, IPRouteData}; use crate::types::fact::Fact; use clap::Parser; +use gatherers::system::SystemData; use serde_json::Value; use std::collections::{HashMap, HashSet}; use yaml_rust::{YamlEmitter, YamlLoader}; @@ -58,6 +59,12 @@ fn gather_list(gatherers: HashSet, output: &str) -> String { .clone(), ); } + "system" => { + outmap.insert( + "system".to_string(), + serde_json::from_str(&SystemData {}.gather()).unwrap(), + ); + } x => { println!("unknown gatherer: {x}"); } @@ -80,6 +87,7 @@ fn main() { "env".to_string(), "ipaddr".to_string(), "iproute".to_string(), + "system".to_string(), ]); let data_output: String = match args.gatherer { None => gather_list(all, output_format), diff --git a/version.sh b/version.sh index 1ea367d..b1089b8 100755 --- a/version.sh +++ b/version.sh @@ -8,7 +8,7 @@ ADC=${DRONE_COMMIT:0:7} if [[ "${LTCOMMIT}" == "${ADC}" ]]; then VERSION="${DRONE_TAG##v}" else - VERSION="${LTAG##v}-next-${ADV}" + VERSION="${LTAG##v}-next-${ADC}" fi sed -i "s/0\\.0\\.0/${VERSION}/" Cargo.toml