def begin_notification(alert_alarm, alert_alarm_definition, user_event_notification):
    """
    Initiate the notification/escalation process for an alert or an alarm.
    Process:
    - Get alert_alarm
    - Get alert_alarm_definition
    - Get user_event_notification information
    * Create initial notification
    - if successful, then:
        alert_alarm.escalated =True;
        alert_alarms_escalated = dt.datetime.strftime(dt.datetime.now(), "%Y-%m-%dT%H:%M:%S")
        alert_alarm.ticket_id = ticket_id (id from redmine or zero (0) otherwise)
        persist escalation field changes with update_alert_alarm
    """
    result = None
    try:
        ticket_id = 0

        # If red mine enabled:
        if user_event_notification.use_redmine:
            ticket_id = begin_notification_redmine(alert_alarm, alert_alarm_definition, user_event_notification)

        # Update escalate fields.
        if ticket_id is not None:
            escalated = True
            ts_escalated = dt.datetime.strftime(dt.datetime.now(), "%Y-%m-%dT%H:%M:%S")
            SystemEvent.update_alert_alarm_escalation(id=alert_alarm.id, ticket_id=ticket_id,
                                                      escalated=escalated, ts_escalated=ts_escalated)
        return ticket_id

    except Exception as err:
        current_app.logger.info('[begin_notification] %s ' % err.message)
        return result
def reissue_notification(alert, definition, notification):
    """ Create another (re-issue notification) as a part of esclation process (for alerts only)
    """
    result = None
    try:
        ticket_id = 0

        # If red mine enabled:
        if notification.use_redmine:
            ticket_id = reissue_notification_redmine(alert, definition, notification)
            if ticket_id is None:
                message = 'Failed to reissue (create another new)_redmine_ticket.'
                current_app.logger.info('[reissue_notification] %s ' % message)
                return result
            # Prepare base exception message for update when using redmine for notification
            exception_message = 'Reissued redmine ticket (id:%d) but failed to update_alert_alarm_escalation; ' % ticket_id
        else:
            # Prepare base exception message for update when NOT using redmine for notification
            exception_message = 'Reissued notification but failed to update_alert_alarm_escalation; '

        # update escalate fields
        escalated = True
        ts_escalated = dt.datetime.strftime(dt.datetime.now(), "%Y-%m-%dT%H:%M:%S")
        try:
            SystemEvent.update_alert_alarm_escalation(id=alert.id, ticket_id=ticket_id,
                                                  escalated=escalated, ts_escalated=ts_escalated)
        except Exception as err:
            message = exception_message + str(err.message)
            current_app.logger.info('[reissue_notification] %s ' % message)
            return result

        result = ticket_id

    except Exception as err:
        current_app.logger.info('[reissue_notification] %s ' % err.message)
    finally:
        return result
