diff --git a/api/tacticalrmm/alerts/models.py b/api/tacticalrmm/alerts/models.py index d89be0bd..050be16e 100644 --- a/api/tacticalrmm/alerts/models.py +++ b/api/tacticalrmm/alerts/models.py @@ -293,7 +293,9 @@ class Alert(models.Model): from agents.models import Agent, AgentHistory from autotasks.models import TaskResult from checks.models import CheckResult + from core.models import CoreSettings + core = CoreSettings.objects.first() # set variables dashboard_severities = None email_severities = None @@ -443,12 +445,23 @@ class Alert(models.Model): alert.hidden = False alert.save(update_fields=["hidden"]) + # TODO rework this + if alert.severity == AlertSeverity.INFO and not core.notify_on_info_alerts: + email_alert = False + always_email = False + + elif ( + alert.severity == AlertSeverity.WARNING + and not core.notify_on_warning_alerts + ): + email_alert = False + always_email = False + # send email if enabled if email_alert or always_email: # check if alert template is set and specific severities are configured - if ( - not alert_template - or alert_template + if not alert_template or ( + alert_template and email_severities and alert.severity in email_severities ): @@ -457,14 +470,22 @@ class Alert(models.Model): alert_interval=alert_interval, ) + # TODO rework this + if alert.severity == AlertSeverity.INFO and not core.notify_on_info_alerts: + text_alert = False + always_text = False + elif ( + alert.severity == AlertSeverity.WARNING + and not core.notify_on_warning_alerts + ): + text_alert = False + always_text = False + # send text if enabled if text_alert or always_text: # check if alert template is set and specific severities are configured - if ( - not alert_template - or alert_template - and text_severities - and alert.severity in text_severities + if not alert_template or ( + alert_template and text_severities and alert.severity in text_severities ): text_task.delay(pk=alert.pk, alert_interval=alert_interval) @@ -513,17 +534,25 @@ class Alert(models.Model): } elif alert_template.action_type == AlertTemplateActionType.REST: - output, status = run_url_rest_action( - action_id=alert_template.action_rest.id, instance=alert - ) - logger.debug(f"{output=} {status=}") + if ( + alert.severity == AlertSeverity.INFO + and not core.notify_on_info_alerts + or alert.severity == AlertSeverity.WARNING + and not core.notify_on_warning_alerts + ): + return + else: + output, status = run_url_rest_action( + action_id=alert_template.action_rest.id, instance=alert + ) + logger.debug(f"{output=} {status=}") - r = { - "stdout": output, - "stderr": "", - "execution_time": 0, - "retcode": status, - } + r = { + "stdout": output, + "stderr": "", + "execution_time": 0, + "retcode": status, + } else: return @@ -555,6 +584,9 @@ class Alert(models.Model): from agents.models import Agent, AgentHistory from autotasks.models import TaskResult from checks.models import CheckResult + from core.models import CoreSettings + + core = CoreSettings.objects.first() # set variables email_on_resolved = False @@ -633,11 +665,29 @@ class Alert(models.Model): # check if a resolved email notification should be send if email_on_resolved and not alert.resolved_email_sent: - resolved_email_task.delay(pk=alert.pk) + if alert.severity == AlertSeverity.INFO and not core.notify_on_info_alerts: + pass + + elif ( + alert.severity == AlertSeverity.WARNING + and not core.notify_on_warning_alerts + ): + pass + else: + resolved_email_task.delay(pk=alert.pk) # check if resolved text should be sent if text_on_resolved and not alert.resolved_sms_sent: - resolved_text_task.delay(pk=alert.pk) + if alert.severity == AlertSeverity.INFO and not core.notify_on_info_alerts: + pass + + elif ( + alert.severity == AlertSeverity.WARNING + and not core.notify_on_warning_alerts + ): + pass + else: + resolved_text_task.delay(pk=alert.pk) # check if resolved script/webhook should be run if alert_template and not alert.resolved_action_run: @@ -685,17 +735,25 @@ class Alert(models.Model): } elif alert_template.action_type == AlertTemplateActionType.REST: - output, status = run_url_rest_action( - action_id=alert_template.resolved_action_rest.id, instance=alert - ) - logger.debug(f"{output=} {status=}") + if ( + alert.severity == AlertSeverity.INFO + and not core.notify_on_info_alerts + or alert.severity == AlertSeverity.WARNING + and not core.notify_on_warning_alerts + ): + return + else: + output, status = run_url_rest_action( + action_id=alert_template.resolved_action_rest.id, instance=alert + ) + logger.debug(f"{output=} {status=}") - r = { - "stdout": output, - "stderr": "", - "execution_time": 0, - "retcode": status, - } + r = { + "stdout": output, + "stderr": "", + "execution_time": 0, + "retcode": status, + } else: return diff --git a/api/tacticalrmm/core/migrations/0046_coresettings_notify_on_info_alerts_and_more.py b/api/tacticalrmm/core/migrations/0046_coresettings_notify_on_info_alerts_and_more.py new file mode 100644 index 00000000..c5acb24f --- /dev/null +++ b/api/tacticalrmm/core/migrations/0046_coresettings_notify_on_info_alerts_and_more.py @@ -0,0 +1,23 @@ +# Generated by Django 4.2.13 on 2024-07-05 19:17 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ("core", "0045_coresettings_enable_server_scripts_and_more"), + ] + + operations = [ + migrations.AddField( + model_name="coresettings", + name="notify_on_info_alerts", + field=models.BooleanField(default=False), + ), + migrations.AddField( + model_name="coresettings", + name="notify_on_warning_alerts", + field=models.BooleanField(default=False), + ), + ] diff --git a/api/tacticalrmm/core/migrations/0047_alter_coresettings_notify_on_warning_alerts.py b/api/tacticalrmm/core/migrations/0047_alter_coresettings_notify_on_warning_alerts.py new file mode 100644 index 00000000..af1729ff --- /dev/null +++ b/api/tacticalrmm/core/migrations/0047_alter_coresettings_notify_on_warning_alerts.py @@ -0,0 +1,18 @@ +# Generated by Django 4.2.13 on 2024-07-05 19:30 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ("core", "0046_coresettings_notify_on_info_alerts_and_more"), + ] + + operations = [ + migrations.AlterField( + model_name="coresettings", + name="notify_on_warning_alerts", + field=models.BooleanField(default=True), + ), + ] diff --git a/api/tacticalrmm/core/models.py b/api/tacticalrmm/core/models.py index 0c66b2d1..54d22d78 100644 --- a/api/tacticalrmm/core/models.py +++ b/api/tacticalrmm/core/models.py @@ -108,6 +108,8 @@ class CoreSettings(BaseAuditModel): ) enable_server_scripts = models.BooleanField(default=True) enable_server_webterminal = models.BooleanField(default=False) + notify_on_info_alerts = models.BooleanField(default=False) + notify_on_warning_alerts = models.BooleanField(default=True) def save(self, *args, **kwargs) -> None: from alerts.tasks import cache_agents_alert_template