💠down_reconnected support v0.5 #611

This commit is contained in:
jokob-sk
2024-05-25 23:28:48 +10:00
parent 2f4423481d
commit 3102b8a76e
11 changed files with 84 additions and 17 deletions

View File

@@ -137,7 +137,7 @@ def importConfigs (db, all_plugins):
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.REPORT_DASHBOARD_URL = ccd('REPORT_DASHBOARD_URL', 'http://netalertx/' , c_d, 'NetAlertX URL', 'text', '', 'General')
conf.UI_LANG = ccd('UI_LANG', 'English' , c_d, 'Language Interface', 'text.select', "['English', 'French', 'German', 'Norwegian', 'Russian', 'Spanish' ]", 'General')
conf.UI_LANG = ccd('UI_LANG', 'English' , c_d, 'Language Interface', 'text.select', "['English', 'French', 'German', 'Norwegian', 'Russian', 'Spanish', 'Italian', 'Portuguese (Brazil)', 'Polish', 'Chinese (zh_cn)' ]", 'General')
conf.UI_PRESENCE = ccd('UI_PRESENCE', ['online', 'offline', 'archived'] , c_d, 'Include in presence', 'text.multiselect', "['online', 'offline', 'archived']", 'General')
conf.UI_DEV_SECTIONS = ccd('UI_DEV_SECTIONS', [] , c_d, 'Show sections', 'text.multiselect', "['Tile Cards', 'Device Presence']", 'General')
conf.UI_MY_DEVICES = ccd('UI_MY_DEVICES', ['online', 'offline', 'archived', 'new', 'down'] , c_d, 'Include in My Devices', 'text.multiselect', "['online', 'offline', 'archived', 'new', 'down']", 'General')

View File

@@ -51,7 +51,7 @@ class Notification_obj:
write_file (logPath + '/report_output.json', json.dumps(JSON))
# Check if nothing to report, end
if JSON["new_devices"] == [] and JSON["down_devices"] == [] and JSON["events"] == [] and JSON["plugins"] == []:
if JSON["new_devices"] == [] and JSON["down_devices"] == [] and JSON["events"] == [] and JSON["plugins"] == [] and JSON["down_reconnected"] == []:
self.HasNotifications = False
else:
self.HasNotifications = True
@@ -119,19 +119,35 @@ class Notification_obj:
mail_html = mail_html.replace ('<BUILD_DATE>', BUILDFILE)
# Start generating the TEXT & HTML notification messages
# new_devices
# ---
html, text = construct_notifications(self.JSON, "new_devices")
mail_text = mail_text.replace ('<NEW_DEVICES_TABLE>', text + '\n')
mail_html = mail_html.replace ('<NEW_DEVICES_TABLE>', html)
mylog('verbose', ['[Notification] New Devices sections done.'])
# down_devices
# ---
html, text = construct_notifications(self.JSON, "down_devices")
mail_text = mail_text.replace ('<DOWN_DEVICES_TABLE>', text + '\n')
mail_html = mail_html.replace ('<DOWN_DEVICES_TABLE>', html)
mylog('verbose', ['[Notification] Down Devices sections done.'])
# down_reconnected
# ---
html, text = construct_notifications(self.JSON, "down_reconnected")
mail_text = mail_text.replace ('<DOWN_RECONNECTED_TABLE>', text + '\n')
mail_html = mail_html.replace ('<DOWN_RECONNECTED_TABLE>', html)
mylog('verbose', ['[Notification] Reconnected Down Devices sections done.'])
# events
# ---
html, text = construct_notifications(self.JSON, "events")
@@ -140,6 +156,8 @@ class Notification_obj:
mylog('verbose', ['[Notification] Events sections done.'])
# plugins
# ---
html, text = construct_notifications(self.JSON, "plugins")
mail_text = mail_text.replace ('<PLUGINS_TABLE>', text + '\n')
@@ -237,6 +255,21 @@ class Notification_obj:
AND eve_DateTime < datetime('now', '-{get_setting_value('NTFPRCS_alert_down_time')} minutes', '{get_timezone_offset()}')
""")
# Clear the pending email flag for reconnected devices
self.db.sql.execute(f"""UPDATE Events_Devices
SET eve_PendingAlertEmail = 0
WHERE eve_MAC IN (
SELECT down_events.eve_MAC
FROM Events_Devices AS down_events
INNER JOIN Events AS connected_events
ON connected_events.eve_MAC = down_events.eve_MAC
WHERE down_events.eve_EventType = 'Device Down'
AND connected_events.eve_EventType = 'Connected'
AND connected_events.eve_DateTime > down_events.eve_DateTime
)
AND eve_EventType = 'Device Down'
""")
# clear plugin events
self.db.sql.execute ("DELETE FROM Plugins_Events")

View File

@@ -37,6 +37,8 @@ def get_notifications (db):
json_new_devices_meta = {}
json_down_devices = []
json_down_devices_meta = {}
json_down_reconnected = []
json_down_reconnected_meta = {}
json_events = []
json_events_meta = {}
json_plugins = []
@@ -72,7 +74,7 @@ def get_notifications (db):
json_obj = db.get_table_as_json(sqlQuery)
json_new_devices_meta = {
"title": "New devices",
"title": "🆕New devices",
"columnNames": json_obj.columnNames
}
@@ -100,11 +102,36 @@ def get_notifications (db):
# Get the events as JSON
json_obj = db.get_table_as_json(sqlQuery)
json_down_devices_meta = {
"title": "Down devices",
json_down_devices_meta = {
"title": "Down devices",
"columnNames": json_obj.columnNames
}
json_down_devices = json_obj.json["data"]
if 'down_reconnected' in sections:
# Compose Reconnected Down Section
# - select only Devices, that were previously down and now are Connected
sqlQuery = f"""
SELECT down_events.dev_Name, down_events.eve_MAC, down_events.dev_Vendor, down_events.eve_IP,
down_events.eve_DateTime AS DownTime, connected_events.eve_DateTime AS ConnectedTime
FROM Events_Devices AS down_events
INNER JOIN Events AS connected_events
ON connected_events.eve_MAC = down_events.eve_MAC
WHERE down_events.eve_EventType = 'Device Down'
AND connected_events.eve_EventType = 'Connected'
AND connected_events.eve_DateTime > down_events.eve_DateTime
AND down_events.eve_PendingAlertEmail = 1
ORDER BY down_events.eve_DateTime;
"""
# Get the events as JSON
json_obj = db.get_table_as_json(sqlQuery)
json_down_reconnected_meta = {
"title": "🔁 Reconnected down devices",
"columnNames": json_obj.columnNames
}
json_down_reconnected = json_obj.json["data"]
if 'events' in sections:
# Compose Events Section
@@ -121,7 +148,7 @@ def get_notifications (db):
json_obj = db.get_table_as_json(sqlQuery)
json_events_meta = {
"title": "Events",
"title": "Events",
"columnNames": json_obj.columnNames
}
json_events = json_obj.json["data"]
@@ -134,7 +161,7 @@ def get_notifications (db):
json_obj = db.get_table_as_json(sqlQuery)
json_plugins_meta = {
"title": "Plugins",
"title": "🔌 Plugins",
"columnNames": json_obj.columnNames
}
json_plugins = json_obj.json["data"]
@@ -145,6 +172,8 @@ def get_notifications (db):
"new_devices_meta": json_new_devices_meta,
"down_devices": json_down_devices,
"down_devices_meta": json_down_devices_meta,
"down_reconnected": json_down_reconnected,
"down_reconnected_meta": json_down_reconnected_meta,
"events": json_events,
"events_meta": json_events_meta,
"plugins": json_plugins,