mirror of
https://github.com/jokob-sk/NetAlertX.git
synced 2025-12-07 09:36:05 -08:00
@@ -1,5 +1,5 @@
|
||||
|
||||
#!/usr/bin/env python
|
||||
# !/usr/bin/env python
|
||||
|
||||
import json
|
||||
import subprocess
|
||||
@@ -13,15 +13,15 @@ INSTALL_PATH = os.getenv('NETALERTX_APP', '/app')
|
||||
sys.path.extend([f"{INSTALL_PATH}/front/plugins", f"{INSTALL_PATH}/server"])
|
||||
|
||||
|
||||
import conf
|
||||
from const import logPath, confFileName
|
||||
from plugin_helper import Plugin_Objects, handleEmpty
|
||||
from utils.datetime_utils import timeNowDB
|
||||
from logger import mylog, Logger
|
||||
from helper import get_setting_value, write_file
|
||||
from models.notification_instance import NotificationInstance
|
||||
from database import DB
|
||||
from pytz import timezone
|
||||
import conf # noqa: E402 [flake8 lint suppression]
|
||||
from const import logPath, confFileName # noqa: E402 [flake8 lint suppression]
|
||||
from plugin_helper import Plugin_Objects, handleEmpty # noqa: E402 [flake8 lint suppression]
|
||||
from utils.datetime_utils import timeNowDB # noqa: E402 [flake8 lint suppression]
|
||||
from logger import mylog, Logger # noqa: E402 [flake8 lint suppression]
|
||||
from helper import get_setting_value, write_file # noqa: E402 [flake8 lint suppression]
|
||||
from models.notification_instance import NotificationInstance # noqa: E402 [flake8 lint suppression]
|
||||
from database import DB # noqa: E402 [flake8 lint suppression]
|
||||
from pytz import timezone # noqa: E402 [flake8 lint suppression]
|
||||
|
||||
# Make sure the TIMEZONE for logging is correct
|
||||
conf.tz = timezone(get_setting_value('TIMEZONE'))
|
||||
@@ -35,13 +35,12 @@ LOG_PATH = logPath + '/plugins'
|
||||
RESULT_FILE = os.path.join(LOG_PATH, f'last_result.{pluginName}.log')
|
||||
|
||||
|
||||
|
||||
def main():
|
||||
|
||||
mylog('verbose', [f'[{pluginName}](publisher) In script'])
|
||||
|
||||
|
||||
mylog('verbose', [f'[{pluginName}](publisher) In script'])
|
||||
|
||||
# Check if basic config settings supplied
|
||||
if check_config() == False:
|
||||
if check_config() is False:
|
||||
mylog('none', [f'[{pluginName}] ⚠ ERROR: Publisher notification gateway not set up correctly. Check your {confFileName} {pluginName}_* variables.'])
|
||||
return
|
||||
|
||||
@@ -62,15 +61,19 @@ def main():
|
||||
for notification in new_notifications:
|
||||
|
||||
# Send notification
|
||||
response_stdout, response_stderr = send(notification["Text"], notification["HTML"], notification["JSON"])
|
||||
response_stdout, response_stderr = send(
|
||||
notification["Text"],
|
||||
notification["HTML"],
|
||||
notification["JSON"]
|
||||
)
|
||||
|
||||
# Log result
|
||||
plugin_objects.add_object(
|
||||
primaryId = pluginName,
|
||||
secondaryId = timeNowDB(),
|
||||
secondaryId = timeNowDB(),
|
||||
watched1 = notification["GUID"],
|
||||
watched2 = handleEmpty(response_stdout),
|
||||
watched3 = handleEmpty(response_stderr),
|
||||
watched2 = handleEmpty(response_stdout),
|
||||
watched3 = handleEmpty(response_stderr),
|
||||
watched4 = 'null',
|
||||
extra = 'null',
|
||||
foreignKey = notification["GUID"]
|
||||
@@ -79,16 +82,16 @@ def main():
|
||||
plugin_objects.write_result_file()
|
||||
|
||||
|
||||
#-------------------------------------------------------------------------------
|
||||
# -------------------------------------------------------------------------------
|
||||
def check_config():
|
||||
if get_setting_value('WEBHOOK_URL') == '':
|
||||
return False
|
||||
else:
|
||||
return True
|
||||
|
||||
#-------------------------------------------------------------------------------
|
||||
if get_setting_value('WEBHOOK_URL') == '':
|
||||
return False
|
||||
else:
|
||||
return True
|
||||
|
||||
def send (text_data, html_data, json_data):
|
||||
|
||||
# -------------------------------------------------------------------------------
|
||||
def send(text_data, html_data, json_data):
|
||||
|
||||
response_stderr = ''
|
||||
response_stdout = ''
|
||||
@@ -102,9 +105,9 @@ def send (text_data, html_data, json_data):
|
||||
|
||||
# use data type based on specified payload type
|
||||
if payloadType == 'json':
|
||||
# In this code, the truncate_json function is used to recursively traverse the JSON object
|
||||
# and remove nodes that exceed the size limit. It checks the size of each node's JSON representation
|
||||
# using json.dumps and includes only the nodes that are within the limit.
|
||||
# In this code, the truncate_json function is used to recursively traverse the JSON object
|
||||
# and remove nodes that exceed the size limit. It checks the size of each node's JSON representation
|
||||
# using json.dumps and includes only the nodes that are within the limit.
|
||||
json_str = json.dumps(json_data)
|
||||
|
||||
if len(json_str) <= limit:
|
||||
@@ -127,45 +130,48 @@ def send (text_data, html_data, json_data):
|
||||
return obj
|
||||
|
||||
payloadData = truncate_json(json_data)
|
||||
if payloadType == 'html':
|
||||
if payloadType == 'html':
|
||||
if len(html_data) > limit:
|
||||
payloadData = html_data[:limit] + " <h1>(text was truncated)</h1>"
|
||||
else:
|
||||
payloadData = html_data
|
||||
if payloadType == 'text':
|
||||
if payloadType == 'text':
|
||||
if len(text_data) > limit:
|
||||
payloadData = text_data[:limit] + " (text was truncated)"
|
||||
else:
|
||||
payloadData = text_data
|
||||
|
||||
# Define slack-compatible payload
|
||||
_json_payload = { "text": payloadData } if payloadType == 'text' else {
|
||||
"username": "NetAlertX",
|
||||
"text": "There are new notifications",
|
||||
"attachments": [{
|
||||
"title": "NetAlertX Notifications",
|
||||
"title_link": get_setting_value('REPORT_DASHBOARD_URL'),
|
||||
"text": payloadData
|
||||
}]
|
||||
}
|
||||
if payloadType == 'text':
|
||||
_json_payload = {"text": payloadData}
|
||||
else:
|
||||
_json_payload = {
|
||||
"username": "NetAlertX",
|
||||
"text": "There are new notifications",
|
||||
"attachments": [{
|
||||
"title": "NetAlertX Notifications",
|
||||
"title_link": get_setting_value('REPORT_DASHBOARD_URL'),
|
||||
"text": payloadData
|
||||
}]
|
||||
}
|
||||
|
||||
# DEBUG - Write the json payload into a log file for debugging
|
||||
write_file (logPath + '/webhook_payload.json', json.dumps(_json_payload))
|
||||
write_file(logPath + '/webhook_payload.json', json.dumps(_json_payload))
|
||||
|
||||
# Using the Slack-Compatible Webhook endpoint for Discord so that the same payload can be used for both
|
||||
# Consider: curl has the ability to load in data to POST from a file + piping
|
||||
if(endpointUrl.startswith('https://discord.com/api/webhooks/') and not endpointUrl.endswith("/slack")):
|
||||
if (endpointUrl.startswith('https://discord.com/api/webhooks/') and not endpointUrl.endswith("/slack")):
|
||||
_WEBHOOK_URL = f"{endpointUrl}/slack"
|
||||
curlParams = ["curl","-i","-H", "Content-Type:application/json" ,"-d", json.dumps(_json_payload), _WEBHOOK_URL]
|
||||
curlParams = ["curl", "-i", "-H", "Content-Type:application/json", "-d", json.dumps(_json_payload), _WEBHOOK_URL]
|
||||
else:
|
||||
_WEBHOOK_URL = endpointUrl
|
||||
curlParams = ["curl","-i","-X", requestMethod , "-H", "Content-Type:application/json", "-d", json.dumps(_json_payload), _WEBHOOK_URL]
|
||||
curlParams = ["curl", "-i", "-X", requestMethod , "-H", "Content-Type:application/json", "-d", json.dumps(_json_payload), _WEBHOOK_URL]
|
||||
|
||||
# Add HMAC signature if configured
|
||||
if(secret != ''):
|
||||
if (secret != ''):
|
||||
h = hmac.new(secret.encode("UTF-8"), json.dumps(_json_payload, separators=(',', ':')).encode(), hashlib.sha256).hexdigest()
|
||||
curlParams.insert(4,"-H")
|
||||
curlParams.insert(5,f"X-Webhook-Signature: sha256={h}")
|
||||
curlParams.insert(4, "-H")
|
||||
curlParams.insert(5, f"X-Webhook-Signature: sha256={h}")
|
||||
|
||||
try:
|
||||
# Execute CURL call
|
||||
@@ -173,13 +179,11 @@ def send (text_data, html_data, json_data):
|
||||
result = subprocess.run(curlParams, capture_output=True, text=True)
|
||||
|
||||
response_stderr = result.stderr
|
||||
response_stdout = result.stdout
|
||||
response_stdout = result.stdout
|
||||
|
||||
# Write stdout and stderr into .log files for debugging if needed
|
||||
mylog('debug', [f'[{pluginName}] stdout: ', response_stdout])
|
||||
mylog('debug', [f'[{pluginName}] stderr: ', response_stderr])
|
||||
|
||||
|
||||
mylog('debug', [f'[{pluginName}] stderr: ', response_stderr])
|
||||
|
||||
except subprocess.CalledProcessError as e:
|
||||
# An error occurred, handle it
|
||||
@@ -187,10 +191,9 @@ def send (text_data, html_data, json_data):
|
||||
|
||||
response_stderr = e.output
|
||||
|
||||
return response_stdout, response_stderr
|
||||
|
||||
return response_stdout, response_stderr
|
||||
|
||||
# -------------------------------------------------------
|
||||
# -------------------------------------------------------
|
||||
if __name__ == '__main__':
|
||||
sys.exit(main())
|
||||
|
||||
|
||||
Reference in New Issue
Block a user