daemon: run analysis in realtime

Currently we just show the results of analysis as a <pre> tagged
JSON blob, but eventually we can make some actual UI
This commit is contained in:
Will Greenberg
2024-04-30 14:43:38 -07:00
parent e8231ad142
commit 3c932f0ce9
11 changed files with 361 additions and 261 deletions

View File

@@ -1,7 +1,10 @@
use std::borrow::Cow;
use chrono::{DateTime, FixedOffset};
use serde::Serialize;
use super::information_element::InformationElement;
use crate::{diag::MessagesContainer, gsmtap_parser};
use super::{information_element::InformationElement, lte_downgrade::LteSib6And7DowngradeAnalyzer};
/// Qualitative measure of how severe a Warning event type is.
/// The levels should break down like this:
@@ -55,22 +58,117 @@ pub trait Analyzer {
fn analyze_information_element(&mut self, ie: &InformationElement) -> Option<Event>;
}
#[derive(Serialize, Debug)]
pub struct AnalyzerMetadata {
name: String,
description: String,
}
#[derive(Serialize, Debug)]
pub struct ReportMetadata {
num_packets_analyzed: usize,
num_packets_skipped: usize,
num_warnings: usize,
first_packet_time: Option<DateTime<FixedOffset>>,
last_packet_time: Option<DateTime<FixedOffset>>,
analyzers: Vec<AnalyzerMetadata>,
}
#[derive(Serialize, Debug, Clone)]
pub struct PacketAnalysis {
timestamp: DateTime<FixedOffset>,
events: Vec<Option<Event>>,
}
#[derive(Serialize, Debug)]
pub struct AnalysisReport {
metadata: ReportMetadata,
analysis: Vec<PacketAnalysis>,
}
pub struct Harness {
analyzers: Vec<Box<dyn Analyzer>>,
analyzers: Vec<Box<dyn Analyzer + Send>>,
pub num_packets_analyzed: usize,
pub num_warnings: usize,
pub skipped_message_reasons: Vec<String>,
pub first_packet_time: Option<DateTime<FixedOffset>>,
pub last_packet_time: Option<DateTime<FixedOffset>>,
pub analysis: Vec<PacketAnalysis>,
}
impl Harness {
pub fn new() -> Self {
Self {
analyzers: Vec::new(),
num_packets_analyzed: 0,
skipped_message_reasons: Vec::new(),
num_warnings: 0,
first_packet_time: None,
last_packet_time: None,
analysis: Vec::new(),
}
}
pub fn add_analyzer(&mut self, analyzer: Box<dyn Analyzer>) {
pub fn new_with_all_analyzers() -> Self {
let mut harness = Harness::new();
harness.add_analyzer(Box::new(LteSib6And7DowngradeAnalyzer{}));
harness
}
pub fn add_analyzer(&mut self, analyzer: Box<dyn Analyzer + Send>) {
self.analyzers.push(analyzer);
}
pub fn analyze_information_element(&mut self, ie: &InformationElement) -> Vec<Option<Event>> {
pub fn analyze_qmdl_messages(&mut self, container: MessagesContainer) {
for maybe_qmdl_message in container.into_messages() {
let qmdl_message = match maybe_qmdl_message {
Ok(msg) => msg,
Err(err) => {
self.skipped_message_reasons.push(format!("{:?}", err));
continue;
}
};
let gsmtap_message = match gsmtap_parser::parse(qmdl_message) {
Ok(msg) => msg,
Err(err) => {
self.skipped_message_reasons.push(format!("{:?}", err));
continue;
}
};
let Some((timestamp, gsmtap_msg)) = gsmtap_message else {
continue;
};
let element = match InformationElement::try_from(&gsmtap_msg) {
Ok(element) => element,
Err(err) => {
self.skipped_message_reasons.push(format!("{:?}", err));
continue;
}
};
if self.first_packet_time.is_none() {
self.first_packet_time = Some(timestamp.to_datetime());
}
self.last_packet_time = Some(timestamp.to_datetime());
self.num_packets_analyzed += 1;
let analysis_result = self.analyze_information_element(&element);
if analysis_result.iter().any(Option::is_some) {
self.num_warnings += analysis_result.iter()
.filter(|maybe_event| matches!(maybe_event, Some(Event { event_type: EventType::QualitativeWarning { .. }, .. })))
.count();
self.analysis.push(PacketAnalysis {
timestamp: timestamp.to_datetime(),
events: analysis_result,
});
}
}
}
fn analyze_information_element(&mut self, ie: &InformationElement) -> Vec<Option<Event>> {
self.analyzers.iter_mut()
.map(|analyzer| analyzer.analyze_information_element(ie))
.collect()
@@ -87,4 +185,28 @@ impl Harness {
.map(|analyzer| analyzer.get_description())
.collect()
}
pub fn build_analysis_report(&self) -> AnalysisReport {
let names = self.get_names();
let descriptions = self.get_names();
let mut analyzers = Vec::new();
for (name, description) in names.iter().zip(descriptions.iter()) {
analyzers.push(AnalyzerMetadata {
name: name.to_string(),
description: description.to_string(),
});
}
AnalysisReport {
metadata: ReportMetadata {
num_packets_analyzed: self.num_packets_analyzed,
num_packets_skipped: self.skipped_message_reasons.len(),
num_warnings: self.num_warnings,
first_packet_time: self.first_packet_time,
last_packet_time: self.last_packet_time,
analyzers,
},
analysis: self.analysis.clone(),
}
}
}