From 465ec57a34308f170ce22b0d09a8a5162eb37228 Mon Sep 17 00:00:00 2001 From: wh1te909 Date: Mon, 3 Feb 2020 05:09:42 +0000 Subject: [PATCH] more work on script checks --- api/tacticalrmm/checks/admin.py | 2 ++ .../migrations/0002_auto_20200203_0452.py | 18 ++++++++++ api/tacticalrmm/checks/models.py | 14 ++++++-- api/tacticalrmm/checks/serializers.py | 33 ++++++++++++++++++- install.sh | 1 + 5 files changed, 64 insertions(+), 4 deletions(-) create mode 100644 api/tacticalrmm/checks/migrations/0002_auto_20200203_0452.py diff --git a/api/tacticalrmm/checks/admin.py b/api/tacticalrmm/checks/admin.py index 33d5735f..01369454 100644 --- a/api/tacticalrmm/checks/admin.py +++ b/api/tacticalrmm/checks/admin.py @@ -15,6 +15,7 @@ from .models import ( WinServiceCheckEmail, ScriptCheck, ScriptCheckEmail, + Script, ) admin.site.register(DiskCheck) @@ -31,3 +32,4 @@ admin.site.register(WinServiceCheck) admin.site.register(WinServiceCheckEmail) admin.site.register(ScriptCheck) admin.site.register(ScriptCheckEmail) +admin.site.register(Script) diff --git a/api/tacticalrmm/checks/migrations/0002_auto_20200203_0452.py b/api/tacticalrmm/checks/migrations/0002_auto_20200203_0452.py new file mode 100644 index 00000000..05097236 --- /dev/null +++ b/api/tacticalrmm/checks/migrations/0002_auto_20200203_0452.py @@ -0,0 +1,18 @@ +# Generated by Django 3.0.2 on 2020-02-03 04:52 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('checks', '0001_initial'), + ] + + operations = [ + migrations.AlterField( + model_name='script', + name='shell', + field=models.CharField(choices=[('powershell', 'Powershell'), ('cmd', 'Batch (CMD)'), ('python', 'Python')], default='powershell', max_length=100), + ), + ] diff --git a/api/tacticalrmm/checks/models.py b/api/tacticalrmm/checks/models.py index e2c822c5..d3598eea 100644 --- a/api/tacticalrmm/checks/models.py +++ b/api/tacticalrmm/checks/models.py @@ -19,7 +19,7 @@ STANDARD_CHECK_CHOICES = [ SCRIPT_CHECK_SHELLS = [ ("powershell", "Powershell"), - ("batch", "Batch"), + ("cmd", "Batch (CMD)"), ("python", "Python"), ] @@ -89,6 +89,14 @@ class Script(models.Model): max_length=100, choices=SCRIPT_CHECK_SHELLS, default="powershell" ) + @property + def filepath(self): + return f"salt://scripts//userdefined//{self.filename}" + + + def __str__(self): + return self.filename + class ScriptCheck(models.Model): agent = models.ForeignKey( Agent, related_name="scriptchecks", on_delete=models.CASCADE @@ -111,14 +119,14 @@ class ScriptCheck(models.Model): def send_email(self): send_mail( f"Script Check Fail on {self.agent.hostname}", - f"Script check {self.name} is failing on {self.agent.hostname}", + f"Script check {self.script.name} is failing on {self.agent.hostname}", settings.EMAIL_HOST_USER, settings.EMAIL_ALERT_RECIPIENTS, fail_silently=False, ) def __str__(self): - return f"{self.agent.hostname} - {self.name}" + return f"{self.agent.hostname} - {self.script.filename}" class ScriptCheckEmail(models.Model): email = models.ForeignKey(ScriptCheck, on_delete=models.CASCADE) diff --git a/api/tacticalrmm/checks/serializers.py b/api/tacticalrmm/checks/serializers.py index df514aaf..9d830a61 100644 --- a/api/tacticalrmm/checks/serializers.py +++ b/api/tacticalrmm/checks/serializers.py @@ -7,6 +7,7 @@ from .models import ( PingCheck, CpuHistory, WinServiceCheck, + Script, ScriptCheck, ) from agents.models import Agent @@ -42,10 +43,40 @@ class PingCheckSerializer(serializers.ModelSerializer): model = PingCheck fields = "__all__" + +class ScriptSerializer(serializers.ModelSerializer): + class Meta: + model = Script + fields = ( + "id", + "name", + "description", + "filename", + "shell", + "filepath", + ) + + class ScriptCheckSerializer(serializers.ModelSerializer): + + script = ScriptSerializer(read_only=True) + class Meta: model = ScriptCheck - fields = "__all__" + fields = ( + "id", + "check_type", + "timeout", + "failures", + "status", + "failure_count", + "email_alert", + "text_alert", + "more_info", + "last_run", + "agent", + "script", + ) class WinServiceCheckSerializer(serializers.ModelSerializer): diff --git a/install.sh b/install.sh index ebade893..71fcd07d 100755 --- a/install.sh +++ b/install.sh @@ -520,6 +520,7 @@ echo "${celerybeatservice}" | sudo tee /etc/systemd/system/celerybeat.service > sudo mkdir -p /srv/salt sudo cp -r /home/${USER}/rmm/_modules /srv/salt/ sudo cp -r /home/${USER}/rmm/scripts /srv/salt/ +sudo mkdir /srv/salt/scripts/userdefined sudo chown root:root -R /srv/salt/ sudo chown root:${USER} -R /srv/salt/scripts/ sudo chmod 770 -R /srv/salt/scripts/