Finish implementing notification rules
This commit is contained in:
38
core/lib/notify.py
Normal file
38
core/lib/notify.py
Normal file
@@ -0,0 +1,38 @@
|
||||
import requests
|
||||
|
||||
from core.util import logs
|
||||
|
||||
NTFY_URL = "https://ntfy.sh"
|
||||
|
||||
log = logs.get_logger(__name__)
|
||||
|
||||
|
||||
# Actual function to send a message to a topic
|
||||
def raw_sendmsg(msg, title=None, priority=None, tags=None, url=None, topic=None):
|
||||
if url is None:
|
||||
url = NTFY_URL
|
||||
headers = {"Title": "Fisk"}
|
||||
if title:
|
||||
headers["Title"] = title
|
||||
if priority:
|
||||
headers["Priority"] = priority
|
||||
if tags:
|
||||
headers["Tags"] = tags
|
||||
requests.post(
|
||||
f"{url}/{topic}",
|
||||
data=msg,
|
||||
headers=headers,
|
||||
)
|
||||
|
||||
|
||||
# Sendmsg helper to send a message to a user's notification settings
|
||||
def sendmsg(user, *args, **kwargs):
|
||||
notification_settings = user.get_notification_settings()
|
||||
|
||||
if notification_settings.ntfy_topic is None:
|
||||
# No topic set, so don't send
|
||||
return
|
||||
else:
|
||||
topic = notification_settings.ntfy_topic
|
||||
|
||||
raw_sendmsg(*args, **kwargs, url=notification_settings.ntfy_url, topic=topic)
|
||||
@@ -1,12 +1,76 @@
|
||||
from core.db.storage import db
|
||||
from yaml import load, dump
|
||||
from yaml.scanner import ScannerError
|
||||
from yaml import dump, load
|
||||
from yaml.parser import ParserError
|
||||
from yaml.scanner import ScannerError
|
||||
|
||||
from core.db.storage import db
|
||||
from core.models import NotificationRule
|
||||
|
||||
try:
|
||||
from yaml import CLoader as Loader, CDumper as Dumper
|
||||
from yaml import CDumper as Dumper
|
||||
from yaml import CLoader as Loader
|
||||
except ImportError:
|
||||
from yaml import Loader, Dumper
|
||||
|
||||
from core.lib.notify import sendmsg
|
||||
from core.util import logs
|
||||
|
||||
log = logs.get_logger("rules")
|
||||
|
||||
|
||||
def rule_matched(rule, message, matched_fields):
|
||||
title = f"Rule {rule.name} matched"
|
||||
|
||||
# Dump the message in YAML for readability
|
||||
message = dump(message, Dumper=Dumper, default_flow_style=False)
|
||||
matched_fields = ", ".join(matched_fields)
|
||||
|
||||
notify_message = f"{rule.name} matched on {matched_fields}\n{message}"
|
||||
notify_message = notify_message.encode("utf-8", "replace")
|
||||
sendmsg(rule.user, notify_message, title=title)
|
||||
|
||||
|
||||
def process_rules(data):
|
||||
all_rules = NotificationRule.objects.filter(enabled=True)
|
||||
|
||||
for index, index_messages in data.items():
|
||||
for message in index_messages:
|
||||
for rule in all_rules:
|
||||
parsed_rule = rule.parse()
|
||||
if "index" not in parsed_rule:
|
||||
log.debug("No index specified in rule, skipping")
|
||||
continue
|
||||
if "source" not in parsed_rule:
|
||||
log.debug("No source specified in rule, skipping")
|
||||
continue
|
||||
rule_index = parsed_rule["index"]
|
||||
rule_source = parsed_rule["source"]
|
||||
if not type(rule_index) == list:
|
||||
rule_index = [rule_index]
|
||||
if not type(rule_source) == list:
|
||||
rule_source = [rule_source]
|
||||
if index not in rule_index:
|
||||
log.debug(f"{index} not in {rule_index}")
|
||||
continue
|
||||
if message["src"] not in rule_source:
|
||||
log.debug(f"{message['src']} not in {rule_source}")
|
||||
continue
|
||||
|
||||
rule_field_length = len(parsed_rule.keys())
|
||||
matched_field_number = 0
|
||||
matched_fields = []
|
||||
for field, value in parsed_rule.items():
|
||||
if not type(value) == list:
|
||||
value = [value]
|
||||
if field == "src":
|
||||
continue
|
||||
if field in message and message[field] in value:
|
||||
matched_field_number += 1
|
||||
matched_fields.append(field)
|
||||
print("Matched field", field, message[field], value)
|
||||
if matched_field_number == rule_field_length - 2:
|
||||
rule_matched(rule, message, matched_fields)
|
||||
|
||||
|
||||
class NotificationRuleData(object):
|
||||
def __init__(self, user, data):
|
||||
self.user = user
|
||||
@@ -57,4 +121,4 @@ class NotificationRuleData(object):
|
||||
return dump(self.parsed, Dumper=Dumper)
|
||||
|
||||
def get_data(self):
|
||||
return self.parsed
|
||||
return self.parsed
|
||||
|
||||
Reference in New Issue
Block a user