more checks rework

This commit is contained in:
wh1te909 2021-05-29 01:37:20 +00:00
parent 26e6a8c409
commit 71d1206277
5 changed files with 33 additions and 66 deletions

View File

@ -321,11 +321,16 @@ class CheckRunner(APIView):
def patch(self, request): def patch(self, request):
check = get_object_or_404(Check, pk=request.data["id"]) check = get_object_or_404(Check, pk=request.data["id"])
if pyver.parse(check.agent.version) < pyver.parse("1.5.7"):
return notify_error("unsupported")
check.last_run = djangotime.now() check.last_run = djangotime.now()
check.save(update_fields=["last_run"]) check.save(update_fields=["last_run"])
status = check.handle_checkv2(request.data) status = check.handle_checkv2(request.data)
if status == "failing" and check.assignedtask.exists(): # type: ignore
check.handle_assigned_task()
return Response(status) return Response("ok")
class CheckRunnerInterval(APIView): class CheckRunnerInterval(APIView):

View File

@ -467,6 +467,11 @@ class Check(BaseAuditModel):
return self.status return self.status
def handle_assigned_task(self) -> None:
for task in self.assignedtask.all(): # type: ignore
if task.enabled:
task.run_win_task()
@staticmethod @staticmethod
def serialize(check): def serialize(check):
# serializes the check and returns json # serializes the check and returns json

View File

@ -158,14 +158,8 @@ class AssignedTaskCheckRunnerField(serializers.ModelSerializer):
class CheckRunnerGetSerializer(serializers.ModelSerializer): class CheckRunnerGetSerializer(serializers.ModelSerializer):
# only send data needed for agent to run a check # only send data needed for agent to run a check
assigned_tasks = serializers.SerializerMethodField()
script = ScriptCheckSerializer(read_only=True) script = ScriptCheckSerializer(read_only=True)
def get_assigned_tasks(self, obj):
if obj.assignedtask.exists():
tasks = obj.assignedtask.all()
return AssignedTaskCheckRunnerField(tasks, many=True).data
class Meta: class Meta:
model = Check model = Check
exclude = [ exclude = [
@ -193,6 +187,7 @@ class CheckRunnerGetSerializer(serializers.ModelSerializer):
"modified_by", "modified_by",
"modified_time", "modified_time",
"history", "history",
"dashboard_alert",
] ]

View File

