mirror of
https://github.com/jokob-sk/NetAlertX.git
synced 2026-03-31 07:12:23 -07:00
Enhance device down event handling for sleeping devices and improve down alerts query
This commit is contained in:
@@ -126,6 +126,8 @@ def get_notifications(db):
|
||||
AND eve_MAC IN (SELECT devMac FROM Devices WHERE devAlertDown = 0)
|
||||
""")
|
||||
|
||||
alert_down_minutes = int(get_setting_value("NTFPRCS_alert_down_time") or 0)
|
||||
|
||||
sections = get_setting_value("NTFPRCS_INCLUDED_SECTIONS") or []
|
||||
mylog("verbose", ["[Notification] Included sections: ", sections])
|
||||
|
||||
@@ -172,7 +174,7 @@ def get_notifications(db):
|
||||
AND eve_EventType = 'New Device' {condition}
|
||||
ORDER BY eve_DateTime
|
||||
""",
|
||||
"down_devices": """
|
||||
"down_devices": f"""
|
||||
SELECT
|
||||
devName,
|
||||
eve_MAC,
|
||||
@@ -183,7 +185,7 @@ def get_notifications(db):
|
||||
FROM Events_Devices AS down_events
|
||||
WHERE eve_PendingAlertEmail = 1
|
||||
AND down_events.eve_EventType = 'Device Down'
|
||||
AND eve_DateTime < datetime('now', '-0 minutes')
|
||||
AND eve_DateTime < datetime('now', '-{alert_down_minutes} minutes')
|
||||
AND NOT EXISTS (
|
||||
SELECT 1
|
||||
FROM Events AS connected_events
|
||||
|
||||
@@ -534,7 +534,11 @@ def print_scan_stats(db):
|
||||
(SELECT COUNT(*) FROM CurrentScan) AS devices_detected,
|
||||
(SELECT COUNT(*) FROM CurrentScan WHERE NOT EXISTS (SELECT 1 FROM Devices WHERE devMac = scanMac)) AS new_devices,
|
||||
(SELECT COUNT(*) FROM DevicesView WHERE devAlertDown != 0 AND devIsSleeping = 0 AND NOT EXISTS (SELECT 1 FROM CurrentScan WHERE devMac = scanMac)) AS down_alerts,
|
||||
(SELECT COUNT(*) FROM DevicesView WHERE devAlertDown != 0 AND devIsSleeping = 0 AND devPresentLastScan = 1 AND NOT EXISTS (SELECT 1 FROM CurrentScan WHERE devMac = scanMac)) AS new_down_alerts,
|
||||
(SELECT COUNT(*) FROM DevicesView
|
||||
WHERE devAlertDown != 0 AND devCanSleep = 0
|
||||
AND devPresentLastScan = 1
|
||||
AND NOT EXISTS (SELECT 1 FROM CurrentScan WHERE devMac = scanMac)
|
||||
) AS new_down_alerts,
|
||||
(SELECT COUNT(*) FROM Devices WHERE devPresentLastScan = 0) AS new_connections,
|
||||
(SELECT COUNT(*) FROM Devices WHERE devPresentLastScan = 1 AND NOT EXISTS (SELECT 1 FROM CurrentScan WHERE devMac = scanMac)) AS disconnections,
|
||||
(SELECT COUNT(*) FROM Devices, CurrentScan
|
||||
|
||||
@@ -169,20 +169,39 @@ def insert_events(db):
|
||||
sql = db.sql # TO-DO
|
||||
startTime = timeNowUTC()
|
||||
|
||||
# Check device down
|
||||
mylog("debug", "[Events] - 1 - Devices down")
|
||||
# Check device down – non-sleeping devices (immediate on first absence)
|
||||
mylog("debug", "[Events] - 1a - Devices down (non-sleeping)")
|
||||
sql.execute(f"""INSERT OR IGNORE INTO Events (eve_MAC, eve_IP, eve_DateTime,
|
||||
eve_EventType, eve_AdditionalInfo,
|
||||
eve_PendingAlertEmail)
|
||||
SELECT devMac, devLastIP, '{startTime}', 'Device Down', '', 1
|
||||
FROM DevicesView
|
||||
WHERE devAlertDown != 0
|
||||
AND devIsSleeping = 0
|
||||
AND devCanSleep = 0
|
||||
AND devPresentLastScan = 1
|
||||
AND NOT EXISTS (SELECT 1 FROM CurrentScan
|
||||
WHERE devMac = scanMac
|
||||
) """)
|
||||
|
||||
# Check device down – sleeping devices whose sleep window has expired
|
||||
mylog("debug", "[Events] - 1b - Devices down (sleep expired)")
|
||||
sql.execute(f"""INSERT OR IGNORE INTO Events (eve_MAC, eve_IP, eve_DateTime,
|
||||
eve_EventType, eve_AdditionalInfo,
|
||||
eve_PendingAlertEmail)
|
||||
SELECT devMac, devLastIP, '{startTime}', 'Device Down', '', 1
|
||||
FROM DevicesView
|
||||
WHERE devAlertDown != 0
|
||||
AND devCanSleep = 1
|
||||
AND devIsSleeping = 0
|
||||
AND devPresentLastScan = 0
|
||||
AND NOT EXISTS (SELECT 1 FROM CurrentScan
|
||||
WHERE devMac = scanMac)
|
||||
AND NOT EXISTS (SELECT 1 FROM Events
|
||||
WHERE eve_MAC = devMac
|
||||
AND eve_EventType = 'Device Down'
|
||||
AND eve_DateTime >= devLastConnection
|
||||
) """)
|
||||
|
||||
# Check new Connections or Down Reconnections
|
||||
mylog("debug", "[Events] - 2 - New Connections")
|
||||
sql.execute(f""" INSERT OR IGNORE INTO Events (eve_MAC, eve_IP, eve_DateTime,
|
||||
|
||||
Reference in New Issue
Block a user