示例#1
0
def uninstall(request):
    agent = get_object_or_404(Agent, pk=request.data["pk"])
    asyncio.run(agent.nats_cmd({"func": "uninstall"}, wait=False))
    name = agent.hostname
    agent.delete()
    reload_nats()
    return Response(f"{name} will now be uninstalled.")
示例#2
0
    def post(self, request):
        from logs.models import AuditLog

        """ Creates the agent """

        if Agent.objects.filter(agent_id=request.data["agent_id"]).exists():
            return notify_error(
                "Agent already exists. Remove old agent first if trying to re-install"
            )

        agent = Agent(
            agent_id=request.data["agent_id"],
            hostname=request.data["hostname"],
            site_id=int(request.data["site"]),
            monitoring_type=request.data["monitoring_type"],
            description=request.data["description"],
            mesh_node_id=request.data["mesh_node_id"],
            last_seen=djangotime.now(),
        )
        agent.save()
        agent.salt_id = f"{agent.hostname}-{agent.pk}"
        agent.save(update_fields=["salt_id"])

        user = User.objects.create_user(
            username=request.data["agent_id"],
            agent=agent,
            password=User.objects.make_random_password(60),
        )

        token = Token.objects.create(user=user)

        if agent.monitoring_type == "workstation":
            WinUpdatePolicy(agent=agent, run_time_days=[5, 6]).save()
        else:
            WinUpdatePolicy(agent=agent).save()

        reload_nats()

        # Generate policies for new agent
        agent.generate_checks_from_policies()
        agent.generate_tasks_from_policies()

        # create agent install audit record
        AuditLog.objects.create(
            username=request.user,
            agent=agent.hostname,
            object_type="agent",
            action="agent_install",
            message=f"{request.user} installed new agent {agent.hostname}",
            after_value=Agent.serialize(agent),
        )

        return Response(
            {
                "pk": agent.pk,
                "saltid": f"{agent.hostname}-{agent.pk}",
                "token": token.key,
            }
        )
示例#3
0
def server_maintenance(request):
    from tacticalrmm.utils import reload_nats

    if "action" not in request.data:
        return notify_error("The data is incorrect")

    if request.data["action"] == "reload_nats":
        reload_nats()
        return Response("Nats configuration was reloaded successfully.")

    if request.data["action"] == "rm_orphaned_tasks":
        from agents.models import Agent
        from autotasks.tasks import remove_orphaned_win_tasks

        agents = Agent.objects.only("pk", "last_seen", "overdue_time",
                                    "offline_time")
        online = [i for i in agents if i.status == "online"]
        for agent in online:
            remove_orphaned_win_tasks.delay(agent.pk)

        return Response(
            "The task has been initiated. Check the Debug Log in the UI for progress."
        )

    if request.data["action"] == "prune_db":
        from logs.models import AuditLog, PendingAction

        if "prune_tables" not in request.data:
            return notify_error("The data is incorrect.")

        tables = request.data["prune_tables"]
        records_count = 0
        if "audit_logs" in tables:
            auditlogs = AuditLog.objects.filter(action="check_run")
            records_count += auditlogs.count()
            auditlogs.delete()

        if "pending_actions" in tables:
            pendingactions = PendingAction.objects.filter(status="completed")
            records_count += pendingactions.count()
            pendingactions.delete()

        if "alerts" in tables:
            from alerts.models import Alert

            alerts = Alert.objects.all()
            records_count += alerts.count()
            alerts.delete()

        return Response(
            f"{records_count} records were pruned from the database")

    return notify_error("The data is incorrect")
示例#4
0
def uninstall(request):
    agent = get_object_or_404(Agent, pk=request.data["pk"])
    if agent.has_nats:
        asyncio.run(agent.nats_cmd({"func": "uninstall"}, wait=False))

    salt_id = agent.salt_id
    name = agent.hostname
    has_nats = agent.has_nats
    agent.delete()
    reload_nats()

    uninstall_agent_task.delay(salt_id, has_nats)
    return Response(f"{name} will now be uninstalled.")
示例#5
0
    def get(self, request, agentid):
        agent = get_object_or_404(Agent, agent_id=agentid)
        recovery = agent.recoveryactions.filter(last_run=None).last()  # type: ignore
        ret = {"mode": "pass", "shellcmd": ""}
        if recovery is None:
            return Response(ret)

        recovery.last_run = djangotime.now()
        recovery.save(update_fields=["last_run"])

        ret["mode"] = recovery.mode

        if recovery.mode == "command":
            ret["shellcmd"] = recovery.command
        elif recovery.mode == "rpc":
            reload_nats()

        return Response(ret)
示例#6
0
 def handle(self, *args, **kwargs):
     reload_nats()