@ -400,7 +400,7 @@ class TestCheckTasks(TacticalTestCase):
def setUp(self): def setUp(self):
self.authenticate() self.authenticate()
self.setup_coresettings() self.setup_coresettings()
self.agent = baker.make_recipe("agents.agent") self.agent = baker.make_recipe("agents.agent", version="1.5.7")
def test_prune_check_history(self): def test_prune_check_history(self):
from .tasks import prune_check_history from .tasks import prune_check_history
@ -526,6 +526,7 @@ class TestCheckTasks(TacticalTestCase):
"percent_used": 85, "percent_used": 85,
"total": 500, "total": 500,
"free": 400, "free": 400,
"more_info": "More info",
} }
resp = self.client.patch(url, data, format="json") resp = self.client.patch(url, data, format="json")
@ -543,6 +544,7 @@ class TestCheckTasks(TacticalTestCase):
"percent_used": 95, "percent_used": 95,
"total": 500, "total": 500,
"free": 400, "free": 400,
"more_info": "More info",
} }
resp = self.client.patch(url, data, format="json") resp = self.client.patch(url, data, format="json")
@ -573,6 +575,7 @@ class TestCheckTasks(TacticalTestCase):
"percent_used": 95, "percent_used": 95,
"total": 500, "total": 500,
"free": 400, "free": 400,
"more_info": "More info",
} }
resp = self.client.patch(url, data, format="json") resp = self.client.patch(url, data, format="json")
@ -592,6 +595,7 @@ class TestCheckTasks(TacticalTestCase):
"percent_used": 95, "percent_used": 95,
"total": 500, "total": 500,
"free": 400, "free": 400,
"more_info": "More info",
} }
resp = self.client.patch(url, data, format="json") resp = self.client.patch(url, data, format="json")
@ -608,6 +612,7 @@ class TestCheckTasks(TacticalTestCase):
"percent_used": 50, "percent_used": 50,
"total": 500, "total": 500,
"free": 400, "free": 400,
"more_info": "More info",
} }
resp = self.client.patch(url, data, format="json") resp = self.client.patch(url, data, format="json")
@ -791,12 +796,7 @@ class TestCheckTasks(TacticalTestCase):
) )
# test failing info # test failing info
data = { data = {"id": ping.id, "status": "failing", "output": "reply from a.com"}
"id": ping.id,
"output": "Reply from 192.168.1.27: Destination host unreachable",
"has_stdout": True,
"has_stderr": False,
}
resp = self.client.patch(url, data, format="json") resp = self.client.patch(url, data, format="json")
self.assertEqual(resp.status_code, 200) self.assertEqual(resp.status_code, 200)
@ -806,13 +806,6 @@ class TestCheckTasks(TacticalTestCase):
self.assertEqual(new_check.alert_severity, "info") self.assertEqual(new_check.alert_severity, "info")
# test failing warning # test failing warning
data = {
"id": ping.id,
"output": "Reply from 192.168.1.27: Destination host unreachable",
"has_stdout": True,
"has_stderr": False,
}
ping.alert_severity = "warning" ping.alert_severity = "warning"
ping.save() ping.save()
@ -824,13 +817,6 @@ class TestCheckTasks(TacticalTestCase):
self.assertEqual(new_check.alert_severity, "warning") self.assertEqual(new_check.alert_severity, "warning")
# test failing error # test failing error
data = {
"id": ping.id,
"output": "Reply from 192.168.1.27: Destination host unreachable",
"has_stdout": True,
"has_stderr": False,
}
ping.alert_severity = "error" ping.alert_severity = "error"
ping.save() ping.save()
@ -842,13 +828,6 @@ class TestCheckTasks(TacticalTestCase):
self.assertEqual(new_check.alert_severity, "error") self.assertEqual(new_check.alert_severity, "error")
# test failing error # test failing error
data = {
"id": ping.id,
"output": "some output",
"has_stdout": False,
"has_stderr": True,
}
resp = self.client.patch(url, data, format="json") resp = self.client.patch(url, data, format="json")
self.assertEqual(resp.status_code, 200) self.assertEqual(resp.status_code, 200)
@ -857,12 +836,7 @@ class TestCheckTasks(TacticalTestCase):
self.assertEqual(new_check.alert_severity, "error") self.assertEqual(new_check.alert_severity, "error")
# test passing # test passing
data = { data = {"id": ping.id, "status": "passing", "output": "reply from a.com"}
"id": ping.id,
"output": "Reply from 192.168.1.1: bytes=32 time<1ms TTL=64",
"has_stdout": True,
"has_stderr": False,
}
resp = self.client.patch(url, data, format="json") resp = self.client.patch(url, data, format="json")
self.assertEqual(resp.status_code, 200) self.assertEqual(resp.status_code, 200)
@ -881,7 +855,7 @@ class TestCheckTasks(TacticalTestCase):
) )
# test passing running # test passing running
data = {"id": winsvc.id, "exists": True, "status": "running"} data = {"id": winsvc.id, "status": "passing", "more_info": "ok"}
resp = self.client.patch(url, data, format="json") resp = self.client.patch(url, data, format="json")
self.assertEqual(resp.status_code, 200) self.assertEqual(resp.status_code, 200)
@ -889,20 +863,8 @@ class TestCheckTasks(TacticalTestCase):
new_check = Check.objects.get(pk=winsvc.id) new_check = Check.objects.get(pk=winsvc.id)
self.assertEqual(new_check.status, "passing") self.assertEqual(new_check.status, "passing")
# test passing start pending # test failing
winsvc.pass_if_start_pending = True data = {"id": winsvc.id, "status": "failing", "more_info": "ok"}
winsvc.save()
data = {"id": winsvc.id, "exists": True, "status": "start_pending"}
resp = self.client.patch(url, data, format="json")
self.assertEqual(resp.status_code, 200)
new_check = Check.objects.get(pk=winsvc.id)
self.assertEqual(new_check.status, "passing")
# test failing no start
data = {"id": winsvc.id, "exists": True, "status": "not running"}
resp = self.client.patch(url, data, format="json") resp = self.client.patch(url, data, format="json")
self.assertEqual(resp.status_code, 200) self.assertEqual(resp.status_code, 200)
@ -911,7 +873,7 @@ class TestCheckTasks(TacticalTestCase):
self.assertEqual(new_check.status, "failing") self.assertEqual(new_check.status, "failing")
self.assertEqual(new_check.alert_severity, "info") self.assertEqual(new_check.alert_severity, "info")
# test failing and attempt start """ # test failing and attempt start
winsvc.restart_if_stopped = True winsvc.restart_if_stopped = True
winsvc.alert_severity = "warning" winsvc.alert_severity = "warning"
winsvc.save() winsvc.save()
@ -976,9 +938,9 @@ class TestCheckTasks(TacticalTestCase):
self.assertEqual(resp.status_code, 200) self.assertEqual(resp.status_code, 200)
new_check = Check.objects.get(pk=winsvc.id) new_check = Check.objects.get(pk=winsvc.id)
self.assertEqual(new_check.status, "passing") self.assertEqual(new_check.status, "passing") """
def test_handle_eventlog_check(self): """ def test_handle_eventlog_check(self):
from checks.models import Check from checks.models import Check
url = "/api/v3/checkrunner/" url = "/api/v3/checkrunner/"
@ -1180,4 +1142,4 @@ class TestCheckTasks(TacticalTestCase):
new_check = Check.objects.get(pk=eventlog.id) new_check = Check.objects.get(pk=eventlog.id)
self.assertEquals(new_check.status, "passing") self.assertEquals(new_check.status, "passing") """

View File

@ -72,18 +72,18 @@ export default {
isValidThreshold(warning, error, diskcheck = false) { isValidThreshold(warning, error, diskcheck = false) {
if (warning === 0 && error === 0) { if (warning === 0 && error === 0) {
Notify.create(notifyErrorConfig("Warning Threshold or Error Threshold need to be set", 2000)); Notify.create({ type: "negative", timeout: 2000, message: "Warning Threshold or Error Threshold need to be set" });
return false return false;
} }
if (!diskcheck && warning > error && warning > 0 && error > 0) { if (!diskcheck && warning > error && warning > 0 && error > 0) {
Notify.create(notifyErrorConfig("Warning Threshold must be less than Error Threshold", 2000)); Notify.create({ type: "negative", timeout: 2000, message: "Warning Threshold must be less than Error Threshold" });
return false return false;
} }
if (diskcheck && warning < error && warning > 0 && error > 0) { if (diskcheck && warning < error && warning > 0 && error > 0) {
Notify.create(notifyErrorConfig("Warning Threshold must be more than Error Threshold", 2000)); Notify.create({ type: "negative", timeout: 2000, message: "Warning Threshold must be more than Error Threshold" });
return false return false;
} }
return true; return true;