From eaf0579e2f0af355681b17c219f1cce8319489c8 Mon Sep 17 00:00:00 2001 From: wh1te909 Date: Sun, 27 Oct 2019 04:48:03 +0000 Subject: [PATCH] optimize agent add/update methods --- .../migrations/0007_auto_20191027_0411.py | 49 ++++ api/djangormm/agents/models.py | 14 +- api/djangormm/api/urls.py | 2 + api/djangormm/api/views.py | 223 +++++++++--------- 4 files changed, 176 insertions(+), 112 deletions(-) create mode 100644 api/djangormm/agents/migrations/0007_auto_20191027_0411.py diff --git a/api/djangormm/agents/migrations/0007_auto_20191027_0411.py b/api/djangormm/agents/migrations/0007_auto_20191027_0411.py new file mode 100644 index 00000000..0f74325d --- /dev/null +++ b/api/djangormm/agents/migrations/0007_auto_20191027_0411.py @@ -0,0 +1,49 @@ +# Generated by Django 2.2.6 on 2019-10-27 04:11 + +import django.contrib.postgres.fields.jsonb +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('agents', '0006_agent_ping_check_interval'), + ] + + operations = [ + migrations.AlterField( + model_name='agent', + name='cpu_info', + field=django.contrib.postgres.fields.jsonb.JSONField(null=True), + ), + migrations.AlterField( + model_name='agent', + name='disks', + field=django.contrib.postgres.fields.jsonb.JSONField(null=True), + ), + migrations.AlterField( + model_name='agent', + name='local_ip', + field=models.TextField(null=True), + ), + migrations.AlterField( + model_name='agent', + name='logged_in_username', + field=models.CharField(max_length=200, null=True), + ), + migrations.AlterField( + model_name='agent', + name='operating_system', + field=models.CharField(max_length=255, null=True), + ), + migrations.AlterField( + model_name='agent', + name='public_ip', + field=models.CharField(max_length=100, null=True), + ), + migrations.AlterField( + model_name='agent', + name='services', + field=django.contrib.postgres.fields.jsonb.JSONField(null=True), + ), + ] diff --git a/api/djangormm/agents/models.py b/api/djangormm/agents/models.py index f7a5ecd8..56f0a414 100644 --- a/api/djangormm/agents/models.py +++ b/api/djangormm/agents/models.py @@ -13,22 +13,22 @@ from django.conf import settings from django.contrib.postgres.fields import JSONField class Agent(models.Model): - operating_system = models.CharField(max_length=255) + operating_system = models.CharField(null=True, max_length=255) plat = models.CharField(max_length=255, null=True) plat_release = models.CharField(max_length=255, null=True) hostname = models.CharField(max_length=255) - local_ip = models.TextField() + local_ip = models.TextField(null=True) agent_id = models.CharField(max_length=200) last_seen = models.DateTimeField(auto_now=True) - services = JSONField() - public_ip = models.CharField(max_length=100) + services = JSONField(null=True) + public_ip = models.CharField(null=True, max_length=100) cpu_load = models.FloatField(null=True) total_ram = models.IntegerField(null=True) used_ram = models.IntegerField(null=True) - disks = JSONField() + disks = JSONField(null=True) boot_time = models.FloatField(null=True) - logged_in_username = models.CharField(max_length=200) - cpu_info = JSONField() + logged_in_username = models.CharField(null=True, max_length=200) + cpu_info = JSONField(null=True) client = models.CharField(max_length=200) site = models.CharField(max_length=150) monitoring_type = models.CharField(max_length=30) diff --git a/api/djangormm/api/urls.py b/api/djangormm/api/urls.py index 9ca291cb..0bc97717 100644 --- a/api/djangormm/api/urls.py +++ b/api/djangormm/api/urls.py @@ -3,6 +3,8 @@ from . import views urlpatterns = [ path("hello/", views.hello), + path("update/", views.update), + path("add/", views.add), path("agentauth/", views.agent_auth), path("getmeshnodes/", views.get_mesh_nodes), path("token/", views.create_auth_token), diff --git a/api/djangormm/api/views.py b/api/djangormm/api/views.py index 460127fe..82a07e1a 100644 --- a/api/djangormm/api/views.py +++ b/api/djangormm/api/views.py @@ -236,132 +236,145 @@ def delete_agent(request): @api_view(["POST"]) @authentication_classes((TokenAuthentication,)) @permission_classes((IsAuthenticated,)) -def hello(request): +def add(request): data = request.data agent_id = data["agentid"] - os = data["operating_system"] hostname = data["hostname"] - local_ip = data["local_ip"] - services = data["services"] - public_ip = data["public_ip"] - cpu_load = data["cpu_load"] - total_ram = data["total_ram"] - used_ram = data["used_ram"] - disks = data["disks"] - boot_time = data["boot_time"] - logged_in_username = data["logged_in_username"] - cpu_info = data["cpu_info"] client = data["client"] site = data["site"] monitoring_type = data["monitoring_type"] description = data["description"] mesh_node_id = data["mesh_node_id"] - plat = data["platform"] - plat_release = data["platform_release"] - # if new agent, add to database + if not Agent.objects.filter(agent_id=agent_id).exists(): Agent( agent_id=agent_id, - operating_system=os, - plat=plat, - plat_release=plat_release, hostname=hostname, - local_ip=local_ip, - services=services, - public_ip=public_ip, - cpu_load=cpu_load, - total_ram=total_ram, - used_ram=used_ram, - disks=disks, - logged_in_username=logged_in_username, client=client, site=site, monitoring_type=monitoring_type, - cpu_info=cpu_info, description=description, mesh_node_id=mesh_node_id, ).save() - response = "added" agent = Agent.objects.get(agent_id=agent_id) - MemoryHistory(agent=agent, mem_history=[used_ram]).save() - CpuHistory(agent=agent, cpu_history=[cpu_load]).save() + MemoryHistory(agent=agent).save() + CpuHistory(agent=agent).save() + + return Response("ok") - # Wait for salt agent to start then run first check update and software checks - sync_salt_modules_task.delay(agent.pk) +@api_view(["PATCH"]) +@authentication_classes((TokenAuthentication,)) +@permission_classes((IsAuthenticated,)) +def update(request): + data = request.data + agent_id = data["agentid"] + hostname = data["hostname"] + os = data["operating_system"] + total_ram = data["total_ram"] + cpu_info = data["cpu_info"] + plat = data["platform"] + plat_release = data["platform_release"] + + agent = get_object_or_404(Agent, agent_id=agent_id) + + agent.hostname = hostname + agent.operating_system = os + agent.total_ram = total_ram + agent.cpu_info = cpu_info + agent.plat = plat + agent.plat_release = plat_release + + agent.save(update_fields=[ + "last_seen", + "hostname", + "operating_system", + "total_ram", + "cpu_info", + "plat", + "plat_release", + ]) + + sync_salt_modules_task.delay(agent.pk) + + return Response("ok") + + +@api_view(["PATCH"]) +@authentication_classes((TokenAuthentication,)) +@permission_classes((IsAuthenticated,)) +def hello(request): + data = request.data + agent_id = data["agentid"] + local_ip = data["local_ip"] + services = data["services"] + public_ip = data["public_ip"] + cpu_load = data["cpu_load"] + used_ram = data["used_ram"] + disks = data["disks"] + boot_time = data["boot_time"] + logged_in_username = data["logged_in_username"] + + agent = get_object_or_404(Agent, agent_id=agent_id) + + if agent.uninstall_pending: + if agent.uninstall_inprogress: + return Response("uninstallip") + else: + uninstall_agent_task.delay(agent.pk) + return Response("ok") + + + agent.local_ip = local_ip + agent.public_ip = public_ip + agent.services = services + agent.cpu_load = cpu_load + + agent.used_ram = used_ram + agent.disks = disks + agent.boot_time = boot_time + agent.logged_in_username = logged_in_username + + agent.save( + update_fields=[ + "last_seen", + "local_ip", + "public_ip", + "services", + "cpu_load", + "used_ram", + "disks", + "boot_time", + "logged_in_username", + ] + ) + + # create a list of the last 35 mem averages + mem = MemoryHistory.objects.get(agent=agent) + mem_list = mem.mem_history + + if len(mem_list) < 35: + mem_list.append(used_ram) + mem.mem_history = mem_list + mem.save(update_fields=["mem_history"]) else: - agent = Agent.objects.get(agent_id=agent_id) + mem_list.append(used_ram) + new_mem_list = mem_list[-35:] + mem.mem_history = new_mem_list + mem.save(update_fields=["mem_history"]) - if agent.uninstall_pending: - if agent.uninstall_inprogress: - return Response("uninstallip") - else: - uninstall_agent_task.delay(agent.pk) - return Response("ok") + # create list of the last 35 cpu load averages + cpu = CpuHistory.objects.get(agent=agent) + cpu_list = cpu.cpu_history - agent.hostname = hostname - agent.operating_system = os - agent.local_ip = local_ip - agent.public_ip = public_ip - agent.services = services - agent.cpu_load = cpu_load - agent.total_ram = total_ram - agent.used_ram = used_ram - agent.disks = disks - agent.boot_time = boot_time - agent.logged_in_username = logged_in_username - agent.cpu_info = cpu_info - agent.plat = plat - agent.plat_release = plat_release - - agent.save( - update_fields=[ - "last_seen", - "local_ip", - "public_ip", - "services", - "hostname", - "operating_system", - "cpu_load", - "total_ram", - "used_ram", - "disks", - "boot_time", - "logged_in_username", - "cpu_info", - "plat", - "plat_release", - ] - ) - - response = "ok" - - # create a list of the last 35 mem averages - mem = MemoryHistory.objects.get(agent=agent) - mem_list = mem.mem_history - - if len(mem_list) < 35: - mem_list.append(used_ram) - mem.mem_history = mem_list - mem.save(update_fields=["mem_history"]) - else: - mem_list.append(used_ram) - new_mem_list = mem_list[-35:] - mem.mem_history = new_mem_list - mem.save(update_fields=["mem_history"]) - - # create list of the last 35 cpu load averages - cpu = CpuHistory.objects.get(agent=agent) - cpu_list = cpu.cpu_history - - if len(cpu_list) < 35: - cpu_list.append(cpu_load) - cpu.cpu_history = cpu_list - cpu.save(update_fields=["cpu_history"]) - else: - cpu_list.append(cpu_load) - new_cpu_list = cpu_list[-35:] - cpu.cpu_history = new_cpu_list - cpu.save(update_fields=["cpu_history"]) - - return Response({"response": response}) \ No newline at end of file + if len(cpu_list) < 35: + cpu_list.append(cpu_load) + cpu.cpu_history = cpu_list + cpu.save(update_fields=["cpu_history"]) + else: + cpu_list.append(cpu_load) + new_cpu_list = cpu_list[-35:] + cpu.cpu_history = new_cpu_list + cpu.save(update_fields=["cpu_history"]) + + return Response("ok") \ No newline at end of file