def hello(request): agent = get_object_or_404(Agent, agent_id=request.data["agent_id"]) serializer = WinAgentSerializer(instance=agent, data=request.data, partial=True) serializer.is_valid(raise_exception=True) serializer.save(last_seen=djangotime.now()) outages = AgentOutage.objects.filter(agent=agent) if outages.exists() and outages.last().is_active: last_outage = outages.last() last_outage.recovery_time = djangotime.now() last_outage.save(update_fields=["recovery_time"]) if agent.overdue_email_alert: agent_recovery_email_task.delay(pk=last_outage.pk) if agent.overdue_text_alert: # TODO pass recovery = agent.recoveryactions.filter(last_run=None).last() if recovery is not None: recovery.last_run = djangotime.now() recovery.save(update_fields=["last_run"]) return Response(recovery.send()) # get any pending actions if agent.pendingactions.filter(status="pending").count() > 0: agent.handle_pending_actions() return Response("ok")
def patch(self, request): agent = get_object_or_404(Agent, agent_id=request.data["agent_id"]) agent.version = request.data["version"] agent.last_seen = djangotime.now() agent.save(update_fields=["version", "last_seen"]) if agent.agentoutages.exists() and agent.agentoutages.last().is_active: last_outage = agent.agentoutages.last() last_outage.recovery_time = djangotime.now() last_outage.save(update_fields=["recovery_time"]) if agent.overdue_email_alert: agent_recovery_email_task.delay(pk=last_outage.pk) if agent.overdue_text_alert: agent_recovery_sms_task.delay(pk=last_outage.pk) recovery = agent.recoveryactions.filter(last_run=None).last() if recovery is not None: recovery.last_run = djangotime.now() recovery.save(update_fields=["last_run"]) handle_agent_recovery_task.delay(pk=recovery.pk) return Response("ok") # get any pending actions if agent.pendingactions.filter(status="pending").exists(): agent.handle_pending_actions() return Response("ok")
def patch(self, request): agent = get_object_or_404(Agent, agent_id=request.data["agent_id"]) serializer = WinAgentSerializer(instance=agent, data=request.data, partial=True) serializer.is_valid(raise_exception=True) disks = request.data["disks"] new = [] # python agent if isinstance(disks, dict): for k, v in disks.items(): new.append(v) else: # golang agent for disk in disks: tmp = {} for k, v in disk.items(): tmp["device"] = disk["device"] tmp["fstype"] = disk["fstype"] tmp["total"] = bytes2human(disk["total"]) tmp["used"] = bytes2human(disk["used"]) tmp["free"] = bytes2human(disk["free"]) tmp["percent"] = int(disk["percent"]) new.append(tmp) if request.data["logged_in_username"] == "None": serializer.save(last_seen=djangotime.now(), disks=new) else: serializer.save( last_seen=djangotime.now(), disks=new, last_logged_in_user=request.data["logged_in_username"], ) if agent.agentoutages.exists() and agent.agentoutages.last().is_active: last_outage = agent.agentoutages.last() last_outage.recovery_time = djangotime.now() last_outage.save(update_fields=["recovery_time"]) if agent.overdue_email_alert: agent_recovery_email_task.delay(pk=last_outage.pk) if agent.overdue_text_alert: agent_recovery_sms_task.delay(pk=last_outage.pk) recovery = agent.recoveryactions.filter(last_run=None).last() if recovery is not None: recovery.last_run = djangotime.now() recovery.save(update_fields=["last_run"]) return Response(recovery.send()) # get any pending actions if agent.pendingactions.filter(status="pending").exists(): agent.handle_pending_actions() return Response("ok")
def patch(self, request): updated = False agent = get_object_or_404(Agent, agent_id=request.data["agent_id"]) if pyver.parse(request.data["version"]) > pyver.parse( agent.version) or pyver.parse( request.data["version"]) == pyver.parse( settings.LATEST_AGENT_VER): updated = True agent.version = request.data["version"] agent.last_seen = djangotime.now() agent.save(update_fields=["version", "last_seen"]) # change agent update pending status to completed if agent has just updated if (updated and agent.pendingactions.filter(action_type="agentupdate", status="pending").exists()): agent.pendingactions.filter( action_type="agentupdate", status="pending").update(status="completed") if agent.agentoutages.exists() and agent.agentoutages.last().is_active: last_outage = agent.agentoutages.last() last_outage.recovery_time = djangotime.now() last_outage.save(update_fields=["recovery_time"]) if agent.overdue_email_alert: agent_recovery_email_task.delay(pk=last_outage.pk) if agent.overdue_text_alert: agent_recovery_sms_task.delay(pk=last_outage.pk) recovery = agent.recoveryactions.filter(last_run=None).last() if recovery is not None: recovery.last_run = djangotime.now() recovery.save(update_fields=["last_run"]) handle_agent_recovery_task.delay(pk=recovery.pk) return Response("ok") # get any pending actions if agent.pendingactions.filter(status="pending").exists(): agent.handle_pending_actions() return Response("ok")
def hello(request): agent = get_object_or_404(Agent, agent_id=request.data["agent_id"]) serializer = WinAgentSerializer(instance=agent, data=request.data, partial=True) serializer.is_valid(raise_exception=True) serializer.save(last_seen=djangotime.now()) outages = AgentOutage.objects.filter(agent=agent) if outages.exists() and outages.last().is_active: last_outage = outages.last() last_outage.recovery_time = djangotime.now() last_outage.save(update_fields=["recovery_time"]) if agent.overdue_email_alert: agent_recovery_email_task.delay(pk=last_outage.pk) if agent.overdue_text_alert: # TODO pass return Response("ok")