From 1cefbe0299dc02abf96fc9fc01be593c53b86316 Mon Sep 17 00:00:00 2001 From: Antoine Date: Thu, 20 Oct 2022 09:42:26 +0100 Subject: [PATCH 1/3] Add a text option for webhooks and different repoting levels --- back/pialert.py | 41 ++++++++++++++++++++++++++++++++++++++++- config/pialert.conf | 3 ++- 2 files changed, 42 insertions(+), 2 deletions(-) diff --git a/back/pialert.py b/back/pialert.py index 61c2201d..38295247 100644 --- a/back/pialert.py +++ b/back/pialert.py @@ -1614,14 +1614,22 @@ def send_webhook (_json, _html): except NameError: # variable not defined, use a default webhookRequestMethod = 'GET' + # Event level reporting (int, 0=none, 1=internet only, 2=new devices & 1, 3=down devices & 2, 4= events & 3 i.e. everything) + try: + eventLevel = EVENT_LEVEL + except NameError: # variable not defined, use a default + eventLevel = 4 # report on everythin + # use data type based on specified payload type if webhookPayload == 'json': payloadData = _json if webhookPayload == 'html': payloadData = _html + if webhookPayload == 'text': + payloadData = to_text(_json, eventLevel) #Define slack-compatible payload - _json_payload={ + _json_payload = { "text": payloadData } if webhookPayload == 'text' else { "username": "Pi.Alert", "text": "There are new notifications", "attachments": [{ @@ -1816,7 +1824,38 @@ def logResult (stdout, stderr): if stdout != None: append_file_binary (LOG_PATH + '/stdout.log', stdout) +#------------------------------------------------------------------------------- +def to_text(_json, eventLevel): + payloadData = "" + if len(_json['internet']) > 0 and eventLevel > 0: + payloadData += "INTERNET\n" + for event in _json['internet']: + payloadData += event[3] + ' on ' + event[2] + '. ' + event[4] + '. New address:' + event[1] + '\n' + + if len(_json['new_devices']) > 0 and eventLevel > 1: + payloadData += "NEW DEVICES:\n" + for event in _json['new_devices']: + if event[4] is None: + event[4] = event[11] + payloadData += event[1] + ' - ' + event[4] + '\n' + + if len(_json['down_devices']) > 0 and eventLevel > 2: + write_file (LOG_PATH + '/down_devices_example.log', _json['down_devices']) + payloadData += 'DOWN DEVICES:\n' + for event in _json['down_devices']: + if event[4] is None: + event[4] = event[11] + payloadData += event[1] + ' - ' + event[4] + '\n' + + if len(_json['events']) > 0 and eventLevel > 3: + payloadData += "EVENTS:\n" + for event in _json['events']: + if event[8] != "Internet": + payloadData += event[8] + " on " + event[1] + " " + event[3] + " at " + event[2] + "\n" + + return payloadData + #=============================================================================== # BEGIN #=============================================================================== diff --git a/config/pialert.conf b/config/pialert.conf index 5be6f6d3..3adf33f7 100644 --- a/config/pialert.conf +++ b/config/pialert.conf @@ -36,10 +36,11 @@ REPORT_DASHBOARD_URL = 'http://pi.alert/' REPORT_WEBHOOK = False WEBHOOK_URL = 'http://n8n.local:5555/webhook-test/aaaaaaaa-aaaa-aaaa-aaaaa-aaaaaaaaaaaa' # webhook payload data format for the "body > attachements > text" attribute in https://github.com/jokob-sk/Pi.Alert/blob/main/docs/webhook_json_sample.json -# supported values: 'json' or 'html' +# supported values: 'json' or 'html' or 'text' # e.g.: for discord use 'html' WEBHOOK_PAYLOAD = 'json' WEBHOOK_REQUEST_METHOD = 'GET' # POST, GET... +EVENT_LEVEL = 5 # Apprise settings #----------------------- From 059017e71afa69f6e63008be33ea1d766f562928 Mon Sep 17 00:00:00 2001 From: Antoine Date: Thu, 20 Oct 2022 09:57:06 +0100 Subject: [PATCH 2/3] Add EVENT_LEVEL comments to config file --- back/pialert.py | 4 ++-- config/pialert.conf | 3 ++- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/back/pialert.py b/back/pialert.py index 38295247..8ed834bb 100644 --- a/back/pialert.py +++ b/back/pialert.py @@ -1618,7 +1618,7 @@ def send_webhook (_json, _html): try: eventLevel = EVENT_LEVEL except NameError: # variable not defined, use a default - eventLevel = 4 # report on everythin + eventLevel = 4 # report on everything # use data type based on specified payload type if webhookPayload == 'json': @@ -1855,7 +1855,7 @@ def to_text(_json, eventLevel): payloadData += event[8] + " on " + event[1] + " " + event[3] + " at " + event[2] + "\n" return payloadData - + #=============================================================================== # BEGIN #=============================================================================== diff --git a/config/pialert.conf b/config/pialert.conf index 3adf33f7..13a626e6 100644 --- a/config/pialert.conf +++ b/config/pialert.conf @@ -40,7 +40,8 @@ WEBHOOK_URL = 'http://n8n.local:5555/webhook-test/aaaaaaaa-aaaa-aaaa # e.g.: for discord use 'html' WEBHOOK_PAYLOAD = 'json' WEBHOOK_REQUEST_METHOD = 'GET' # POST, GET... -EVENT_LEVEL = 5 +# set event level to 0 for no message, 1 for Internet changes only, 2 for new devices and 1, 3 for down devices and 2 and 4 for events and 3. i.e. everything +EVENT_LEVEL = 4 # Apprise settings #----------------------- From 6eef2b094d2dbd5a0be60670d95111a7dcab7577 Mon Sep 17 00:00:00 2001 From: Antoine Date: Fri, 21 Oct 2022 15:50:12 +0100 Subject: [PATCH 3/3] Remove EVENT_LEVEL and add INCLUDED_SECTIONS --- back/pialert.py | 33 +++++++++++++++++---------------- config/pialert.conf | 1 + 2 files changed, 18 insertions(+), 16 deletions(-) diff --git a/back/pialert.py b/back/pialert.py index 8ed834bb..df408645 100644 --- a/back/pialert.py +++ b/back/pialert.py @@ -54,6 +54,13 @@ def main (): global log_timestamp global sql_connection global sql + global includedSections + + # Which sections to include. Include everything by default + try: + includedSections = INCLUDED_SECTIONS + except NameError: + includedSections = ['internet', 'new_devices', 'down_devices', 'events'] # Empty stdout and stderr .log files for debugging if needed write_file (LOG_PATH + '/stderr.log', '') @@ -1302,7 +1309,7 @@ def email_reporting (): for eventAlert in sql : - mail_section_Internet = True + mail_section_Internet = 'internet' in includedSections # collect "internet" (IP changes) for the webhook json json_internet = add_json_list (eventAlert, json_internet) @@ -1334,7 +1341,7 @@ def email_reporting (): ORDER BY eve_DateTime""") for eventAlert in sql : - mail_section_new_devices = True + mail_section_new_devices = 'new_devices' in includedSections # collect "new_devices" for the webhook json json_new_devices = add_json_list (eventAlert, json_new_devices) @@ -1365,7 +1372,7 @@ def email_reporting (): ORDER BY eve_DateTime""") for eventAlert in sql : - mail_section_devices_down = True + mail_section_devices_down = 'down_devices' in includedSections # collect "down_devices" for the webhook json json_down_devices = add_json_list (eventAlert, json_down_devices) @@ -1398,7 +1405,7 @@ def email_reporting (): ORDER BY eve_DateTime""") for eventAlert in sql : - mail_section_events = True + mail_section_events = 'events' in includedSections # collect "events" for the webhook json json_events = add_json_list (eventAlert, json_events) @@ -1614,19 +1621,13 @@ def send_webhook (_json, _html): except NameError: # variable not defined, use a default webhookRequestMethod = 'GET' - # Event level reporting (int, 0=none, 1=internet only, 2=new devices & 1, 3=down devices & 2, 4= events & 3 i.e. everything) - try: - eventLevel = EVENT_LEVEL - except NameError: # variable not defined, use a default - eventLevel = 4 # report on everything - # use data type based on specified payload type if webhookPayload == 'json': payloadData = _json if webhookPayload == 'html': payloadData = _html if webhookPayload == 'text': - payloadData = to_text(_json, eventLevel) + payloadData = to_text(_json) #Define slack-compatible payload _json_payload = { "text": payloadData } if webhookPayload == 'text' else { @@ -1826,21 +1827,21 @@ def logResult (stdout, stderr): #------------------------------------------------------------------------------- -def to_text(_json, eventLevel): +def to_text(_json): payloadData = "" - if len(_json['internet']) > 0 and eventLevel > 0: + if len(_json['internet']) > 0 and 'internet' in includedSections: payloadData += "INTERNET\n" for event in _json['internet']: payloadData += event[3] + ' on ' + event[2] + '. ' + event[4] + '. New address:' + event[1] + '\n' - if len(_json['new_devices']) > 0 and eventLevel > 1: + if len(_json['new_devices']) > 0 and 'new_devices' in includedSections: payloadData += "NEW DEVICES:\n" for event in _json['new_devices']: if event[4] is None: event[4] = event[11] payloadData += event[1] + ' - ' + event[4] + '\n' - if len(_json['down_devices']) > 0 and eventLevel > 2: + if len(_json['down_devices']) > 0 and 'down_devices' in includedSections: write_file (LOG_PATH + '/down_devices_example.log', _json['down_devices']) payloadData += 'DOWN DEVICES:\n' for event in _json['down_devices']: @@ -1848,7 +1849,7 @@ def to_text(_json, eventLevel): event[4] = event[11] payloadData += event[1] + ' - ' + event[4] + '\n' - if len(_json['events']) > 0 and eventLevel > 3: + if len(_json['events']) > 0 and 'events' in includedSections: payloadData += "EVENTS:\n" for event in _json['events']: if event[8] != "Internet": diff --git a/config/pialert.conf b/config/pialert.conf index 13a626e6..3d34b1fb 100644 --- a/config/pialert.conf +++ b/config/pialert.conf @@ -15,6 +15,7 @@ PRINT_LOG = False TIMEZONE = 'Europe/Berlin' PIALERT_WEB_PROTECTION = False PIALERT_WEB_PASSWORD = '8d969eef6ecad3c29a3a629280e686cf0c3f5d5a86aff3ca12020c923adc6c92' +INCLUDED_SECTIONS = ['internet', 'new_devices', 'down_devices', 'events'] # EMAIL settings # ----------------------