From 1373db17767d6a2d5caf9b1715031c561dcb431d Mon Sep 17 00:00:00 2001 From: Michael Ziegler Date: Mon, 9 Mar 2020 09:35:05 +0100 Subject: [PATCH 1/2] add processes table [WIP] --- Cargo.lock | 16 ++++++++++++++++ Cargo.toml | 1 + src/main.rs | 8 +++++++- src/mib_procs.rs | 33 +++++++++++++++++++++++++++++++++ 4 files changed, 57 insertions(+), 1 deletion(-) create mode 100644 src/mib_procs.rs diff --git a/Cargo.lock b/Cargo.lock index c43e88a..1342c66 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -178,6 +178,19 @@ dependencies = [ "ws2_32-sys 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "pkg-config" +version = "0.3.9" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "procps-sys" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "pkg-config 0.3.9 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "redox_syscall" version = "0.1.31" @@ -219,6 +232,7 @@ dependencies = [ "error-chain 0.11.0 (registry+https://github.com/rust-lang/crates.io-index)", "libc 0.2.33 (registry+https://github.com/rust-lang/crates.io-index)", "mio 0.6.11 (registry+https://github.com/rust-lang/crates.io-index)", + "procps-sys 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)", "snmp 0.2.2 (git+https://github.com/Svedrin/rust-snmp?branch=feature/response-pdus)", "uname 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)", "yaml-rust 0.3.5 (registry+https://github.com/rust-lang/crates.io-index)", @@ -306,6 +320,8 @@ source = "registry+https://github.com/rust-lang/crates.io-index" "checksum mio 0.6.11 (registry+https://github.com/rust-lang/crates.io-index)" = "0e8411968194c7b139e9105bc4ae7db0bae232af087147e72f0616ebf5fdb9cb" "checksum miow 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "8c1f2f3b1cf331de6896aabf6e9d55dca90356cc9960cca7eaaf408a355ae919" "checksum net2 0.2.31 (registry+https://github.com/rust-lang/crates.io-index)" = "3a80f842784ef6c9a958b68b7516bc7e35883c614004dd94959a4dca1b716c09" +"checksum pkg-config 0.3.9 (registry+https://github.com/rust-lang/crates.io-index)" = "3a8b4c6b8165cd1a1cd4b9b120978131389f64bdaf456435caa41e630edba903" +"checksum procps-sys 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "0dd9e001e5c1c43cba2908b96a8d74bf65378cd40efdfa583a0cd6b002cfd260" "checksum redox_syscall 0.1.31 (registry+https://github.com/rust-lang/crates.io-index)" = "8dde11f18c108289bef24469638a04dce49da56084f2d50618b226e47eb04509" "checksum redox_termios 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "7e891cfe48e9100a70a3b6eb652fef28920c117d366339687bd5576160db0f76" "checksum rustc-demangle 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)" = "aee45432acc62f7b9a108cc054142dac51f979e69e71ddce7d6fc7adf29e817e" diff --git a/Cargo.toml b/Cargo.toml index 38553ad..c193c21 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -11,3 +11,4 @@ uname = "*" libc = "*" clap = "*" yaml-rust = "0.3" +procps-sys = "*" diff --git a/src/main.rs b/src/main.rs index 42f2b0e..1ee7c91 100644 --- a/src/main.rs +++ b/src/main.rs @@ -12,6 +12,7 @@ extern crate error_chain; extern crate clap; extern crate uname; extern crate libc; +extern crate procps_sys; extern crate yaml_rust; use std::collections::BTreeMap; @@ -39,6 +40,7 @@ use value::Value; mod mib_sys; mod mib_disks; mod mib_net; +mod mib_procs; mod mib_extend; @@ -89,6 +91,10 @@ fn run(matches: clap::ArgMatches) -> Result<()> { "1.3.6.1.2.1.2.2.1", "1.3.6.1.2.1.31.1.1.1" ); + mib_procs::get_processes( + &mut values, + "1.3.6.1.2.1.25.4.2.1" + ); mib_extend::get_extend( &mut values, &conf, @@ -151,7 +157,7 @@ fn respond ( vals.push( (&oid.as_vec()[..], val.as_snmp_value()) ); } - if vals.len() >= 100 { + if vals.len() >= 50 { break } } diff --git a/src/mib_procs.rs b/src/mib_procs.rs new file mode 100644 index 0000000..d5cbab5 --- /dev/null +++ b/src/mib_procs.rs @@ -0,0 +1,33 @@ +use std::collections::BTreeMap; +use value::Value; +use oid::OID; +use std::fs; + +pub fn get_processes( + values: &mut BTreeMap, + hr_sw_run_table_oid: &str +) { + + if let Ok(entries) = fs::read_dir("/proc") { + for entry in entries { + if let Ok(entry) = entry { + if let Ok(pid) = entry.file_name().into_string().unwrap().parse::() { + if let Ok(cmdline) = entry.path().join("exe").read_link() { + values.insert( + OID::from_parts_and_instance(&[hr_sw_run_table_oid, "1"], pid), + Value::Integer( pid as i64 ) + ); + values.insert( + OID::from_parts_and_instance(&[hr_sw_run_table_oid, "2"], pid as u32), + Value::OctetString(String::from(cmdline.file_name().unwrap().to_str().unwrap())) + ); + values.insert( + OID::from_parts_and_instance(&[hr_sw_run_table_oid, "4"], pid as u32), + Value::OctetString(String::from(cmdline.to_str().unwrap())) + ); + } + } + } + } + } +} From 0755d2bc1578b3929b883ce5b99327342ef983e9 Mon Sep 17 00:00:00 2001 From: Michael Ziegler Date: Mon, 9 Mar 2020 09:51:16 +0100 Subject: [PATCH 2/2] install libprocps-dev --- .drone.yml | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/.drone.yml b/.drone.yml index 449a70e..d5108c6 100644 --- a/.drone.yml +++ b/.drone.yml @@ -6,9 +6,10 @@ steps: - name: build for Linux image: rust:latest commands: + - apt-get update && apt-get install libprocps-dev - cargo build --release --- kind: signature -hmac: 4b9b8dc348d4d694758245c4964d59fe30af2334028e84f9be75a4e9b9975439 +hmac: 8cf10f7d07bef8b089c0f56638dccfaeef3a98e8ca11a27a411a2a8554f4bee0 ...