From e7ed9e0896c58350a26f93128df4de35b409d9e7 Mon Sep 17 00:00:00 2001 From: jokob-sk Date: Sat, 1 Nov 2025 17:58:22 +1100 Subject: [PATCH] BE: logging fix and comments why eve_PendingAlertEmail not cleared Signed-off-by: jokob-sk --- server/__main__.py | 28 ++++++++++++++++++-------- server/messaging/reporting.py | 9 +++++++++ server/models/notification_instance.py | 5 ++++- 3 files changed, 33 insertions(+), 9 deletions(-) diff --git a/server/__main__.py b/server/__main__.py index 591a3c4e..9c7fe073 100755 --- a/server/__main__.py +++ b/server/__main__.py @@ -75,7 +75,7 @@ def main (): updateState("Initializing", None, None, None, 0) # Open DB once and keep open - # Opening / closing DB frequently actually casues more issues + # Opening/closing the DB frequently actually causes more issues db = DB() # instance of class DB db.open() sql = db.sql # To-Do replace with the db class @@ -148,12 +148,14 @@ def main (): mylog('debug', [f'[MAIN] processScan: {processScan}']) if processScan == True: - mylog('debug', "[MAIN] start processig scan results") + mylog('debug', "[MAIN] start processing scan results") process_scan(db) updateState("Scan processed", None, None, None, None, False) - # -------- - # Reporting + # ------------------------------------------------------------------------------ + # Reporting + # ------------------------------------------------------------------------------ + # run plugins before notification processing (e.g. Plugins to discover device names) pm.run_plugin_scripts('before_name_updates') @@ -181,19 +183,29 @@ def main (): notification = NotificationInstance(db) notificationObj = notification.create(final_json, "") - # run all enabled publisher gateways + # ------------------------------------------------------------------------------ + # Run all enabled publisher gateways (notification delivery) + # ------------------------------------------------------------------------------ + # Design notes: + # - The eve_PendingAlertEmail flag is only cleared *after* a notification is sent. + # - If no notification is sent (HasNotifications == False), the flag stays set, + # meaning the event may still trigger alerts later depending on user settings + # (e.g. down-event reporting, delay timers, plugin conditions). + # - A pending flag means “still under evaluation,” not “missed.” + # It will clear automatically once its event is included in a sent alert. + # ------------------------------------------------------------------------------ if notificationObj.HasNotifications: pm.run_plugin_scripts('on_notification') notification.setAllProcessed() - # clear pending email flag - # and the plugin events + # Only clear pending email flags and plugins_events once notifications are sent. notification.clearPendingEmailFlag() else: # If there are no notifications to process, - # we still need to clear all plugin events + # we still need to clear all plugin events to prevent database growth if + # no notification gateways are configured notification.clearPluginEvents() mylog('verbose', ['[Notification] No changes to report']) diff --git a/server/messaging/reporting.py b/server/messaging/reporting.py index d22bf6d0..a9343f87 100755 --- a/server/messaging/reporting.py +++ b/server/messaging/reporting.py @@ -223,6 +223,15 @@ def get_notifications (db): #------------------------------------------------------------------------------- def skip_repeated_notifications (db): + """ + Skips sending alerts for devices recently notified. + + Clears `eve_PendingAlertEmail` for events linked to devices whose last + notification time is within their `devSkipRepeated` interval. + + Args: + db: Database object with `.sql.execute()` and `.commitDB()`. + """ # Skip repeated notifications # due strfime : Overflow --> use "strftime / 60" diff --git a/server/models/notification_instance.py b/server/models/notification_instance.py index dabad488..d01cbec9 100755 --- a/server/models/notification_instance.py +++ b/server/models/notification_instance.py @@ -13,7 +13,7 @@ sys.path.extend([f"{INSTALL_PATH}/server"]) # Register NetAlertX modules import conf from const import applicationPath, logPath, apiPath, reportTemplatesPath -from logger import mylog +from logger import mylog, Logger from helper import generate_mac_links, \ removeDuplicateNewLines, \ timeNowTZ, \ @@ -46,6 +46,9 @@ class NotificationInstance: ); """) + # Make sure log level is initialized correctly + Logger(get_setting_value('LOG_LEVEL')) + self.save() # Method to override processing of notifications