Skip to content

Commit 5178438

Browse files
committed
add global option for handling info/warning notifications closes #1834
1 parent dbbbd53 commit 5178438

File tree

4 files changed

+133
-32
lines changed

4 files changed

+133
-32
lines changed

api/tacticalrmm/alerts/models.py

+90-32
Original file line numberDiff line numberDiff line change
@@ -293,7 +293,9 @@ def handle_alert_failure(
293293
from agents.models import Agent, AgentHistory
294294
from autotasks.models import TaskResult
295295
from checks.models import CheckResult
296+
from core.models import CoreSettings
296297

298+
core = CoreSettings.objects.first()
297299
# set variables
298300
dashboard_severities = None
299301
email_severities = None
@@ -443,12 +445,23 @@ def handle_alert_failure(
443445
alert.hidden = False
444446
alert.save(update_fields=["hidden"])
445447

448+
# TODO rework this
449+
if alert.severity == AlertSeverity.INFO and not core.notify_on_info_alerts:
450+
email_alert = False
451+
always_email = False
452+
453+
elif (
454+
alert.severity == AlertSeverity.WARNING
455+
and not core.notify_on_warning_alerts
456+
):
457+
email_alert = False
458+
always_email = False
459+
446460
# send email if enabled
447461
if email_alert or always_email:
448462
# check if alert template is set and specific severities are configured
449-
if (
450-
not alert_template
451-
or alert_template
463+
if not alert_template or (
464+
alert_template
452465
and email_severities
453466
and alert.severity in email_severities
454467
):
@@ -457,14 +470,22 @@ def handle_alert_failure(
457470
alert_interval=alert_interval,
458471
)
459472

473+
# TODO rework this
474+
if alert.severity == AlertSeverity.INFO and not core.notify_on_info_alerts:
475+
text_alert = False
476+
always_text = False
477+
elif (
478+
alert.severity == AlertSeverity.WARNING
479+
and not core.notify_on_warning_alerts
480+
):
481+
text_alert = False
482+
always_text = False
483+
460484
# send text if enabled
461485
if text_alert or always_text:
462486
# check if alert template is set and specific severities are configured
463-
if (
464-
not alert_template
465-
or alert_template
466-
and text_severities
467-
and alert.severity in text_severities
487+
if not alert_template or (
488+
alert_template and text_severities and alert.severity in text_severities
468489
):
469490
text_task.delay(pk=alert.pk, alert_interval=alert_interval)
470491

@@ -513,17 +534,25 @@ def handle_alert_failure(
513534
}
514535

515536
elif alert_template.action_type == AlertTemplateActionType.REST:
516-
output, status = run_url_rest_action(
517-
action_id=alert_template.action_rest.id, instance=alert
518-
)
519-
logger.debug(f"{output=} {status=}")
520-
521-
r = {
522-
"stdout": output,
523-
"stderr": "",
524-
"execution_time": 0,
525-
"retcode": status,
526-
}
537+
if (
538+
alert.severity == AlertSeverity.INFO
539+
and not core.notify_on_info_alerts
540+
or alert.severity == AlertSeverity.WARNING
541+
and not core.notify_on_warning_alerts
542+
):
543+
return
544+
else:
545+
output, status = run_url_rest_action(
546+
action_id=alert_template.action_rest.id, instance=alert
547+
)
548+
logger.debug(f"{output=} {status=}")
549+
550+
r = {
551+
"stdout": output,
552+
"stderr": "",
553+
"execution_time": 0,
554+
"retcode": status,
555+
}
527556
else:
528557
return
529558

@@ -555,6 +584,9 @@ def handle_alert_resolve(
555584
from agents.models import Agent, AgentHistory
556585
from autotasks.models import TaskResult
557586
from checks.models import CheckResult
587+
from core.models import CoreSettings
588+
589+
core = CoreSettings.objects.first()
558590

559591
# set variables
560592
email_on_resolved = False
@@ -633,11 +665,29 @@ def handle_alert_resolve(
633665

634666
# check if a resolved email notification should be send
635667
if email_on_resolved and not alert.resolved_email_sent:
636-
resolved_email_task.delay(pk=alert.pk)
668+
if alert.severity == AlertSeverity.INFO and not core.notify_on_info_alerts:
669+
pass
670+
671+
elif (
672+
alert.severity == AlertSeverity.WARNING
673+
and not core.notify_on_warning_alerts
674+
):
675+
pass
676+
else:
677+
resolved_email_task.delay(pk=alert.pk)
637678

638679
# check if resolved text should be sent
639680
if text_on_resolved and not alert.resolved_sms_sent:
640-
resolved_text_task.delay(pk=alert.pk)
681+
if alert.severity == AlertSeverity.INFO and not core.notify_on_info_alerts:
682+
pass
683+
684+
elif (
685+
alert.severity == AlertSeverity.WARNING
686+
and not core.notify_on_warning_alerts
687+
):
688+
pass
689+
else:
690+
resolved_text_task.delay(pk=alert.pk)
641691

642692
# check if resolved script/webhook should be run
643693
if alert_template and not alert.resolved_action_run:
@@ -685,17 +735,25 @@ def handle_alert_resolve(
685735
}
686736

687737
elif alert_template.action_type == AlertTemplateActionType.REST:
688-
output, status = run_url_rest_action(
689-
action_id=alert_template.resolved_action_rest.id, instance=alert
690-
)
691-
logger.debug(f"{output=} {status=}")
692-
693-
r = {
694-
"stdout": output,
695-
"stderr": "",
696-
"execution_time": 0,
697-
"retcode": status,
698-
}
738+
if (
739+
alert.severity == AlertSeverity.INFO
740+
and not core.notify_on_info_alerts
741+
or alert.severity == AlertSeverity.WARNING
742+
and not core.notify_on_warning_alerts
743+
):
744+
return
745+
else:
746+
output, status = run_url_rest_action(
747+
action_id=alert_template.resolved_action_rest.id, instance=alert
748+
)
749+
logger.debug(f"{output=} {status=}")
750+
751+
r = {
752+
"stdout": output,
753+
"stderr": "",
754+
"execution_time": 0,
755+
"retcode": status,
756+
}
699757
else:
700758
return
701759

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# Generated by Django 4.2.13 on 2024-07-05 19:17
2+
3+
from django.db import migrations, models
4+
5+
6+
class Migration(migrations.Migration):
7+
8+
dependencies = [
9+
("core", "0045_coresettings_enable_server_scripts_and_more"),
10+
]
11+
12+
operations = [
13+
migrations.AddField(
14+
model_name="coresettings",
15+
name="notify_on_info_alerts",
16+
field=models.BooleanField(default=False),
17+
),
18+
migrations.AddField(
19+
model_name="coresettings",
20+
name="notify_on_warning_alerts",
21+
field=models.BooleanField(default=False),
22+
),
23+
]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# Generated by Django 4.2.13 on 2024-07-05 19:30
2+
3+
from django.db import migrations, models
4+
5+
6+
class Migration(migrations.Migration):
7+
8+
dependencies = [
9+
("core", "0046_coresettings_notify_on_info_alerts_and_more"),
10+
]
11+
12+
operations = [
13+
migrations.AlterField(
14+
model_name="coresettings",
15+
name="notify_on_warning_alerts",
16+
field=models.BooleanField(default=True),
17+
),
18+
]

api/tacticalrmm/core/models.py

+2
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,8 @@ class CoreSettings(BaseAuditModel):
108108
)
109109
enable_server_scripts = models.BooleanField(default=True)
110110
enable_server_webterminal = models.BooleanField(default=False)
111+
notify_on_info_alerts = models.BooleanField(default=False)
112+
notify_on_warning_alerts = models.BooleanField(default=True)
111113

112114
def save(self, *args, **kwargs) -> None:
113115
from alerts.tasks import cache_agents_alert_template

0 commit comments

Comments
 (0)