From d530576e9b2aad93ca8a5a45df1aee4a04ad3aa7 Mon Sep 17 00:00:00 2001 From: Jokob-sk Date: Thu, 4 Jan 2024 15:28:43 +1100 Subject: [PATCH] NTFPRCS work + docs --- back/pialert.conf | 1 - front/php/templates/language/de_de.json | 4 +- front/php/templates/language/en_us.json | 4 +- front/php/templates/language/es_es.json | 4 +- front/plugins/README.md | 57 ++++++++-------- .../plugins/notification_processing/README.md | 2 +- .../notification_processing/config.json | 35 ++++++++++ .../plugins/notification_processing/script.py | 66 ------------------- pialert/conf.py | 1 - pialert/initialise.py | 6 +- pialert/reporting.py | 27 ++++---- 11 files changed, 87 insertions(+), 120 deletions(-) delete mode 100755 front/plugins/notification_processing/script.py diff --git a/back/pialert.conf b/back/pialert.conf index 166ee8c7..abf33dbe 100755 --- a/back/pialert.conf +++ b/back/pialert.conf @@ -21,7 +21,6 @@ SCAN_SUBNETS=['192.168.1.0/24 --interface=eth1'] TIMEZONE='Europe/Berlin' PIALERT_WEB_PROTECTION=False PIALERT_WEB_PASSWORD='8d969eef6ecad3c29a3a629280e686cf0c3f5d5a86aff3ca12020c923adc6c92' -INCLUDED_SECTIONS=['internet','new_devices','down_devices','events'] DAYS_TO_KEEP_EVENTS=90 # Used for generating links in emails. Make sure not to add a trailing slash! REPORT_DASHBOARD_URL='http://pi.alert' diff --git a/front/php/templates/language/de_de.json b/front/php/templates/language/de_de.json index 4f23ed65..9d637680 100755 --- a/front/php/templates/language/de_de.json +++ b/front/php/templates/language/de_de.json @@ -558,9 +558,7 @@ "PIALERT_WEB_PROTECTION_name": "Login aktivieren", "PIALERT_WEB_PROTECTION_description": "Ein Loginfenster wird angezeigt wenn aktiviert. Untere Beschreibung genau durchlesen falls Sie sich aus Ihrer Instanz aussperren.", "PIALERT_WEB_PASSWORD_name": "Login-Passwort", - "PIALERT_WEB_PASSWORD_description": "Das Standardpasswort ist 123456. Um das Passwort zu ändern, entweder /home/pi/pialert/back/pialert-cli im Container starten oder SETPWD_RUN Set password plugin nutzen.", - "INCLUDED_SECTIONS_name": "Benachrichtigungen", - "INCLUDED_SECTIONS_description": "Spezifiziert, bei welchen Events Benachrichtigungen versendet werden. Entfernen Sie die Eventtypen, bei welchen Sie nicht benachrichtigt werden wollen. Diese Einstellung überschreibt gerätespezifische Einstellungen im UI. (STRG + klicken zum aus-/abwählen).", + "PIALERT_WEB_PASSWORD_description": "Das Standardpasswort ist 123456. Um das Passwort zu ändern, entweder /home/pi/pialert/back/pialert-cli im Container starten oder SETPWD_RUN Set password plugin nutzen.", "DAYS_TO_KEEP_EVENTS_name": "Lösche Events älter als", "DAYS_TO_KEEP_EVENTS_description": "Dies ist eine Wartungseinstellung. Spezifiziert wie viele Tage Events gespeichert bleiben. Alle älteren Events werden periodisch gelöscht. Wird auch auf die Plugins History angewendet.", "HRS_TO_KEEP_NEWDEV_name": "Neue Geräte speichern für", diff --git a/front/php/templates/language/en_us.json b/front/php/templates/language/en_us.json index bfd5855f..4a0a8e52 100755 --- a/front/php/templates/language/en_us.json +++ b/front/php/templates/language/en_us.json @@ -504,9 +504,7 @@ "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", - "PIALERT_WEB_PASSWORD_description" : "The default password is 123456. To change the password run /home/pi/pialert/back/pialert-cli in the container or use the SETPWD_RUN Set password plugin.", - "INCLUDED_SECTIONS_name" : "Notify on", - "INCLUDED_SECTIONS_description" : "Specifies which events trigger notifications. Remove the event type(s) you do not want to get notified on. This setting overrides device-specific settings in the UI. (CTRL + Click to select/deselect).", + "PIALERT_WEB_PASSWORD_description" : "The default password is 123456. To change the password run /home/pi/pialert/back/pialert-cli in the container or use the SETPWD_RUN Set password plugin.", "DAYS_TO_KEEP_EVENTS_name" : "Delete events older than", "DAYS_TO_KEEP_EVENTS_description" : "This is a maintenance setting. This specifies the number of days worth of event entries that will be kept. All older events will be deleted periodically. Also applies on Plugin Events History.", "HRS_TO_KEEP_NEWDEV_name" : "Keep new devices for", diff --git a/front/php/templates/language/es_es.json b/front/php/templates/language/es_es.json index 0d298b11..6950d57a 100755 --- a/front/php/templates/language/es_es.json +++ b/front/php/templates/language/es_es.json @@ -470,9 +470,7 @@ "PIALERT_WEB_PROTECTION_name" : "Habilitar inicio de sesión", "PIALERT_WEB_PROTECTION_description" : "Cuando está habilitado, se muestra un cuadro de diálogo de inicio de sesión. Lea detenidamente a continuación si se le bloquea el acceso a su instancia.", "PIALERT_WEB_PASSWORD_name" : "Contraseña de inicio de sesión", - "PIALERT_WEB_PASSWORD_description" : "La contraseña predeterminada es 123456. Para cambiar la contraseña, ejecute /home/pi/pialert/back/pialert-cli en el contenedor", - "INCLUDED_SECTIONS_name" : "Notificar en", - "INCLUDED_SECTIONS_description" : "Especifica que eventos envían notificaciones. Elimina los tipos de eventos de los que no quieras recibir notificaciones. Este ajuste sobreescribe los ajustes específicos de los dispositivos en la interfaz. (CTRL + Clic para seleccionar / deseleccionar).", + "PIALERT_WEB_PASSWORD_description" : "La contraseña predeterminada es 123456. Para cambiar la contraseña, ejecute /home/pi/pialert/back/pialert-cli en el contenedor", "DAYS_TO_KEEP_EVENTS_name" : "Eliminar eventos anteriores a", "DAYS_TO_KEEP_EVENTS_description" : "Esta es una configuración de mantenimiento. Esto especifica el número de días de entradas de eventos que se guardarán. Todos los eventos anteriores se eliminarán periódicamente.", "HRS_TO_KEEP_NEWDEV_name": "Guardar nuevos dispositivos para", diff --git a/front/plugins/README.md b/front/plugins/README.md index 0d9d38ec..cd65b9de 100755 --- a/front/plugins/README.md +++ b/front/plugins/README.md @@ -14,34 +14,35 @@ ### 🔌 Plugins & 📚 Docs -| Required | CurrentScan | Unique Prefix | Data source | Type | Link + Docs | -|----------|-------------|---------------|--------------------|----------------|------------------------------------------------------------------| -| | | APPRISE | Script | 💬 publisher | 📚[_publisher_apprise](/front/plugins/_publisher_apprise/) | -| | Yes | ARPSCAN | Script | 🔍dev scanner | 📚[arp_scan](/front/plugins/arp_scan/) | -| | | CSVBCKP | Script | ⚙ system | 📚[csv_backup](/front/plugins/csv_backup/) | -| Yes* | | DBCLNP | Script | ⚙ system | 📚[db_cleanup](/front/plugins/db_cleanup/) | -| | | DDNS | Script | ⚙ system | 📚[ddns_update](/front/plugins/ddns_update/) | -| | Yes | DHCPLSS | Script | 🔍dev scanner | 📚[dhcp_leases](/front/plugins/dhcp_leases/) | -| | | DHCPSRVS | Script | ♻ other | 📚[dhcp_servers](/front/plugins/dhcp_servers/) | -| | Yes | INTRNT | Script | 🔍dev scanner | 📚[internet_ip](/front/plugins/internet_ip/) | -| | | INTRSPD | Script | ♻ other | 📚[internet_speedtest](/front/plugins/internet_speedtest/) | -| | | MAINT | Script | ⚙ system | 📚[maintenance](/front/plugins/maintenance/) | -| | | MQTT | Script | 💬 publisher | 📚[_publisher_mqtt](/front/plugins/_publisher_mqtt/) | -| Yes | | NEWDEV | Template | ⚙ system | 📚[newdev_template](/front/plugins/newdev_template/) | -| | | NMAP | Script | ♻ other | 📚[nmap_scan](/front/plugins/nmap_scan/) | -| | | NTFY | Script | 💬 publisher | 📚[_publisher_ntfy](/front/plugins/_publisher_ntfy/) | -| | | PHOLUS | Script | ♻ other | 📚[pholus_scan](/front/plugins/pholus_scan/) | -| | Yes | PIHOLE | External SQLite DB | 🔍dev scanner | 📚[pihole_scan](/front/plugins/pihole_scan/) | -| | | PUSHSAFER | Script | 💬 publisher | 📚[_publisher_pushsafer](/front/plugins/_publisher_pushsafer/) | -| | | SETPWD | Script | ⚙ system | 📚[set_password](/front/plugins/set_password/) | -| | | SMTP | Script | 💬 publisher | 📚[_publisher_email](/front/plugins/_publisher_email/) | -| | Yes | SNMPDSC | Script | 🔍dev scanner | 📚[snmp_discovery](/front/plugins/snmp_discovery/) | -| | Yes** | UNDIS | Script | ♻ other | 📚[undiscoverables](/front/plugins/undiscoverables/) | -| | Yes | UNFIMP | Script | 🔍dev scanner | 📚[unifi_import](/front/plugins/unifi_import/) | -| | | VNDRPDT | Script | ⚙ system | 📚[vendor_update](/front/plugins/vendor_update/) | -| | | WEBHOOK | Script | 💬 publisher | 📚[_publisher_webhook](/front/plugins/_publisher_webhook/) | -| | | WEBMON | Script | ♻ other | 📚[website_monitor](/front/plugins/website_monitor/) | -| N/A | | N/A | SQL query | | N/A, but the External SQLite DB plugins work similar | +| Required | CurrentScan | Unique Prefix | Data source | Type | Link + Docs | +|----------|-------------|---------------|--------------------|----------------|---------------------------------------------------------------------| +| | | APPRISE | Script | 💬 publisher | 📚[_publisher_apprise](/front/plugins/_publisher_apprise/) | +| | Yes | ARPSCAN | Script | 🔍dev scanner | 📚[arp_scan](/front/plugins/arp_scan/) | +| | | CSVBCKP | Script | ⚙ system | 📚[csv_backup](/front/plugins/csv_backup/) | +| Yes* | | DBCLNP | Script | ⚙ system | 📚[db_cleanup](/front/plugins/db_cleanup/) | +| | | DDNS | Script | ⚙ system | 📚[ddns_update](/front/plugins/ddns_update/) | +| | Yes | DHCPLSS | Script | 🔍dev scanner | 📚[dhcp_leases](/front/plugins/dhcp_leases/) | +| | | DHCPSRVS | Script | ♻ other | 📚[dhcp_servers](/front/plugins/dhcp_servers/) | +| | Yes | INTRNT | Script | 🔍dev scanner | 📚[internet_ip](/front/plugins/internet_ip/) | +| | | INTRSPD | Script | ♻ other | 📚[internet_speedtest](/front/plugins/internet_speedtest/) | +| | | MAINT | Script | ⚙ system | 📚[maintenance](/front/plugins/maintenance/) | +| | | MQTT | Script | 💬 publisher | 📚[_publisher_mqtt](/front/plugins/_publisher_mqtt/) | +| Yes | | NEWDEV | Template | ⚙ system | 📚[newdev_template](/front/plugins/newdev_template/) | +| | | NMAP | Script | ♻ other | 📚[nmap_scan](/front/plugins/nmap_scan/) | +| | Yes | NTFPRCS | Template | ⚙ system | 📚[notification_processing](/front/plugins/notification_processing/)| +| | | NTFY | Script | 💬 publisher | 📚[_publisher_ntfy](/front/plugins/_publisher_ntfy/) | +| | | PHOLUS | Script | ♻ other | 📚[pholus_scan](/front/plugins/pholus_scan/) | +| | Yes | PIHOLE | External SQLite DB | 🔍dev scanner | 📚[pihole_scan](/front/plugins/pihole_scan/) | +| | | PUSHSAFER | Script | 💬 publisher | 📚[_publisher_pushsafer](/front/plugins/_publisher_pushsafer/) | +| | | SETPWD | Script | ⚙ system | 📚[set_password](/front/plugins/set_password/) | +| | | SMTP | Script | 💬 publisher | 📚[_publisher_email](/front/plugins/_publisher_email/) | +| | Yes | SNMPDSC | Script | 🔍dev scanner | 📚[snmp_discovery](/front/plugins/snmp_discovery/) | +| | Yes** | UNDIS | Script | ♻ other | 📚[undiscoverables](/front/plugins/undiscoverables/) | +| | Yes | UNFIMP | Script | 🔍dev scanner | 📚[unifi_import](/front/plugins/unifi_import/) | +| | | VNDRPDT | Script | ⚙ system | 📚[vendor_update](/front/plugins/vendor_update/) | +| | | WEBHOOK | Script | 💬 publisher | 📚[_publisher_webhook](/front/plugins/_publisher_webhook/) | +| | | WEBMON | Script | ♻ other | 📚[website_monitor](/front/plugins/website_monitor/) | +| N/A | | N/A | SQL query | | N/A, but the External SQLite DB plugins work similarly | > \* The database cleanup plugin (`DBCLNP`) is not _required_ but the app will become unusable after a while if not executed. diff --git a/front/plugins/notification_processing/README.md b/front/plugins/notification_processing/README.md index 03fa70f4..1dbdc6d1 100755 --- a/front/plugins/notification_processing/README.md +++ b/front/plugins/notification_processing/README.md @@ -1,6 +1,6 @@ ## Overview -Plugin to run regular database cleanup tasks. It is strongly recommended to have an hourly or at least daily schedule running. +Plugin supplying settings for Notification Processing. ### Usage diff --git a/front/plugins/notification_processing/config.json b/front/plugins/notification_processing/config.json index 72e13d0b..1468ef99 100755 --- a/front/plugins/notification_processing/config.json +++ b/front/plugins/notification_processing/config.json @@ -28,6 +28,41 @@ ], "settings": [ + { + "function": "INCLUDED_SECTIONS", + "type": "text.multiselect", + "default_value": ["new_devices", "down_devices", "events"], + "options": ["new_devices", "down_devices", "events", "plugins"], + "localized": ["name", "description"], + "name": [ + { + "language_code": "en_us", + "string": "Notify on" + }, + { + "language_code": "de_de", + "string": "Benachrichtigungen" + }, + { + "language_code": "es_es", + "string": "Notificar en" + } + ], + "description": [ + { + "language_code": "en_us", + "string": "Specifies which events trigger notifications. Remove the event type(s) you do not want to get notified on. This setting overrides device-specific settings in the UI. (CTRL + Click to select/deselect)." + }, + { + "language_code": "de_de", + "string": "Spezifiziert, bei welchen Events Benachrichtigungen versendet werden. Entfernen Sie die Eventtypen, bei welchen Sie nicht benachrichtigt werden wollen. Diese Einstellung überschreibt gerätespezifische Einstellungen im UI. (STRG + klicken zum aus-/abwählen)." + }, + { + "language_code": "es_es", + "string": "Especifica que eventos envían notificaciones. Elimina los tipos de eventos de los que no quieras recibir notificaciones. Este ajuste sobreescribe los ajustes específicos de los dispositivos en la interfaz. (CTRL + Clic para seleccionar / deseleccionar)." + } + ] + }, { "function": "alert_down_time", "type": "integer", diff --git a/front/plugins/notification_processing/script.py b/front/plugins/notification_processing/script.py deleted file mode 100755 index 6d987ef2..00000000 --- a/front/plugins/notification_processing/script.py +++ /dev/null @@ -1,66 +0,0 @@ -#!/usr/bin/env python - -import os -import pathlib -import argparse -import sys -import hashlib -import csv -import sqlite3 -from io import StringIO -from datetime import datetime - -sys.path.append("/home/pi/pialert/front/plugins") -sys.path.append('/home/pi/pialert/pialert') - -from plugin_helper import Plugin_Object, Plugin_Objects, decodeBase64 -from logger import mylog, append_line_to_file -from helper import timeNowTZ, get_setting_value -from const import logPath, pialertPath, fullDbPath - - -CUR_PATH = str(pathlib.Path(__file__).parent.resolve()) -LOG_FILE = os.path.join(CUR_PATH, 'script.log') -RESULT_FILE = os.path.join(CUR_PATH, 'last_result.log') - -pluginName = 'NTFPRCS' - -def main(): - - mylog('verbose', [f'[{pluginName}] In script']) - - # TODO - # process_notifications(fullDbPath) - - mylog('verbose', [f'[{pluginName}] Script finished']) - - return 0 - -#=============================================================================== -# Cleanup / upkeep database -#=============================================================================== -def process_notifications (dbPath): - - # Connect to the PiAlert SQLite database - conn = sqlite3.connect(dbPath) - cursor = conn.cursor() - - # Cleanup Events - # mylog('verbose', [f'[DBCLNP] Events: Delete all older than {str(DAYS_TO_KEEP_EVENTS)} days (DAYS_TO_KEEP_EVENTS setting)']) - - # cursor.execute (f"""DELETE FROM Events - # WHERE eve_DateTime <= date('now', '-{str(DAYS_TO_KEEP_EVENTS)} day')""") - - - conn.commit() - - # Close the database connection - conn.close() - - - -#=============================================================================== -# BEGIN -#=============================================================================== -if __name__ == '__main__': - main() \ No newline at end of file diff --git a/pialert/conf.py b/pialert/conf.py index 2e8e95ee..9c693d02 100755 --- a/pialert/conf.py +++ b/pialert/conf.py @@ -37,7 +37,6 @@ UI_LANG = 'English' UI_PRESENCE = ['online', 'offline', 'archived'] PIALERT_WEB_PROTECTION = False PIALERT_WEB_PASSWORD = '8d969eef6ecad3c29a3a629280e686cf0c3f5d5a86aff3ca12020c923adc6c92' -INCLUDED_SECTIONS = ['new_devices', 'down_devices', 'events'] DAYS_TO_KEEP_EVENTS = 90 REPORT_DASHBOARD_URL = 'http://pi.alert/' diff --git a/pialert/initialise.py b/pialert/initialise.py index e59ff4de..20fac082 100755 --- a/pialert/initialise.py +++ b/pialert/initialise.py @@ -103,8 +103,7 @@ def importConfigs (db): conf.TIMEZONE = ccd('TIMEZONE', 'Europe/Berlin' , c_d, 'Time zone', 'text', '', 'General') conf.PLUGINS_KEEP_HIST = ccd('PLUGINS_KEEP_HIST', 250 , 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', ['new_devices', 'down_devices', 'events'] , c_d, 'Notify on', 'text.multiselect', "['new_devices', 'down_devices', 'events', 'plugins']", 'General') + conf.PIALERT_WEB_PASSWORD = ccd('PIALERT_WEB_PASSWORD', '8d969eef6ecad3c29a3a629280e686cf0c3f5d5a86aff3ca12020c923adc6c92' , c_d, 'Logon password', 'readonly', '', 'General') conf.REPORT_DASHBOARD_URL = ccd('REPORT_DASHBOARD_URL', 'http://pi.alert/' , c_d, 'PiAlert URL', 'text', '', 'General') conf.DIG_GET_IP_ARG = ccd('DIG_GET_IP_ARG', '-4 myip.opendns.com @resolver1.opendns.com' , c_d, 'DIG arguments', 'text', '', 'General') conf.UI_LANG = ccd('UI_LANG', 'English' , c_d, 'Language Interface', 'text.select', "['English', 'German', 'Spanish']", 'General') @@ -260,7 +259,8 @@ replacements = { r'REPORT_WEBHOOK=True': 'WEBHOOK_RUN=\'on_notification\'', r'REPORT_PUSHSAFER=True': 'PUSHSAFER_RUN=\'on_notification\'', r'REPORT_MQTT=True': 'MQTT_RUN=\'on_notification\'', - r'PIHOLE_CMD=': 'PIHOLE_CMD_OLD=' + r'PIHOLE_CMD=': 'PIHOLE_CMD_OLD=', + r'\bINCLUDED_SECTIONS\b': 'NTFPRCS_INCLUDED_SECTIONS' } def renameSettings(config_file): diff --git a/pialert/reporting.py b/pialert/reporting.py index 0498d635..b749c3f2 100755 --- a/pialert/reporting.py +++ b/pialert/reporting.py @@ -55,18 +55,21 @@ def get_notifications (db): ( SELECT dev_MAC FROM Devices WHERE dev_AlertDeviceDown = 0 )""") + + sections = get_setting_value('NTFPRCS_INCLUDED_SECTIONS') - - mylog('verbose', ['[Notification] included sections: ', conf.INCLUDED_SECTIONS ]) + mylog('verbose', ['[Notification] Included sections: ', sections ]) - if 'new_devices' in conf.INCLUDED_SECTIONS: + if 'new_devices' in sections: # Compose New Devices Section sqlQuery = f"""SELECT eve_MAC as MAC, eve_DateTime as Datetime, dev_LastIP as IP, eve_EventType as "Event Type", dev_Name as "Device name", dev_Comments as Comments FROM Events_Devices WHERE eve_PendingAlertEmail = 1 - AND eve_EventType = 'New Device' - {get_setting_value('NTFPRCS_new_dev_condition')} + AND eve_EventType = 'New Device' + {get_setting_value('NTFPRCS_new_dev_condition').replace('{s-quote}',"'")} ORDER BY eve_DateTime""" + mylog('debug', ['[Notification] new_devices SQL query: ', sqlQuery ]) + # Get the events as JSON json_obj = db.get_table_as_json(sqlQuery) @@ -77,7 +80,7 @@ def get_notifications (db): json_new_devices = json_obj.json["data"] - if 'down_devices' in conf.INCLUDED_SECTIONS: + if 'down_devices' in sections: # Compose Devices Down Section # - select only Down Alerts with pending email of devices that didn't reconnect within the specified time window sqlQuery = f""" @@ -105,14 +108,16 @@ def get_notifications (db): } json_down_devices = json_obj.json["data"] - if 'events' in conf.INCLUDED_SECTIONS: + if 'events' in sections: # Compose Events Section sqlQuery = f"""SELECT eve_MAC as MAC, eve_DateTime as Datetime, dev_LastIP as IP, eve_EventType as "Event Type", dev_Name as "Device name", dev_Comments as Comments FROM Events_Devices WHERE eve_PendingAlertEmail = 1 AND eve_EventType IN ('Connected','Disconnected', - 'IP Changed') - {get_setting_value('NTFPRCS_event_condition')} - ORDER BY eve_DateTime""" + 'IP Changed') + {get_setting_value('NTFPRCS_event_condition').replace('{s-quote}',"'")} + ORDER BY eve_DateTime""" + + mylog('debug', ['[Notification] events SQL query: ', sqlQuery ]) # Get the events as JSON json_obj = db.get_table_as_json(sqlQuery) @@ -123,7 +128,7 @@ def get_notifications (db): } json_events = json_obj.json["data"] - if 'plugins' in conf.INCLUDED_SECTIONS: + if 'plugins' in sections: # Compose Plugins Section sqlQuery = """SELECT Plugin, Object_PrimaryId, Object_SecondaryId, DateTimeChanged, Watched_Value1, Watched_Value2, Watched_Value3, Watched_Value4, Status from Plugins_Events"""