migrated to std::future

This commit is contained in:
Francesco Cogno 2019-12-01 19:42:47 +01:00
parent 4612afe684
commit 923b70c4b7
6 changed files with 1169 additions and 631 deletions

1
.rustfmt.toml Normal file
View File

@ -0,0 +1 @@
edition="2018"

1501
Cargo.lock generated

File diff suppressed because it is too large Load Diff

View File

@ -1,6 +1,6 @@
[package]
name = "prometheus_wireguard_exporter"
version = "3.2.1"
version = "3.2.2"
authors = ["Francesco Cogno <francesco.cogno@outlook.com>"]
description = "Prometheus WireGuard Exporter"
edition = "2018"
@ -17,13 +17,13 @@ categories = ["database"]
[dependencies]
log = "0.4.6"
env_logger = "0.6.1"
futures = "0.1.27"
clap = "2.33.0"
serde_json = "1.0.39"
serde = "1.0.91"
serde_derive = "1.0.91"
failure = "0.1.5"
hyper = "0.12.29"
hyper = { version = "0.13.0-alpha.4" , features = ["unstable-stream"] }
http = "0.1.17"
prometheus_exporter_base = "0.2.0"
tokio = "0.2.0-alpha.6"
prometheus_exporter_base = { version = "0.30.1" }
regex = "1.3.1"

View File

