Skip to content

Commit

Permalink
Add options CHECK_LOAD, CHECK_SHMEM, and CHECK_DISK to control checki…
Browse files Browse the repository at this point in the history
…ng levels and disabling warnings.

Signed-off-by: DL6ER <[email protected]>
  • Loading branch information
DL6ER committed Nov 29, 2021
1 parent 4c2c59b commit 6d7bbcb
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 12 deletions.
40 changes: 39 additions & 1 deletion src/config.c
Original file line number Diff line number Diff line change
Expand Up @@ -642,7 +642,7 @@ void read_FTLconf(void)

// BLOCK_ICLOUD_PR
// Should FTL handle the iCloud privacy relay domains specifically and
// always return NXDOMAIN??
// always return NXDOMAIN ?
// defaults to: true
buffer = parse_FTLconf(fp, "BLOCK_ICLOUD_PR");
config.special_domains.icloud_private_relay = read_bool(buffer, true);
Expand All @@ -652,6 +652,44 @@ void read_FTLconf(void)
else
logg(" BLOCK_ICLOUD_PR: Disabled");

// CHECK_LOAD
// Should FTL check the 15 min average of CPU load and complain if the
// load is larger than the number of available CPU cores?
// defaults to: true
buffer = parse_FTLconf(fp, "CHECK_LOAD");
config.check.load = read_bool(buffer, true);

if(config.check.load)
logg(" CHECK_LOAD: Enabled");
else
logg(" CHECK_LOAD: Disabled");

// CHECK_SHMEM
// Limit above which FTL should complain about a shared-memory shortage
// defaults to: 90%
config.check.shmem = 90;
buffer = parse_FTLconf(fp, "CHECK_SHMEM");

if(buffer != NULL &&
sscanf(buffer, "%i", &ivalue) &&
ivalue >= 0 && ivalue <= 100)
config.check.shmem = ivalue;

logg(" CHECK_LOAD: Warning if shared-memory usage exceeds %d%%", config.check.shmem);

// CHECK_disk
// Limit above which FTL should complain about a shared-memory shortage
// defaults to: 90%
config.check.disk = 90;
buffer = parse_FTLconf(fp, "CHECK_DISK");

if(buffer != NULL &&
sscanf(buffer, "%i", &ivalue) &&
ivalue >= 0 && ivalue <= 100)
config.check.disk = ivalue;

logg(" CHECK_LOAD: Warning if shared-memory usage exceeds %d%%", config.check.disk);

// Read DEBUG_... setting from pihole-FTL.conf
read_debuging_settings(fp);

Expand Down
7 changes: 6 additions & 1 deletion src/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,11 @@ typedef struct {
bool mozilla_canary :1;
bool icloud_private_relay :1;
} special_domains;
struct {
bool load :1;
unsigned char shmem;
unsigned char disk;
} check;
enum privacy_level privacylevel;
enum blocking_mode blockingmode;
enum refresh_hostnames refresh_hostnames;
Expand All @@ -78,7 +83,7 @@ typedef struct {
struct in6_addr v6;
} reply_addr;
} ConfigStruct;
ASSERT_SIZEOF(ConfigStruct, 80, 76, 76);
ASSERT_SIZEOF(ConfigStruct, 88, 80, 80);

typedef struct {
const char* conf;
Expand Down
8 changes: 7 additions & 1 deletion src/gc.c
Original file line number Diff line number Diff line change
Expand Up @@ -81,16 +81,22 @@ time_t get_rate_limit_turnaround(const unsigned int rate_limit_count)

static void check_space(const char *file)
{
if(config.check.disk == 0)
return;

int perc = 0;
char buffer[64] = { 0 };
// Warn if space usage at the device holding the corresponding file
// exceeds the configured threshold
if((perc = get_filepath_usage(file, buffer)) > WARN_LIMIT)
if((perc = get_filepath_usage(file, buffer)) > config.check.disk)
log_resource_shortage(-1.0, 0, -1, perc, file, buffer);
}

static void check_load(void)
{
if(!config.check.load)
return;

// Get CPU load averages
double load[3];
if (getloadavg(load, 3) == -1)
Expand Down
10 changes: 3 additions & 7 deletions src/shmem.c
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,6 @@
#define SHARED_DNS_CACHE "FTL-dns-cache"
#define SHARED_PER_CLIENT_REGEX "FTL-per-client-regex"

// Limit from which on we warn users about space running out in SHMEM_PATH
// default: 90%
#define SHMEM_WARN_LIMIT 90

// Allocation step for FTL-strings bucket. This is somewhat special as we use
// this as a general-purpose storage which should always be large enough. If,
// for some reason, more data than this step has to be stored (highly unlikely,
Expand Down Expand Up @@ -599,11 +595,11 @@ static SharedMemory create_shm(const char *name, const size_t size, bool create_
{
char df[64] = { 0 };
const int percentage = get_dev_shm_usage(df);
if(config.debug & DEBUG_SHMEM || percentage > SHMEM_WARN_LIMIT)
if(config.debug & DEBUG_SHMEM || percentage > config.check.shmem)
{
logg("Creating shared memory with name \"%s\" and size %zu (%s)", name, size, df);
}
if(percentage > SHMEM_WARN_LIMIT)
if(config.check.shmem > 0 && percentage > config.check.shmem)
log_resource_shortage(-1.0, 0, percentage, -1, SHMEM_PATH, df);

SharedMemory sharedMemory = {
Expand Down Expand Up @@ -742,7 +738,7 @@ static bool realloc_shm(SharedMemory *sharedMemory, const size_t size1, const si
logg("Remapping \"%s\" from %zu to (%zu * %zu) == %zu",
sharedMemory->name, sharedMemory->size, size1, size2, size);

if(percentage > SHMEM_WARN_LIMIT)
if(config.check.shmem > 0 && percentage > config.check.shmem)
log_resource_shortage(-1.0, 0, percentage, -1, SHMEM_PATH, df);

// Resize shard memory object if requested
Expand Down
4 changes: 2 additions & 2 deletions test/test_suite.bats
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
@test "Running a second instance is detected and prevented" {
run bash -c 'su pihole -s /bin/sh -c "/home/pihole/pihole-FTL -f"'
printf "%s\n" "${lines[@]}"
[[ ${lines[9]} == *"Initialization of shared memory failed." ]]
[[ ${lines[10]} == *"HINT: pihole-FTL is already running!"* ]]
[[ ${lines[10]} == *"Initialization of shared memory failed." ]]
[[ ${lines[11]} == *"HINT: pihole-FTL is already running!"* ]]
}

@test "Starting tests without prior history" {
Expand Down

0 comments on commit 6d7bbcb

Please sign in to comment.