Exemplo n.º 1
0
    def get(self, request, pk):

        response = {}

        policy = (Policy.objects.filter(pk=pk).prefetch_related(
            "clients", "sites").first())

        response["clients"] = ClientSerializer(policy.clients.all(),
                                               many=True).data

        filtered_sites = list()

        for client in policy.clients.all():
            for site in client.sites.all():
                if site not in policy.sites.all():
                    filtered_sites.append(site)

        response["sites"] = TreeSerializer(filtered_sites +
                                           list(policy.sites.all()),
                                           many=True).data

        response["agents"] = AgentHostnameSerializer(policy.related_agents(),
                                                     many=True).data

        return Response(response)
Exemplo n.º 2
0
    def post(self, request):
        if request.data["type"] == "agent":
            agents = Agent.objects.filter(
                hostname__icontains=request.data["pattern"])
            return Response(AgentHostnameSerializer(agents, many=True).data)

        if request.data["type"] == "user":
            users = User.objects.filter(
                username__icontains=request.data["pattern"], agent=None)
            return Response(UserSerializer(users, many=True).data)

        return Response("error", status=status.HTTP_400_BAD_REQUEST)
Exemplo n.º 3
0
    def get(self, request, pk):

        response = {}

        policy = (
            Policy.objects.filter(pk=pk)
            .prefetch_related(
                "workstation_clients",
                "workstation_sites",
                "server_clients",
                "server_sites",
            )
            .first()
        )

        response["default_server_policy"] = policy.is_default_server_policy
        response["default_workstation_policy"] = policy.is_default_workstation_policy

        response["server_clients"] = ClientSerializer(
            policy.server_clients.all(), many=True
        ).data
        response["workstation_clients"] = ClientSerializer(
            policy.workstation_clients.all(), many=True
        ).data

        filtered_server_sites = list()
        filtered_workstation_sites = list()

        for client in policy.server_clients.all():
            for site in client.sites.all():
                if site not in policy.server_sites.all():
                    filtered_server_sites.append(site)

        response["server_sites"] = SiteSerializer(
            filtered_server_sites + list(policy.server_sites.all()), many=True
        ).data

        for client in policy.workstation_clients.all():
            for site in client.sites.all():
                if site not in policy.workstation_sites.all():
                    filtered_workstation_sites.append(site)

        response["workstation_sites"] = SiteSerializer(
            filtered_workstation_sites + list(policy.workstation_sites.all()), many=True
        ).data

        response["agents"] = AgentHostnameSerializer(
            policy.related_agents().only("pk", "hostname"),
            many=True,
        ).data

        return Response(response)
Exemplo n.º 4
0
    def post(self, request):
        if request.data["type"] == "agent":
            agents = Agent.objects.filter(
                hostname__icontains=request.data["pattern"])
            return Response(AgentHostnameSerializer(agents, many=True).data)

        if request.data["type"] == "user":
            agents = Agent.objects.values_list("agent_id", flat=True)
            users = User.objects.exclude(username__in=agents).filter(
                username__icontains=request.data["pattern"])
            return Response(UserSerializer(users, many=True).data)

        return Response("error", status=status.HTTP_400_BAD_REQUEST)
Exemplo n.º 5
0
def get_log(request, mode, hostname, order):
    log_file = os.path.join(settings.BASE_DIR, "log/debug.log")

    agents = Agent.objects.all()
    agent_hostnames = AgentHostnameSerializer(agents, many=True)

    dist = distro.linux_distribution(full_distribution_name=False)[0]
    switch_grep = {
        "centos": "/usr/bin/grep",
        "ubuntu": "/bin/grep",
        "debian": "/usr/bin/grep",
    }
    grep = switch_grep.get(dist, "/bin/grep")
    tac = "/usr/bin/tac"

    switch_mode = {
        "info": "INFO",
        "critical": "CRITICAL",
        "error": "ERROR",
        "warning": "WARNING",
    }
    level = switch_mode.get(mode, "INFO")

    if hostname == "all" and order == "latest":
        cmd = f"{grep} -h {level} {log_file} | {tac}"
    elif hostname == "all" and order == "oldest":
        cmd = f"{grep} -h {level} {log_file}"
    elif hostname != "all" and order == "latest":
        cmd = f"{grep} {hostname} {log_file} | {grep} -h {level} | {tac}"
    elif hostname != "all" and order == "oldest":
        cmd = f"{grep} {hostname} {log_file} | {grep} -h {level}"
    else:
        return Response("error", status=status.HTTP_400_BAD_REQUEST)

    contents = run(cmd,
                   stdout=PIPE,
                   stderr=PIPE,
                   universal_newlines=True,
                   shell=True)

    if not contents.stdout:
        resp = f"Hooray! No {mode} logs!"
    else:
        resp = contents.stdout

    return Response({"log": resp, "agents": agent_hostnames.data})
Exemplo n.º 6
0
class PolicyTableSerializer(ModelSerializer):

    default_server_policy = ReadOnlyField(source="is_default_server_policy")
    default_workstation_policy = ReadOnlyField(source="is_default_workstation_policy")
    agents_count = SerializerMethodField(read_only=True)
    winupdatepolicy = WinUpdatePolicySerializer(many=True, read_only=True)
    alert_template = ReadOnlyField(source="alert_template.id")
    excluded_clients = ClientSerializer(many=True)
    excluded_sites = SiteSerializer(many=True)
    excluded_agents = AgentHostnameSerializer(many=True)

    class Meta:
        model = Policy
        fields = "__all__"
        depth = 1

    def get_agents_count(self, policy):
        return policy.related_agents().count()
Exemplo n.º 7
0
def debug_log(request, mode, hostname, order):
    log_file = settings.LOG_CONFIG["handlers"][0]["sink"]

    agents = Agent.objects.prefetch_related("site").only("pk", "hostname")
    agent_hostnames = AgentHostnameSerializer(agents, many=True)

    switch_mode = {
        "info": "INFO",
        "critical": "CRITICAL",
        "error": "ERROR",
        "warning": "WARNING",
    }
    level = switch_mode.get(mode, "INFO")

    if hostname == "all" and order == "latest":
        cmd = f"grep -h {level} {log_file} | tac"
    elif hostname == "all" and order == "oldest":
        cmd = f"grep -h {level} {log_file}"
    elif hostname != "all" and order == "latest":
        cmd = f"grep {hostname} {log_file} | grep -h {level} | tac"
    elif hostname != "all" and order == "oldest":
        cmd = f"grep {hostname} {log_file} | grep -h {level}"
    else:
        return Response("error", status=status.HTTP_400_BAD_REQUEST)

    contents = subprocess.run(
        cmd,
        stdout=subprocess.PIPE,
        stderr=subprocess.PIPE,
        universal_newlines=True,
        shell=True,
    )

    if not contents.stdout:
        resp = f"No {mode} logs"
    else:
        resp = contents.stdout

    return Response({"log": resp, "agents": agent_hostnames.data})