From bdf41fa6056c397664d351fc7e4cd26aa82be765 Mon Sep 17 00:00:00 2001 From: Paul Lecuq Date: Wed, 2 Oct 2024 19:07:27 +0200 Subject: [PATCH 1/2] add systemd notify statuses --- src/ipblc.rs | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/src/ipblc.rs b/src/ipblc.rs index b5de02d..2586a54 100644 --- a/src/ipblc.rs +++ b/src/ipblc.rs @@ -11,6 +11,7 @@ use chrono::prelude::*; use chrono::prelude::{DateTime, Local}; use chrono::Duration; use nix::sys::inotify::{InitFlags, Inotify, InotifyEvent}; +use sd_notify::*; use std::collections::HashMap; use std::sync::Arc; use tokio::sync::mpsc::{channel, Receiver, Sender}; @@ -21,6 +22,13 @@ const BL_CHAN_SIZE: usize = 32; const WS_CHAN_SIZE: usize = 64; const LOOP_MAX_WAIT: u64 = 5; +macro_rules! log_with_systemd { + ($msg:expr) => { + println!("{}", $msg); + notify(false, &[NotifyState::Status(format!("{}", $msg).as_str())]).unwrap(); + }; +} + pub async fn run() { let inotify = Inotify::init(InitFlags::empty()).unwrap(); let globalctx = Context::new(&inotify).await; @@ -31,7 +39,7 @@ pub async fn run() { let pkgversion = format!("{}@{}", env!("CARGO_PKG_VERSION"), GIT_VERSION); let mut last_cfg_reload: DateTime = Local::now().trunc_subsecs(0); - println!("Launching {}, version {}", PKG_NAME, pkgversion); + log_with_systemd!(format!("Launching {}, version {}", PKG_NAME, pkgversion)); fwglobalinit(); let ctxapi = Arc::clone(&ctxarc); @@ -60,6 +68,8 @@ pub async fn run() { compare_files_changes(&ctxclone, &mut blrx, &ipeventclone).await; }); + notify(false, &[NotifyState::Ready]).unwrap(); + loop { let mut ret: Vec = Vec::new(); @@ -94,7 +104,7 @@ pub async fn run() { // send ip list to api and ws sockets if let Some(ipevent) = filtered_ipevent { if received_ip.msgtype != "init" { - println!("sending {} to api and ws", ipevent.ipdata.clone().unwrap().ip); + log_with_systemd!(format!("sending {} to api and ws", ipevent.ipdata.clone().unwrap().ip)); let ipe = ipevent!("add","ws",gethostname(true),ipevent.ipdata); send_to_ipbl_api(&server.clone(), &ipe).await; if !send_to_ipbl_websocket(&mut wssocketrr, &ipe).await { @@ -119,7 +129,8 @@ pub async fn run() { // log lines if ret.len() > 0 { - println!("{ret}", ret = ret.join(", ")); + let result = ret.join(", "); + log_with_systemd!(format!("{result}")); } let ctxclone = Arc::clone(&ctxarc); -- 2.39.5 From 3c4d6fb2cfbf6d092fd2e90f9b02ef1dc843701d Mon Sep 17 00:00:00 2001 From: Paul Lecuq Date: Wed, 2 Oct 2024 19:11:34 +0200 Subject: [PATCH 2/2] update errors and variable names --- src/config.rs | 32 ++++++++++++++++---------------- src/ipblc.rs | 4 ++-- src/monitoring.rs | 14 +++++++------- src/utils.rs | 6 +++--- src/webservice.rs | 4 ++-- 5 files changed, 30 insertions(+), 30 deletions(-) diff --git a/src/config.rs b/src/config.rs index 1772b8f..75da5ae 100644 --- a/src/config.rs +++ b/src/config.rs @@ -103,12 +103,12 @@ impl Context { .send() .await; let req = match resp { - Ok(re) => re, - Err(err) => return Err(err), + Ok(o) => o, + Err(e) => return Err(e), }; let data: Discovery = match req.json().await { - Ok(res) => res, - Err(err) => return Err(err), + Ok(o) => o, + Err(e) => return Err(e), }; Ok(data) } @@ -127,8 +127,8 @@ impl Context { } break; } - Err(err) => { - println!("error loading config: {err}, retrying in {CONFIG_RETRY_INTERVAL}s"); + Err(e) => { + println!("error loading config: {e}, retrying in {CONFIG_RETRY_INTERVAL}s"); last_in_err = true; sleep_s(CONFIG_RETRY_INTERVAL).await; } @@ -365,12 +365,12 @@ impl Config { .send() .await; let req = match resp { - Ok(re) => re, - Err(err) => return Err(err), + Ok(o) => o, + Err(e) => return Err(e), }; let data: GlobalConfigV2 = match req.json::().await { - Ok(res) => res, - Err(err) => return Err(err), + Ok(o) => o, + Err(e) => return Err(e), }; for d in data.sets { @@ -400,13 +400,13 @@ impl Config { .await; let req = match resp { - Ok(re) => re, - Err(err) => return Err(err), + Ok(o) => o, + Err(e) => return Err(e), }; let data: Vec = match req.json::>().await { - Ok(res) => res, - Err(err) => return Err(err), + Ok(o) => o, + Err(e) => return Err(e), }; Ok(data) @@ -417,8 +417,8 @@ impl Config { for trustnet in &self.trustnets { match trustnet.parse() { Ok(net) => trustnets.push(net), - Err(err) => { - println!("error parsing {trustnet}, error: {err}"); + Err(e) => { + println!("error parsing {trustnet}, error: {e}"); } }; } diff --git a/src/ipblc.rs b/src/ipblc.rs index 2586a54..ceef99b 100644 --- a/src/ipblc.rs +++ b/src/ipblc.rs @@ -189,8 +189,8 @@ async fn handle_fwblock(ctxclone: Arc>, ret: &mut Vec, f // apply firewall blocking match fwblock(&toblock, ret, fwlen) { Ok(_) => {} - Err(err) => { - println!("Err: {err}, unable to push firewall rules, use super user") + Err(e) => { + println!("err: {e}, unable to push firewall rules, use super user") } }; } diff --git a/src/monitoring.rs b/src/monitoring.rs index 27f7364..4ca1634 100644 --- a/src/monitoring.rs +++ b/src/monitoring.rs @@ -11,9 +11,9 @@ pub async fn apiserver(ctxarc: &Arc>) -> io::Result<()> { let ctxarc = ctxarc.clone(); let addr: String = { ctxarc.read().await.cfg.api.parse().unwrap() }; let listener = match TcpListener::bind(addr).await { - Ok(l) => l, - Err(err) => { - println!("Error: {err}"); + Ok(o) => o, + Err(e) => { + println!("error: {e}"); std::process::exit(1); } }; @@ -29,13 +29,13 @@ pub async fn apiserver(ctxarc: &Arc>) -> io::Result<()> { match socket.try_read(&mut buf) { Ok(_) => {} Err(e) => { - println!("{e}"); + println!("error: {e}"); continue; } }; } Err(e) => { - println!("{e}"); + println!("error: {e}"); continue; } } @@ -49,8 +49,8 @@ pub async fn apiserver(ctxarc: &Arc>) -> io::Result<()> { match socket.write_all(res.as_bytes()).await { Ok(_) => {} - Err(err) => { - println!("ee {err}"); + Err(e) => { + println!("error: {e}"); } } } diff --git a/src/utils.rs b/src/utils.rs index 3c4bb40..8066012 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -6,9 +6,9 @@ use tokio::time::{sleep, Duration}; pub fn read_lines(filename: &String, offset: u64) -> Option> { let mut file = match File::open(filename) { - Ok(f) => f, - Err(err) => { - println!("{err}"); + Ok(o) => o, + Err(e) => { + println!("error: {e}"); return None; } }; diff --git a/src/webservice.rs b/src/webservice.rs index d80eee5..9e7334c 100644 --- a/src/webservice.rs +++ b/src/webservice.rs @@ -15,8 +15,8 @@ pub async fn send_to_ipbl_api(server: &str, ip: &IpEvent) { Ok(_) => { break; } - Err(err) => { - println!("{err}"); + Err(e) => { + println!("error: {e}"); sleep_s(1).await; if try_req == MAX_FAILED_API_RATE { break; -- 2.39.5