Skip to content

Commit

Permalink
add metric wireguard_peer_endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
furmur committed Mar 24, 2024
1 parent 8da11de commit 28ca154
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 3 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ capabilities in both the ambient and bounding set:

- Total number of bytes transferred in/out per peer
- Total number of bytes transferred in/out per interface
- Metrics with the info for each connected peer
- Time since last handshake per peer
- Scrape duration in milliseconds
- Scrape success
Expand Down Expand Up @@ -131,6 +132,11 @@ wireguard_peer_bytes_total{direction="tx",interface="custom",peer="N5UQp3XbysLBA
wireguard_peer_bytes_total{direction="tx",interface="custom",peer="QlgHHfYP3aMlRG7d6/Zp9IhUOLrpT5G2GIdODODaUHQ="} 88648
wireguard_peer_bytes_total{direction="tx",interface="custom",peer="FtUeMGdNxgkVN0G9lpvOc5jtAQQ1m9DpvZPDCUdKBx0="} 480852300
wireguard_peer_bytes_total{direction="tx",interface="wg0",peer="bRQZOyOZUvHMhBvCWq2sXO0VsRu6Aq5LCACi/R3AJk8="} 2393043528
# HELP wireguard_peer_endpoint Peers info. static value
# TYPE wireguard_peer_endpoint gauge
wireguard_peer_endpoint{alias="kevin",endpoint_ip="1.1.1.1",interface="custom",peer="q2JWEKWfLPU5UjG2Sq31xx2GsSjdhKNtdT/X/tFVyjs="} 1
wireguard_peer_endpoint{alias="jane",endpoint_ip="8.8.8.8",interface="custom",peer="2ELWFmGnqhtRpu4r2PUKc0cw+ELtuMPLd6l0KsoCUBQ="} 1
wireguard_peer_endpoint{alias="robert",endpoint_ip="127.0.0.1",interface="custom",peer="duVVziZbyIiIPoRprisE69K0By198Cn8dPwY5bFecEk="} 1
# HELP wireguard_peers_total Total number of peers per interfaces
# TYPE wireguard_peers_total gauge
wireguard_peers_total{interface="custom"} 7
Expand Down
23 changes: 23 additions & 0 deletions src/metrics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use crate::wireguard::WireguardState;
pub struct Metrics {
interfaces_total: IntGaugeVec,
peers_total: IntGaugeVec,
peer_endpoint: IntGaugeVec,
bytes_total: IntCounterVec,
peer_bytes_total: IntCounterVec,
duration_since_latest_handshake: IntGaugeVec,
Expand All @@ -30,6 +31,14 @@ impl Metrics {
&["interface"],
)?;

let peer_endpoint = IntGaugeVec::new(
Opts::new(
"wireguard_peer_endpoint",
"Peers info. static value",
),
&["interface", "endpoint_ip", "peer", "alias"],
)?;

let bytes_total = IntCounterVec::new(
Opts::new(
"wireguard_bytes_total",
Expand Down Expand Up @@ -57,6 +66,7 @@ impl Metrics {
debug!("Registering wireguard metrics");
r.register(Box::new(interfaces_total.clone()))?;
r.register(Box::new(peers_total.clone()))?;
r.register(Box::new(peer_endpoint.clone()))?;
r.register(Box::new(bytes_total.clone()))?;
r.register(Box::new(peer_bytes_total.clone()))?;
r.register(Box::new(duration_since_latest_handshake.clone()))?;
Expand All @@ -65,6 +75,7 @@ impl Metrics {
interfaces_total,
bytes_total,
peers_total,
peer_endpoint,
peer_bytes_total,
duration_since_latest_handshake,
})
Expand Down Expand Up @@ -108,6 +119,18 @@ impl Metrics {
for p in &state.peers {
assert!(p.interface < state.interfaces.len());

if let Some(endpoint) = p.endpoint {
self.peer_endpoint.with_label_values(&[
&state.interfaces[p.interface],
&endpoint.ip().to_string(),
&p.pubkey,
&p.alias
.as_ref()
.map(ToOwned::to_owned)
.unwrap_or_else(String::new),
]).set(1);
};

let pbtt = self.peer_bytes_total.with_label_values(&[
&state.interfaces[p.interface],
&p.pubkey,
Expand Down
7 changes: 4 additions & 3 deletions src/wireguard.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use std::collections::HashMap;

use std::{collections::HashMap, str::FromStr, net::SocketAddr};
use color_eyre::eyre::{Result, WrapErr};
use tokio::process::Command;

Expand Down Expand Up @@ -48,7 +47,7 @@ impl WireguardState {
Some(_iface),
Some(pubkey),
Some(_psk),
Some(_endpoint),
Some(endpoint),
Some(_allowed_ips),
Some(handshake_ts),
Some(tx_bytes),
Expand All @@ -60,6 +59,7 @@ impl WireguardState {
interface: interfaces.len() - 1,
alias: aliases.get(pubkey).map(|&s| s.into()),
pubkey: pubkey.into(),
endpoint: SocketAddr::from_str(endpoint).ok(),
handshake_timestamp: if ts == 0 { None } else { Some(ts) },
tx_bytes: tx_bytes.parse()?,
rx_bytes: rx_bytes.parse()?,
Expand Down Expand Up @@ -87,6 +87,7 @@ impl WireguardState {
#[derive(Clone, PartialEq, Debug, Default)]
pub struct Peer {
pub pubkey: String,
pub endpoint: Option<SocketAddr>,
pub alias: Option<String>,
pub interface: usize,
pub tx_bytes: u64,
Expand Down

0 comments on commit 28ca154

Please sign in to comment.