Esempio n. 1
0
    def patch(self, request, pk, agentid):
        from alerts.models import Alert
        from logs.models import AuditLog

        agent = get_object_or_404(Agent, agent_id=agentid)
        task = get_object_or_404(AutomatedTask, pk=pk)

        serializer = TaskRunnerPatchSerializer(
            instance=task, data=request.data, partial=True
        )
        serializer.is_valid(raise_exception=True)
        serializer.save(last_run=djangotime.now())

        status = "failing" if task.retcode != 0 else "passing"

        new_task: AutomatedTask = AutomatedTask.objects.get(pk=task.pk)
        new_task.status = status
        new_task.save()

        if status == "passing":
            if Alert.objects.filter(assigned_task=new_task, resolved=False).exists():
                Alert.handle_alert_resolve(new_task)
        else:
            Alert.handle_alert_failure(new_task)

        AuditLog.objects.create(
            username=agent.hostname,
            agent=agent.hostname,
            object_type="agent",
            action="task_run",
            message=f"Scheduled Task {task.name} was run on {agent.hostname}",
            after_value=AutomatedTask.serialize(new_task),
        )

        return Response("ok")
Esempio n. 2
0
    def patch(self, request, pk):
        task = get_object_or_404(AutomatedTask, pk=pk)

        serializer = TaskRunnerPatchSerializer(instance=task,
                                               data=request.data,
                                               partial=True)
        serializer.is_valid(raise_exception=True)
        serializer.save(last_run=djangotime.now())
        return Response("ok")
Esempio n. 3
0
    def patch(self, request, pk, agentid):
        from logs.models import AuditLog

        agent = get_object_or_404(Agent, agent_id=agentid)
        task = get_object_or_404(AutomatedTask, pk=pk)

        serializer = TaskRunnerPatchSerializer(instance=task,
                                               data=request.data,
                                               partial=True)
        serializer.is_valid(raise_exception=True)
        serializer.save(last_run=djangotime.now())

        new_task = AutomatedTask.objects.get(pk=task.pk)
        AuditLog.objects.create(
            username=agent.hostname,
            agent=agent.hostname,
            object_type="agent",
            action="task_run",
            message=f"Scheduled Task {task.name} was run on {agent.hostname}",
            after_value=AutomatedTask.serialize(new_task),
        )

        return Response("ok")
Esempio n. 4
0
    def patch(self, request, pk, agentid):
        from alerts.models import Alert
        from logs.models import AuditLog

        agent = get_object_or_404(Agent, agent_id=agentid)
        task = get_object_or_404(AutomatedTask, pk=pk)

        serializer = TaskRunnerPatchSerializer(instance=task,
                                               data=request.data,
                                               partial=True)
        serializer.is_valid(raise_exception=True)
        new_task = serializer.save(last_run=djangotime.now())

        # check if task is a collector and update the custom field
        if task.custom_field:
            if not task.stderr:

                if AgentCustomField.objects.filter(field=task.custom_field,
                                                   agent=task.agent).exists():
                    agent_field = AgentCustomField.objects.get(
                        field=task.custom_field, agent=task.agent)
                else:
                    agent_field = AgentCustomField.objects.create(
                        field=task.custom_field, agent=task.agent)

                # get last line of stdout
                value = new_task.stdout.split("\n")[-1].strip()

                if task.custom_field.type in [
                        "text", "number", "single", "datetime"
                ]:
                    agent_field.string_value = value
                    agent_field.save()
                elif task.custom_field.type == "multiple":
                    agent_field.multiple_value = value.split(",")
                    agent_field.save()
                elif task.custom_field.type == "checkbox":
                    agent_field.bool_value = bool(value)
                    agent_field.save()

                status = "passing"
            else:
                status = "failing"
        else:
            status = "failing" if task.retcode != 0 else "passing"

        new_task.status = status
        new_task.save()

        if status == "passing":
            if Alert.objects.filter(assigned_task=new_task,
                                    resolved=False).exists():
                Alert.handle_alert_resolve(new_task)
        else:
            Alert.handle_alert_failure(new_task)

        AuditLog.objects.create(
            username=agent.hostname,
            agent=agent.hostname,
            object_type="agent",
            action="task_run",
            message=f"Scheduled Task {task.name} was run on {agent.hostname}",
            after_value=AutomatedTask.serialize(new_task),
        )

        return Response("ok")