Skip to content

Commit

Permalink
try fix file read
Browse files Browse the repository at this point in the history
  • Loading branch information
ragibkl committed Aug 23, 2024
1 parent 1ed820a commit 4c9466f
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 4 deletions.
12 changes: 9 additions & 3 deletions src/logs_store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ use std::{
use chrono::{DateTime, Duration, NaiveDateTime, Utc};
use serde::Deserialize;

use crate::tasks::dnstap::read_dnstap_logs;

#[allow(dead_code)]
#[derive(serde::Deserialize, Debug, Clone, PartialEq)]
pub struct RawMessage {
Expand Down Expand Up @@ -128,11 +130,15 @@ impl LogsStore {
}
}

pub fn ingest_logs_from_file(&self) {
pub async fn ingest_logs_from_file(&self) {
tracing::info!("LogsStore remove_expired_logs");
self.remove_expired_logs();
tracing::info!("LogsStore remove_expired_logs. DONE");

tracing::info!("LogsStore read_dnstap_logs");
let content = read_dnstap_logs().await;
tracing::info!("LogsStore read_dnstap_logs. DONE");

let content = std::fs::read_to_string("./logs.yaml").unwrap_or_default();
let _ = std::fs::write("./logs.yaml", "");
let logs_hash_map = extract_query_logs(&content);

self.merge_logs(logs_hash_map);
Expand Down
2 changes: 1 addition & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ async fn main() -> anyhow::Result<()> {
}

tracing::info!("Reading logs");
cloned_logs_store.ingest_logs_from_file();
cloned_logs_store.ingest_logs_from_file().await;
tracing::info!("Reading logs. DONE");
}
});
Expand Down
44 changes: 44 additions & 0 deletions src/tasks/dnstap.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,40 @@
use tokio::process::{Child, Command};

// # dnstap -h
// Usage: dnstap [OPTION]...
// -T value
// write dnstap payloads to tcp/ip address
// -U value
// write dnstap payloads to unix socket
// -a append to the given file, do not overwrite. valid only when outputting a text or YAML file.
// -j use verbose JSON output
// -l value
// read dnstap payloads from tcp/ip
// -q use quiet text output
// -r value
// read dnstap payloads from file
// -t duration
// I/O timeout for tcp/ip and unix domain sockets
// -u value
// read dnstap payloads from unix socket
// -w string
// write output to file
// -y use verbose YAML output

// Quiet text output format mnemonics:
// AQ: AUTH_QUERY
// AR: AUTH_RESPONSE
// RQ: RESOLVER_QUERY
// RR: RESOLVER_RESPONSE
// CQ: CLIENT_QUERY
// CR: CLIENT_RESPONSE
// FQ: FORWARDER_QUERY
// FR: FORWARDER_RESPONSE
// SQ: STUB_QUERY
// SR: STUB_RESPONSE
// TQ: TOOL_QUERY
// TR: TOOL_RESPONSE

pub fn spawn_dnstap() -> Result<Child, anyhow::Error> {
let child = Command::new("dnstap")
.arg("-y")
Expand All @@ -13,3 +48,12 @@ pub fn spawn_dnstap() -> Result<Child, anyhow::Error> {

Ok(child)
}

pub async fn read_dnstap_logs() -> String {
let content = tokio::fs::read_to_string("./logs.yaml")
.await
.unwrap_or_default();
let _ = tokio::fs::remove_file("./logs.yaml").await;

content
}

0 comments on commit 4c9466f

Please sign in to comment.