def update_notification_redmine(alert, definition, notification):
    """ Update redmine ticket as a part of alert notification process.
    """
    result = None
    try:
        # Get alert ticket id
        ticket_id = alert.ticket_id

        # Populate required fields to update a redmine ticket
        project = current_app.config['REDMINE_PROJECT_ID']

        # Use user_event_notification for determining assigned user's redmine id
        redmine_id = 1
        assigned_user = User.query.get(notification.user_id)
        if assigned_user is not None:
            name = assigned_user.first_name + ' ' + assigned_user.last_name
            tmp_id = get_user_redmine_id(project, name)
            if tmp_id is not None:
                redmine_id = tmp_id
        else:
            message = 'Invalid User ID, User record not found.'
            current_app.logger.info(message)
            raise Exception(message)
        assigned_id = redmine_id

        ts_updated = dt.datetime.strftime(dt.datetime.now(), "%Y-%m-%dT%H:%M:%S")
        update_info = '\nUpdated: %s ' % ts_updated

        # If no previous ticket_id associated (case where 'use_redmine' was enabled AFter alert has escalated)
        if ticket_id == 0:

            # Create redmine ticket
            prefix = (alert.event_type).upper() + ': '
            subject = prefix + alert.event_response
            description = alert.event_response
            priority = definition.severity
            ticket_id = create_redmine_ticket_for_notification(project, subject, description, priority, redmine_id)
            if ticket_id is None:
                message = 'Failed to create base redmine ticket (on update) for alert notification (id:%d).' % alert.id
                current_app.logger.info(message)
                raise Exception(message)

            # Update escalate fields.
            if ticket_id is not None:
                escalated = True
                ts_escalated = dt.datetime.strftime(dt.datetime.now(), "%Y-%m-%dT%H:%M:%S")
                SystemEvent.update_alert_alarm_escalation(id=alert.id, ticket_id=ticket_id,
                                                          escalated=escalated, ts_escalated=ts_escalated)
        # Get existing redmine ticket
        redmine_ticket = get_redmine_ticket_for_notification(ticket_id)

        # Set fields for update
        project = redmine_ticket['project']
        subject = redmine_ticket['subject']
        description = redmine_ticket['description']
        priority = redmine_ticket['priority']
        if 'assigned_to' in redmine_ticket:
            assigned_id = redmine_ticket['assigned_to']

        # Update subject for recent receipt of alert (not past escalate boundary yet)
        description += update_info
        result = update_redmine_ticket_for_notification(ticket_id, project, subject, description, priority, assigned_id)
        if result is None:
            message = 'Failed to update redmine ticket (ticket_id: %d)' % ticket_id
            current_app.logger.info(message)
            raise Exception(message)

        return ticket_id

    except Exception as err:
        message = '[update_redmine_notification_ticket] %s ' % str(err.message)
        current_app.logger.info(message)
        return result
Example #4
0
def reissue_notification_ticket(id):
    """ Create another (re-issue) redmine ticket as a part of notification process (for alerts only)
    """
    debug = False  # development debug
    #log = False     # used in except blocks
    result = None
    try:
        if debug:
            print '\n \t(debug) entered reissue_notification_ticket, id: ', id

        # Get alert, definition and notification to update redmine ticket
        alert, definition, notification = get_redmine_info_from_alert(id)
        if alert is None or definition is None or notification is None:
            message = 'Failed to retrieve alert, definition or notification for update_notification_ticket. (id: %d)' % id
            current_app.logger.exception('[update_notification_ticket] %s ' %
                                         message)
            raise Exception(message)

        # Populate required fields to create a redmine ticket
        project = current_app.config['REDMINE_PROJECT_ID']

        if debug:
            print '\n \t(debug) -- Reissue redmine ticket (project: %s), get ticket_id...' % project
            print '\n \t(debug) -- Current ticket id: ', alert.ticket_id

        # Get current redmine ticket
        redmine_ticket = get_redmine_ticket_for_notification(alert.ticket_id)
        if debug:
            print '\n \t(debug) -- Current redmine ticket: ', redmine_ticket

        # Set assigned id to reflect update to user currently assigned to alert.ticket_id
        #assigned_id = redmine_ticket['assigned_to']
        if 'assigned_to' in redmine_ticket:
            assigned_id = redmine_ticket['assigned_to']
        else:
            # Use user_event_notification for determining assigned user's redmine id
            redmine_id = 1
            name = None
            assigned_user = User.query.get(notification.user_id)
            if assigned_user is not None:
                name = assigned_user.first_name + ' ' + assigned_user.last_name
                tmp_id = get_user_redmine_id(project, name)
                if tmp_id is not None:
                    redmine_id = tmp_id
            else:
                # todo issue - assigned_user is None
                message = "Invalid User ID, User record not found."
                #if log: print '\n message: ', message
                return bad_request(message)
            assigned_id = redmine_id

        # Update description to indicate previously issued ticket id for this alert.
        update_info = '\n* Associated with previously issued ticket: %d' % alert.ticket_id
        if debug:
            print '\n \t(debug) -- New reissued ticket update_info: ', update_info

        # Create new redmine ticket
        prefix = (alert.event_type).upper() + '*: '
        subject = prefix + alert.event_response
        description = alert.event_response + update_info
        priority = definition.severity
        if debug: print '\n \t(debug) New ticket subject: ', subject
        if debug: print '\n \t(debug) New ticket description: ', description

        ticket_id = create_redmine_ticket_for_notification(
            project, subject, description, priority, assigned_id)
        if ticket_id is None:
            message = 'Failed to reissue (create another new)_redmine_ticket.'
            current_app.logger.exception('[reissue_notification_ticket] %s ' %
                                         message)
            return result

        # update escalate fields if successful creating the redmine ticket
        escalated = True
        ts_escalated = dt.datetime.strftime(
            dt.datetime.now(),
            "%Y-%m-%dT%H:%M:%S")  # should this be event_time?
        SystemEvent.update_alert_alarm_escalation(id=alert.id,
                                                  ticket_id=ticket_id,
                                                  escalated=escalated,
                                                  ts_escalated=ts_escalated)

        if debug:
            print '\n \t(debug) reissue_notification_ticket -- updated alert_alarm: ', alert.to_json(
            )
        ticket_link_id = TicketSystemEventLink.insert_ticket_link(
            system_event_id=alert.id, ticket_id=ticket_id)
        if debug:
            print '\n \t(debug) -- (notifications) ticket_link_id: ', ticket_link_id
        '''
        # debug - view contents of alert_alarm escalation fields; verify changes have been persisted
        escalated_alert_alarm = SystemEvent.query.get(alert_alarm.id)
        print '\n (debug) *** escalated alert_alarm.to_json(): ', escalated_alert_alarm.to_json()
        '''

        result = ticket_id
        if debug:
            print '\n \treissue_notification_ticket - ticket_id: ', ticket_id

    except Exception as err:
        message = err.message
        current_app.logger.exception('[reissue_notification_ticket] %s ' %
                                     message)
    finally:
        return result
