Skip to content

Commit

Permalink
Only show connection errors in Pi-hole diagnosis system if more than …
Browse files Browse the repository at this point in the history
…10 messages are generated within a minute

Signed-off-by: DL6ER <[email protected]>
  • Loading branch information
DL6ER committed Feb 19, 2025
1 parent f68a6bb commit 5112315
Showing 1 changed file with 25 additions and 11 deletions.
36 changes: 25 additions & 11 deletions src/database/message-table.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@
// GIT_HASH, FTL_ARCH
#include "version.h"

// Connection error threshold and interval
#define CONN_ERR_THRESHOLD 10
#define CONN_ERR_INTERVAL 60

// Number of arguments in a variadic macro
// Credit: https://stackoverflow.com/a/35693080/2087442
#define PP_NARG(...) \
Expand Down Expand Up @@ -1409,7 +1413,6 @@ void logg_hostname_warning(const char *ip, const char *name, const unsigned int

// Log to database
add_message(HOSTNAME_MESSAGE, ip, name, (const int)pos);

}

void logg_fatal_dnsmasq_message(const char *message)
Expand All @@ -1423,7 +1426,6 @@ void logg_fatal_dnsmasq_message(const char *message)

// Log to database
add_message_no_args(DNSMASQ_CONFIG_MESSAGE, message);

}

void logg_rate_limit_message(const char *clientIP, const unsigned int rate_limit_count)
Expand All @@ -1439,7 +1441,6 @@ void logg_rate_limit_message(const char *clientIP, const unsigned int rate_limit

// Log to database
add_message(RATE_LIMIT_MESSAGE, clientIP, config.dns.rateLimit.count.v.ui, config.dns.rateLimit.interval.v.ui, turnaround);

}

void logg_warn_dnsmasq_message(char *message)
Expand All @@ -1453,7 +1454,6 @@ void logg_warn_dnsmasq_message(char *message)

// Log to database
add_message_no_args(DNSMASQ_WARN_MESSAGE, message);

}

void log_resource_shortage(const double load, const int nprocs, const int shmem, const int disk, const char *path, const char *msg)
Expand Down Expand Up @@ -1536,7 +1536,6 @@ void logg_inaccessible_adlist(const int dbindex, const char *address)

// Log to database
add_message(INACCESSIBLE_ADLIST_MESSAGE, address, dbindex);

}

void log_certificate_domain_mismatch(const char *certfile, const char *domain)
Expand All @@ -1550,7 +1549,6 @@ void log_certificate_domain_mismatch(const char *certfile, const char *domain)

// Log to database
add_message(CERTIFICATE_DOMAIN_MISMATCH_MESSAGE, certfile, domain);

}

void log_connection_error(const char *server, const char *reason, const char *error)
Expand All @@ -1562,9 +1560,28 @@ void log_connection_error(const char *server, const char *reason, const char *er
// Log to FTL.log
log_warn("%s", buf);

// Log to database
add_message(CONNECTION_ERROR_MESSAGE, server, reason, error);
// Only log to the database if more than 30 warnings are seen per hour
static unsigned int connection_errors_sum = 0;
if(connection_errors_sum < CONN_ERR_THRESHOLD)
{
// Check for roll-over of the hour and reset the counter
static time_t last_connection_error = 0;
if(last_connection_error == 0)

This comment has been minimized.

Copy link
@rdwebdesign

rdwebdesign Feb 19, 2025

Member

I think last_connection_error == 0 will always be true here.

The variable is set on the line above.

This comment has been minimized.

Copy link
@DL6ER

DL6ER Feb 20, 2025

Author Member

The lifetime of a variable is the period over which it exists. If last_connection_error were defined without the keyword static, the lifetime would be from the entry to the return from log_connection_error(); so it would be re-initialized to 0 on every call.

However, the keyword static acts to extend the lifetime of a variable to the entire lifetime of the program itself; e.g. initialization occurs once and once only and then the variable retains its value - whatever it has come to be - over all future calls to this function.

last_connection_error = time(NULL);
else if(time(NULL) - last_connection_error > CONN_ERR_INTERVAL)
{
connection_errors_sum = 0;
last_connection_error = time(NULL);
}

// Increment the counter
connection_errors_sum++;
}
else
{
// Log to database
add_message(CONNECTION_ERROR_MESSAGE, server, reason, error);
}
}

void log_ntp_message(const bool error, const bool server, const char *message)
Expand All @@ -1584,7 +1601,6 @@ void log_ntp_message(const bool error, const bool server, const char *message)

// Log to database
add_message(NTP_MESSAGE, message, level, who);

}

void log_verify_message(const char *expected, const char *actual)
Expand All @@ -1598,7 +1614,6 @@ void log_verify_message(const char *expected, const char *actual)

// Log to database
add_message(VERIFY_MESSAGE, buf, expected, actual, git_hash(), ftl_arch());

}

void log_gravity_restored(const char *status)
Expand All @@ -1612,5 +1627,4 @@ void log_gravity_restored(const char *status)

// Log to database
add_message_no_args(GRAVITY_RESTORED_MESSAGE, status);

}

0 comments on commit 5112315

Please sign in to comment.