Merge pull request #45 from MindFlavor/issue/27/sudo_wg_optional

Added optional sudo to wg command
This commit is contained in:
Francesco Cogno 2020-10-11 20:38:56 +02:00 committed by GitHub
commit 38b55fca32
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 539 additions and 528 deletions

1032
Cargo.lock generated

File diff suppressed because it is too large Load Diff

View File

@ -1,6 +1,6 @@
[package] [package]
name = "prometheus_wireguard_exporter" name = "prometheus_wireguard_exporter"
version = "3.4.0" version = "3.4.1"
authors = ["Francesco Cogno <francesco.cogno@outlook.com>"] authors = ["Francesco Cogno <francesco.cogno@outlook.com>"]
description = "Prometheus WireGuard Exporter" description = "Prometheus WireGuard Exporter"
edition = "2018" edition = "2018"

View File

@ -47,11 +47,21 @@ async fn perform_request(
let mut wg_accumulator: Option<WireGuard> = None; let mut wg_accumulator: Option<WireGuard> = None;
for interface_to_handle in interfaces_to_handle { for interface_to_handle in interfaces_to_handle {
let output = Command::new("wg") let output = if options.prepend_sudo {
.arg("show") Command::new("sudo")
.arg(&interface_to_handle) .arg("wg")
.arg("dump") .arg("show")
.output()?; .arg(&interface_to_handle)
.arg("dump")
.output()?
} else {
Command::new("wg")
.arg("show")
.arg(&interface_to_handle)
.arg("dump")
.output()?
};
let output_stdout_str = String::from_utf8(output.stdout)?; let output_stdout_str = String::from_utf8(output.stdout)?;
trace!( trace!(
"wg show {} dump stdout == {}", "wg show {} dump stdout == {}",
@ -125,6 +135,12 @@ async fn main() {
.help("verbose logging") .help("verbose logging")
.takes_value(false), .takes_value(false),
) )
.arg(
Arg::with_name("prepend_sudo")
.short("a")
.help("Prepend sudo to the wg show commands")
.takes_value(false),
)
.arg( .arg(
Arg::with_name("separate_allowed_ips") Arg::with_name("separate_allowed_ips")
.short("s") .short("s")

View File

@ -1,6 +1,7 @@
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub(crate) struct Options { pub(crate) struct Options {
pub verbose: bool, pub verbose: bool,
pub prepend_sudo: bool,
pub separate_allowed_ips: bool, pub separate_allowed_ips: bool,
pub extract_names_config_file: Option<String>, pub extract_names_config_file: Option<String>,
pub interfaces: Option<Vec<String>>, pub interfaces: Option<Vec<String>>,
@ -11,6 +12,7 @@ impl Options {
pub fn from_claps(matches: &clap::ArgMatches<'_>) -> Options { pub fn from_claps(matches: &clap::ArgMatches<'_>) -> Options {
let options = Options { let options = Options {
verbose: matches.is_present("verbose"), verbose: matches.is_present("verbose"),
prepend_sudo: matches.is_present("prepend_sudo"),
separate_allowed_ips: matches.is_present("separate_allowed_ips"), separate_allowed_ips: matches.is_present("separate_allowed_ips"),
extract_names_config_file: matches extract_names_config_file: matches
.value_of("extract_names_config_files") .value_of("extract_names_config_files")
@ -29,8 +31,3 @@ impl Options {
options options
} }
} }
#[cfg(test)]
mod tests {
use super::*;
}