-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7fc002b
commit ae63c48
Showing
6 changed files
with
384 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
{ | ||
"rust-analyzer.checkOnSave.allTargets": false, | ||
"rust-analyzer.checkOnSave.allTargets": true, | ||
"rust-analyzer.cargo.features": ["log"], | ||
"rust-analyzer.cargo.target": "x86_64-unknown-linux-gnu" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
use core::str::FromStr; | ||
|
||
use heapless::{LinearMap, String, Vec}; | ||
use serde::{Deserialize, Serialize}; | ||
|
||
#[derive(Debug, Serialize, Deserialize)] | ||
pub struct TcpConnections { | ||
#[serde(rename = "ec")] | ||
pub established_connections: Option<EstablishedConnections>, | ||
} | ||
|
||
#[derive(Debug, Serialize, Deserialize)] | ||
pub struct EstablishedConnections { | ||
#[serde(rename = "cs")] | ||
pub connections: Option<Vec<Connection, { MAX_CONNECTIONS }>>, | ||
|
||
#[serde(rename = "t")] | ||
pub total: Option<u64>, | ||
} | ||
|
||
#[derive(Debug, Serialize, Deserialize)] | ||
pub struct Connection { | ||
#[serde(rename = "rad")] | ||
pub remote_addr: String<REMOTE_ADDR_SIZE>, | ||
|
||
/// Port number, must be >= 0 | ||
#[serde(rename = "lp")] | ||
pub local_port: Option<u16>, | ||
|
||
/// Interface name | ||
#[serde(rename = "li")] | ||
pub local_interface: Option<String<LOCAL_INTERFACE_SIZE>>, | ||
} | ||
|
||
#[derive(Debug, Serialize, Deserialize)] | ||
pub struct ListeningTcpPorts { | ||
#[serde(rename = "pts")] | ||
pub ports: Option<Vec<TcpPort, MAX_PORTS>>, | ||
|
||
#[serde(rename = "t")] | ||
pub total: Option<u64>, | ||
} | ||
|
||
#[derive(Debug, Serialize, Deserialize)] | ||
pub struct TcpPort { | ||
#[serde(rename = "pt")] | ||
pub port: u16, | ||
|
||
#[serde(rename = "if")] | ||
pub interface: Option<String<LOCAL_INTERFACE_SIZE>>, | ||
} | ||
|
||
#[derive(Debug, Serialize, Deserialize)] | ||
pub struct ListeningUdpPorts { | ||
#[serde(rename = "pts")] | ||
pub ports: Option<Vec<UdpPort, MAX_PORTS>>, | ||
|
||
#[serde(rename = "t")] | ||
pub total: Option<u64>, | ||
} | ||
|
||
#[derive(Debug, Serialize, Deserialize)] | ||
pub struct UdpPort { | ||
#[serde(rename = "pt")] | ||
pub port: u16, | ||
|
||
#[serde(rename = "if")] | ||
pub interface: Option<String<LOCAL_INTERFACE_SIZE>>, | ||
} | ||
|
||
#[derive(Debug, Serialize, Deserialize)] | ||
pub struct NetworkStats { | ||
#[serde(rename = "bi")] | ||
pub bytes_in: Option<u64>, | ||
|
||
#[serde(rename = "bo")] | ||
pub bytes_out: Option<u64>, | ||
|
||
#[serde(rename = "pi")] | ||
pub packets_in: Option<u64>, | ||
|
||
#[serde(rename = "po")] | ||
pub packets_out: Option<u64>, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
use core::str::FromStr; | ||
|
||
use heapless::{LinearMap, String, Vec}; | ||
use serde::{Deserialize, Serialize}; | ||
|
||
// Constants for heapless container sizes | ||
pub const HEADER_VERSION_SIZE: usize = 6; | ||
pub const REMOTE_ADDR_SIZE: usize = 64; | ||
pub const LOCAL_INTERFACE_SIZE: usize = 32; | ||
pub const MAX_METRICS: usize = 8; | ||
pub const MAX_CUSTOM_METRICS: usize = 16; | ||
pub const MAX_CUSTOM_METRICS_NAME: usize = 32; | ||
|
||
pub enum MetricError { | ||
Malformed, | ||
Throttled, | ||
MissingHeader, | ||
Other, | ||
} | ||
|
||
#[derive(Debug, Serialize, Deserialize)] | ||
pub struct Metric { | ||
#[serde(rename = "hed")] | ||
pub header: Header, | ||
|
||
#[serde(rename = "cmet")] | ||
pub custom_metrics: Option< | ||
LinearMap<String<MAX_CUSTOM_METRICS_NAME>, Vec<CustomMetric, 1>, MAX_CUSTOM_METRICS>, | ||
>, | ||
} | ||
|
||
impl Metric { | ||
pub fn new( | ||
custom_metrics: Option< | ||
LinearMap<String<MAX_CUSTOM_METRICS_NAME>, Vec<CustomMetric, 1>, MAX_CUSTOM_METRICS>, | ||
>, | ||
timestamp: i64, | ||
) -> Self { | ||
let header = Header { | ||
report_id: timestamp, | ||
version: String::<HEADER_VERSION_SIZE>::from_str("1.0").unwrap(), //FIXME: Don't | ||
}; | ||
|
||
Self { | ||
header, | ||
custom_metrics, | ||
} | ||
} | ||
} | ||
|
||
#[derive(Debug, Serialize, Deserialize)] | ||
pub struct Header { | ||
/// Monotonically increasing value. Epoch timestamp recommended. | ||
#[serde(rename = "rid")] | ||
pub report_id: i64, | ||
|
||
/// Version in Major.Minor format. | ||
#[serde(rename = "v")] | ||
pub version: String<HEADER_VERSION_SIZE>, | ||
} | ||
|
||
#[derive(Debug, Serialize, Deserialize)] | ||
#[serde(rename_all = "snake_case")] | ||
pub enum CustomMetric { | ||
Number(u64), | ||
NumberList(Vec<u64, MAX_METRICS>), | ||
StringList(Vec<String<LOCAL_INTERFACE_SIZE>, MAX_METRICS>), | ||
IpList(Vec<String<REMOTE_ADDR_SIZE>, MAX_METRICS>), | ||
} | ||
|
||
impl CustomMetric { | ||
pub fn new_number(value: u64) -> heapless::Vec<Self, 1> { | ||
let mut custom_metric_map = Vec::new(); | ||
|
||
custom_metric_map.push(CustomMetric::Number(value)).unwrap(); | ||
|
||
custom_metric_map | ||
} | ||
|
||
pub fn new_number_list(values: &[u64]) -> heapless::Vec<Self, 1> { | ||
let mut custom_metric_map = Vec::new(); | ||
|
||
let mut vec = Vec::new(); | ||
for &v in values { | ||
vec.push(v).unwrap(); | ||
} | ||
|
||
custom_metric_map | ||
.push(CustomMetric::NumberList(vec)) | ||
.unwrap(); | ||
|
||
custom_metric_map | ||
} | ||
|
||
pub fn new_string_list(values: &[&str]) -> heapless::Vec<Self, 1> { | ||
let mut custom_metric_map = Vec::new(); | ||
|
||
let mut vec = Vec::new(); | ||
for &v in values { | ||
vec.push(String::from_str(v).unwrap()).unwrap(); | ||
} | ||
custom_metric_map | ||
.push(CustomMetric::StringList(vec)) | ||
.unwrap(); | ||
|
||
custom_metric_map | ||
} | ||
|
||
pub fn new_ip_list(values: &[&str]) -> heapless::Vec<Self, 1> { | ||
let mut custom_metric_map = Vec::new(); | ||
|
||
let mut vec = Vec::new(); | ||
for &v in values { | ||
vec.push(String::from_str(v).unwrap()).unwrap(); | ||
} | ||
custom_metric_map.push(CustomMetric::IpList(vec)).unwrap(); | ||
|
||
custom_metric_map | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.