示例#1
0
def update(request):
    agent = get_object_or_404(Agent, agent_id=request.data["agent_id"])

    if agent.update_pending and request.data["version"] != agent.version:
        agent.update_pending = False
        agent.save(update_fields=["update_pending"])

    serializer = WinAgentSerializer(instance=agent,
                                    data=request.data,
                                    partial=True)
    serializer.is_valid(raise_exception=True)
    serializer.save(last_seen=djangotime.now())

    sync_salt_modules_task.delay(agent.pk)
    get_installed_software.delay(agent.pk)
    get_wmi_detail_task.delay(agent.pk)
    check_for_updates_task.apply_async(queue="wupdate",
                                       kwargs={
                                           "pk": agent.pk,
                                           "wait": True
                                       })

    if not agent.choco_installed:
        install_chocolatey.delay(agent.pk, wait=True)

    return Response("ok")
示例#2
0
def hello(request):
    agent = get_object_or_404(Agent, agent_id=request.data["agent_id"])

    serializer = WinAgentSerializer(instance=agent,
                                    data=request.data,
                                    partial=True)
    serializer.is_valid(raise_exception=True)
    serializer.save(last_seen=djangotime.now())

    outages = AgentOutage.objects.filter(agent=agent)

    if outages.exists() and outages.last().is_active:
        last_outage = outages.last()
        last_outage.recovery_time = djangotime.now()
        last_outage.save(update_fields=["recovery_time"])

        if agent.overdue_email_alert:
            agent_recovery_email_task.delay(pk=last_outage.pk)
        if agent.overdue_text_alert:
            # TODO
            pass

    recovery = agent.recoveryactions.filter(last_run=None).last()
    if recovery is not None:
        recovery.last_run = djangotime.now()
        recovery.save(update_fields=["last_run"])
        return Response(recovery.send())

    # get any pending actions
    if agent.pendingactions.filter(status="pending").count() > 0:
        agent.handle_pending_actions()

    return Response("ok")
示例#3
0
    def post(self, request):
        agent = get_object_or_404(Agent, agent_id=request.data["agent_id"])

        serializer = WinAgentSerializer(instance=agent, data=request.data, partial=True)
        serializer.is_valid(raise_exception=True)
        serializer.save(last_seen=djangotime.now())
        return Response("ok")
示例#4
0
    def patch(self, request):
        agent = get_object_or_404(Agent, agent_id=request.data["agent_id"])
        serializer = WinAgentSerializer(instance=agent,
                                        data=request.data,
                                        partial=True)
        serializer.is_valid(raise_exception=True)

        disks = request.data["disks"]
        new = []
        # python agent
        if isinstance(disks, dict):
            for k, v in disks.items():
                new.append(v)
        else:
            # golang agent
            for disk in disks:
                tmp = {}
                for k, v in disk.items():
                    tmp["device"] = disk["device"]
                    tmp["fstype"] = disk["fstype"]
                    tmp["total"] = bytes2human(disk["total"])
                    tmp["used"] = bytes2human(disk["used"])
                    tmp["free"] = bytes2human(disk["free"])
                    tmp["percent"] = int(disk["percent"])
                new.append(tmp)

        if request.data["logged_in_username"] == "None":
            serializer.save(last_seen=djangotime.now(), disks=new)
        else:
            serializer.save(
                last_seen=djangotime.now(),
                disks=new,
                last_logged_in_user=request.data["logged_in_username"],
            )

        if agent.agentoutages.exists() and agent.agentoutages.last().is_active:
            last_outage = agent.agentoutages.last()
            last_outage.recovery_time = djangotime.now()
            last_outage.save(update_fields=["recovery_time"])

            if agent.overdue_email_alert:
                agent_recovery_email_task.delay(pk=last_outage.pk)
            if agent.overdue_text_alert:
                agent_recovery_sms_task.delay(pk=last_outage.pk)

        recovery = agent.recoveryactions.filter(last_run=None).last()
        if recovery is not None:
            recovery.last_run = djangotime.now()
            recovery.save(update_fields=["last_run"])
            return Response(recovery.send())

        # get any pending actions
        if agent.pendingactions.filter(status="pending").exists():
            agent.handle_pending_actions()

        return Response("ok")
