diff --git a/server/messaging/notification_sections.py b/server/messaging/notification_sections.py index f88821b2..031d54b2 100644 --- a/server/messaging/notification_sections.py +++ b/server/messaging/notification_sections.py @@ -83,15 +83,22 @@ SQL_TEMPLATES = { "down_reconnected": """ SELECT devName, - eveMac, + reconnected_devices.eveMac, devVendor, - eveIp, - eveDateTime, - eveEventType, + reconnected_devices.eveIp, + reconnected_devices.eveDateTime, + reconnected_devices.eveEventType, devComments FROM Events_Devices AS reconnected_devices WHERE reconnected_devices.eveEventType = 'Down Reconnected' AND reconnected_devices.evePendingAlertEmail = 1 + AND NOT EXISTS ( + SELECT 1 FROM Events AS newer + WHERE newer.eveMac = reconnected_devices.eveMac + AND newer.eveEventType = 'Down Reconnected' + AND newer.evePendingAlertEmail = 1 + AND newer.eveDateTime > reconnected_devices.eveDateTime + ) ORDER BY reconnected_devices.eveDateTime """, "events": """ @@ -105,7 +112,7 @@ SQL_TEMPLATES = { devComments FROM Events_Devices WHERE evePendingAlertEmail = 1 - AND eveEventType IN ('Connected', 'Down Reconnected', 'Disconnected','IP Changed') {condition} + AND eveEventType IN ({event_types}) {condition} ORDER BY eveDateTime """, "plugins": """ diff --git a/server/messaging/reporting.py b/server/messaging/reporting.py index 45a37453..dfe9f087 100755 --- a/server/messaging/reporting.py +++ b/server/messaging/reporting.py @@ -198,6 +198,14 @@ def get_notifications(db): format_vars = {"condition": safe_condition} if section == "down_devices": format_vars["alert_down_minutes"] = alert_down_minutes + if section == "events": + # 'Down Reconnected' has its own dedicated section; exclude it + # from events when that section is also active to prevent the + # same device appearing twice with different IP sources. + if "down_reconnected" in sections: + format_vars["event_types"] = "'Connected', 'Disconnected','IP Changed'" + else: + format_vars["event_types"] = "'Connected', 'Down Reconnected', 'Disconnected','IP Changed'" sqlQuery = template.format(**format_vars) except Exception as e: @@ -205,6 +213,11 @@ def get_notifications(db): fallback_vars = {"condition": ""} if section == "down_devices": fallback_vars["alert_down_minutes"] = alert_down_minutes + if section == "events": + if "down_reconnected" in sections: + fallback_vars["event_types"] = "'Connected', 'Disconnected','IP Changed'" + else: + fallback_vars["event_types"] = "'Connected', 'Down Reconnected', 'Disconnected','IP Changed'" sqlQuery = template.format(**fallback_vars) parameters = {}