Example #5
0
def begin_notification_process(id):
    """
    Initiate the escalation process for an alert or an alarm (id provided)
    Process:
    - Get alert_alarm
    - Get alert_alarm_definition
    - Get user_event_notification information
    * Create initial [red mine] ticket, get ticket id
    - if successful creating ticket, then:
        alert_alarm.escalated =True;
        alert_alarms_escalated = dt.datetime.strftime(dt.datetime.now(), "%Y-%m-%dT%H:%M:%S")
        alert_alarm.ticket_id = ticket_id
        persist escalation field changes with update_alert_alarm
    """
    debug = False
    #log = False
    result = None
    try:
        # Get alert_alarm, alert_alarm_definition and user_event_notification to update redmine ticket
        alert_alarm, alert_alarm_definition, user_event_notification = get_redmine_info_from_alert(
            id)
        if alert_alarm is None or alert_alarm_definition is None or user_event_notification is None:
            message = 'Failed to retrieve alert, definition or notification for update_notification_ticket. (id: %d)' % id
            current_app.logger.exception('[update_notification_ticket] %s ' %
                                         message)
            raise Exception(message)

        # Populate required fields to create a redmine ticket
        project = current_app.config['REDMINE_PROJECT_ID']

        if debug:
            print '\n \t(debug) ---- Get targeted user for redmine assignment using user_event_notification...'
        # Use user_event_notification for determining assigned user's redmine id
        redmine_id = 1  # this assumes Redmine ADmin is assigned to redmine user id 1; should lookup 'Redmine Admin' todo
        name = None
        assigned_user = User.query.get(user_event_notification.user_id)
        if assigned_user is not None:
            name = assigned_user.first_name + ' ' + assigned_user.last_name
            tmp_id = get_user_redmine_id(project, name)
            if tmp_id is not None:
                redmine_id = tmp_id

        if debug:            print '\n \t(debug) -- Creating redmine ticket for \'%s\' (project: %s), get ticket_id...' % \
                  (name, project)

        # Create redmine ticket
        prefix = (alert_alarm.event_type).upper() + ': '
        subject = prefix + alert_alarm.event_response
        description = alert_alarm.event_response
        priority = alert_alarm_definition.severity
        ticket_id = create_redmine_ticket_for_notification(
            project, subject, description, priority, redmine_id)
        if ticket_id is None:
            message = 'Failed to create_redmine_ticket.'
            print '\n \t-- message: ', message
            current_app.logger.exception('[begin_notification_process] %s ' %
                                         message)
            return result

        # If successful creating redmine ticket, update escalate fields
        escalated = True
        ts_escalated = dt.datetime.strftime(
            dt.datetime.now(),
            "%Y-%m-%dT%H:%M:%S")  # should this be event_time?
        SystemEvent.update_alert_alarm_escalation(id=alert_alarm.id,
                                                  ticket_id=ticket_id,
                                                  escalated=escalated,
                                                  ts_escalated=ts_escalated)

        if debug:
            print '\n \t(debug) -- updated alert_alarm: ', alert_alarm.to_json(
            )
        ticket_link_id = TicketSystemEventLink.insert_ticket_link(
            system_event_id=alert_alarm.id, ticket_id=ticket_id)
        if debug:
            print '\n \t(debug) -- [begin_notification_process] \'%s\' has been escalated!' % alert_alarm.event_type
        if debug:
            print '\n \tbegin_notification_process - ticket_id: ', ticket_id
        result = ticket_id

    except Exception as err:
        current_app.logger.exception('[begin_notification_process] %s ' %
                                     err.message)
    finally:
        return result