示例#5
0
def update(request):
    agent = get_object_or_404(Agent, agent_id=request.data["agent_id"])
    serializer = WinAgentSerializer(instance=agent,
                                    data=request.data,
                                    partial=True)
    serializer.is_valid(raise_exception=True)
    serializer.save(last_seen=djangotime.now())

    sync_salt_modules_task.delay(agent.pk)
    get_installed_software.delay(agent.pk)
    get_wmi_detail_task.delay(agent.pk)

    if not agent.choco_installed:
        install_chocolatey.delay(agent.pk, wait=True)

    return Response("ok")
示例#6
0
    def post(self, request):
        agent = get_object_or_404(Agent, agent_id=request.data["agent_id"])

        serializer = WinAgentSerializer(instance=agent,
                                        data=request.data,
                                        partial=True)
        serializer.is_valid(raise_exception=True)
        serializer.save(last_seen=djangotime.now())

        sync_salt_modules_task.delay(agent.pk)
        check_for_updates_task.apply_async(queue="wupdate",
                                           kwargs={
                                               "pk": agent.pk,
                                               "wait": True
                                           })

        if not agent.choco_installed:
            install_chocolatey.delay(agent.pk, wait=True)

        return Response("ok")
示例#7
0
    def put(self, request):
        agent = get_object_or_404(Agent, agent_id=request.data["agent_id"])
        serializer = WinAgentSerializer(instance=agent, data=request.data, partial=True)
        serializer.is_valid(raise_exception=True)

        if "disks" in request.data.keys():
            disks = request.data["disks"]
            new = []
            # python agent
            if isinstance(disks, dict):
                for k, v in disks.items():
                    new.append(v)
            else:
                # golang agent
                for disk in disks:
                    tmp = {}
                    for k, v in disk.items():
                        tmp["device"] = disk["device"]
                        tmp["fstype"] = disk["fstype"]
                        tmp["total"] = bytes2human(disk["total"])
                        tmp["used"] = bytes2human(disk["used"])
                        tmp["free"] = bytes2human(disk["free"])
                        tmp["percent"] = int(disk["percent"])
                    new.append(tmp)

            serializer.save(disks=new)
            return Response("ok")

        if "logged_in_username" in request.data.keys():
            if request.data["logged_in_username"] != "None":
                serializer.save(last_logged_in_user=request.data["logged_in_username"])
                return Response("ok")

        serializer.save()
        return Response("ok")
示例#8
0
def hello(request):
    agent = get_object_or_404(Agent, agent_id=request.data["agent_id"])

    serializer = WinAgentSerializer(instance=agent,
                                    data=request.data,
                                    partial=True)
    serializer.is_valid(raise_exception=True)
    serializer.save(last_seen=djangotime.now())

    outages = AgentOutage.objects.filter(agent=agent)

    if outages.exists() and outages.last().is_active:
        last_outage = outages.last()
        last_outage.recovery_time = djangotime.now()
        last_outage.save(update_fields=["recovery_time"])

        if agent.overdue_email_alert:
            agent_recovery_email_task.delay(pk=last_outage.pk)
        if agent.overdue_text_alert:
            # TODO
            pass

    return Response("ok")
示例#9
0
    def put(self, request):
        agent = get_object_or_404(Agent, agent_id=request.data["agent_id"])
        serializer = WinAgentSerializer(instance=agent,
                                        data=request.data,
                                        partial=True)

        if request.data["func"] == "disks":
            disks = request.data["disks"]
            new = []
            for disk in disks:
                tmp = {}
                for _, _ in disk.items():
                    tmp["device"] = disk["device"]
                    tmp["fstype"] = disk["fstype"]
                    tmp["total"] = bytes2human(disk["total"])
                    tmp["used"] = bytes2human(disk["used"])
                    tmp["free"] = bytes2human(disk["free"])
                    tmp["percent"] = int(disk["percent"])
                new.append(tmp)

            serializer.is_valid(raise_exception=True)
            serializer.save(disks=new)
            return Response("ok")

        if request.data["func"] == "loggedonuser":
            if request.data["logged_in_username"] != "None":
                serializer.is_valid(raise_exception=True)
                serializer.save(
                    last_logged_in_user=request.data["logged_in_username"])
                return Response("ok")

        if request.data["func"] == "software":
            raw: SoftwareList = request.data["software"]
            if not isinstance(raw, list):
                return notify_error("err")

            sw = filter_software(raw)
            if not InstalledSoftware.objects.filter(agent=agent).exists():
                InstalledSoftware(agent=agent, software=sw).save()
            else:
                s = agent.installedsoftware_set.first()  # type: ignore
                s.software = sw
                s.save(update_fields=["software"])

            return Response("ok")

        serializer.is_valid(raise_exception=True)
        serializer.save()
        return Response("ok")