mirror of https://github.com/dswd/vpncloud.git
Change stats output format ot yaml
This commit is contained in:
parent
02ccea326e
commit
ed0fdbc366
|
@ -28,7 +28,7 @@ use super::{
|
|||
traffic::TrafficStats,
|
||||
types::{Error, HeaderMagic, NodeId, Protocol, Range, Table},
|
||||
udpmessage::{decode, encode, Message},
|
||||
util::{resolve, CtrlC, Duration, Time, TimeSource}
|
||||
util::{addr_nice, resolve, CtrlC, Duration, Time, TimeSource}
|
||||
};
|
||||
|
||||
pub type Hash = BuildHasherDefault<FnvHasher>;
|
||||
|
@ -199,10 +199,10 @@ impl<TS: TimeSource> PeerList<TS> {
|
|||
|
||||
#[inline]
|
||||
fn write_out<W: Write>(&self, out: &mut W) -> Result<(), io::Error> {
|
||||
writeln!(out, "Peers:")?;
|
||||
writeln!(out, "peers:")?;
|
||||
let now = TS::now();
|
||||
for (addr, data) in &self.peers {
|
||||
writeln!(out, " - {} (ttl: {} s)", addr, data.timeout - now)?;
|
||||
writeln!(out, " - \"{}\": {{ ttl_secs: {} }}", addr_nice(*addr), data.timeout - now)?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
@ -631,6 +631,7 @@ impl<D: Device, P: Protocol, T: Table, S: Socket, TS: TimeSource> GenericCloud<D
|
|||
self.broadcast_msg(&mut msg)?;
|
||||
} else {
|
||||
debug!("No destination for {} found, dropping", dst);
|
||||
self.traffic.count_dropped_payload(end - start);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -756,6 +757,7 @@ impl<D: Device, P: Protocol, T: Table, S: Socket, TS: TimeSource> GenericCloud<D
|
|||
self.handle_net_message(src, msg)
|
||||
}) {
|
||||
error!("Error: {}, from: {}", e, src);
|
||||
self.traffic.count_invalid_protocol(size);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -14,7 +14,7 @@ use fnv::FnvHasher;
|
|||
|
||||
use super::{
|
||||
types::{Address, Error, Protocol, Table},
|
||||
util::{Duration, Time, TimeSource}
|
||||
util::{addr_nice, Duration, Time, TimeSource}
|
||||
};
|
||||
|
||||
/// An ethernet frame dissector
|
||||
|
@ -110,9 +110,15 @@ impl<TS: TimeSource> Table for SwitchTable<TS> {
|
|||
/// Write out the table
|
||||
fn write_out<W: Write>(&self, out: &mut W) -> Result<(), io::Error> {
|
||||
let now = TS::now();
|
||||
writeln!(out, "Switch table:")?;
|
||||
writeln!(out, "switch_table:")?;
|
||||
for (addr, val) in &self.table {
|
||||
writeln!(out, " - {} => {} (ttl: {} s)", addr, val.address, val.timeout - now)?;
|
||||
writeln!(
|
||||
out,
|
||||
" - \"{}\": {{ peer: \"{}\", ttl_secs: {} }}",
|
||||
addr,
|
||||
addr_nice(val.address),
|
||||
val.timeout - now
|
||||
)?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
|
15
src/ip.rs
15
src/ip.rs
|
@ -11,7 +11,10 @@ use std::{
|
|||
|
||||
use fnv::FnvHasher;
|
||||
|
||||
use super::types::{Address, Error, Protocol, Table};
|
||||
use super::{
|
||||
types::{Address, Error, Protocol, Table},
|
||||
util::addr_nice
|
||||
};
|
||||
|
||||
|
||||
/// An IP packet dissector
|
||||
|
@ -153,10 +156,16 @@ impl Table for RoutingTable {
|
|||
|
||||
/// Write out the table
|
||||
fn write_out<W: Write>(&self, out: &mut W) -> Result<(), io::Error> {
|
||||
writeln!(out, "Routing table:")?;
|
||||
writeln!(out, "routing_table:")?;
|
||||
for entries in self.0.values() {
|
||||
for entry in entries {
|
||||
writeln!(out, " - {}/{} => {}", entry.bytes, entry.prefix_len, entry.address)?;
|
||||
writeln!(
|
||||
out,
|
||||
" - \"{}/{}\": {{ peer: \"{}\" }}",
|
||||
entry.bytes,
|
||||
entry.prefix_len,
|
||||
addr_nice(entry.address)
|
||||
)?;
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
|
|
|
@ -8,7 +8,11 @@ use std::{
|
|||
net::SocketAddr
|
||||
};
|
||||
|
||||
use super::{cloud::Hash, types::Address, util::Bytes};
|
||||
use super::{
|
||||
cloud::Hash,
|
||||
types::Address,
|
||||
util::{addr_nice, Bytes}
|
||||
};
|
||||
|
||||
|
||||
#[derive(Default)]
|
||||
|
@ -58,7 +62,8 @@ impl TrafficEntry {
|
|||
#[derive(Default)]
|
||||
pub struct TrafficStats {
|
||||
peers: HashMap<SocketAddr, TrafficEntry, Hash>,
|
||||
payload: HashMap<(Address, Address), TrafficEntry, Hash>
|
||||
payload: HashMap<(Address, Address), TrafficEntry, Hash>,
|
||||
dropped: TrafficEntry
|
||||
}
|
||||
|
||||
impl TrafficStats {
|
||||
|
@ -82,6 +87,14 @@ impl TrafficStats {
|
|||
self.payload.entry((remote, local)).or_insert_with(TrafficEntry::default).count_in(bytes);
|
||||
}
|
||||
|
||||
pub fn count_invalid_protocol(&mut self, bytes: usize) {
|
||||
self.dropped.count_in(bytes)
|
||||
}
|
||||
|
||||
pub fn count_dropped_payload(&mut self, bytes: usize) {
|
||||
self.dropped.count_out(bytes)
|
||||
}
|
||||
|
||||
pub fn period(&mut self, cleanup_idle: Option<usize>) {
|
||||
for entry in self.peers.values_mut() {
|
||||
entry.period();
|
||||
|
@ -89,6 +102,7 @@ impl TrafficStats {
|
|||
for entry in self.payload.values_mut() {
|
||||
entry.period();
|
||||
}
|
||||
self.dropped.period();
|
||||
if let Some(periods) = cleanup_idle {
|
||||
self.peers.retain(|_, entry| entry.idle_periods < periods);
|
||||
self.payload.retain(|_, entry| entry.idle_periods < periods);
|
||||
|
@ -105,26 +119,55 @@ impl TrafficStats {
|
|||
|
||||
#[inline]
|
||||
pub fn write_out<W: Write>(&self, out: &mut W) -> Result<(), io::Error> {
|
||||
writeln!(out, "Peer traffic:")?;
|
||||
writeln!(out, "peer_traffic:")?;
|
||||
let mut peers: Vec<_> = self.get_peer_traffic().collect();
|
||||
peers.sort_unstable_by_key(|(_, data)| (data.out_bytes + data.in_bytes));
|
||||
for (addr, data) in peers.iter().rev() {
|
||||
writeln!(out, " - {}: in={}/s, out={}/s", addr, Bytes(data.in_bytes / 60), Bytes(data.out_bytes / 60))?;
|
||||
writeln!(
|
||||
out,
|
||||
" - peer: \"{}\"\n in: {{ display: \"{}/s\", bytes: {}, packets: {} }}\n out: {{ display: \"{}/s\", bytes: {}, packets: {} }}",
|
||||
addr_nice(**addr),
|
||||
Bytes(data.in_bytes / 60),
|
||||
data.in_bytes,
|
||||
data.in_packets,
|
||||
Bytes(data.out_bytes / 60),
|
||||
data.out_bytes,
|
||||
data.out_packets
|
||||
)?;
|
||||
}
|
||||
writeln!(out)?;
|
||||
writeln!(out, "Payload traffic:")?;
|
||||
writeln!(out, "payload_traffic:")?;
|
||||
let mut payload: Vec<_> = self.get_payload_traffic().collect();
|
||||
payload.sort_unstable_by_key(|(_, data)| (data.out_bytes + data.in_bytes));
|
||||
for ((remote, local), data) in payload.iter().rev() {
|
||||
writeln!(
|
||||
out,
|
||||
" - {} <-> {}: in={}/s, out={}/s",
|
||||
" - addrs: [\"{}\", \"{}\"]\n in: {{ display: \"{}/s\", bytes: {}, packets: {} }}\n out: {{ display: \"{}/s\", bytes: {}, packets: {} }}",
|
||||
remote,
|
||||
local,
|
||||
Bytes(data.in_bytes / 60),
|
||||
Bytes(data.out_bytes / 60)
|
||||
data.in_bytes,
|
||||
data.in_packets,
|
||||
Bytes(data.out_bytes / 60),
|
||||
data.out_bytes,
|
||||
data.out_packets
|
||||
)?;
|
||||
}
|
||||
writeln!(out)?;
|
||||
writeln!(
|
||||
out,
|
||||
"invalid_protocol_traffic: {{ display: \"{}/s\", bytes: {}, packets: {} }}",
|
||||
Bytes(self.dropped.in_bytes / 60),
|
||||
self.dropped.in_bytes,
|
||||
self.dropped.in_packets
|
||||
)?;
|
||||
writeln!(
|
||||
out,
|
||||
"dropped_payload_traffic: {{ display: \"{}/s\", bytes: {}, packets: {} }}",
|
||||
Bytes(self.dropped.out_bytes / 60),
|
||||
self.dropped.out_bytes,
|
||||
self.dropped.out_packets
|
||||
)?;
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
|
|
@ -31,6 +31,15 @@ pub fn bytes_to_hex(bytes: &[u8]) -> String {
|
|||
s
|
||||
}
|
||||
|
||||
pub fn addr_nice(addr: SocketAddr) -> SocketAddr {
|
||||
if let SocketAddr::V6(v6addr) = addr {
|
||||
if let Some(ip) = v6addr.ip().to_ipv4() {
|
||||
return (ip, addr.port()).into()
|
||||
}
|
||||
}
|
||||
addr
|
||||
}
|
||||
|
||||
|
||||
pub struct Encoder;
|
||||
|
||||
|
|
Loading…
Reference in New Issue