+ def update_alarm_configuration(self, new_alarm_config):
+ """Update alarm configuration (i.e. Symptom & alarm) as per request
+ """
+ #1) Get Alarm details from it's uuid & find the symptom defination
+ alarm_details_json, alarm_details = self.get_alarm_defination_details(new_alarm_config['alarm_uuid'])
+ if alarm_details_json is None:
+ return None
+
+ try:
+ #2) Update the symptom defination
+ if alarm_details['alarm_id'] is not None and alarm_details['symptom_definition_id'] is not None:
+ symptom_defination_id = alarm_details['symptom_definition_id']
+ else:
+ log.info("Symptom Defination ID not found for {}".format(new_alarm_config['alarm_uuid']))
+ return None
+
+ symptom_uuid = self.update_symptom_defination(symptom_defination_id, new_alarm_config)
+
+ #3) Update the alarm defination & Return UUID if successful update
+ if symptom_uuid is None:
+ return None
+ else:
+ alarm_uuid = self.reconfigure_alarm(alarm_details_json, new_alarm_config)
+ if alarm_uuid is None:
+ return None
+ else:
+ return alarm_uuid
+ except:
+ log.error("Exception while updating alarm: {}".format(traceback.format_exc()))
+
+ def get_alarm_defination_details(self, alarm_uuid):
+ """Get alarm details based on alarm UUID
+ """
+ if alarm_uuid is None:
+ log.warn("get_alarm_defination_details: Alarm UUID not provided")
+ return None
+
+ alarm_details = {}
+ json_data = {}
+ api_url = '/suite-api/api/alertdefinitions/AlertDefinition-'
+ headers = {'Accept': 'application/json'}
+
+ resp = requests.get(self.vrops_site + api_url + alarm_uuid,
+ auth=(self.vrops_user, self.vrops_password),
+ verify = False, headers = headers)
+
+ if resp.status_code is not 200:
+ log.warn("Failed to get alarm details from vROPs for {}\nResponse code:{}\nResponse Content: {}"\
+ .format(alarm_uuid, resp.status_code, resp.content))
+ return None
+
+ try:
+ json_data = json.loads(resp.content)
+ if json_data['id'] is not None:
+ alarm_details['alarm_id'] = json_data['id']
+ alarm_details['alarm_name'] = json_data['name']
+ alarm_details['adapter_kind'] = json_data['adapterKindKey']
+ alarm_details['resource_kind'] = json_data['resourceKindKey']
+ alarm_details['type'] = ['type']
+ alarm_details['sub_type'] = json_data['subType']
+ alarm_details['symptom_definition_id'] = json_data['states'][0]['base-symptom-set']['symptomDefinitionIds'][0]
+ except exception as exp:
+ log.warn("Exception while retriving alarm defination details: {}".format(exp))
+
+ return json_data, alarm_details
+
+
+ def update_symptom_defination(self, symptom_uuid, new_alarm_config):
+ """Update symptom defination based on new alarm input configuration
+ """
+ #1) Get symptom defination details
+ symptom_details = self.get_symptom_defination_details(symptom_uuid)
+ #print "\n\nsymptom_details: {}".format(symptom_details)
+ if symptom_details is None:
+ return None
+
+ if new_alarm_config.has_key('severity') and new_alarm_config['severity'] is not None:
+ symptom_details['state']['severity'] = severity_mano2vrops[new_alarm_config['severity']]
+ if new_alarm_config.has_key('operation') and new_alarm_config['operation'] is not None:
+ symptom_details['state']['condition']['operator'] = OPERATION_MAPPING[new_alarm_config['operation']]
+ if new_alarm_config.has_key('threshold_value') and new_alarm_config['threshold_value'] is not None:
+ symptom_details['state']['condition']['value'] = new_alarm_config['threshold_value']
+ #Find vrops metric key from metric_name, if required
+ """
+ if new_alarm_config.has_key('metric_name') and new_alarm_config['metric_name'] is not None:
+ metric_key_params = self.get_default_Params(new_alarm_config['metric_name'])
+ if not metric_key_params:
+ log.warn("Metric not supported: {}".format(config_dict['metric_name']))
+ return None
+ symptom_details['state']['condition']['key'] = metric_key_params['metric_key']
+ """
+ log.info("Fetched Symptom details : {}".format(symptom_details))
+
+ api_url = '/suite-api/api/symptomdefinitions'
+ headers = {'Content-Type': 'application/json', 'Accept':'application/json'}
+ data = json.dumps(symptom_details)
+ resp = requests.put(self.vrops_site + api_url,
+ auth=(self.vrops_user, self.vrops_password),
+ headers=headers,
+ verify = False,
+ data=data)
+
+ if resp.status_code != 200:
+ log.warn("Failed to update Symptom definition: {}, response {}"\
+ .format(symptom_uuid, resp.content))
+ return None
+
+
+ if symptom_uuid is not None:
+ log.info("Symptom defination updated {} for alarm: {}"\
+ .format(symptom_uuid, new_alarm_config['alarm_uuid']))
+ return symptom_uuid
+ else:
+ log.warn("Failed to update Symptom Defination {} for : {}"\
+ .format(symptom_uuid, new_alarm_config['alarm_uuid']))
+ return None
+
+
+ def get_symptom_defination_details(self, symptom_uuid):
+ """Get symptom defination details
+ """
+ symptom_details = {}
+ if symptom_uuid is None:
+ log.warn("get_symptom_defination_details: Symptom UUID not provided")
+ return None
+
+ api_url = '/suite-api/api/symptomdefinitions/'
+ headers = {'Accept': 'application/json'}
+
+ resp = requests.get(self.vrops_site + api_url + symptom_uuid,
+ auth=(self.vrops_user, self.vrops_password),
+ verify = False, headers = headers)
+
+ if resp.status_code is not 200:
+ log.warn("Failed to get symptom details for {} \nResponse code:{}\nResponse Content: {}"\
+ .format(symptom_uuid, resp.status_code, resp.content))
+ return None
+
+ symptom_details = json.loads(resp.content)
+ #print "New symptom Details: {}".format(symptom_details)
+ return symptom_details
+
+
+ def reconfigure_alarm(self, alarm_details_json, new_alarm_config):
+ """Reconfigure alarm defination as per input
+ """
+ if new_alarm_config.has_key('severity') and new_alarm_config['severity'] is not None:
+ alarm_details_json['states'][0]['severity'] = new_alarm_config['severity']
+
+ api_url = '/suite-api/api/alertdefinitions'
+ headers = {'Content-Type': 'application/json', 'Accept':'application/json'}
+ data = json.dumps(alarm_details_json)
+ resp = requests.put(self.vrops_site + api_url,
+ auth=(self.vrops_user, self.vrops_password),
+ headers=headers,
+ verify = False,
+ data=data)
+
+ if resp.status_code != 200:
+ log.warn("Failed to create Symptom definition: {}, response code {}, response content: {}"\
+ .format(symptom_uuid, resp.status_code, resp.content))
+ return None
+ else:
+ parsed_alarm_details = json.loads(resp.content)
+ alarm_def_uuid = parsed_alarm_details['id'].split('-', 1)[1]
+ log.info("Successfully updated Alarm defination: {}".format(alarm_def_uuid))
+ return alarm_def_uuid
+
+ def delete_alarm_configuration(self, delete_alarm_req_dict):
+ """Delete complete alarm configuration
+ """
+ if delete_alarm_req_dict['alarm_uuid'] is None:
+ log.info("delete_alarm_configuration: Alarm UUID not provided")
+ return None
+ #1)Get alarm & symptom defination details
+ alarm_details_json, alarm_details = self.get_alarm_defination_details(delete_alarm_req_dict['alarm_uuid'])
+ if alarm_details is None or alarm_details_json is None:
+ return None
+
+ #2) Delete alarm notfication
+ rule_id = self.delete_notification_rule(alarm_details['alarm_name'])
+ if rule_id is None:
+ return None
+
+ #3) Delete alarm configuraion
+ alarm_id = self.delete_alarm_defination(alarm_details['alarm_id'])
+ if alarm_id is None:
+ return None
+
+ #4) Delete alarm symptom
+ symptom_id = self.delete_symptom_definition(alarm_details['symptom_definition_id'])
+ if symptom_id is None:
+ return None
+ else:
+ log.info("Completed deleting alarm configuration: {}"\
+ .format(delete_alarm_req_dict['alarm_uuid']))
+ return delete_alarm_req_dict['alarm_uuid']
+
+ def delete_notification_rule(self, alarm_name):
+ """Deleted notification rule defined for a particular alarm
+ """
+ rule_id = self.get_notification_rule_id_by_alarm_name(alarm_name)
+ if rule_id is None:
+ return None
+ else:
+ api_url = '/suite-api/api/notifications/rules/'
+ headers = {'Accept':'application/json'}
+ resp = requests.delete(self.vrops_site + api_url + rule_id,
+ auth=(self.vrops_user, self.vrops_password),
+ verify = False, headers = headers)
+ if resp.status_code is not 204:
+ log.warn("Failed to delete notification rules for {}".format(alarm_name))
+ return None
+ else:
+ log.info("Deleted notification rules for {}".format(alarm_name))
+ return rule_id
+
+ def get_notification_rule_id_by_alarm_name(self, alarm_name):
+ """Find created Alarm notification rule id by alarm name
+ """
+ alarm_notify_id = 'notify_' + alarm_name
+ api_url = '/suite-api/api/notifications/rules'
+ headers = {'Content-Type': 'application/json', 'Accept':'application/json'}
+ resp = requests.get(self.vrops_site + api_url,
+ auth=(self.vrops_user, self.vrops_password),
+ verify = False, headers = headers)
+
+ if resp.status_code is not 200:
+ log.warn("Failed to get notification rules details for {}"\
+ .format(delete_alarm_req_dict['alarm_name']))
+ return None
+
+ notifications = json.loads(resp.content)
+ if notifications is not None and notifications.has_key('notification-rule'):
+ notifications_list = notifications['notification-rule']
+ for dict in notifications_list:
+ if dict['name'] is not None and dict['name'] == alarm_notify_id:
+ notification_id = dict['id']
+ log.info("Found Notification id to be deleted: {} for {}"\
+ .format(notification_id, alarm_name))
+ return notification_id
+
+ log.warn("Notification id to be deleted not found for {}"\
+ .format(notification_id, alarm_name))
+ return None
+
+ def delete_alarm_defination(self, alarm_id):
+ """Delete created Alarm defination
+ """
+ api_url = '/suite-api/api/alertdefinitions/'
+ headers = {'Accept':'application/json'}
+ resp = requests.delete(self.vrops_site + api_url + alarm_id,
+ auth=(self.vrops_user, self.vrops_password),
+ verify = False, headers = headers)
+ if resp.status_code is not 204:
+ log.warn("Failed to delete alarm definition {}".format(alarm_id))
+ return None
+ else:
+ log.info("Deleted alarm definition {}".format(alarm_id))
+ return alarm_id
+
+ def delete_symptom_definition(self, symptom_id):
+ """Delete symptom defination
+ """
+ api_url = '/suite-api/api/symptomdefinitions/'
+ headers = {'Accept':'application/json'}
+ resp = requests.delete(self.vrops_site + api_url + symptom_id,
+ auth=(self.vrops_user, self.vrops_password),
+ verify = False, headers = headers)
+ if resp.status_code is not 204:
+ log.warn("Failed to delete symptom definition {}".format(symptom_id))
+ return None
+ else:
+ log.info("Deleted symptom definition {}".format(symptom_id))
+ return symptom_id
+
+
+ def verify_metric_support(self, metric_info):
+ """Verify, if Metric is supported by vROPs plugin, verify metric unit & return status
+ Returns:
+ status: True if supported, False if not supported
+ """
+ status = False
+ metric_key_params = self.get_default_Params(metric_info['metric_name'])
+ if not metric_key_params:
+ log.warn("Metric not supported: {}".format(metric_info['metric_name']))
+ return status
+ else:
+ #If Metric is supported, verify metric unit & return status
+ if metric_key_params['unit'] == metric_info['metric_unit']:
+ log.info("Metric is supported: {}".format(metric_info['metric_name']))
+ status = True
+ else:
+ log.warn("Metric not supported: {}".format(metric_info['metric_name']))
+ status = False
+ return status
+
+ def get_triggered_alarms_list(self, list_alarm_input):
+ """Get list of triggered alarms on a resource based on alarm input request.