From a10c6c119899a8a9b36884e4e93e3f6bd873d793 Mon Sep 17 00:00:00 2001 From: Albin Suresh Date: Fri, 27 May 2022 16:59:53 +0530 Subject: Fix watchdog health check with timestamp validation --- crates/core/tedge_mapper/src/collectd/monitor.rs | 4 +++- crates/core/tedge_mapper/src/core/mapper.rs | 7 +++++-- 2 files changed, 8 insertions(+), 3 deletions(-) (limited to 'crates/core/tedge_mapper/src') diff --git a/crates/core/tedge_mapper/src/collectd/monitor.rs b/crates/core/tedge_mapper/src/collectd/monitor.rs index f927f887..61de30a5 100644 --- a/crates/core/tedge_mapper/src/collectd/monitor.rs +++ b/crates/core/tedge_mapper/src/collectd/monitor.rs @@ -3,6 +3,7 @@ use std::process; use batcher::{BatchConfigBuilder, BatchDriver, BatchDriverInput, BatchDriverOutput, Batcher}; use mqtt_channel::{Connection, Message, QoS, SinkExt, StreamExt, Topic, TopicFilter}; use serde_json::json; +use time::OffsetDateTime; use tracing::{error, info, instrument}; use super::{batcher::MessageBatch, collectd::CollectdMessage, error::DeviceMonitorError}; @@ -109,7 +110,8 @@ impl DeviceMonitor { if health_check_topics.accept(&message) { let health_status = json!({ "status": "up", - "pid": process::id() + "pid": process::id(), + "time": OffsetDateTime::now_utc().unix_timestamp() }) .to_string(); let health_message = Message::new(&health_status_topic, health_status); diff --git a/crates/core/tedge_mapper/src/core/mapper.rs b/crates/core/tedge_mapper/src/core/mapper.rs index 9309a31e..58a212a1 100644 --- a/crates/core/tedge_mapper/src/core/mapper.rs +++ b/crates/core/tedge_mapper/src/core/mapper.rs @@ -7,6 +7,7 @@ use mqtt_channel::{ UnboundedSender, }; use serde_json::json; +use time::OffsetDateTime; use tracing::{error, info, instrument}; const SYNC_WINDOW: Duration = Duration::from_secs(3); @@ -133,7 +134,8 @@ impl Mapper { if self.health_check_topics.accept(&message) { let health_status = json!({ "status": "up", - "pid": process::id() + "pid": process::id(), + "time": OffsetDateTime::now_utc().unix_timestamp() }) .to_string(); let health_message = Message::new(&self.health_status_topic, health_status); @@ -242,7 +244,7 @@ mod tests { let common_health_check_topic = "tedge/health-check"; let health_status = broker .wait_for_response_on_publish( - &common_health_check_topic, + common_health_check_topic, "", &health_topic, Duration::from_secs(1), @@ -252,6 +254,7 @@ mod tests { let health_status: Value = serde_json::from_str(health_status.as_str())?; assert_json_include!(actual: &health_status, expected: json!({"status": "up"})); assert!(health_status["pid"].is_number()); + assert!(health_status["time"].is_number()); Ok(()) } -- cgit v1.2.3