webhooks
This commit is contained in:
@@ -61,7 +61,7 @@ In charge of:
|
||||
- Scan the network searching connected devices using the scanning methods
|
||||
described
|
||||
- Store the information in the DB
|
||||
- Report the changes detected by e-mail and/or other services (Pushsafer, NTFY, Gotify)
|
||||
- Report the changes detected by e-mail and/or other services (Pushsafer, NTFY, Gotify, Webhooks ([sample JSON](docs/webhook_json_sample.json)))
|
||||
- Optional speedtest for Device "Internet"
|
||||
- DB cleanup tasks via cron
|
||||
- a pialert-cli that helps to configure login and password
|
||||
|
||||
@@ -1219,6 +1219,14 @@ def email_reporting ():
|
||||
print ('\nReporting...')
|
||||
openDB()
|
||||
|
||||
# prepare variables for JSON construction
|
||||
json_internet = []
|
||||
json_new_devices = []
|
||||
json_down_devices = []
|
||||
json_events = []
|
||||
|
||||
|
||||
|
||||
# Disable reporting on events for devices where reporting is disabled based on the MAC address
|
||||
sql.execute ("""UPDATE Events SET eve_PendingAlertEmail = 0
|
||||
WHERE eve_PendingAlertEmail = 1 AND eve_EventType != 'Device Down' AND eve_MAC IN
|
||||
@@ -1279,6 +1287,9 @@ def email_reporting ():
|
||||
|
||||
for eventAlert in sql :
|
||||
mail_section_Internet = True
|
||||
# collect "internet" (IP changes) for the webhook json
|
||||
json_internet = add_json_list (eventAlert, json_internet)
|
||||
|
||||
mail_text_Internet += text_line_template.format (
|
||||
'Event:', eventAlert['eve_EventType'], 'Time:', eventAlert['eve_DateTime'],
|
||||
'IP:', eventAlert['eve_IP'], 'More Info:', eventAlert['eve_AdditionalInfo'])
|
||||
@@ -1291,6 +1302,7 @@ def email_reporting ():
|
||||
format_report_section (mail_section_Internet, 'SECTION_INTERNET',
|
||||
'TABLE_INTERNET', mail_text_Internet, mail_html_Internet)
|
||||
|
||||
|
||||
# Compose New Devices Section
|
||||
mail_section_new_devices = False
|
||||
mail_text_new_devices = ''
|
||||
@@ -1307,6 +1319,9 @@ def email_reporting ():
|
||||
|
||||
for eventAlert in sql :
|
||||
mail_section_new_devices = True
|
||||
# collect "new_devices" for the webhook json
|
||||
json_new_devices = add_json_list (eventAlert, json_new_devices)
|
||||
|
||||
mail_text_new_devices += text_line_template.format (
|
||||
'Name: ', eventAlert['dev_Name'], 'MAC: ', eventAlert['eve_MAC'], 'IP: ', eventAlert['eve_IP'],
|
||||
'Time: ', eventAlert['eve_DateTime'], 'More Info: ', eventAlert['eve_AdditionalInfo'])
|
||||
@@ -1318,6 +1333,7 @@ def email_reporting ():
|
||||
format_report_section (mail_section_new_devices, 'SECTION_NEW_DEVICES',
|
||||
'TABLE_NEW_DEVICES', mail_text_new_devices, mail_html_new_devices)
|
||||
|
||||
|
||||
# Compose Devices Down Section
|
||||
mail_section_devices_down = False
|
||||
mail_text_devices_down = ''
|
||||
@@ -1334,6 +1350,9 @@ def email_reporting ():
|
||||
|
||||
for eventAlert in sql :
|
||||
mail_section_devices_down = True
|
||||
# collect "down_devices" for the webhook json
|
||||
json_down_devices = add_json_list (eventAlert, json_down_devices)
|
||||
|
||||
mail_text_devices_down += text_line_template.format (
|
||||
'Name: ', eventAlert['dev_Name'], 'MAC: ', eventAlert['eve_MAC'],
|
||||
'Time: ', eventAlert['eve_DateTime'],'IP: ', eventAlert['eve_IP'])
|
||||
@@ -1345,6 +1364,7 @@ def email_reporting ():
|
||||
format_report_section (mail_section_devices_down, 'SECTION_DEVICES_DOWN',
|
||||
'TABLE_DEVICES_DOWN', mail_text_devices_down, mail_html_devices_down)
|
||||
|
||||
|
||||
# Compose Events Section
|
||||
mail_section_events = False
|
||||
mail_text_events = ''
|
||||
@@ -1363,6 +1383,9 @@ def email_reporting ():
|
||||
|
||||
for eventAlert in sql :
|
||||
mail_section_events = True
|
||||
# collect "events" for the webhook json
|
||||
json_events = add_json_list (eventAlert, json_events)
|
||||
|
||||
mail_text_events += text_line_template.format (
|
||||
'Name: ', eventAlert['dev_Name'], 'MAC: ', eventAlert['eve_MAC'],
|
||||
'IP: ', eventAlert['eve_IP'],'Time: ', eventAlert['eve_DateTime'],
|
||||
@@ -1376,13 +1399,23 @@ def email_reporting ():
|
||||
format_report_section (mail_section_events, 'SECTION_EVENTS',
|
||||
'TABLE_EVENTS', mail_text_events, mail_html_events)
|
||||
|
||||
|
||||
# create a json for webhook notifications to provide further integration options
|
||||
json_final = []
|
||||
|
||||
json_final = {
|
||||
"internet": json_internet,
|
||||
"new_devices": json_new_devices,
|
||||
"down_devices": json_down_devices,
|
||||
"events": json_events
|
||||
}
|
||||
|
||||
# DEBUG - Write output emails for testing
|
||||
#if True :
|
||||
# write_file (LOG_PATH + '/report_output.txt', mail_text)
|
||||
# write_file (LOG_PATH + '/report_output.html', mail_html)
|
||||
|
||||
|
||||
|
||||
# Send Mail
|
||||
if mail_section_Internet == True or mail_section_new_devices == True \
|
||||
or mail_section_devices_down == True or mail_section_events == True :
|
||||
@@ -1393,7 +1426,7 @@ def email_reporting ():
|
||||
print (' Skip mail...')
|
||||
if REPORT_WEBHOOK :
|
||||
print (' Sending report by webhook...')
|
||||
send_webhook (mail_text)
|
||||
send_webhook (json_final)
|
||||
else :
|
||||
print (' Skip webhook...')
|
||||
if REPORT_NTFY :
|
||||
@@ -1546,7 +1579,7 @@ def SafeParseGlobalBool(boolVariable):
|
||||
return False
|
||||
|
||||
#-------------------------------------------------------------------------------
|
||||
def send_webhook (_Text):
|
||||
def send_webhook (_json):
|
||||
#Define slack-compatible payload
|
||||
_json_payload={
|
||||
"username": "Pi.Alert",
|
||||
@@ -1554,7 +1587,7 @@ def send_webhook (_Text):
|
||||
"attachments": [{
|
||||
"title": "Pi.Alert Notifications",
|
||||
"title_link": REPORT_DASHBOARD_URL,
|
||||
"text": _Text
|
||||
"text": _json
|
||||
}]
|
||||
}
|
||||
|
||||
@@ -1700,6 +1733,18 @@ def print_log (pText):
|
||||
log_timestamp = log_timestamp2
|
||||
|
||||
|
||||
#-------------------------------------------------------------------------------
|
||||
|
||||
def add_json_list (row, list):
|
||||
new_row = []
|
||||
for column in row :
|
||||
new_row.append(column)
|
||||
|
||||
list.append(new_row)
|
||||
|
||||
return list
|
||||
|
||||
|
||||
#===============================================================================
|
||||
# BEGIN
|
||||
#===============================================================================
|
||||
|
||||
96
docs/webhook_json_sample.json
Normal file
96
docs/webhook_json_sample.json
Normal file
@@ -0,0 +1,96 @@
|
||||
[
|
||||
{
|
||||
"headers": {
|
||||
"host": "192.168.1.82:5678",
|
||||
"user-agent": "curl/7.74.0",
|
||||
"accept": "*/*",
|
||||
"content-type": "application/json",
|
||||
"content-length": "872"
|
||||
},
|
||||
"params": {},
|
||||
"query": {},
|
||||
"body": {
|
||||
"username": "Pi.Alert",
|
||||
"text": "There are new notifications",
|
||||
"attachments": [
|
||||
{
|
||||
"title": "Pi.Alert Notifications",
|
||||
"title_link": "",
|
||||
"text": {
|
||||
"internet": [],
|
||||
"new_devices": [],
|
||||
"down_devices": [],
|
||||
"events": [
|
||||
[
|
||||
"aa:77:aa:77:aa:77",
|
||||
"192.168.1.151",
|
||||
"2022-08-12 21:48:00",
|
||||
"Connected",
|
||||
"",
|
||||
1,
|
||||
null,
|
||||
"aa:77:aa:77:aa:77",
|
||||
"ESP32 - display",
|
||||
"House",
|
||||
"",
|
||||
"Espressif Inc.",
|
||||
0,
|
||||
"",
|
||||
"",
|
||||
"2022-07-21 20:35:00",
|
||||
"2022-08-12 21:48:00",
|
||||
"192.168.1.151",
|
||||
0,
|
||||
1,
|
||||
1,
|
||||
1,
|
||||
0,
|
||||
0,
|
||||
"2022-08-12 21:42:47.937413",
|
||||
1,
|
||||
0,
|
||||
"",
|
||||
0,
|
||||
"aa:77:aa:77:aa:77",
|
||||
""
|
||||
],
|
||||
[
|
||||
"aa:77:aa:77:aa:77",
|
||||
"192.168.1.149",
|
||||
"2022-08-12 21:48:00",
|
||||
"Connected",
|
||||
"",
|
||||
1,
|
||||
null,
|
||||
"aa:77:aa:77:aa:77",
|
||||
"ESP32 - 1",
|
||||
"House",
|
||||
"Singleboard Computer (SBC)",
|
||||
"Espressif Inc.",
|
||||
0,
|
||||
"",
|
||||
"",
|
||||
"2022-07-15 05:30:00",
|
||||
"2022-08-12 21:48:00",
|
||||
"192.168.1.149",
|
||||
0,
|
||||
1,
|
||||
1,
|
||||
1,
|
||||
0,
|
||||
0,
|
||||
"2022-08-12 21:42:47.937413",
|
||||
1,
|
||||
1,
|
||||
"",
|
||||
0,
|
||||
"aa:77:aa:77:aa:77",
|
||||
""
|
||||
]
|
||||
]
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
||||
]
|
||||
@@ -158,7 +158,7 @@
|
||||
dev_DeviceType as type,
|
||||
dev_LastIP as last_ip,
|
||||
(select dev_DeviceType from Devices a where dev_MAC = "'.$node_mac.'") as node_type
|
||||
FROM Devices WHERE dev_Network_Node_MAC_ADDR = "'.$node_mac.'" order by port asc';
|
||||
FROM Devices WHERE dev_Network_Node_MAC_ADDR = "'.$node_mac.'" order by port, name asc';
|
||||
|
||||
global $db;
|
||||
$func_result = $db->query($func_sql);
|
||||
|
||||
@@ -368,7 +368,7 @@ $pia_lang['HelpFAQ_Cat_General_102_text'] = 'Check in the Pi.Alert directory if
|
||||
chmod -R 770 ~/pialert/db
|
||||
</span><br>
|
||||
If the database is still read-only, try reinstalling or restoring a database backup from the maintenance page.';
|
||||
$pia_lang['HelpFAQ_Cat_General_102docker_head'] = '(Docker only 🐳) Database issues (AJAX errors, read-only, not found)';
|
||||
$pia_lang['HelpFAQ_Cat_General_102docker_head'] = '(🐳 Docker only) Database issues (AJAX errors, read-only, not found)';
|
||||
$pia_lang['HelpFAQ_Cat_General_102docker_text'] = 'Double-check you\'ve followed the <a href="https://github.com/jokob-sk/Pi.Alert/tree/main/dockerfiles">dockerfile readme (most up-to-date info)</a>. <br/> <br/> <ul data-sourcepos="49:4-52:146" dir="auto">
|
||||
<li data-sourcepos="49:4-49:106">Download the <a href="https://github.com/jokob-sk/Pi.Alert/blob/main/db/pialert.db">original DB from GitHub</a>.</li>
|
||||
<li data-sourcepos="50:4-50:195">Map the <code>pialert.db</code> file (<g-emoji class="g-emoji" alias="warning" fallback-src="https://github.githubassets.com/images/icons/emoji/unicode/26a0.png">⚠</g-emoji> not folder) from above to <code>/home/pi/pialert/db/pialert.db</code> (see <a href="https://github.com/jokob-sk/Pi.Alert/tree/main/dockerfiles#-examples">Examples</a> for details).</li>
|
||||
|
||||
Reference in New Issue
Block a user