From f9a400c34c0691af8dc5d690781249c491cd3633 Mon Sep 17 00:00:00 2001 From: Jokob-sk Date: Fri, 15 Sep 2023 20:25:48 +1000 Subject: [PATCH] Optimize init, code clenup --- docs/SMTP.md | 24 +++++----- front/plugins/unifi_import/config.json | 0 front/plugins/unifi_import/script.py | 0 pialert/__main__.py | 2 +- pialert/device.py | 65 -------------------------- pialert/helper.py | 18 +++---- pialert/initialise.py | 20 ++++++-- 7 files changed, 39 insertions(+), 90 deletions(-) mode change 100644 => 100755 front/plugins/unifi_import/config.json mode change 100644 => 100755 front/plugins/unifi_import/script.py diff --git a/docs/SMTP.md b/docs/SMTP.md index 8a0a8187..184273f2 100755 --- a/docs/SMTP.md +++ b/docs/SMTP.md @@ -1,4 +1,4 @@ - +# 📧 SMTP guides ## Use the GMX SMTP server @@ -9,16 +9,16 @@ 5. In PiAlert specify these settings: ```python -REPORT_MAIL=True -SMTP_SERVER='mail.gmx.com' -SMTP_PORT=465 -SMTP_USER='gmx_email@gmx.com' -SMTP_PASS='' -SMTP_SKIP_TLS=True -SMTP_FORCE_SSL=True -SMTP_SKIP_LOGIN=False -REPORT_FROM='gmx_email@gmx.com' -REPORT_TO='some_target_email@gmail.com' + REPORT_MAIL=True + SMTP_SERVER='mail.gmx.com' + SMTP_PORT=465 + SMTP_USER='gmx_email@gmx.com' + SMTP_PASS='' + SMTP_SKIP_TLS=True + SMTP_FORCE_SSL=True + SMTP_SKIP_LOGIN=False + REPORT_FROM='gmx_email@gmx.com' # this has to be the same email as in SMTP_USER + REPORT_TO='some_target_email@gmail.com' ``` @@ -30,10 +30,12 @@ REPORT_TO='some_target_email@gmail.com' 2. Specify the following settings: ```python + REPORT_MAIL=True SMTP_SKIP_TLS=True SMTP_FORCE_SSL=True SMTP_PORT=465 SMTP_SERVER='smtp.gmail.com' SMTP_PASS='16-digit passcode from google' + REPORT_TO='some_target_email@gmail.com' ``` diff --git a/front/plugins/unifi_import/config.json b/front/plugins/unifi_import/config.json old mode 100644 new mode 100755 diff --git a/front/plugins/unifi_import/script.py b/front/plugins/unifi_import/script.py old mode 100644 new mode 100755 diff --git a/pialert/__main__.py b/pialert/__main__.py index 274e766f..319d1542 100755 --- a/pialert/__main__.py +++ b/pialert/__main__.py @@ -107,7 +107,7 @@ def main (): last_cleanup = conf.last_cleanup last_version_check = conf.last_version_check - + # check if new version is available / only check once an hour if conf.last_version_check + datetime.timedelta(hours=1) < conf.loop_start_time : # if newVersionAvailable is already true the function does nothing and returns true again diff --git a/pialert/device.py b/pialert/device.py index 3ad25a2c..ffb6d602 100755 --- a/pialert/device.py +++ b/pialert/device.py @@ -165,72 +165,7 @@ def create_new_devices (db): sql.execute (sqlQuery, (startTime, startTime) ) - # Pi-hole - Insert events for new devices - # NOT STRICYLY NECESARY (Devices can be created through CurrentScan) - # Bugfix #2 - Pi-hole devices w/o IP - # mylog('debug','[New Devices] 3 Pi-hole Events') - # sql.execute ("""INSERT INTO Events (eve_MAC, eve_IP, eve_DateTime, - # eve_EventType, eve_AdditionalInfo, - # eve_PendingAlertEmail) - # SELECT PH_MAC, IFNULL (PH_IP,'-'), ?, 'New Device', - # '(Pi-Hole) ' || PH_Vendor, 1 - # FROM PiHole_Network - # WHERE NOT EXISTS (SELECT 1 FROM Devices - # WHERE dev_MAC = PH_MAC) """, - # (startTime, ) ) - - # # Pi-hole - Create New Devices - # # Bugfix #2 - Pi-hole devices w/o IP - # mylog('debug','[New Devices] 4 Pi-hole Create devices') - - # sqlQuery = f"""INSERT INTO Devices (dev_MAC, dev_name, dev_Vendor, - # dev_LastIP, dev_FirstConnection, dev_LastConnection, - # {newDevColumns}) - # SELECT PH_MAC, PH_Name, PH_Vendor, IFNULL (PH_IP,'-'), - # ?, ?, - # {newDevDefaults} - # FROM PiHole_Network - # WHERE NOT EXISTS (SELECT 1 FROM Devices - # WHERE dev_MAC = PH_MAC) """ - # mylog('debug',f'[New Devices] 4 Create devices SQL: {sqlQuery}') - - # sql.execute (sqlQuery, (startTime, startTime) ) - - # # DHCP Leases - Insert events for new devices - # mylog('debug','[New Devices] 5 DHCP Leases Events') - - # sql.execute (f"""INSERT INTO Events (eve_MAC, eve_IP, eve_DateTime, - # eve_EventType, eve_AdditionalInfo, - # eve_PendingAlertEmail) - # SELECT DHCP_MAC, DHCP_IP, '{startTime}', 'New Device', '(DHCP lease)',1 - # FROM DHCP_Leases - # WHERE NOT EXISTS (SELECT 1 FROM Devices - # WHERE dev_MAC = DHCP_MAC) """) - - # # DHCP Leases - Create New Devices - # mylog('debug','[New Devices] 6 DHCP Leases Create devices') - - # sqlQuery = f"""INSERT INTO Devices (dev_MAC, dev_name, dev_LastIP, - # dev_Vendor, dev_FirstConnection, dev_LastConnection, - # {newDevColumns}) - # SELECT DISTINCT DHCP_MAC, - # (SELECT DHCP_Name FROM DHCP_Leases AS D2 - # WHERE D2.DHCP_MAC = D1.DHCP_MAC - # ORDER BY DHCP_DateTime DESC LIMIT 1), - # (SELECT DHCP_IP FROM DHCP_Leases AS D2 - # WHERE D2.DHCP_MAC = D1.DHCP_MAC - # ORDER BY DHCP_DateTime DESC LIMIT 1), - # '(unknown)', ?, ?, - # {newDevDefaults} - # FROM DHCP_Leases AS D1 - # WHERE NOT EXISTS (SELECT 1 FROM Devices - # WHERE dev_MAC = DHCP_MAC) """ - - # mylog('debug',f'[New Devices] 6 Create devices SQL: {sqlQuery}') - - # sql.execute (sqlQuery, (startTime, startTime) ) - mylog('debug','[New Devices] New Devices end') db.commitDB() diff --git a/pialert/helper.py b/pialert/helper.py index f1a41de5..881a5979 100755 --- a/pialert/helper.py +++ b/pialert/helper.py @@ -159,11 +159,18 @@ def if_byte_then_to_str(input): return input #------------------------------------------------------------------------------- -def collect_lang_strings(db, json, pref): +def collect_lang_strings(db, json, pref, stringSqlParams): for prop in json["localized"]: for language_string in json[prop]: - import_language_string(db, language_string["language_code"], pref + "_" + prop, language_string["string"]) + # db.sql.execute ("""INSERT INTO Plugins_Language_Strings ("Language_Code", "String_Key", "String_Value", "Extra") VALUES (?, ?, ?, ?)""", + + stringSqlParams.append((str(language_string["language_code"]), str(pref + "_" + prop), str(language_string["string"]), "")) + + # db.commitDB() + # sqlParams = import_language_string(db, language_string["language_code"], pref + "_" + prop, language_string["string"]) + + return stringSqlParams #------------------------------------------------------------------------------- @@ -179,13 +186,6 @@ def row_to_json(names, row): return rowEntry -#------------------------------------------------------------------------------- -def import_language_string(db, code, key, value, extra = ""): - - db.sql.execute ("""INSERT INTO Plugins_Language_Strings ("Language_Code", "String_Key", "String_Value", "Extra") VALUES (?, ?, ?, ?)""", (str(code), str(key), str(value), str(extra))) - - db.commitDB() - #------------------------------------------------------------------------------- diff --git a/pialert/initialise.py b/pialert/initialise.py index 4a14b64e..244a2020 100755 --- a/pialert/initialise.py +++ b/pialert/initialise.py @@ -9,7 +9,7 @@ import json import conf from const import fullConfPath -from helper import collect_lang_strings, updateSubnets, initOrSetParam, isJsonObject +from helper import collect_lang_strings, updateSubnets, initOrSetParam, isJsonObject, updateState from logger import mylog from api import update_api from scheduler import schedule_class @@ -72,6 +72,9 @@ def importConfigs (db): if (fileModifiedTime == conf.lastImportedConfFile) : mylog('debug', ['[Import Config] skipping config file import']) return + + # Header + updateState(db,"Import config") mylog('debug', ['[Import Config] importing config file']) conf.mySettings = [] # reset settings @@ -197,9 +200,11 @@ def importConfigs (db): print_plugin_info(plugin, ['display_name','description']) # if plugin["enabled"] == 'true': + + stringSqlParams = [] # collect plugin level language strings - collect_lang_strings(db, plugin, pref) + stringSqlParams = collect_lang_strings(db, plugin, pref, stringSqlParams) for set in plugin["settings"]: setFunction = set["function"] @@ -230,12 +235,19 @@ def importConfigs (db): # Collect settings related language strings # Creates an entry with key, for example ARPSCAN_CMD_name - collect_lang_strings(db, set, pref + "_" + set["function"]) + stringSqlParams = collect_lang_strings(db, set, pref + "_" + set["function"], stringSqlParams) # Collect column related language strings for clmn in plugin.get('database_column_definitions', []): # Creates an entry with key, for example ARPSCAN_Object_PrimaryID_name - collect_lang_strings(db, clmn, pref + "_" + clmn.get("column", "")) + stringSqlParams = collect_lang_strings(db, clmn, pref + "_" + clmn.get("column", ""), stringSqlParams) + + # bulk-import language strings + sql.executemany ("""INSERT INTO Plugins_Language_Strings ("Language_Code", "String_Key", "String_Value", "Extra") VALUES (?, ?, ?, ?)""", stringSqlParams ) + + db.commitDB() + +