heartbeat

Simple server monitor system using encrypted messages over udp
git clone https://noulin.net/git/heartbeat.git
Log | Files | Refs | README

commit 044b88b44746a85c0d683e6a5b9513670a571760
parent 8e979e27d2cf1f7e266695bca0ce824183ebb28f
Author: Remy Noulin <loader2x@gmail.com>
Date:   Thu, 13 Jul 2023 13:44:43 +0200

add initialize drift time because some machines don't have the correct time

heartbeat.c | 15 +++++++++++----
1 file changed, 11 insertions(+), 4 deletions(-)

Diffstat:
Mheartbeat.c | 15+++++++++++----
1 file changed, 11 insertions(+), 4 deletions(-)

diff --git a/heartbeat.c b/heartbeat.c @@ -714,6 +714,7 @@ void probe(char *cfgfile, char *secretFile, char *publicFile, char *loggerPublic // open log files and load agent public keys int agentf[lenG(agents)]; u8 agentPublicKeys[lenG(agents)][sizeof(keys.remotePublicKey)]; + i64 drift[lenG(agents)]; // drift time, initialize after first packet from an agent if (logger) { arange(i, agentf) { agentf[i] = open(agentdbs[i], O_APPEND|O_CREAT|O_WRONLY, S_IRUSR|S_IWUSR); @@ -1029,10 +1030,18 @@ void probe(char *cfgfile, char *secretFile, char *publicFile, char *loggerPublic goto handleEventLoopSleep; } + // update state for this agent + cleanFinishSmallDictP(agent) = getG(completeCfg, rtSmallDictt, agents[data.id]); + // packet is valid for 2 seconds u64 now = getCurrentUnixTime(); - if (payload.time < now - 2 or payload.time > now + 2) { - logW("Dropping packet. Wrong timestamp %"PRIu64" now %"PRIu64" diff %"PRIi64, payload.time, now, (i64)now - (i64)payload.time); + if (u$(agent, "time") == 0) { + // initialize drift time + drift[data.id] = (i64)now - (i64)payload.time; + } + + if (payload.time + drift[data.id] < now - 2 or payload.time + drift[data.id] > now + 2) { + logW("Dropping packet. Wrong timestamp %"PRIu64" now %"PRIu64" diff %"PRIi64, payload.time, now, (i64)now - (i64)payload.time + drift[data.id]); goto handleEventLoopSleep; } @@ -1048,8 +1057,6 @@ void probe(char *cfgfile, char *secretFile, char *publicFile, char *loggerPublic cleanAllocateSmallArray(mailMsg); - // update state for this agent - cleanFinishSmallDictP(agent) = getG(completeCfg, rtSmallDictt, agents[data.id]); // count packets in each period, if more than 2 packets arrive during a period, // something is wrong. // Don't measure time between packet because