def _get_users(self, sessionKey, query_params): logger.debug("START _get_users()") users = AlertManagerUsers(sessionKey=sessionKey) user_list = users.getUserList() logger.debug("user_list: %s " % json.dumps(user_list)) return self.response(user_list, httplib.OK)
def get_users(self, **kwargs): logger.info("Get users") user = cherrypy.session['user']['name'] sessionKey = cherrypy.session.get('sessionKey') users = AlertManagerUsers(sessionKey=sessionKey) user_list = users.getUserList() logger.debug("user_list: %s " % json.dumps(user_list)) return json.dumps(user_list)
def handleEvent(self, event, alert, incident, context): notificationSchemeName = self.getNotificationSchemeName(alert) notificationScheme = NotificationScheme(self.sessionKey, notificationSchemeName) notifications = notificationScheme.getNotifications(event) if len(notifications) > 0: for notification in notifications: # Parse template template_match = re.match("^\$(.*)\$$", notification["template"]) if bool(template_match): self.log.debug( "Template (%s) references to a field name, starting to parse" % notification["template"]) field_name = template_match.group(1) if "result" in context and len( context["result"] ) > 0 and field_name in context["result"][0]: notification["template"] = context["result"][0][ field_name] self.log.debug( "%s found in result. Parsed value %s as template name." % (field_name, notification["template"])) else: self.log.warn( "Field %s not found in results. Won't send a notification." % field_name) # Parse sender if notification["sender"] == "default_sender": notification["sender"] = self.default_sender # Parse recipients recipients = [] recipients_cc = [] recipients_bcc = [] for recipient in notification["recipients"]: recipient_ok = True # Parse recipient mode if ":" in recipient: search = re.search("(mailto|mailcc|mailbcc)\:(.+)", recipient) mode = search.group(1) recipient = search.group(2) else: mode = "mailto" # Parse recipient string if recipient == "current_owner": users = AlertManagerUsers(sessionKey=self.sessionKey) user = users.getUser(incident["owner"]) if incident["owner"] != "unassigned" and user[ "notify_user"]: recipient = user["email"] else: self.log.info( "Can't send a notification to 'unassigned' or a user who is configured to not receive notifications. alert=%s owner=%s event=%s" % (alert, incident["owner"], event)) recipient_ok = False else: # Check if recipient is a crosslink to a result field and parse field_recipient = re.search("\$(.+)\$", recipient) if field_recipient != None: field_name = field_recipient.group(1) self.log.debug( "Should use a recipient from results. field: %s." % field_name) if "result" in context and len( context["result"] ) > 0 and field_name in context["result"][0]: recipient = context["result"][0][field_name] self.log.debug( "%s found in result. Parsed value %s." % (field_name, recipient)) else: self.log.warn( "Field %s not found in results. Won't send a notification." % field_name) recipient_ok = False if recipient_ok: if mode == "mailto": if isinstance(recipient, list): recipients = recipients + recipient else: recipients.append(recipient) elif mode == "mailcc": if isinstance(recipient, list): recipients_cc = recipients_cc + recipient else: recipients_cc.append(recipient) elif mode == "mailbcc": if isinstance(recipient, list): recipients_bcc = recipients_bcc + recipient else: recipients_bcc.append(recipient) if len(recipients) > 0 or len(recipients_cc) > 0 or len( recipients_bcc) > 0: self.log.info( "Prepared notification. event=%s, alert=%s, template=%s, sender=%s, recipients=%s, recipients_cc=%s, recipients_bcc=%s" % (event, alert, notification["template"], notification["sender"], recipients, recipients_cc, recipients_bcc)) self.send_notification(event, alert, notification["template"], notification["sender"], recipients, recipients_cc, recipients_bcc, context) else: self.log.info( "Done parsing notifications but will stop here since no recipients are present." ) return True
def handleEvent(self, event, alert, incident, context): notificationSchemeName = self.getNotificationSchemeName(alert) notificationScheme = NotificationScheme(self.sessionKey, notificationSchemeName) notifications = notificationScheme.getNotifications(event) if len(notifications) > 0: for notification in notifications: # Parse template template_match = re.search("^\$(.+)\.(.+)\$$", notification["template"]) if template_match != None: result_type = template_match.group(1) field_name = template_match.group(2) self.log.debug("Template (%s) references to a field name, starting to parse" % notification["template"] ) if result_type == 'result' and "result" in context and field_name in context["result"]: notification["template"] = context["result"][field_name] self.log.debug("%s found in result. Parsed value %s as template name." % (field_name, notification["template"])) else: self.log.warn("Field %s not found in '%s'. Won't send a notification." % (field_name, result_type)) # Parse sender if notification["sender"] == "default_sender": notification["sender"] = self.default_sender # Parse recipients recipients = [] recipients_cc = [] recipients_bcc = [] for recipient in notification["recipients"]: recipient_ok = True # Parse recipient mode if ":" in recipient: search = re.search("(mailto|mailcc|mailbcc)\:(.+)", recipient) mode = search.group(1) recipient = search.group(2) else: mode = "mailto" # Parse recipient string if recipient == "current_owner": users = AlertManagerUsers(sessionKey=self.sessionKey) user = users.getUser(incident["owner"]) if incident["owner"] != "unassigned": recipient = user["email"] else: self.log.info("Can't send a notification to 'unassigned' or a user who is configured to not receive notifications. alert=%s owner=%s event=%s" % (alert, incident["owner"], event)) recipient_ok = False else: # Check if recipient is a crosslink to a result field and parse field_recipient = re.search("\$(.+)\.(.+)\$", recipient) if field_recipient != None: result_type = field_recipient.group(1) field_name = field_recipient.group(2) self.log.debug("Should use a recipient from array '%s'. field: %s." % (result_type, field_name)) if result_type == 'result' and "result" in context and field_name in context["result"]: recipient = context["result"][field_name].split(",") self.log.debug("%s found in result. Parsed value %s." % (field_name, recipient)) else: self.log.warn("Field %s not found in '%s'. Won't send a notification." % (field_name, result_type)) recipient_ok = False if recipient_ok: if mode == "mailto": if isinstance(recipient, list): recipients = recipients + recipient else: recipients.append(recipient) elif mode == "mailcc": if isinstance(recipient, list): recipients_cc = recipients_cc + recipient else: recipients_cc.append(recipient) elif mode == "mailbcc": if isinstance(recipient, list): recipients_bcc = recipients_bcc + recipient else: recipients_bcc.append(recipient) if len(recipients) > 0 or len(recipients_cc) > 0 or len(recipients_bcc) > 0: self.log.info("Prepared notification. event=%s, alert=%s, template=%s, sender=%s, recipients=%s, recipients_cc=%s, recipients_bcc=%s" % (event, alert, notification["template"], notification["sender"], recipients, recipients_cc, recipients_bcc)) self.send_notification(event, alert, notification["template"], notification["sender"], recipients, recipients_cc, recipients_bcc, context) else: self.log.info("Done parsing notifications but will stop here since no recipients are present.") return True
args_stdout = "sessionKey:%s" % sessionKeyOrig + "\n" args_stdout = args_stdout + "namespace:%s" % alert_app + "\n" log.debug("stdout args for %s: %s" % (incident_config['alert_script'], args_stdout)) log.debug("args for %s: %s" % (incident_config['alert_script'], args)) try: p = Popen(args, stdin=PIPE, stdout=PIPE, stderr=PIPE, shell=False) output = p.communicate(input=args_stdout) log.debug("Alert script run finished. RC=%s. Output: %s" % (p.returncode, output[0])) except OSError, e: log.debug("Alert script failed. Error: %s" % str(e)) log.info("Creating incident for job_id=%s" % job_id) users = AlertManagerUsers(sessionKey=sessionKey) user_list = users.getUserList() notifier = AlertManagerNotifications(sessionKey=sessionKey) ############################### # Incident creation starts here ##################################################################### # BEGIN CUSTOM DEVELOPMENT SWISSCOM 2015-11-02 # # The following code has been moved to lines 422 - 426 # # # Create unique id # incident_id = str(uuid.uuid4()) # # # Get results
# Auto assign owner = '' status = 'new' auto_assgined = False if alert_config['auto_assign'] and alert_config[ 'auto_assign_owner'] != 'unassigned': entry['owner'] = alert_config['auto_assign_owner'] owner = alert_config['auto_assign_owner'] log.info("Assigning incident to %s" % alert_config['auto_assign_owner']) auto_assgined = True status = 'auto_assigned' # Send notification users = AlertManagerUsers(sessionKey=sessionKey) user_list = users.getUserList() log.debug("User list: %s" % user_list) user = {} user_found = False for item in user_list: if item["name"] == alert_config['auto_assign_owner']: user = item user_found = True if user_found: log.debug("Got user settings for user %s" % alert_config['auto_assign_owner']) if user['notify_user'] != False or user['email'] == "": log.info(