@ -3,8 +3,7 @@ extern crate serde_json;
extern crate failure;
use clap;
use clap::{crate_authors, crate_name, crate_version, Arg};
use futures::future::{done, ok, Either, Future};
use hyper::{Body, Request, Response};
use hyper::{Body, Request};
use log::{debug, info, trace};
use std::env;
mod options;
@ -18,7 +17,6 @@ mod exporter_error;
mod wireguard_config;
use wireguard_config::peer_entry_hashmap_try_from;
extern crate prometheus_exporter_base;
use crate::exporter_error::ExporterError;
use prometheus_exporter_base::render_prometheus;
use std::net::Ipv4Addr;
use std::sync::Arc;
@ -27,22 +25,22 @@ fn wg_with_text(
wg_config_str: &str,
wg_output_str: &str,
options: Arc<Options>,
) -> Result<Response<Body>, ExporterError> {
) -> Result<String, failure::Error> {
let pehm = peer_entry_hashmap_try_from(wg_config_str)?;
trace!("pehm == {:?}", pehm);
let wg = WireGuard::try_from(wg_output_str)?;
Ok(Response::new(Body::from(wg.render_with_names(
Ok(wg.render_with_names(
Some(&pehm),
options.separate_allowed_ips,
options.export_remote_ip_and_port,
))))
))
}
fn perform_request(
async fn perform_request(
_req: Request<Body>,
options: &Arc<Options>,
) -> impl Future<Item = Response<Body>, Error = failure::Error> {
options: Arc<Options>,
) -> Result<String, failure::Error> {
trace!("perform_request");
// this is needed to satisfy the borrow checker
let options = options.clone();
@ -58,64 +56,45 @@ fn perform_request(
debug!("using inteface_str {}", interface_str);
done(
Command::new("wg")
.arg("show")
.arg(&interface_str)
.arg("dump")
.output(),
)
.from_err()
.and_then(move |output| {
done(String::from_utf8(output.stdout))
.from_err()
.and_then(move |output_str| {
trace!("wg show output == {}", output_str);
let output = Command::new("wg")
.arg("show")
.arg(&interface_str)
.arg("dump")
.output()?;
let output_str = String::from_utf8(output.stdout)?;
trace!("wg show output == {}", output_str);
// the output of wg show is different if we use all or we specify an interface.
// In the first case the first column will be the interface name. In the second case
// the interface name will be omitted. We need to compensate for the skew somehow (one
// column less in the second case). We solve this prepending the interface name in every
// line so the output of the second case will be equal to the first case.
let output_str = if interface_str != "all" {
debug!("injecting {} to the wg show output", interface_str);
let mut result = String::new();
for s in output_str.lines() {
result.push_str(&format!("{}\t{}\n", interface_str, s));
}
result
} else {
output_str
};
// the output of wg show is different if we use all or we specify an interface.
// In the first case the first column will be the interface name. In the second case
// the interface name will be omitted. We need to compensate for the skew somehow (one
// column less in the second case). We solve this prepending the interface name in every
// line so the output of the second case will be equal to the first case.
let output_str = if interface_str != "all" {
debug!("injecting {} to the wg show output", interface_str);
let mut result = String::new();
for s in output_str.lines() {
result.push_str(&format!("{}\t{}\n", interface_str, s));
}
result
} else {
output_str
};
if let Some(extract_names_config_file) = &options.extract_names_config_file {
Either::A(
done(::std::fs::read_to_string(extract_names_config_file))
.from_err()
.and_then(move |wg_config_string| {
wg_with_text(&wg_config_string as &str, &output_str, options)
}),
)
} else {
Either::B({
trace!("{}", output_str);
done(WireGuard::try_from(&output_str as &str))
.from_err()
.and_then(move |wg| {
ok(Response::new(Body::from(wg.render_with_names(
None,
options.separate_allowed_ips,
options.export_remote_ip_and_port,
))))
})
})
}
})
.from_err()
})
if let Some(extract_names_config_file) = &options.extract_names_config_file {
let wg_config_string = ::std::fs::read_to_string(extract_names_config_file)?;
wg_with_text(&wg_config_string as &str, &output_str, options)
} else {
let wg = WireGuard::try_from(&output_str as &str)?;
Ok(wg.render_with_names(
None,
options.separate_allowed_ips,
options.export_remote_ip_and_port,
))
}
}
fn main() {
#[tokio::main]
async fn main() {
let matches = clap::App::new(crate_name!())
.version(crate_version!())
.author(crate_authors!("\n"))
@ -151,7 +130,7 @@ fn main() {
.help("exports peer's remote ip and port as labels (if available)")
.takes_value(false),
)
.arg(
.arg(
Arg::with_name("extract_names_config_file")
.short("n")
.help("If set, the exporter will look in the specified WireGuard config file for peer names (must be in [Peer] definition and be a comment)")
@ -186,7 +165,8 @@ fn main() {
info!("starting exporter on http://{}/metrics", addr);
render_prometheus(&addr, options, |request, options| {
Box::new(perform_request(request, options))
});
render_prometheus(addr, options, |request, options| {
Box::pin(perform_request(request, options))
})
.await;
}

View File

@ -1,7 +1,7 @@
use crate::exporter_error::ExporterError;
use crate::wireguard_config::PeerEntryHashMap;
use log::{debug, trace};
use prometheus_exporter_base::PrometheusCounter;
use prometheus_exporter_base::{MetricType, PrometheusMetric};
use regex::Regex;
use std::collections::HashMap;
use std::convert::TryFrom;
@ -56,14 +56,14 @@ impl TryFrom<&str> for WireGuard {
type Error = ExporterError;
fn try_from(input: &str) -> Result<Self, Self::Error> {
debug!("wireguard::try_from({}) called", input);
debug!("WireGuard::try_from({}) called", input);
let mut wg = WireGuard {
interfaces: HashMap::new(),
};
for line in input.lines() {
let v: Vec<&str> = line.split('\t').filter(|s| !s.is_empty()).collect();
debug!("v == {:?}", v);
debug!("WireGuard::try_from v == {:?}", v);
let endpoint = if v.len() == 5 {
// this is the local interface
@ -109,7 +109,7 @@ impl TryFrom<&str> for WireGuard {
})
};
trace!("{:?}", endpoint);
trace!("WireGuard::try_from endpoint == {:?}", endpoint);
if let Some(endpoints) = wg.interfaces.get_mut(v[0]) {
endpoints.push(endpoint);
@ -132,20 +132,22 @@ impl WireGuard {
split_allowed_ips: bool,
export_remote_ip_and_port: bool,
) -> String {
debug!("WireGuard::render_with_names(self == {:?}, pehm == {:?}, split_allowed_ips == {:?}, export_remote_ip_and_port == {:?} called", self, pehm, split_allowed_ips,export_remote_ip_and_port);
// these are the exported counters
let pc_sent_bytes_total = PrometheusCounter::new(
let pc_sent_bytes_total = PrometheusMetric::new(
"wireguard_sent_bytes_total",
"counter",
MetricType::Counter,
"Bytes sent to the peer",
);
let pc_received_bytes_total = PrometheusCounter::new(
let pc_received_bytes_total = PrometheusMetric::new(
"wireguard_received_bytes_total",
"counter",
MetricType::Counter,
"Bytes received from the peer",
);
let pc_latest_handshake = PrometheusCounter::new(
let pc_latest_handshake = PrometheusMetric::new(
"wireguard_latest_handshake_seconds",
"gauge",
MetricType::Gauge,
"Seconds from the last handshake",
);
@ -167,7 +169,7 @@ impl WireGuard {
for endpoint in endpoints {
// only show remote endpoints
if let Endpoint::Remote(ep) = endpoint {
debug!("{:?}", ep);
debug!("WireGuard::render_with_names ep == {:?}", ep);
// we store in attributes_owned the ownership of the values in order to
// store in attibutes their references. attributes_owned is onyl
@ -183,9 +185,12 @@ impl WireGuard {
.allowed_ips
.split(',')
.map(|ip_and_subnet| {
debug!("ip_and_subnet == {:?}", ip_and_subnet);
debug!(
"WireGuard::render_with_names ip_and_subnet == {:?}",
ip_and_subnet
);
let tokens: Vec<&str> = ip_and_subnet.split('/').collect();
debug!("tokens == {:?}", tokens);
debug!("WireGuard::render_with_names tokens == {:?}", tokens);
let addr = tokens[0];
let subnet = tokens[1];
(addr, subnet)
@ -197,7 +202,10 @@ impl WireGuard {
attributes_owned
.push((format!("allowed_subnet_{}", idx), subnet.to_string()));
}
debug!("attributes == {:?}", attributes);
debug!(
"WireGuard::render_with_names attributes == {:?}",
attributes
);
} else {
attributes.push(("allowed_ips", &ep.allowed_ips));
}
@ -226,13 +234,12 @@ impl WireGuard {
}
s_sent_bytes_total
.push(pc_sent_bytes_total.render_counter(Some(&attributes), ep.sent_bytes));
.push(pc_sent_bytes_total.render_sample(Some(&attributes), ep.sent_bytes));
s_received_bytes_total.push(
pc_received_bytes_total
.render_counter(Some(&attributes), ep.received_bytes),
pc_received_bytes_total.render_sample(Some(&attributes), ep.received_bytes),
);
s_latest_handshake.push(
pc_latest_handshake.render_counter(Some(&attributes), ep.latest_handshake),
pc_latest_handshake.render_sample(Some(&attributes), ep.latest_handshake),
);
}
}
@ -268,6 +275,125 @@ pollo\tYdVOIPKt9K2MPsO2NlWQbOnFJcL/qX80mmhQwsUlA=\t(none)\t(none)\t10.70.70.50/3
wg0\t928vO9Lf4+Mo84cWu4k1oRyzf0AR7FTGoPKHGoTMSHk=\t(none)\t5.90.62.106:21741\t10.70.0.80/32\t1555344925\t283012\t6604620\toff
";
const TEXT_ISSUE_19 : &'static str = "wg0\twJyy0Xcqk76dNQI8bnzaQvrtle5Od+wft1RBK3fC8kc=\tVfjHGauX8OxotDMm2vi3JdwOUTDsFbxCnyInJ/wAXlk=\t51820\toff
wg0\t923V/iAdcz8BcqB0Xo6pDJzARGBJCQ6fWe+peixQyB4=\t(none)\t10.211.123.112:51820\t10.90.0.10/32,10.0.1.0/24\t0\t0\t0\toff
wg0\t9M1fhLa9sIlT39z+SI/0a5H3mNSHYmM+NGA6sirD2nU=\t(none)\t10.211.123.113:51820\t10.90.0.3/32,10.198.171.0/24\t0\t0\t0\toff
wg0\tgnRKXngxSppcYegsg38kEFn5Lmk4NcnRXLcZTtg2A2E=\t(none)\t10.211.123.114:51820\t10.90.0.11/32,10.189.143.0/24\t0\t0\t0\toff
wg0\tYW7NBDEPXuW9GQlFWFzpgrivMxzdR55M8VOTX+E0thw=\t(none)\t10.211.123.115:51820\t10.90.0.12/32,10.0.2.0/24\t0\t0\t0\toff
wg0\teVfg1BH1hcteASE16+TjShxAJNyFLQ9QIcnCaylD/AA=\t(none)\t10.211.123.116:51820\t10.90.0.13/32,10.0.3.0/24\t0\t0\t0\toff
wg0\tlh1h+tWPahB+PAWW62ExHVVrOp9IwdjYwaGnPIXgNwY=\t(none)\t10.211.123.117:51820\t10.90.0.9/32,10.0.4.0/24\t0\t0\t0\toff
wg0\tVQIrk1BiBfbOkkKGPiarEvhA4iPuszIL1lddvvFDvE0=\t(none)\t10.211.123.118:51820\t10.90.0.8/32,10.0.5.0/24\t0\t0\t0\toff
wg0\tSMp58OwCNnwlzu+OdpA8xiNJzOwbl2gdMaD9CSZCC24=\t(none)\t10.211.123.119:51820\t10.90.0.14/32,10.0.6.0/24\t0\t0\t0\toff
wg0\t+0+yMIHVCqyIf4by1gxAjqQ92iKv3bQ/JctNVUEpSlU=\t(none)\t10.211.123.120:51820\t10.90.0.7/32,10.0.7.0/24\t0\t0\t0\toff
wg0\t2StYqQY9tyVkGcO4ykKTiTu6AQp/yIYx8I4hwBLO1jA=\t(none)\t10.211.123.121:51820\t10.90.0.15/32,10.0.8.0/24\t0\t0\t0\toff
wg0\tqa0AMD2puDBBrs8NYQ+skIrIi/Q5NgQRZLEh5p80Mnc=\t(none)\t10.211.123.122:51820\t10.90.0.1/32,10.0.10.0/24\t0\t0\t0\toff
wg0\tYwObmKDK4lfr5F6FHqJhDy9nkUQwbuK8wh4ac2VNSEU=\t(none)\t10.211.123.123:51820\t10.90.0.2/32,10.0.11.0/24\t0\t0\t0\toff
wg0\tq07dm9n1UMLFbG6Dh+BNztCt7jVb9VtpVshQEf580kA=\t(none)\t10.211.123.124:51820\t10.90.0.6/32,10.0.13.0/24\t0\t0\t0\toff
wg0\tyZOoC2t6pBcXvoczuiJqrQ+8CYvJCzcq8aqyp+APaAE=\t(none)\t10.211.123.125:51820\t10.90.0.16/32,10.0.14.0/24\t1574770531\t1232856\t12306832\toff
wg0\tyjeBkrZqUThSSHySFzWCjxAH8cxtiWSI2I8JFD6t1UM=\t(none)\t10.211.123.126:51820\t10.90.0.5/32\t1574770705\t18576788764\t10642564136\toff
wg0\tHtOSi37ALMnSkeAFqeWYZqlBnZqAJERhb5o/i3ZPEFI=\t(none)\t10.211.123.127:51820\t10.90.0.17/32\t1574770783\t62592693520\t1439257868\toff
wg0\tsUsR6xufQQ8Tf0FuyY9tfEeYdhVMeFelr4ZMUrj+B0E=\t(none)\t10.211.123.128:51820\t10.90.0.18/32\t1574770693\t75066288152\t1624251784\toff";
#[test]
fn test_parse_issue_19() {
println!("starting debug");
let a = WireGuard::try_from(TEXT_ISSUE_19).unwrap();
assert!(a.interfaces.len() == 1);
assert!(a.interfaces["wg0"].len() == 18);
let e1 = match &a.interfaces["wg0"][1] {
Endpoint::Local(_) => panic!(),
Endpoint::Remote(re) => re,
};
assert_eq!(
e1.public_key,
"923V/iAdcz8BcqB0Xo6pDJzARGBJCQ6fWe+peixQyB4="
);
assert_eq!(e1.remote_ip, Some("10.211.123.112".to_owned()));
assert_eq!(e1.allowed_ips, "10.90.0.10/32,10.0.1.0/24".to_owned());
let e17 = match &a.interfaces["wg0"][17] {
Endpoint::Local(_) => panic!(),
Endpoint::Remote(re) => re,
};
assert_eq!(
e17.public_key,
"sUsR6xufQQ8Tf0FuyY9tfEeYdhVMeFelr4ZMUrj+B0E="
);
assert_eq!(e17.remote_ip, Some("10.211.123.128".to_owned()));
assert_eq!(e17.allowed_ips, "10.90.0.18/32".to_owned());
assert_eq!(e17.latest_handshake, 1574770693);
assert_eq!(e17.sent_bytes, 1624251784);
assert_eq!(e17.received_bytes, 75066288152);
let pe = PeerEntryHashMap::new();
let s = a.render_with_names(Some(&pe), true, true);
println!("{}", s);
let s_ok = "# HELP wireguard_sent_bytes_total Bytes sent to the peer
# TYPE wireguard_sent_bytes_total counter
wireguard_sent_bytes_total{interface=\"wg0\",public_key=\"923V/iAdcz8BcqB0Xo6pDJzARGBJCQ6fWe+peixQyB4=\",remote_ip=\"10.211.123.112\",allowed_ip_0=\"10.90.0.10\",allowed_subnet_0=\"32\",allowed_ip_1=\"10.0.1.0\",allowed_subnet_1=\"24\",remote_port=\"51820\"} 0
wireguard_sent_bytes_total{interface=\"wg0\",public_key=\"9M1fhLa9sIlT39z+SI/0a5H3mNSHYmM+NGA6sirD2nU=\",remote_ip=\"10.211.123.113\",allowed_ip_0=\"10.90.0.3\",allowed_subnet_0=\"32\",allowed_ip_1=\"10.198.171.0\",allowed_subnet_1=\"24\",remote_port=\"51820\"} 0
wireguard_sent_bytes_total{interface=\"wg0\",public_key=\"gnRKXngxSppcYegsg38kEFn5Lmk4NcnRXLcZTtg2A2E=\",remote_ip=\"10.211.123.114\",allowed_ip_0=\"10.90.0.11\",allowed_subnet_0=\"32\",allowed_ip_1=\"10.189.143.0\",allowed_subnet_1=\"24\",remote_port=\"51820\"} 0
wireguard_sent_bytes_total{interface=\"wg0\",public_key=\"YW7NBDEPXuW9GQlFWFzpgrivMxzdR55M8VOTX+E0thw=\",remote_ip=\"10.211.123.115\",allowed_ip_0=\"10.90.0.12\",allowed_subnet_0=\"32\",allowed_ip_1=\"10.0.2.0\",allowed_subnet_1=\"24\",remote_port=\"51820\"} 0
wireguard_sent_bytes_total{interface=\"wg0\",public_key=\"eVfg1BH1hcteASE16+TjShxAJNyFLQ9QIcnCaylD/AA=\",remote_ip=\"10.211.123.116\",allowed_ip_0=\"10.90.0.13\",allowed_subnet_0=\"32\",allowed_ip_1=\"10.0.3.0\",allowed_subnet_1=\"24\",remote_port=\"51820\"} 0
wireguard_sent_bytes_total{interface=\"wg0\",public_key=\"lh1h+tWPahB+PAWW62ExHVVrOp9IwdjYwaGnPIXgNwY=\",remote_ip=\"10.211.123.117\",allowed_ip_0=\"10.90.0.9\",allowed_subnet_0=\"32\",allowed_ip_1=\"10.0.4.0\",allowed_subnet_1=\"24\",remote_port=\"51820\"} 0
wireguard_sent_bytes_total{interface=\"wg0\",public_key=\"VQIrk1BiBfbOkkKGPiarEvhA4iPuszIL1lddvvFDvE0=\",remote_ip=\"10.211.123.118\",allowed_ip_0=\"10.90.0.8\",allowed_subnet_0=\"32\",allowed_ip_1=\"10.0.5.0\",allowed_subnet_1=\"24\",remote_port=\"51820\"} 0
wireguard_sent_bytes_total{interface=\"wg0\",public_key=\"SMp58OwCNnwlzu+OdpA8xiNJzOwbl2gdMaD9CSZCC24=\",remote_ip=\"10.211.123.119\",allowed_ip_0=\"10.90.0.14\",allowed_subnet_0=\"32\",allowed_ip_1=\"10.0.6.0\",allowed_subnet_1=\"24\",remote_port=\"51820\"} 0
wireguard_sent_bytes_total{interface=\"wg0\",public_key=\"+0+yMIHVCqyIf4by1gxAjqQ92iKv3bQ/JctNVUEpSlU=\",remote_ip=\"10.211.123.120\",allowed_ip_0=\"10.90.0.7\",allowed_subnet_0=\"32\",allowed_ip_1=\"10.0.7.0\",allowed_subnet_1=\"24\",remote_port=\"51820\"} 0
wireguard_sent_bytes_total{interface=\"wg0\",public_key=\"2StYqQY9tyVkGcO4ykKTiTu6AQp/yIYx8I4hwBLO1jA=\",remote_ip=\"10.211.123.121\",allowed_ip_0=\"10.90.0.15\",allowed_subnet_0=\"32\",allowed_ip_1=\"10.0.8.0\",allowed_subnet_1=\"24\",remote_port=\"51820\"} 0
wireguard_sent_bytes_total{interface=\"wg0\",public_key=\"qa0AMD2puDBBrs8NYQ+skIrIi/Q5NgQRZLEh5p80Mnc=\",remote_ip=\"10.211.123.122\",allowed_ip_0=\"10.90.0.1\",allowed_subnet_0=\"32\",allowed_ip_1=\"10.0.10.0\",allowed_subnet_1=\"24\",remote_port=\"51820\"} 0
wireguard_sent_bytes_total{interface=\"wg0\",public_key=\"YwObmKDK4lfr5F6FHqJhDy9nkUQwbuK8wh4ac2VNSEU=\",remote_ip=\"10.211.123.123\",allowed_ip_0=\"10.90.0.2\",allowed_subnet_0=\"32\",allowed_ip_1=\"10.0.11.0\",allowed_subnet_1=\"24\",remote_port=\"51820\"} 0
wireguard_sent_bytes_total{interface=\"wg0\",public_key=\"q07dm9n1UMLFbG6Dh+BNztCt7jVb9VtpVshQEf580kA=\",remote_ip=\"10.211.123.124\",allowed_ip_0=\"10.90.0.6\",allowed_subnet_0=\"32\",allowed_ip_1=\"10.0.13.0\",allowed_subnet_1=\"24\",remote_port=\"51820\"} 0
wireguard_sent_bytes_total{interface=\"wg0\",public_key=\"yZOoC2t6pBcXvoczuiJqrQ+8CYvJCzcq8aqyp+APaAE=\",remote_ip=\"10.211.123.125\",allowed_ip_0=\"10.90.0.16\",allowed_subnet_0=\"32\",allowed_ip_1=\"10.0.14.0\",allowed_subnet_1=\"24\",remote_port=\"51820\"} 12306832
wireguard_sent_bytes_total{interface=\"wg0\",public_key=\"yjeBkrZqUThSSHySFzWCjxAH8cxtiWSI2I8JFD6t1UM=\",remote_ip=\"10.211.123.126\",allowed_ip_0=\"10.90.0.5\",allowed_subnet_0=\"32\",remote_port=\"51820\"} 10642564136
wireguard_sent_bytes_total{interface=\"wg0\",public_key=\"HtOSi37ALMnSkeAFqeWYZqlBnZqAJERhb5o/i3ZPEFI=\",remote_ip=\"10.211.123.127\",allowed_ip_0=\"10.90.0.17\",allowed_subnet_0=\"32\",remote_port=\"51820\"} 1439257868
wireguard_sent_bytes_total{interface=\"wg0\",public_key=\"sUsR6xufQQ8Tf0FuyY9tfEeYdhVMeFelr4ZMUrj+B0E=\",remote_ip=\"10.211.123.128\",allowed_ip_0=\"10.90.0.18\",allowed_subnet_0=\"32\",remote_port=\"51820\"} 1624251784
# HELP wireguard_received_bytes_total Bytes received from the peer
# TYPE wireguard_received_bytes_total counter
wireguard_received_bytes_total{interface=\"wg0\",public_key=\"923V/iAdcz8BcqB0Xo6pDJzARGBJCQ6fWe+peixQyB4=\",remote_ip=\"10.211.123.112\",allowed_ip_0=\"10.90.0.10\",allowed_subnet_0=\"32\",allowed_ip_1=\"10.0.1.0\",allowed_subnet_1=\"24\",remote_port=\"51820\"} 0
wireguard_received_bytes_total{interface=\"wg0\",public_key=\"9M1fhLa9sIlT39z+SI/0a5H3mNSHYmM+NGA6sirD2nU=\",remote_ip=\"10.211.123.113\",allowed_ip_0=\"10.90.0.3\",allowed_subnet_0=\"32\",allowed_ip_1=\"10.198.171.0\",allowed_subnet_1=\"24\",remote_port=\"51820\"} 0
wireguard_received_bytes_total{interface=\"wg0\",public_key=\"gnRKXngxSppcYegsg38kEFn5Lmk4NcnRXLcZTtg2A2E=\",remote_ip=\"10.211.123.114\",allowed_ip_0=\"10.90.0.11\",allowed_subnet_0=\"32\",allowed_ip_1=\"10.189.143.0\",allowed_subnet_1=\"24\",remote_port=\"51820\"} 0
wireguard_received_bytes_total{interface=\"wg0\",public_key=\"YW7NBDEPXuW9GQlFWFzpgrivMxzdR55M8VOTX+E0thw=\",remote_ip=\"10.211.123.115\",allowed_ip_0=\"10.90.0.12\",allowed_subnet_0=\"32\",allowed_ip_1=\"10.0.2.0\",allowed_subnet_1=\"24\",remote_port=\"51820\"} 0
wireguard_received_bytes_total{interface=\"wg0\",public_key=\"eVfg1BH1hcteASE16+TjShxAJNyFLQ9QIcnCaylD/AA=\",remote_ip=\"10.211.123.116\",allowed_ip_0=\"10.90.0.13\",allowed_subnet_0=\"32\",allowed_ip_1=\"10.0.3.0\",allowed_subnet_1=\"24\",remote_port=\"51820\"} 0
wireguard_received_bytes_total{interface=\"wg0\",public_key=\"lh1h+tWPahB+PAWW62ExHVVrOp9IwdjYwaGnPIXgNwY=\",remote_ip=\"10.211.123.117\",allowed_ip_0=\"10.90.0.9\",allowed_subnet_0=\"32\",allowed_ip_1=\"10.0.4.0\",allowed_subnet_1=\"24\",remote_port=\"51820\"} 0
wireguard_received_bytes_total{interface=\"wg0\",public_key=\"VQIrk1BiBfbOkkKGPiarEvhA4iPuszIL1lddvvFDvE0=\",remote_ip=\"10.211.123.118\",allowed_ip_0=\"10.90.0.8\",allowed_subnet_0=\"32\",allowed_ip_1=\"10.0.5.0\",allowed_subnet_1=\"24\",remote_port=\"51820\"} 0
wireguard_received_bytes_total{interface=\"wg0\",public_key=\"SMp58OwCNnwlzu+OdpA8xiNJzOwbl2gdMaD9CSZCC24=\",remote_ip=\"10.211.123.119\",allowed_ip_0=\"10.90.0.14\",allowed_subnet_0=\"32\",allowed_ip_1=\"10.0.6.0\",allowed_subnet_1=\"24\",remote_port=\"51820\"} 0
wireguard_received_bytes_total{interface=\"wg0\",public_key=\"+0+yMIHVCqyIf4by1gxAjqQ92iKv3bQ/JctNVUEpSlU=\",remote_ip=\"10.211.123.120\",allowed_ip_0=\"10.90.0.7\",allowed_subnet_0=\"32\",allowed_ip_1=\"10.0.7.0\",allowed_subnet_1=\"24\",remote_port=\"51820\"} 0
wireguard_received_bytes_total{interface=\"wg0\",public_key=\"2StYqQY9tyVkGcO4ykKTiTu6AQp/yIYx8I4hwBLO1jA=\",remote_ip=\"10.211.123.121\",allowed_ip_0=\"10.90.0.15\",allowed_subnet_0=\"32\",allowed_ip_1=\"10.0.8.0\",allowed_subnet_1=\"24\",remote_port=\"51820\"} 0
wireguard_received_bytes_total{interface=\"wg0\",public_key=\"qa0AMD2puDBBrs8NYQ+skIrIi/Q5NgQRZLEh5p80Mnc=\",remote_ip=\"10.211.123.122\",allowed_ip_0=\"10.90.0.1\",allowed_subnet_0=\"32\",allowed_ip_1=\"10.0.10.0\",allowed_subnet_1=\"24\",remote_port=\"51820\"} 0
wireguard_received_bytes_total{interface=\"wg0\",public_key=\"YwObmKDK4lfr5F6FHqJhDy9nkUQwbuK8wh4ac2VNSEU=\",remote_ip=\"10.211.123.123\",allowed_ip_0=\"10.90.0.2\",allowed_subnet_0=\"32\",allowed_ip_1=\"10.0.11.0\",allowed_subnet_1=\"24\",remote_port=\"51820\"} 0
wireguard_received_bytes_total{interface=\"wg0\",public_key=\"q07dm9n1UMLFbG6Dh+BNztCt7jVb9VtpVshQEf580kA=\",remote_ip=\"10.211.123.124\",allowed_ip_0=\"10.90.0.6\",allowed_subnet_0=\"32\",allowed_ip_1=\"10.0.13.0\",allowed_subnet_1=\"24\",remote_port=\"51820\"} 0
wireguard_received_bytes_total{interface=\"wg0\",public_key=\"yZOoC2t6pBcXvoczuiJqrQ+8CYvJCzcq8aqyp+APaAE=\",remote_ip=\"10.211.123.125\",allowed_ip_0=\"10.90.0.16\",allowed_subnet_0=\"32\",allowed_ip_1=\"10.0.14.0\",allowed_subnet_1=\"24\",remote_port=\"51820\"} 1232856
wireguard_received_bytes_total{interface=\"wg0\",public_key=\"yjeBkrZqUThSSHySFzWCjxAH8cxtiWSI2I8JFD6t1UM=\",remote_ip=\"10.211.123.126\",allowed_ip_0=\"10.90.0.5\",allowed_subnet_0=\"32\",remote_port=\"51820\"} 18576788764
wireguard_received_bytes_total{interface=\"wg0\",public_key=\"HtOSi37ALMnSkeAFqeWYZqlBnZqAJERhb5o/i3ZPEFI=\",remote_ip=\"10.211.123.127\",allowed_ip_0=\"10.90.0.17\",allowed_subnet_0=\"32\",remote_port=\"51820\"} 62592693520
wireguard_received_bytes_total{interface=\"wg0\",public_key=\"sUsR6xufQQ8Tf0FuyY9tfEeYdhVMeFelr4ZMUrj+B0E=\",remote_ip=\"10.211.123.128\",allowed_ip_0=\"10.90.0.18\",allowed_subnet_0=\"32\",remote_port=\"51820\"} 75066288152
# HELP wireguard_latest_handshake_seconds Seconds from the last handshake
# TYPE wireguard_latest_handshake_seconds gauge
wireguard_latest_handshake_seconds{interface=\"wg0\",public_key=\"923V/iAdcz8BcqB0Xo6pDJzARGBJCQ6fWe+peixQyB4=\",remote_ip=\"10.211.123.112\",allowed_ip_0=\"10.90.0.10\",allowed_subnet_0=\"32\",allowed_ip_1=\"10.0.1.0\",allowed_subnet_1=\"24\",remote_port=\"51820\"} 0
wireguard_latest_handshake_seconds{interface=\"wg0\",public_key=\"9M1fhLa9sIlT39z+SI/0a5H3mNSHYmM+NGA6sirD2nU=\",remote_ip=\"10.211.123.113\",allowed_ip_0=\"10.90.0.3\",allowed_subnet_0=\"32\",allowed_ip_1=\"10.198.171.0\",allowed_subnet_1=\"24\",remote_port=\"51820\"} 0
wireguard_latest_handshake_seconds{interface=\"wg0\",public_key=\"gnRKXngxSppcYegsg38kEFn5Lmk4NcnRXLcZTtg2A2E=\",remote_ip=\"10.211.123.114\",allowed_ip_0=\"10.90.0.11\",allowed_subnet_0=\"32\",allowed_ip_1=\"10.189.143.0\",allowed_subnet_1=\"24\",remote_port=\"51820\"} 0
wireguard_latest_handshake_seconds{interface=\"wg0\",public_key=\"YW7NBDEPXuW9GQlFWFzpgrivMxzdR55M8VOTX+E0thw=\",remote_ip=\"10.211.123.115\",allowed_ip_0=\"10.90.0.12\",allowed_subnet_0=\"32\",allowed_ip_1=\"10.0.2.0\",allowed_subnet_1=\"24\",remote_port=\"51820\"} 0
wireguard_latest_handshake_seconds{interface=\"wg0\",public_key=\"eVfg1BH1hcteASE16+TjShxAJNyFLQ9QIcnCaylD/AA=\",remote_ip=\"10.211.123.116\",allowed_ip_0=\"10.90.0.13\",allowed_subnet_0=\"32\",allowed_ip_1=\"10.0.3.0\",allowed_subnet_1=\"24\",remote_port=\"51820\"} 0
wireguard_latest_handshake_seconds{interface=\"wg0\",public_key=\"lh1h+tWPahB+PAWW62ExHVVrOp9IwdjYwaGnPIXgNwY=\",remote_ip=\"10.211.123.117\",allowed_ip_0=\"10.90.0.9\",allowed_subnet_0=\"32\",allowed_ip_1=\"10.0.4.0\",allowed_subnet_1=\"24\",remote_port=\"51820\"} 0
wireguard_latest_handshake_seconds{interface=\"wg0\",public_key=\"VQIrk1BiBfbOkkKGPiarEvhA4iPuszIL1lddvvFDvE0=\",remote_ip=\"10.211.123.118\",allowed_ip_0=\"10.90.0.8\",allowed_subnet_0=\"32\",allowed_ip_1=\"10.0.5.0\",allowed_subnet_1=\"24\",remote_port=\"51820\"} 0
wireguard_latest_handshake_seconds{interface=\"wg0\",public_key=\"SMp58OwCNnwlzu+OdpA8xiNJzOwbl2gdMaD9CSZCC24=\",remote_ip=\"10.211.123.119\",allowed_ip_0=\"10.90.0.14\",allowed_subnet_0=\"32\",allowed_ip_1=\"10.0.6.0\",allowed_subnet_1=\"24\",remote_port=\"51820\"} 0
wireguard_latest_handshake_seconds{interface=\"wg0\",public_key=\"+0+yMIHVCqyIf4by1gxAjqQ92iKv3bQ/JctNVUEpSlU=\",remote_ip=\"10.211.123.120\",allowed_ip_0=\"10.90.0.7\",allowed_subnet_0=\"32\",allowed_ip_1=\"10.0.7.0\",allowed_subnet_1=\"24\",remote_port=\"51820\"} 0
wireguard_latest_handshake_seconds{interface=\"wg0\",public_key=\"2StYqQY9tyVkGcO4ykKTiTu6AQp/yIYx8I4hwBLO1jA=\",remote_ip=\"10.211.123.121\",allowed_ip_0=\"10.90.0.15\",allowed_subnet_0=\"32\",allowed_ip_1=\"10.0.8.0\",allowed_subnet_1=\"24\",remote_port=\"51820\"} 0
wireguard_latest_handshake_seconds{interface=\"wg0\",public_key=\"qa0AMD2puDBBrs8NYQ+skIrIi/Q5NgQRZLEh5p80Mnc=\",remote_ip=\"10.211.123.122\",allowed_ip_0=\"10.90.0.1\",allowed_subnet_0=\"32\",allowed_ip_1=\"10.0.10.0\",allowed_subnet_1=\"24\",remote_port=\"51820\"} 0
wireguard_latest_handshake_seconds{interface=\"wg0\",public_key=\"YwObmKDK4lfr5F6FHqJhDy9nkUQwbuK8wh4ac2VNSEU=\",remote_ip=\"10.211.123.123\",allowed_ip_0=\"10.90.0.2\",allowed_subnet_0=\"32\",allowed_ip_1=\"10.0.11.0\",allowed_subnet_1=\"24\",remote_port=\"51820\"} 0
wireguard_latest_handshake_seconds{interface=\"wg0\",public_key=\"q07dm9n1UMLFbG6Dh+BNztCt7jVb9VtpVshQEf580kA=\",remote_ip=\"10.211.123.124\",allowed_ip_0=\"10.90.0.6\",allowed_subnet_0=\"32\",allowed_ip_1=\"10.0.13.0\",allowed_subnet_1=\"24\",remote_port=\"51820\"} 0
wireguard_latest_handshake_seconds{interface=\"wg0\",public_key=\"yZOoC2t6pBcXvoczuiJqrQ+8CYvJCzcq8aqyp+APaAE=\",remote_ip=\"10.211.123.125\",allowed_ip_0=\"10.90.0.16\",allowed_subnet_0=\"32\",allowed_ip_1=\"10.0.14.0\",allowed_subnet_1=\"24\",remote_port=\"51820\"} 1574770531
wireguard_latest_handshake_seconds{interface=\"wg0\",public_key=\"yjeBkrZqUThSSHySFzWCjxAH8cxtiWSI2I8JFD6t1UM=\",remote_ip=\"10.211.123.126\",allowed_ip_0=\"10.90.0.5\",allowed_subnet_0=\"32\",remote_port=\"51820\"} 1574770705
wireguard_latest_handshake_seconds{interface=\"wg0\",public_key=\"HtOSi37ALMnSkeAFqeWYZqlBnZqAJERhb5o/i3ZPEFI=\",remote_ip=\"10.211.123.127\",allowed_ip_0=\"10.90.0.17\",allowed_subnet_0=\"32\",remote_port=\"51820\"} 1574770783
wireguard_latest_handshake_seconds{interface=\"wg0\",public_key=\"sUsR6xufQQ8Tf0FuyY9tfEeYdhVMeFelr4ZMUrj+B0E=\",remote_ip=\"10.211.123.128\",allowed_ip_0=\"10.90.0.18\",allowed_subnet_0=\"32\",remote_port=\"51820\"} 1574770693
";
assert_eq!(s, s_ok);
}
#[test]
fn test_parse() {
let a = WireGuard::try_from(TEXT).unwrap();

View File

@ -27,6 +27,8 @@ impl<'a> TryFrom<&[&'a str]> for PeerEntry<'a> {
type Error = PeerEntryParseError;
fn try_from(lines: &[&'a str]) -> Result<PeerEntry<'a>, Self::Error> {
debug!("PeerEntry::TryFrom called with lines == {:?}", lines);
let mut public_key = "";
let mut allowed_ips = "";
let mut name = None;
@ -54,16 +56,18 @@ impl<'a> TryFrom<&[&'a str]> for PeerEntry<'a> {
let lines_owned: Vec<String> = lines.iter().map(|line| line.to_string()).collect();
Err(PeerEntryParseError::AllowedIPsEntryNotFound { lines: lines_owned })
} else {
Ok(PeerEntry {
let pe = PeerEntry {
public_key,
allowed_ips,
name, // name can be None
})
};
debug!("PeerEntry::TryFrom returning PeerEntryHasMap == {:?}", pe);
Ok(pe)
}
}
}
pub(crate) type PeerEntryHashMap<'a> = (HashMap<&'a str, PeerEntry<'a>>);
pub(crate) type PeerEntryHashMap<'a> = HashMap<&'a str, PeerEntry<'a>>;
pub(crate) fn peer_entry_hashmap_try_from(
txt: &str,
@ -100,14 +104,14 @@ pub(crate) fn peer_entry_hashmap_try_from(
v_blocks.push(cur_block);
}
debug!("v_blocks == {:?}", v_blocks);
debug!("peer_entry_hashmap_try_from 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);
debug!("peer_entry_hashmap_try_from hm == {:?}", hm);
Ok(hm)
}