forked from torrust/torrust-tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlogs_parser.rs
More file actions
114 lines (101 loc) · 4.49 KB
/
logs_parser.rs
File metadata and controls
114 lines (101 loc) · 4.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
//! Utilities to parse Torrust Tracker logs.
use serde::{Deserialize, Serialize};
const UDP_TRACKER_PATTERN: &str = "[UDP TRACKER][INFO] Starting on: udp://";
const HTTP_TRACKER_PATTERN: &str = "[HTTP TRACKER][INFO] Starting on: ";
const HEALTH_CHECK_PATTERN: &str = "[HEALTH CHECK API][INFO] Starting on: ";
#[derive(Serialize, Deserialize, Debug, Default)]
pub struct RunningServices {
pub udp_trackers: Vec<String>,
pub http_trackers: Vec<String>,
pub health_checks: Vec<String>,
}
impl RunningServices {
/// It parses the tracker logs to extract the running services.
///
/// For example, from this logs:
///
/// ```text
/// Loading default configuration file: `./share/default/config/tracker.development.sqlite3.toml` ...
/// 2024-01-24T16:36:14.614898789+00:00 [torrust_tracker::bootstrap::logging][INFO] logging initialized.
/// 2024-01-24T16:36:14.615586025+00:00 [UDP TRACKER][INFO] Starting on: udp://0.0.0.0:6969
/// 2024-01-24T16:36:14.615623705+00:00 [torrust_tracker::bootstrap::jobs][INFO] TLS not enabled
/// 2024-01-24T16:36:14.615694484+00:00 [HTTP TRACKER][INFO] Starting on: http://0.0.0.0:7070
/// 2024-01-24T16:36:14.615710534+00:00 [HTTP TRACKER][INFO] Started on: http://0.0.0.0:7070
/// 2024-01-24T16:36:14.615716574+00:00 [torrust_tracker::bootstrap::jobs][INFO] TLS not enabled
/// 2024-01-24T16:36:14.615764904+00:00 [API][INFO] Starting on http://127.0.0.1:1212
/// 2024-01-24T16:36:14.615767264+00:00 [API][INFO] Started on http://127.0.0.1:1212
/// 2024-01-24T16:36:14.615777574+00:00 [HEALTH CHECK API][INFO] Starting on: http://127.0.0.1:1313
/// 2024-01-24T16:36:14.615791124+00:00 [HEALTH CHECK API][INFO] Started on: http://127.0.0.1:1313
/// ```
///
/// It would extract these services:
///
/// ```json
/// {
/// "udp_trackers": [
/// "127.0.0.1:6969"
/// ],
/// "http_trackers": [
/// "http://127.0.0.1:7070"
/// ],
/// "health_checks": [
/// "http://127.0.0.1:1313/health_check"
/// ]
/// }
/// ```
#[must_use]
pub fn parse_from_logs(logs: &str) -> Self {
let mut udp_trackers: Vec<String> = Vec::new();
let mut http_trackers: Vec<String> = Vec::new();
let mut health_checks: Vec<String> = Vec::new();
for line in logs.lines() {
if let Some(address) = Self::extract_address_if_matches(line, UDP_TRACKER_PATTERN) {
udp_trackers.push(address);
} else if let Some(address) = Self::extract_address_if_matches(line, HTTP_TRACKER_PATTERN) {
http_trackers.push(address);
} else if let Some(address) = Self::extract_address_if_matches(line, HEALTH_CHECK_PATTERN) {
health_checks.push(format!("{address}/health_check"));
}
}
Self {
udp_trackers,
http_trackers,
health_checks,
}
}
fn extract_address_if_matches(line: &str, pattern: &str) -> Option<String> {
line.find(pattern)
.map(|start| Self::replace_wildcard_ip_with_localhost(line[start + pattern.len()..].trim()))
}
fn replace_wildcard_ip_with_localhost(address: &str) -> String {
address.replace("0.0.0.0", "127.0.0.1")
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn it_should_parse_from_logs_with_valid_logs() {
let logs = "\
[UDP TRACKER][INFO] Starting on: udp://0.0.0.0:8080\n\
[HTTP TRACKER][INFO] Starting on: 0.0.0.0:9090\n\
[HEALTH CHECK API][INFO] Starting on: 0.0.0.0:10010";
let running_services = RunningServices::parse_from_logs(logs);
assert_eq!(running_services.udp_trackers, vec!["127.0.0.1:8080"]);
assert_eq!(running_services.http_trackers, vec!["127.0.0.1:9090"]);
assert_eq!(running_services.health_checks, vec!["127.0.0.1:10010/health_check"]);
}
#[test]
fn it_should_ignore_logs_with_no_matching_lines() {
let logs = "[Other Service][INFO] Starting on: 0.0.0.0:7070";
let running_services = RunningServices::parse_from_logs(logs);
assert!(running_services.udp_trackers.is_empty());
assert!(running_services.http_trackers.is_empty());
assert!(running_services.health_checks.is_empty());
}
#[test]
fn it_should_replace_wildcard_ip_with_localhost() {
let address = "0.0.0.0:8080";
assert_eq!(RunningServices::replace_wildcard_ip_with_localhost(address), "127.0.0.1:8080");
}
}