Example #6
0
def reissue_notification_ticket(id):
    """ Create another (re-issue) redmine ticket as a part of notification process (for alerts only)
    """
    debug = False   # development debug
    #log = False     # used in except blocks
    result = None
    try:
        if debug: print '\n \t(debug) entered reissue_notification_ticket, id: ', id

        # Get alert, definition and notification to update redmine ticket
        alert, definition, notification = get_redmine_info_from_alert(id)
        if alert is None or definition is None or notification is None:
            message = 'Failed to retrieve alert, definition or notification for update_notification_ticket. (id: %d)' % id
            current_app.logger.exception('[update_notification_ticket] %s ' % message)
            raise Exception(message)

        # Populate required fields to create a redmine ticket
        project = current_app.config['REDMINE_PROJECT_ID']

        if debug:
            print '\n \t(debug) -- Reissue redmine ticket (project: %s), get ticket_id...' % project
            print'\n \t(debug) -- Current ticket id: ', alert.ticket_id

        # Get current redmine ticket
        redmine_ticket = get_redmine_ticket_for_notification(alert.ticket_id)
        if debug: print '\n \t(debug) -- Current redmine ticket: ', redmine_ticket

        # Set assigned id to reflect update to user currently assigned to alert.ticket_id
        #assigned_id = redmine_ticket['assigned_to']
        if 'assigned_to' in redmine_ticket:
            assigned_id = redmine_ticket['assigned_to']
        else:
            # Use user_event_notification for determining assigned user's redmine id
            redmine_id = 1
            name = None
            assigned_user = User.query.get(notification.user_id)
            if assigned_user is not None:
                name = assigned_user.first_name + ' ' + assigned_user.last_name
                tmp_id = get_user_redmine_id(project, name)
                if tmp_id is not None:
                    redmine_id = tmp_id
            else:
                # todo issue - assigned_user is None
                message = "Invalid User ID, User record not found."
                #if log: print '\n message: ', message
                return bad_request(message)
            assigned_id = redmine_id

        # Update description to indicate previously issued ticket id for this alert.
        update_info = '\n* Associated with previously issued ticket: %d' % alert.ticket_id
        if debug: print '\n \t(debug) -- New reissued ticket update_info: ', update_info

        # Create new redmine ticket
        prefix = (alert.event_type).upper() + '*: '
        subject = prefix + alert.event_response
        description = alert.event_response + update_info
        priority = definition.severity
        if debug: print '\n \t(debug) New ticket subject: ', subject
        if debug: print '\n \t(debug) New ticket description: ', description

        ticket_id = create_redmine_ticket_for_notification(project, subject, description, priority, assigned_id)
        if ticket_id is None:
            message = 'Failed to reissue (create another new)_redmine_ticket.'
            current_app.logger.exception('[reissue_notification_ticket] %s ' % message)
            return result

        # update escalate fields if successful creating the redmine ticket
        escalated = True
        ts_escalated = dt.datetime.strftime(dt.datetime.now(), "%Y-%m-%dT%H:%M:%S") # should this be event_time?
        SystemEvent.update_alert_alarm_escalation(id=alert.id, ticket_id=ticket_id,
                                                  escalated=escalated, ts_escalated=ts_escalated)

        if debug: print '\n \t(debug) reissue_notification_ticket -- updated alert_alarm: ', alert.to_json()
        ticket_link_id = TicketSystemEventLink.insert_ticket_link(system_event_id=alert.id, ticket_id=ticket_id)
        if debug: print '\n \t(debug) -- (notifications) ticket_link_id: ', ticket_link_id
        '''
        # debug - view contents of alert_alarm escalation fields; verify changes have been persisted
        escalated_alert_alarm = SystemEvent.query.get(alert_alarm.id)
        print '\n (debug) *** escalated alert_alarm.to_json(): ', escalated_alert_alarm.to_json()
        '''

        result = ticket_id
        if debug: print '\n \treissue_notification_ticket - ticket_id: ', ticket_id

    except Exception as err:
        message = err.message
        current_app.logger.exception('[reissue_notification_ticket] %s ' % message)
    finally:
        return result
