optimize agent add/update methods

This commit is contained in:
wh1te909 2019-10-27 04:48:03 +00:00
parent c8da32d2da
commit eaf0579e2f
4 changed files with 176 additions and 112 deletions

View File

@ -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),
),
]

View File

@ -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)

View File

@ -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),

View File

@ -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})
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")