mirror of
https://github.com/jokob-sk/NetAlertX.git
synced 2026-04-09 03:31:51 -07:00
Merge pull request #1176 from ingoratsdorf/plugin_events-fix
clearPluginEvents
This commit is contained in:
@@ -186,9 +186,15 @@ def main ():
|
||||
|
||||
pm.run_plugin_scripts('on_notification')
|
||||
notification.setAllProcessed()
|
||||
|
||||
# clear pending email flag
|
||||
# and the plugin events
|
||||
notification.clearPendingEmailFlag()
|
||||
|
||||
else:
|
||||
# If there are no notifications to process,
|
||||
# we still need to clear all plugin events
|
||||
notification.clearPluginEvents()
|
||||
mylog('verbose', ['[Notification] No changes to report'])
|
||||
|
||||
# Commit SQL
|
||||
|
||||
@@ -1,12 +1,8 @@
|
||||
import datetime
|
||||
import os
|
||||
import _io
|
||||
import json
|
||||
import sys
|
||||
import uuid
|
||||
import socket
|
||||
import subprocess
|
||||
import requests
|
||||
from yattag import indent
|
||||
from json2table import convert
|
||||
|
||||
@@ -16,15 +12,20 @@ sys.path.extend([f"{INSTALL_PATH}/server"])
|
||||
|
||||
# Register NetAlertX modules
|
||||
import conf
|
||||
from const import applicationPath, logPath, apiPath, confFileName, reportTemplatesPath
|
||||
from logger import logResult, mylog
|
||||
from helper import generate_mac_links, removeDuplicateNewLines, timeNowTZ, get_file_content, write_file, get_setting_value, get_timezone_offset
|
||||
from const import applicationPath, logPath, apiPath, reportTemplatesPath
|
||||
from logger import mylog
|
||||
from helper import generate_mac_links, \
|
||||
removeDuplicateNewLines, \
|
||||
timeNowTZ, \
|
||||
write_file, \
|
||||
get_setting_value, \
|
||||
get_timezone_offset
|
||||
from messaging.in_app import write_notification
|
||||
|
||||
|
||||
#-------------------------------------------------------------------------------
|
||||
# -----------------------------------------------------------------------------
|
||||
# Notification object handling
|
||||
#-------------------------------------------------------------------------------
|
||||
# -----------------------------------------------------------------------------
|
||||
class NotificationInstance:
|
||||
def __init__(self, db):
|
||||
self.db = db
|
||||
@@ -52,7 +53,6 @@ class NotificationInstance:
|
||||
|
||||
return JSON, Extra
|
||||
|
||||
|
||||
# Create a new DB entry if new notifications available, otherwise skip
|
||||
def create(self, JSON, Extra=""):
|
||||
|
||||
@@ -78,8 +78,6 @@ class NotificationInstance:
|
||||
self.Extra = Extra
|
||||
|
||||
if self.HasNotifications:
|
||||
|
||||
|
||||
# if not notiStruc.json['data'] and not notiStruc.text and not notiStruc.html:
|
||||
# mylog('debug', '[Notification] notiStruc is empty')
|
||||
# else:
|
||||
@@ -89,7 +87,6 @@ class NotificationInstance:
|
||||
HTML = ""
|
||||
template_file_path = reportTemplatesPath + 'report_template.html'
|
||||
|
||||
|
||||
# Open text Template
|
||||
mylog('verbose', ['[Notification] Open text Template'])
|
||||
template_file = open(reportTemplatesPath + 'report_template.txt', 'r')
|
||||
@@ -121,12 +118,28 @@ class NotificationInstance:
|
||||
mail_html = mail_html.replace('<SERVER_NAME>', socket.gethostname())
|
||||
|
||||
# Report "VERSION" in Header & footer
|
||||
VERSIONFILE = subprocess.check_output(['php', applicationPath + '/front/php/templates/version.php']).decode('utf-8')
|
||||
try:
|
||||
VERSIONFILE = subprocess.check_output(
|
||||
['php', applicationPath + '/front/php/templates/version.php'],
|
||||
timeout=5
|
||||
).decode('utf-8')
|
||||
except Exception as e:
|
||||
mylog('debug', [f'[Notification] Unable to read version.php: {e}'])
|
||||
VERSIONFILE = 'unknown'
|
||||
|
||||
mail_text = mail_text.replace('<BUILD_VERSION>', VERSIONFILE)
|
||||
mail_html = mail_html.replace('<BUILD_VERSION>', VERSIONFILE)
|
||||
|
||||
# Report "BUILD" in Header & footer
|
||||
BUILDFILE = subprocess.check_output(['php', applicationPath + '/front/php/templates/build.php']).decode('utf-8')
|
||||
try:
|
||||
BUILDFILE = subprocess.check_output(
|
||||
['php', applicationPath + '/front/php/templates/build.php'],
|
||||
timeout=5
|
||||
).decode('utf-8')
|
||||
except Exception as e:
|
||||
mylog('debug', [f'[Notification] Unable to read build.php: {e}'])
|
||||
BUILDFILE = 'unknown'
|
||||
|
||||
mail_text = mail_text.replace('<BUILD_DATE>', BUILDFILE)
|
||||
mail_html = mail_html.replace('<BUILD_DATE>', BUILDFILE)
|
||||
|
||||
@@ -256,50 +269,56 @@ class NotificationInstance:
|
||||
|
||||
self.save()
|
||||
|
||||
|
||||
|
||||
|
||||
# Clear the Pending Email flag from all events and devices
|
||||
def clearPendingEmailFlag(self):
|
||||
|
||||
# Clean Pending Alert Events
|
||||
self.db.sql.execute ("""UPDATE Devices SET devLastNotification = ?
|
||||
self.db.sql.execute("""
|
||||
UPDATE Devices SET devLastNotification = ?
|
||||
WHERE devMac IN (
|
||||
SELECT eve_MAC FROM Events
|
||||
WHERE eve_PendingAlertEmail = 1
|
||||
)
|
||||
""", (timeNowTZ(),))
|
||||
|
||||
self.db.sql.execute ("""UPDATE Events SET eve_PendingAlertEmail = 0
|
||||
self.db.sql.execute("""
|
||||
UPDATE Events SET eve_PendingAlertEmail = 0
|
||||
WHERE eve_PendingAlertEmail = 1
|
||||
AND eve_EventType !='Device Down' """)
|
||||
|
||||
# Clear down events flag after the reporting window passed
|
||||
self.db.sql.execute (f"""UPDATE Events SET eve_PendingAlertEmail = 0
|
||||
minutes = int(get_setting_value('NTFPRCS_alert_down_time') or 0)
|
||||
tz_offset = get_timezone_offset()
|
||||
self.db.sql.execute("""
|
||||
UPDATE Events
|
||||
SET eve_PendingAlertEmail = 0
|
||||
WHERE eve_PendingAlertEmail = 1
|
||||
AND eve_EventType =='Device Down'
|
||||
AND eve_DateTime < datetime('now', '-{get_setting_value('NTFPRCS_alert_down_time')} minutes', '{get_timezone_offset()}')
|
||||
""")
|
||||
AND eve_EventType = 'Device Down'
|
||||
AND eve_DateTime < datetime('now', ?, ?)
|
||||
""", (f"-{minutes} minutes", tz_offset))
|
||||
|
||||
mylog('minimal', ['[Notification] Notifications changes: ',
|
||||
self.db.sql.rowcount])
|
||||
|
||||
# clear plugin events
|
||||
self.clearPluginEvents()
|
||||
|
||||
|
||||
def clearPluginEvents(self):
|
||||
# clear plugin events table
|
||||
self.db.sql.execute("DELETE FROM Plugins_Events")
|
||||
|
||||
# DEBUG - print number of rows updated
|
||||
mylog('minimal', ['[Notification] Notifications changes: ', self.db.sql.rowcount])
|
||||
|
||||
self.save()
|
||||
|
||||
def save(self):
|
||||
# Commit changes
|
||||
self.db.commitDB()
|
||||
|
||||
#-------------------------------------------------------------------------------
|
||||
|
||||
# -----------------------------------------------------------------------------
|
||||
# Reporting
|
||||
#-------------------------------------------------------------------------------
|
||||
# -----------------------------------------------------------------------------
|
||||
|
||||
|
||||
|
||||
#-------------------------------------------------------------------------------
|
||||
# ------------------------------------------------------------------------------
|
||||
def construct_notifications(JSON, section):
|
||||
|
||||
jsn = JSON[section]
|
||||
@@ -314,14 +333,20 @@ def construct_notifications(JSON, section):
|
||||
html = ''
|
||||
text = ''
|
||||
|
||||
table_attributes = {"style" : "border-collapse: collapse; font-size: 12px; color:#70707", "width" : "100%", "cellspacing" : 0, "cellpadding" : "3px", "bordercolor" : "#C0C0C0", "border":"1"}
|
||||
table_attributes = {
|
||||
"style": "border-collapse: collapse; font-size: 12px; color:#70707",
|
||||
"width": "100%",
|
||||
"cellspacing": 0,
|
||||
"cellpadding": "3px",
|
||||
"bordercolor": "#C0C0C0",
|
||||
"border": "1"
|
||||
}
|
||||
headerProps = "width='120px' style='color:white; font-size: 16px;' bgcolor='#64a0d6' "
|
||||
thProps = "width='120px' style='color:#F0F0F0' bgcolor='#64a0d6' "
|
||||
|
||||
build_direction = "TOP_TO_BOTTOM"
|
||||
text_line = '{}\t{}\n'
|
||||
|
||||
|
||||
if len(jsn) > 0:
|
||||
text = tableTitle + "\n---------\n"
|
||||
|
||||
@@ -329,7 +354,13 @@ def construct_notifications(JSON, section):
|
||||
html = convert({"data": jsn}, build_direction=build_direction, table_attributes=table_attributes)
|
||||
|
||||
# Cleanup the generated HTML table notification
|
||||
html = format_table(html, "data", headerProps, tableTitle).replace('<ul>','<ul style="list-style:none;padding-left:0">').replace("<td>null</td>", "<td></td>")
|
||||
html = format_table(html,
|
||||
"data",
|
||||
headerProps,
|
||||
tableTitle).replace('<ul>',
|
||||
'<ul style="list-style:none;padding-left:0">'
|
||||
).replace("<td>null</td>",
|
||||
"<td></td>")
|
||||
|
||||
# prepare text-only message
|
||||
for device in jsn:
|
||||
@@ -346,7 +377,8 @@ def construct_notifications(JSON, section):
|
||||
|
||||
return html, text
|
||||
|
||||
#-------------------------------------------------------------------------------
|
||||
|
||||
# -----------------------------------------------------------------------------
|
||||
def send_api(json_final, mail_text, mail_html):
|
||||
mylog('verbose', ['[Send API] Updating notification_* files in ', apiPath])
|
||||
|
||||
@@ -355,8 +387,7 @@ def send_api(json_final, mail_text, mail_html):
|
||||
write_file(apiPath + 'notification_json_final.json', json.dumps(json_final))
|
||||
|
||||
|
||||
|
||||
#-------------------------------------------------------------------------------
|
||||
# -----------------------------------------------------------------------------
|
||||
# Replacing table headers
|
||||
def format_table(html, thValue, props, newThValue=''):
|
||||
|
||||
@@ -364,6 +395,3 @@ def format_table (html, thValue, props, newThValue = ''):
|
||||
newThValue = thValue
|
||||
|
||||
return html.replace("<th>"+thValue+"</th>", "<th "+props+" >"+newThValue+"</th>")
|
||||
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user