before live test
This commit is contained in:
parent
be9bf63fac
commit
82188d1494
@ -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")
|
||||||
|
@ -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,12 +125,38 @@ 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();
|
||||||
|
|
||||||
|
if let Some(pehm) = pehm {
|
||||||
|
// here we try to add the "friendly" name looking in the hashmap
|
||||||
|
for (interface, endpoints) in self.interfaces.iter() {
|
||||||
|
for endpoint in endpoints {
|
||||||
|
// only show remote endpoints
|
||||||
|
if let Endpoint::Remote(ep) = endpoint {
|
||||||
|
debug!("{:?}", ep);
|
||||||
|
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 (interface, endpoints) in self.interfaces.iter() {
|
||||||
for endpoint in endpoints {
|
for endpoint in endpoints {
|
||||||
// only show remote endpoints
|
// only show remote endpoints
|
||||||
@ -141,6 +168,7 @@ impl RenderToPrometheus for WireGuard {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
let mut s = String::new();
|
let mut s = String::new();
|
||||||
|
|
||||||
@ -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::*;
|
||||||
|
@ -63,13 +63,11 @@ 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> {
|
||||||
fn try_from(txt: &'a str) -> Result<PeerEntryHashMap, Self::Error> {
|
|
||||||
let mut hm = HashMap::new();
|
let mut hm = HashMap::new();
|
||||||
|
|
||||||
let mut v_blocks = Vec::new();
|
let mut v_blocks = Vec::new();
|
||||||
@ -111,8 +109,7 @@ impl<'a> TryFrom<&'a str> for PeerEntryHashMap<'a> {
|
|||||||
|
|
||||||
debug!("hm == {:?}", hm);
|
debug!("hm == {:?}", hm);
|
||||||
|
|
||||||
Ok(PeerEntryHashMap(hm))
|
Ok(hm)
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
|
Loading…
Reference in New Issue
Block a user