Change stats output format ot yaml

pull/61/head
Dennis Schwerdel 2020-05-29 10:04:14 +02:00
parent 02ccea326e
commit ed0fdbc366
5 changed files with 85 additions and 16 deletions

View File

@ -28,7 +28,7 @@ use super::{
traffic::TrafficStats, traffic::TrafficStats,
types::{Error, HeaderMagic, NodeId, Protocol, Range, Table}, types::{Error, HeaderMagic, NodeId, Protocol, Range, Table},
udpmessage::{decode, encode, Message}, udpmessage::{decode, encode, Message},
util::{resolve, CtrlC, Duration, Time, TimeSource} util::{addr_nice, resolve, CtrlC, Duration, Time, TimeSource}
}; };
pub type Hash = BuildHasherDefault<FnvHasher>; pub type Hash = BuildHasherDefault<FnvHasher>;
@ -199,10 +199,10 @@ impl<TS: TimeSource> PeerList<TS> {
#[inline] #[inline]
fn write_out<W: Write>(&self, out: &mut W) -> Result<(), io::Error> { fn write_out<W: Write>(&self, out: &mut W) -> Result<(), io::Error> {
writeln!(out, "Peers:")?; writeln!(out, "peers:")?;
let now = TS::now(); let now = TS::now();
for (addr, data) in &self.peers { 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(()) Ok(())
} }
@ -631,6 +631,7 @@ impl<D: Device, P: Protocol, T: Table, S: Socket, TS: TimeSource> GenericCloud<D
self.broadcast_msg(&mut msg)?; self.broadcast_msg(&mut msg)?;
} else { } else {
debug!("No destination for {} found, dropping", dst); 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) self.handle_net_message(src, msg)
}) { }) {
error!("Error: {}, from: {}", e, src); error!("Error: {}, from: {}", e, src);
self.traffic.count_invalid_protocol(size);
} }
} }

View File

@ -14,7 +14,7 @@ use fnv::FnvHasher;
use super::{ use super::{
types::{Address, Error, Protocol, Table}, types::{Address, Error, Protocol, Table},
util::{Duration, Time, TimeSource} util::{addr_nice, Duration, Time, TimeSource}
}; };
/// An ethernet frame dissector /// An ethernet frame dissector
@ -110,9 +110,15 @@ impl<TS: TimeSource> Table for SwitchTable<TS> {
/// Write out the table /// Write out the table
fn write_out<W: Write>(&self, out: &mut W) -> Result<(), io::Error> { fn write_out<W: Write>(&self, out: &mut W) -> Result<(), io::Error> {
let now = TS::now(); let now = TS::now();
writeln!(out, "Switch table:")?; writeln!(out, "switch_table:")?;
for (addr, val) in &self.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(()) Ok(())
} }

View File

@ -11,7 +11,10 @@ use std::{
use fnv::FnvHasher; use fnv::FnvHasher;
use super::types::{Address, Error, Protocol, Table}; use super::{
types::{Address, Error, Protocol, Table},
util::addr_nice
};
/// An IP packet dissector /// An IP packet dissector
@ -153,10 +156,16 @@ impl Table for RoutingTable {
/// Write out the table /// Write out the table
fn write_out<W: Write>(&self, out: &mut W) -> Result<(), io::Error> { 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 entries in self.0.values() {
for entry in entries { 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(()) Ok(())

View File

@ -8,7 +8,11 @@ use std::{
net::SocketAddr net::SocketAddr
}; };
use super::{cloud::Hash, types::Address, util::Bytes}; use super::{
cloud::Hash,
types::Address,
util::{addr_nice, Bytes}
};
#[derive(Default)] #[derive(Default)]
@ -58,7 +62,8 @@ impl TrafficEntry {
#[derive(Default)] #[derive(Default)]
pub struct TrafficStats { pub struct TrafficStats {
peers: HashMap<SocketAddr, TrafficEntry, Hash>, peers: HashMap<SocketAddr, TrafficEntry, Hash>,
payload: HashMap<(Address, Address), TrafficEntry, Hash> payload: HashMap<(Address, Address), TrafficEntry, Hash>,
dropped: TrafficEntry
} }
impl TrafficStats { impl TrafficStats {
@ -82,6 +87,14 @@ impl TrafficStats {
self.payload.entry((remote, local)).or_insert_with(TrafficEntry::default).count_in(bytes); 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>) { pub fn period(&mut self, cleanup_idle: Option<usize>) {
for entry in self.peers.values_mut() { for entry in self.peers.values_mut() {
entry.period(); entry.period();
@ -89,6 +102,7 @@ impl TrafficStats {
for entry in self.payload.values_mut() { for entry in self.payload.values_mut() {
entry.period(); entry.period();
} }
self.dropped.period();
if let Some(periods) = cleanup_idle { if let Some(periods) = cleanup_idle {
self.peers.retain(|_, entry| entry.idle_periods < periods); self.peers.retain(|_, entry| entry.idle_periods < periods);
self.payload.retain(|_, entry| entry.idle_periods < periods); self.payload.retain(|_, entry| entry.idle_periods < periods);
@ -105,26 +119,55 @@ impl TrafficStats {
#[inline] #[inline]
pub fn write_out<W: Write>(&self, out: &mut W) -> Result<(), io::Error> { 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(); let mut peers: Vec<_> = self.get_peer_traffic().collect();
peers.sort_unstable_by_key(|(_, data)| (data.out_bytes + data.in_bytes)); peers.sort_unstable_by_key(|(_, data)| (data.out_bytes + data.in_bytes));
for (addr, data) in peers.iter().rev() { 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)?;
writeln!(out, "Payload traffic:")?; writeln!(out, "payload_traffic:")?;
let mut payload: Vec<_> = self.get_payload_traffic().collect(); let mut payload: Vec<_> = self.get_payload_traffic().collect();
payload.sort_unstable_by_key(|(_, data)| (data.out_bytes + data.in_bytes)); payload.sort_unstable_by_key(|(_, data)| (data.out_bytes + data.in_bytes));
for ((remote, local), data) in payload.iter().rev() { for ((remote, local), data) in payload.iter().rev() {
writeln!( writeln!(
out, out,
" - {} <-> {}: in={}/s, out={}/s", " - addrs: [\"{}\", \"{}\"]\n in: {{ display: \"{}/s\", bytes: {}, packets: {} }}\n out: {{ display: \"{}/s\", bytes: {}, packets: {} }}",
remote, remote,
local, local,
Bytes(data.in_bytes / 60), 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(()) Ok(())
} }
} }

View File

@ -31,6 +31,15 @@ pub fn bytes_to_hex(bytes: &[u8]) -> String {
s 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; pub struct Encoder;