From 4c9466f260b321449dbb8d578b9aaf4ae4c59bca Mon Sep 17 00:00:00 2001 From: Ragib Badaruddin Date: Fri, 23 Aug 2024 19:22:19 +0800 Subject: [PATCH] try fix file read --- src/logs_store.rs | 12 +++++++++--- src/main.rs | 2 +- src/tasks/dnstap.rs | 44 ++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 54 insertions(+), 4 deletions(-) diff --git a/src/logs_store.rs b/src/logs_store.rs index d790f68..6879981 100644 --- a/src/logs_store.rs +++ b/src/logs_store.rs @@ -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 { @@ -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); diff --git a/src/main.rs b/src/main.rs index 4caea58..bb46e3c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -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"); } }); diff --git a/src/tasks/dnstap.rs b/src/tasks/dnstap.rs index 5cf31ff..55bd469 100644 --- a/src/tasks/dnstap.rs +++ b/src/tasks/dnstap.rs @@ -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 { let child = Command::new("dnstap") .arg("-y") @@ -13,3 +48,12 @@ pub fn spawn_dnstap() -> Result { 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 +}