def test_escalate_event(self): """ Tests if a created event is escalated successfully """ state = mixer.blend('base.State', name='Active') mixer.blend('base.state', name="Investigating") system = mixer.blend('core.System', state=state) interface = mixer.blend('core.Interface', system=system, state=state) event_type = mixer.blend('base.EventType', state=state, name='Critical') escalation_level = mixer.blend("base.EscalationLevel", state=state, name='High') mixer.blend('base.IncidentType', state=state, name="Realtime") mixer.blend("core.EscalationRule", system=system, event_type=event_type, nth_event=1, duration=timedelta(hours=2), escalation_level=escalation_level) event = mixer.blend('core.Event', event_type=event_type, system=system, interface=interface, state=state, description='description', code='123') event_escalation = EventLog().escalate_event(event) assert event_escalation.get( 'code') == '800.200.001', "Should escalate event successfully"
def test_create(self): """ Tests create event method """ state = mixer.blend('base.State', name="Active") system = mixer.blend('core.System', state=state) interface = mixer.blend('core.Interface', system=system, state=state) event_type = mixer.blend('base.EventType', state=state) event = EventLog.log_event(system=system.id, event_type=event_type.name, description="Test Event description", interface=interface.name, state=state.name, code="404") failing_event = EventLog.log_event( system='system', event_type=event_type.name, description="Test Event description", interface='interface', state=state.name, code="500") assert event.get( 'code') == '800.200.001', "Should create an event successfully" assert failing_event.get( 'code') != '800.200.001', "Should fail during event creation"
def get_event(request): """ Get an event from a system @param request: The Django WSGI Request to process @type request: WSGIRequest @return: The requested recipient or a status code indicating errors if any. @rtype: dict """ try: data = get_request_data(request) event = EventLog.get_event( event_id = data.get('event_id'), system_id = data.get('system_id') ) return JsonResponse(event) except Exception as ex: lgr.exception('Get Event Exception: %s' % ex) return JsonResponse({'code': '800.500.001'})
def report_event(request): """ Creates an event reported from an external system @param request: The Django WSGI Request to process @type request: WSGIRequest @return: A response code to indicate successful event creation or otherwise @rtype: dict """ try: data = get_request_data(request) event = EventLog.log_event( event_type = data.get('event_type'), system = data.get('system_id'), interface = data.get('interface'), response = data.get('response'), request = data.get('request'), code = data.get('code'), description = data.get('description'), stack_trace = data.get('stack_trace'), method = data.get('method') ) return JsonResponse(event) except Exception as ex: lgr.exception("Event logging Exception: %s" % ex) return JsonResponse({'code': '800.500.001'})
def get_incident_events(incident_id, system_id, **kwargs): """ Retrieves the events that have caused the incident in a selected system. @param incident_id: The id of the incident @type incident_id: str @param system_id: System where the incident is created in @type system_id: str @param kwargs: Extra key-value arguments to pass for incident_event retrieval @return: Response code dictionary to indicate if the incident_events were retrieved or not @rtype: dict """ from api.backend.interfaces.event_administration import EventLog try: system = SystemService().filter(pk=system_id, state__name='Active').first() incident = IncidentService().filter(pk=incident_id, system=system).first() if system is None or incident is None: return {"code": "800.400.002"} incident_events = list(IncidentEventService().filter( incident=incident, state__name='Active').values( incident_id=F('incident'), status=F('state__name'), event_id=F('event')).order_by('-date_created')) events = [] for incident_event in incident_events: event = EventLog.get_event(incident_event.get('event_id'), system.id) if event.get('code') != '800.200.001': lgr.error('Event get Failed') else: incident_event.update(incident_event=event.get('data')) # incident_event.update(code = event.get('code')) events.append(incident_event) return {'code': '800.200.001', 'data': events} except Exception as ex: lgr.exception("Get Incident Event exception %s" % ex) return {"code": "800.400.001"}
def perform_health_check(): """ This method formats system data and logs system status to system monitor model @return: Systems: a dictionary containing a success code and a list of dictionaries containing system status data @rtype:dict """ systems = [] try: for endpoint in EndpointService().filter( system__state__name="Active", endpoint_type__is_queried=True): try: health_state = requests.get(endpoint.url) monitor_data = { 'system': endpoint.system.name, 'endpoint': endpoint.name, 'response_body': health_state.content, 'response_code': health_state.status_code, 'state': StateService().get(name='Operational').name, } if health_state.status_code == 200: if health_state.elapsed > endpoint.optimal_response_time: monitor_data.update({ "response_time_speed": 'Slow', "event_type": EventTypeService().get(name='Warning'), "description": 'Response time is not within the expected time', "state": StateService().get( name='Degraded Performance'), "response_time": health_state.elapsed.total_seconds() }) else: monitor_data.update({ 'response_time_speed': 'Normal', "response_time": health_state.elapsed.total_seconds() }) else: monitor_data.update({ "response_time_speed": None, "event_type": EventTypeService().get(name='Critical'), "description": 'The system is not accessible', "state": StateService().get(name='Major Outage') }) system_status = SystemMonitorService().create( system=SystemService().get( name=monitor_data.get('system')), response_time=timedelta( seconds=int(monitor_data.get('response_time'))), response_time_speed=monitor_data.get( "response_time_speed"), state=StateService().get( name=monitor_data.get('state')), response_body=monitor_data.get("response_body"), endpoint=EndpointService().get( name=monitor_data.get("endpoint")), response_code=monitor_data.get("response_code")) if system_status is not None: systems.append({ "system": system_status.system.name, "status": system_status.state.name, "endpoint": endpoint.url }) else: systems.append({ "system": system_status.system, "status": "failed", "endpoint": endpoint }) if monitor_data.get("event_type") is not None: event = EventLog.log_event( event_type=monitor_data.get("event_type").name, system=monitor_data.get("system"), description=monitor_data.get("description"), response=monitor_data.get('response'), request=health_state.request) if event['code'] != "800.200.001": lgr.warning("Event creation failed %s" % event) except requests.ConnectionError as e: lgr.exception('Endpoint health check failed: %s' % e) return {"code": "800.200.001", "data": {"systems": systems}} except Exception as ex: lgr.exception("Health Status exception: %s" % ex) return { "code": "800.400.001", "message": "Error while performing health check" }