Added optional sudo

This commit is contained in:
Francesco Cogno 2020-10-11 16:40:10 +02:00
parent e77a3e26ee
commit fa739c423c
No known key found for this signature in database
GPG key ID: 89082D7200C96BD1
4 changed files with 533 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 {
Command::new("sudo")
.arg("wg")
.arg("show") .arg("show")
.arg(&interface_to_handle) .arg(&interface_to_handle)
.arg("dump") .arg("dump")
.output()?; .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 == {}",

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::*;
}