before live test

This commit is contained in:
Francesco Cogno 2019-05-20 10:09:02 +02:00
parent be9bf63fac
commit 82188d1494
No known key found for this signature in database
GPG key ID: 20883E192428EA7A
3 changed files with 87 additions and 56 deletions

View file

@ -21,7 +21,7 @@ use std::process::Command;
use std::string::String; use std::string::String;
use wireguard::WireGuard; use wireguard::WireGuard;
mod wireguard_config; mod wireguard_config;
use wireguard_config::PeerEntryHashMap; use wireguard_config::peer_entry_hashmap_try_from;
fn check_compliance(req: &Request<Body>) -> Result<(), Response<Body>> { fn check_compliance(req: &Request<Body>) -> Result<(), Response<Body>> {
if req.uri() != "/metrics" { if req.uri() != "/metrics" {
@ -66,8 +66,8 @@ fn wg_with_text(
wg_config_str: &str, wg_config_str: &str,
wg_output: ::std::process::Output, wg_output: ::std::process::Output,
) -> Result<Response<Body>, ExporterError> { ) -> Result<Response<Body>, ExporterError> {
let pehm = PeerEntryHashMap::try_from(wg_config_str)?; let pehm = peer_entry_hashmap_try_from(wg_config_str)?;
println!("pehm == {:?}", pehm); trace!("pehm == {:?}", pehm);
let wg_output_string = String::from_utf8(wg_output.stdout)?; let wg_output_string = String::from_utf8(wg_output.stdout)?;
let wg = WireGuard::try_from(&wg_output_string as &str)?; let wg = WireGuard::try_from(&wg_output_string as &str)?;
@ -115,7 +115,7 @@ fn perform_request(
fn main() { fn main() {
let matches = clap::App::new("prometheus_wireguard_exporter") let matches = clap::App::new("prometheus_wireguard_exporter")
.version("0.1") .version("1.2.0")
.author("Francesco Cogno <francesco.cogno@outlook.com>") .author("Francesco Cogno <francesco.cogno@outlook.com>")
.arg( .arg(
Arg::with_name("port") Arg::with_name("port")

View file

@ -1,5 +1,6 @@
use crate::exporter_error::ExporterError; use crate::exporter_error::ExporterError;
use crate::render_to_prometheus::RenderToPrometheus; use crate::render_to_prometheus::RenderToPrometheus;
use crate::wireguard_config::PeerEntryHashMap;
use log::{debug, trace}; use log::{debug, trace};
use std::collections::HashMap; use std::collections::HashMap;
use std::convert::TryFrom; use std::convert::TryFrom;
@ -124,20 +125,47 @@ impl TryFrom<&str> for WireGuard {
} }
} }
impl RenderToPrometheus for WireGuard { impl WireGuard {
fn render(&self) -> String { fn render_with_names(&self, pehm: Option<&PeerEntryHashMap>) -> String {
let mut latest_handshakes = Vec::new(); let mut latest_handshakes = Vec::new();
let mut sent_bytes = Vec::new(); let mut sent_bytes = Vec::new();
let mut received_bytes = Vec::new(); let mut received_bytes = Vec::new();
for (interface, endpoints) in self.interfaces.iter() { if let Some(pehm) = pehm {
for endpoint in endpoints { // here we try to add the "friendly" name looking in the hashmap
// only show remote endpoints for (interface, endpoints) in self.interfaces.iter() {
if let Endpoint::Remote(ep) = endpoint { for endpoint in endpoints {
debug!("{:?}", ep); // only show remote endpoints
sent_bytes.push(format!("wireguard_sent_bytes{{inteface=\"{}\", public_key=\"{}\", local_ip=\"{}\", local_subnet=\"{}\"}} {}\n", interface, ep.public_key, ep.local_ip, ep.local_subnet, ep.sent_bytes)); if let Endpoint::Remote(ep) = endpoint {
received_bytes.push(format!("wireguard_received_bytes{{inteface=\"{}\", public_key=\"{}\", local_ip=\"{}\", local_subnet=\"{}\"}} {}\n", interface, ep.public_key, ep.local_ip, ep.local_subnet, ep.received_bytes)); debug!("{:?}", ep);
latest_handshakes.push(format!("wireguard_latest_handshake_seconds{{inteface=\"{}\", public_key=\"{}\", local_ip=\"{}\", local_subnet=\"{}\"}} {}\n", interface, ep.public_key, ep.local_ip, ep.local_subnet, ::std::time::SystemTime::now().duration_since(ep.latest_handshake).unwrap().as_secs())); if let Some(ep_friendly_name) = pehm.get(&ep.public_key as &str) {
if let Some(ep_friendly_name) = ep_friendly_name.name {
sent_bytes.push(format!("wireguard_sent_bytes{{inteface=\"{}\", public_key=\"{}\", local_ip=\"{}\", local_subnet=\"{}\", friendly_name=\"{}\"}} {}\n", interface, ep.public_key, ep.local_ip, ep.local_subnet, ep_friendly_name, ep.sent_bytes));
received_bytes.push(format!("wireguard_received_bytes{{inteface=\"{}\", public_key=\"{}\", local_ip=\"{}\", local_subnet=\"{}\", friendly_name=\"{}\"}} {}\n", interface, ep.public_key, ep.local_ip, ep.local_subnet, ep_friendly_name, ep.received_bytes));
latest_handshakes.push(format!("wireguard_latest_handshake_seconds{{inteface=\"{}\", public_key=\"{}\", local_ip=\"{}\", local_subnet=\"{}\", friendly_name=\"{}\"}} {}\n", interface, ep.public_key, ep.local_ip, ep.local_subnet, ep_friendly_name, ::std::time::SystemTime::now().duration_since(ep.latest_handshake).unwrap().as_secs()));
} else {
sent_bytes.push(format!("wireguard_sent_bytes{{inteface=\"{}\", public_key=\"{}\", local_ip=\"{}\", local_subnet=\"{}\"}} {}\n", interface, ep.public_key, ep.local_ip, ep.local_subnet, ep.sent_bytes));
received_bytes.push(format!("wireguard_received_bytes{{inteface=\"{}\", public_key=\"{}\", local_ip=\"{}\", local_subnet=\"{}\"}} {}\n", interface, ep.public_key, ep.local_ip, ep.local_subnet, ep.received_bytes));
latest_handshakes.push(format!("wireguard_latest_handshake_seconds{{inteface=\"{}\", public_key=\"{}\", local_ip=\"{}\", local_subnet=\"{}\"}} {}\n", interface, ep.public_key, ep.local_ip, ep.local_subnet, ::std::time::SystemTime::now().duration_since(ep.latest_handshake).unwrap().as_secs()));
}
} else {
sent_bytes.push(format!("wireguard_sent_bytes{{inteface=\"{}\", public_key=\"{}\", local_ip=\"{}\", local_subnet=\"{}\"}} {}\n", interface, ep.public_key, ep.local_ip, ep.local_subnet, ep.sent_bytes));
received_bytes.push(format!("wireguard_received_bytes{{inteface=\"{}\", public_key=\"{}\", local_ip=\"{}\", local_subnet=\"{}\"}} {}\n", interface, ep.public_key, ep.local_ip, ep.local_subnet, ep.received_bytes));
latest_handshakes.push(format!("wireguard_latest_handshake_seconds{{inteface=\"{}\", public_key=\"{}\", local_ip=\"{}\", local_subnet=\"{}\"}} {}\n", interface, ep.public_key, ep.local_ip, ep.local_subnet, ::std::time::SystemTime::now().duration_since(ep.latest_handshake).unwrap().as_secs()));
}
}
}
}
} else {
for (interface, endpoints) in self.interfaces.iter() {
for endpoint in endpoints {
// only show remote endpoints
if let Endpoint::Remote(ep) = endpoint {
debug!("{:?}", ep);
sent_bytes.push(format!("wireguard_sent_bytes{{inteface=\"{}\", public_key=\"{}\", local_ip=\"{}\", local_subnet=\"{}\"}} {}\n", interface, ep.public_key, ep.local_ip, ep.local_subnet, ep.sent_bytes));
received_bytes.push(format!("wireguard_received_bytes{{inteface=\"{}\", public_key=\"{}\", local_ip=\"{}\", local_subnet=\"{}\"}} {}\n", interface, ep.public_key, ep.local_ip, ep.local_subnet, ep.received_bytes));
latest_handshakes.push(format!("wireguard_latest_handshake_seconds{{inteface=\"{}\", public_key=\"{}\", local_ip=\"{}\", local_subnet=\"{}\"}} {}\n", interface, ep.public_key, ep.local_ip, ep.local_subnet, ::std::time::SystemTime::now().duration_since(ep.latest_handshake).unwrap().as_secs()));
}
} }
} }
} }
@ -173,6 +201,12 @@ impl RenderToPrometheus for WireGuard {
} }
} }
impl RenderToPrometheus for WireGuard {
fn render(&self) -> String {
self.render_with_names(None)
}
}
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use super::*; use super::*;

View file

@ -63,56 +63,53 @@ impl<'a> TryFrom<&[&'a str]> for PeerEntry<'a> {
} }
} }
#[derive(Debug, Default, Clone)] pub(crate) type PeerEntryHashMap<'a> = (HashMap<&'a str, PeerEntry<'a>>);
pub(crate) struct PeerEntryHashMap<'a>(HashMap<&'a str, PeerEntry<'a>>);
impl<'a> TryFrom<&'a str> for PeerEntryHashMap<'a> { pub(crate) fn peer_entry_hashmap_try_from<'a>(
type Error = PeerEntryParseError; txt: &'a str,
) -> Result<PeerEntryHashMap, PeerEntryParseError> {
let mut hm = HashMap::new();
fn try_from(txt: &'a str) -> Result<PeerEntryHashMap, Self::Error> { let mut v_blocks = Vec::new();
let mut hm = HashMap::new(); let mut cur_block: Option<Vec<&str>> = None;
let mut v_blocks = Vec::new(); for line in txt.lines().into_iter() {
let mut cur_block: Option<Vec<&str>> = None; if line.starts_with("[") {
if let Some(inner_cur_block) = cur_block {
// close the block
v_blocks.push(inner_cur_block);
cur_block = None;
}
for line in txt.lines().into_iter() { if line == "[Peer]" {
if line.starts_with("[") { // start a new block
if let Some(inner_cur_block) = cur_block { cur_block = Some(Vec::new());
// close the block }
v_blocks.push(inner_cur_block); } else {
cur_block = None; // push the line if we are in a block (only if not empty)
} if let Some(inner_cur_block) = &mut cur_block {
if line != "" {
if line == "[Peer]" { inner_cur_block.push(line);
// start a new block
cur_block = Some(Vec::new());
}
} else {
// push the line if we are in a block (only if not empty)
if let Some(inner_cur_block) = &mut cur_block {
if line != "" {
inner_cur_block.push(line);
}
} }
} }
} }
if let Some(cur_block) = cur_block {
// we have a leftover block
v_blocks.push(cur_block);
}
debug!("v_blocks == {:?}", v_blocks);
for block in &v_blocks {
let p: PeerEntry = PeerEntry::try_from(&block as &[&str])?;
hm.insert(p.public_key, p);
}
debug!("hm == {:?}", hm);
Ok(PeerEntryHashMap(hm))
} }
if let Some(cur_block) = cur_block {
// we have a leftover block
v_blocks.push(cur_block);
}
debug!("v_blocks == {:?}", v_blocks);
for block in &v_blocks {
let p: PeerEntry = PeerEntry::try_from(&block as &[&str])?;
hm.insert(p.public_key, p);
}
debug!("hm == {:?}", hm);
Ok(hm)
} }
#[cfg(test)] #[cfg(test)]