def __init__(self, settings): """ @param settings: the payload from the alert action script call """ self.logger = setup_logging(XM_ALERT_ACTION_LOG, 'xmatters.alert_actions') self.settings = settings self.config = settings.get('configuration') self.endpoint_url = self.config.get('endpoint_url') self.username = self.config.get('xMuser') self.recipients = self.config.get('recipients') self.session_key = settings.get('session_key')
xm_event.set_priority(self.config.get('priority')) request_id = xm_client.send_event(self.endpoint_url, xm_event) if request_id is False: self.logger.error( 'An error occurred while sending request to xMatters. See %s for details.', XM_ALERT_ACTION_LOG) raise Exception( 'Failed to execute one or more send event actions.') return request_id if __name__ == "__main__": try: LOGGER = setup_logging(XM_ALERT_ACTION_LOG, 'xmatters.alert_action.main') if len(sys.argv) > 1 and sys.argv[1] == "--execute": try: # Capture the input, which is a big JSON object # We stick it in a variable so we can print it if needed SYSTEM_IN = sys.stdin.read() INCOMING = json.loads(SYSTEM_IN) LOGGER.debug('Incoming %s', SYSTEM_IN) XM_ALERT = XMattersAlert(INCOMING) REQUEST_ID = XM_ALERT.execute() if REQUEST_ID: LOGGER.info( 'action=ALERT_EXECUTE success=true request_id=%s', REQUEST_ID) else:
""" Includes the XMattersEvent class which wraps the xMatters Event to make it easier to use correct formatting """ import json # pylint: disable = import-error from common_utils.setup_logging import setup_logging # pylint: enable = import-error DEFAULT_LOGGER = setup_logging('xmatters_alert_action.log', 'xmatters_event') class XMattersEvent(object): """ Class that wraps an xMatters Event so that it is easier to use correct formatting """ def __init__(self, **kwargs): """ Constructor, takes no arguments """ self.logger = kwargs.get('logger', DEFAULT_LOGGER) self.properties = {} self.recipients = [] self.priority = None self.valid_priorities = ['HIGH', 'MEDIUM', 'LOW'] def add_property(self, key, value): """ Adds a property to the event