From 331599435637dd33e00c27b6bb559cc0856d6084 Mon Sep 17 00:00:00 2001 From: Jokob-sk Date: Sat, 22 Jul 2023 07:07:21 +1000 Subject: [PATCH] #253 --- front/php/templates/language/en_us.php | 2 +- front/plugins/README.md | 0 pialert/database.py | 25 +++++++++++++++---------- pialert/initialise.py | 2 +- 4 files changed, 17 insertions(+), 12 deletions(-) mode change 100644 => 100755 front/plugins/README.md diff --git a/front/php/templates/language/en_us.php b/front/php/templates/language/en_us.php index 4d9424c8..1038c793 100755 --- a/front/php/templates/language/en_us.php +++ b/front/php/templates/language/en_us.php @@ -536,7 +536,7 @@ The arp-scan time itself depends on the number of IP addresses to check so set t 'ENABLE_PLUGINS_name' => 'Enable Plugins', 'ENABLE_PLUGINS_description' => 'Enables the plugins functionality. Loading plugins requires more hardware resources so you might want to disable them on low-powered system.', 'PLUGINS_KEEP_HIST_name' => 'Plugins History', -'PLUGINS_KEEP_HIST_description' => 'How many days of Plugins History scan entries should be kept (globally, not device specific!).', +'PLUGINS_KEEP_HIST_description' => 'How many entries of Plugins History scan results should be kept (globally, not device specific!).', 'PIALERT_WEB_PROTECTION_name' => 'Enable login', 'PIALERT_WEB_PROTECTION_description' => 'When enabled a login dialog is displayed. Read below carefully if you get locked out of your instance.', 'PIALERT_WEB_PASSWORD_name' => 'Login password', diff --git a/front/plugins/README.md b/front/plugins/README.md old mode 100644 new mode 100755 diff --git a/pialert/database.py b/pialert/database.py index d1146cf1..69d2e744 100755 --- a/pialert/database.py +++ b/pialert/database.py @@ -91,22 +91,30 @@ class DB(): order by Scan_Date desc limit 150)""") mylog('verbose', ['[DB Cleanup] Optimize Database']) # Cleanup Events - mylog('verbose', [f'[DB Cleanup] Events: Delete all older than {str(DAYS_TO_KEEP_EVENTS)} days']) + mylog('verbose', [f'[DB Cleanup] Events: Delete all older than {str(DAYS_TO_KEEP_EVENTS)} days (DAYS_TO_KEEP_EVENTS setting)']) self.sql.execute (f"""DELETE FROM Events WHERE eve_DateTime <= date('now', '-{str(DAYS_TO_KEEP_EVENTS)} day')""") + # Cleanup Plugin Events History - mylog('verbose', ['[DB Cleanup] Plugin Events History: Delete all older than '+str(DAYS_TO_KEEP_EVENTS)+' days']) + mylog('verbose', ['[DB Cleanup] Plugin Events History: Delete all older than '+str(DAYS_TO_KEEP_EVENTS)+' days (DAYS_TO_KEEP_EVENTS setting)']) self.sql.execute (f"""DELETE FROM Plugins_History WHERE DateTimeChanged <= date('now', '{str(DAYS_TO_KEEP_EVENTS)} day')""") + + # Trim Plugins_History entries to less than PLUGINS_KEEP_HIST setting + mylog('verbose', [f'[DB Cleanup] Plugins_History: Trim Plugins_History entries to less than {str(PLUGINS_KEEP_HIST)} (PLUGINS_KEEP_HIST setting)']) + self.sql.execute (f"""DELETE from Plugins_History where "Index" not in ( + SELECT "Index" from Plugins_History + order by "Index" desc limit {str(PLUGINS_KEEP_HIST)})""") + # Cleanup Pholus_Scan if PHOLUS_DAYS_DATA != 0: - mylog('verbose', ['[DB Cleanup] Pholus_Scan: Delete all older than ' + str(PHOLUS_DAYS_DATA) + ' days']) + mylog('verbose', ['[DB Cleanup] Pholus_Scan: Delete all older than ' + str(PHOLUS_DAYS_DATA) + ' days (PHOLUS_DAYS_DATA setting)']) # todo: improvement possibility: keep at least N per mac self.sql.execute (f"""DELETE FROM Pholus_Scan WHERE Time <= date('now', '-{str(PHOLUS_DAYS_DATA)} day')""") # Cleanup New Devices if HRS_TO_KEEP_NEWDEV != 0: - mylog('verbose', [f'[DB Cleanup] Devices: Delete all New Devices older than {str(HRS_TO_KEEP_NEWDEV)} hours']) + mylog('verbose', [f'[DB Cleanup] Devices: Delete all New Devices older than {str(HRS_TO_KEEP_NEWDEV)} hours (HRS_TO_KEEP_NEWDEV setting)']) self.sql.execute (f"""DELETE FROM Devices WHERE dev_NewDevice = 1 AND dev_FirstConnection < date('now', '+{str(HRS_TO_KEEP_NEWDEV)} hour')""") @@ -120,7 +128,7 @@ class DB(): AND Pholus_Scan.Record_Type = p2.Record_Type );""") # De-Dupe (de-duplicate - remove duplicate entries) from the Nmap_Scan table - mylog('verbose', [' Nmap_Scan: Delete all duplicates']) + mylog('verbose', ['[DB Cleanup] Nmap_Scan: Delete all duplicates']) self.sql.execute ("""DELETE FROM Nmap_Scan WHERE rowid > ( SELECT MIN(rowid) FROM Nmap_Scan p2 @@ -130,13 +138,10 @@ class DB(): AND Nmap_Scan.Service = p2.Service );""") - # Delete all Plugins_History entries older than PLUGINS_KEEP_HIST setting - mylog('verbose', [f' Plugins_History: Delete all Plugins_History entries older than {str(PLUGINS_KEEP_HIST)} (PLUGINS_KEEP_HIST setting) days']) - self.sql.execute (f"""DELETE FROM Plugins_History - WHERE DateTimeChanged <= date('now', '-{str(PLUGINS_KEEP_HIST)} day')""") + # Shrink DB - mylog('verbose', [' Shrink Database']) + mylog('verbose', ['[DB Cleanup] Shrink Database']) self.sql.execute ("VACUUM;") self.commitDB() diff --git a/pialert/initialise.py b/pialert/initialise.py index 405137e1..9dcef252 100755 --- a/pialert/initialise.py +++ b/pialert/initialise.py @@ -72,7 +72,7 @@ def importConfigs (db): conf.LOG_LEVEL = ccd('LOG_LEVEL', 'verbose' , c_d, 'Log verboseness', 'text.select', "['none', 'minimal', 'verbose', 'debug']", 'General') conf.TIMEZONE = ccd('TIMEZONE', 'Europe/Berlin' , c_d, 'Time zone', 'text', '', 'General') conf.ENABLE_PLUGINS = ccd('ENABLE_PLUGINS', True , c_d, 'Enable plugins', 'boolean', '', 'General') - conf.PLUGINS_KEEP_HIST = ccd('PLUGINS_KEEP_HIST', 7 , c_d, 'Keep history days', 'integer', '', 'General') + conf.PLUGINS_KEEP_HIST = ccd('PLUGINS_KEEP_HIST', 10000 , c_d, 'Keep history entries', 'integer', '', 'General') conf.PIALERT_WEB_PROTECTION = ccd('PIALERT_WEB_PROTECTION', False , c_d, 'Enable logon', 'boolean', '', 'General') conf.PIALERT_WEB_PASSWORD = ccd('PIALERT_WEB_PASSWORD', '8d969eef6ecad3c29a3a629280e686cf0c3f5d5a86aff3ca12020c923adc6c92' , c_d, 'Logon password', 'readonly', '', 'General') conf.INCLUDED_SECTIONS = ccd('INCLUDED_SECTIONS', ['internet', 'new_devices', 'down_devices', 'events', 'ports'] , c_d, 'Notify on', 'text.multiselect', "['internet', 'new_devices', 'down_devices', 'events', 'ports', 'plugins']", 'General')