Example #7
0
def begin_notification_process(id):
    """
    Initiate the escalation process for an alert or an alarm (id provided)
    Process:
    - Get alert_alarm
    - Get alert_alarm_definition
    - Get user_event_notification information
    * Create initial [red mine] ticket, get ticket id
    - if successful creating ticket, then:
        alert_alarm.escalated =True;
        alert_alarms_escalated = dt.datetime.strftime(dt.datetime.now(), "%Y-%m-%dT%H:%M:%S")
        alert_alarm.ticket_id = ticket_id
        persist escalation field changes with update_alert_alarm
    """
    debug = False
    #log = False
    result = None
    try:
        # Get alert_alarm, alert_alarm_definition and user_event_notification to update redmine ticket
        alert_alarm, alert_alarm_definition, user_event_notification = get_redmine_info_from_alert(id)
        if alert_alarm is None or alert_alarm_definition is None or user_event_notification is None:
            message = 'Failed to retrieve alert, definition or notification for update_notification_ticket. (id: %d)' % id
            current_app.logger.exception('[update_notification_ticket] %s ' % message)
            raise Exception(message)

        # Populate required fields to create a redmine ticket
        project = current_app.config['REDMINE_PROJECT_ID']

        if debug: print '\n \t(debug) ---- Get targeted user for redmine assignment using user_event_notification...'
        # Use user_event_notification for determining assigned user's redmine id
        redmine_id = 1  # this assumes Redmine ADmin is assigned to redmine user id 1; should lookup 'Redmine Admin' todo
        name = None
        assigned_user = User.query.get(user_event_notification.user_id)
        if assigned_user is not None:
            name = assigned_user.first_name + ' ' + assigned_user.last_name
            tmp_id = get_user_redmine_id(project, name)
            if tmp_id is not None:
                redmine_id = tmp_id

        if debug: print '\n \t(debug) -- Creating redmine ticket for \'%s\' (project: %s), get ticket_id...' % \
                        (name, project)

        # Create redmine ticket
        prefix = (alert_alarm.event_type).upper() + ': '
        subject = prefix + alert_alarm.event_response
        description = alert_alarm.event_response
        priority = alert_alarm_definition.severity
        ticket_id = create_redmine_ticket_for_notification(project, subject, description, priority, redmine_id)
        if ticket_id is None:
            message = 'Failed to create_redmine_ticket.'
            print'\n \t-- message: ', message
            current_app.logger.exception('[begin_notification_process] %s ' % message)
            return result

        # If successful creating redmine ticket, update escalate fields
        escalated = True
        ts_escalated = dt.datetime.strftime(dt.datetime.now(), "%Y-%m-%dT%H:%M:%S") # should this be event_time?
        SystemEvent.update_alert_alarm_escalation(id=alert_alarm.id, ticket_id=ticket_id,
                                                  escalated=escalated, ts_escalated=ts_escalated)

        if debug: print '\n \t(debug) -- updated alert_alarm: ', alert_alarm.to_json()
        ticket_link_id = TicketSystemEventLink.insert_ticket_link(system_event_id=alert_alarm.id, ticket_id=ticket_id)
        if debug: print '\n \t(debug) -- [begin_notification_process] \'%s\' has been escalated!' % alert_alarm.event_type
        if debug: print '\n \tbegin_notification_process - ticket_id: ', ticket_id
        result = ticket_id

    except Exception as err:
        current_app.logger.exception('[begin_notification_process] %s ' % err.message)
    finally:
        return result