Ejemplo n.º 1
0
    def patch(self, request):
        """ 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"
            )

        client = get_object_or_404(Client, pk=int(request.data["client"]))
        site = get_object_or_404(Site, pk=int(request.data["site"]))

        agent = Agent(
            agent_id=request.data["agent_id"],
            hostname=request.data["hostname"],
            client=client.client,
            site=site.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"])

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

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

        return Response({"pk": agent.pk, "saltid": f"{agent.hostname}-{agent.pk}"})
Ejemplo n.º 2
0
def add(request):
    data = request.data

    client = get_object_or_404(Client, pk=int(data["client"]))
    site = get_object_or_404(Site, pk=int(data["site"]))

    if not Agent.objects.filter(agent_id=data["agent_id"]).exists():
        agent = Agent(
            agent_id=data["agent_id"],
            hostname=data["hostname"],
            client=client.client,
            site=site.site,
            monitoring_type=data["monitoring_type"],
            description=data["description"],
            mesh_node_id=data["mesh_node_id"],
            last_seen=djangotime.now(),
        )

        agent.save()

        agent.salt_id = f"{agent.hostname}-{agent.pk}"
        agent.save(update_fields=["salt_id"])

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

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

        return Response({"pk": agent.pk})
    else:
        return Response("err", status=status.HTTP_400_BAD_REQUEST)
Ejemplo n.º 3
0
def get_all_agents_url():

    existing = [agent.url for agent in Agent.objects.all()]
    agents = []
    for c in string.ascii_uppercase:
        num = get_num_page(c)
        if num:
            page_num = (num - 1) / 50 + 1
            for page in range(1, page_num + 1):
                url = AGENTS_FIRSTNAME_URL + "/%c/%d" % (c, page) + "?items_per_page=50"
                agent_url_list = get_agent_url_list_from_url(url)
                for agent_url in agent_url_list:
                    if not (agent_url in existing):
                        agent = Agent(url=agent_url)
                        agent.save()
                    # agent = get_agent_by_url(HOST + agent_url)
                    # if agent is None:  # some error happens, try to process again
                    #     time.sleep(1)
                    #     agent = get_agent_by_url(HOST + agent_url)
                    # else:
                    #     agent.save()
                        agents.append(agent)
                time.sleep(1)
        logger.info("Get all agents with first_letter = %c done!" % c)
    logger.info("Get agents: %d" % (len(agents)))
    return agents
Ejemplo n.º 4
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,
            }
        )
Ejemplo n.º 5
0
def read_agent_list(input_path='agents/SalesPersons.xlsx'):

    book = xlrd.open_workbook(input_path)
    sheet = book.sheet_by_index(0)

    for row in range(2, sheet.nrows):
        reg_number = sheet.cell(row, 0).value
        name = sheet.cell(row, 1).value
        estate_name = sheet.cell(row, 2).value
        lic_number = sheet.cell(row, 3).value
        agent = Agent(reg_number=reg_number,
                      name=name,
                      estate_name=estate_name,
                      lic_number=lic_number)
        agent.save()
        print "done %d" % row
    print "done"
Ejemplo n.º 6
0
def signup_agent(request):
    if not request.user.is_authenticated():
        if request.method == 'POST':
            form = SignUpAgentForm(request.POST)
            if form.is_valid():
                user = form.save()
                user.save()
                password = form.cleaned_data.get('password1')
                user = authenticate(username=user.username, password=password)
                agent = Agent()
                agent.agent_id = user
                agent.total_transaction = 0
                agent.save()

                auth_login(request, user)
                return HttpResponseRedirect(reverse('catalog:index'))
            else:
                return render(request, 'del3/signupagent.html', {'form': form})
        else:
            form = SignUpAgentForm()
            return render(request, 'del3/signupagent.html', {'form': form})
    else:
        return HttpResponseRedirect(reverse('index'))
Ejemplo n.º 7
0
        'name': 'CMIS Rest API',
        'is_active': True,
        'script': 'agents.lib.cmis_rest_agent.CmisRestAgent',
        'description': (
            'Tests availability and performance of Alfresco '
            'CMIS Rest API endpoints.'
        ),
    },
    {
        'name': 'JMXDump',
        'is_active': True,
        'script': 'agents.lib.jmxdump_agent.JmxDumpAgent',
        'description': (
            'Stores all settings of a running Alfresco system '
            'for archival purposes.'
        ),
    },
]


if __name__ == '__main__':

    for agent in agent_list:
        a = Agent()
        for k, v in agent.items():
            setattr(a, k, v)
        try:
            a.save()
        except IntegrityError:
            print('Agent already exists.')