more work on script checks

This commit is contained in:
wh1te909 2020-02-03 05:09:42 +00:00
parent 7da264842a
commit 465ec57a34
5 changed files with 64 additions and 4 deletions

View File

@ -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)

View File

@ -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),
),
]

View File

@ -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)

View File

@ -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):

View File

@ -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/