From: hamid Date: Tue, 26 Sep 2017 06:31:35 +0000 (+0500) Subject: Updated code with listing functions and error handling X-Git-Tag: v4.0.0~76 X-Git-Url: https://osm.etsi.org/gitweb/?a=commitdiff_plain;h=0291ac0cd2a6ef56d9e3ae4ea84447f55d926ae6;p=osm%2FMON.git Updated code with listing functions and error handling Change-Id: Ia038239dc71cd873575a2fd6cb592a7a7ce9b112 Signed-off-by: hamid --- diff --git a/plugins/CloudWatch/metric_alarms.py b/plugins/CloudWatch/metric_alarms.py index bd76c81..97b3c07 100644 --- a/plugins/CloudWatch/metric_alarms.py +++ b/plugins/CloudWatch/metric_alarms.py @@ -45,6 +45,19 @@ try: except: exit("Boto not avialable. Try activating your virtualenv OR `pip install boto`") +STATISTICS = { + "AVERAGE": "Average", + "MINIMUM": "Minimum", + "MAXIMUM": "Maximum", + "COUNT" : "SampleCount", + "SUM" : "Sum"} + +OPERATIONS = { + "GE" : ">=", + "LE" : "<=", + "GT" : ">", + "LT" : "<", + "EQ" : "="} class MetricAlarm(): """Alarms Functionality Handler -- Carries out alarming requests and responses via BOTO.Cloudwatch """ @@ -63,42 +76,50 @@ class MetricAlarm(): log.debug ("Alarm already exists, Try updating the alarm using 'update_alarm_configuration()'") else: try: - alarm = boto.ec2.cloudwatch.alarm.MetricAlarm( - connection = cloudwatch_conn, - name = alarm_info['alarm_name'] + "_" + alarm_info['resource_uuid'], - metric = alarm_info['metric_name'], - namespace = "AWS/EC2", - statistic = alarm_info['statistic'], - comparison = alarm_info['operation'], - threshold = alarm_info['threshold_value'], - period = 60, - evaluation_periods = 1, - unit=alarm_info['unit'], - description = alarm_info['severity'] + ";" + alarm_id + ";" + alarm_info['description'], - dimensions = {'InstanceId':alarm_info['resource_uuid']}, - alarm_actions = None, - ok_actions = None, - insufficient_data_actions = None) - - """Setting Alarm Actions : - alarm_actions = ['arn:aws:swf:us-west-2:465479087178:action/actions/AWS_EC2.InstanceId.Stop/1.0']""" - - status=cloudwatch_conn.put_metric_alarm(alarm) - - log.debug ("Alarm Configured Succesfully") - self.alarm_resp['schema_version'] = str(create_info['schema_version']) - self.alarm_resp['schema_type'] = 'create_alarm_response' - - inner_dict['correlation_id'] = str(alarm_info['correlation_id']) - inner_dict['alarm_uuid'] = str(alarm_id) - inner_dict['status'] = status - - self.alarm_resp['alarm_create_response'] = inner_dict - if status == True: - return self.alarm_resp - else: - return None + if alarm_info['statistic'] in STATISTICS: + if alarm_info['operation'] in OPERATIONS: + alarm = boto.ec2.cloudwatch.alarm.MetricAlarm( + connection = cloudwatch_conn, + name = alarm_info['alarm_name'] + "_" + alarm_info['resource_uuid'], + metric = alarm_info['metric_name'], + namespace = "AWS/EC2", + statistic = STATISTICS[alarm_info['statistic']], + comparison = OPERATIONS[alarm_info['operation']], + threshold = alarm_info['threshold_value'], + period = 60, + evaluation_periods = 1, + unit=alarm_info['unit'], + description = alarm_info['severity'] + ";" + alarm_id + ";" + alarm_info['description'], + dimensions = {'InstanceId':alarm_info['resource_uuid']}, + alarm_actions = None, + ok_actions = None, + insufficient_data_actions = None) + + """Setting Alarm Actions : + alarm_actions = ['arn:aws:swf:us-west-2:465479087178:action/actions/AWS_EC2.InstanceId.Stop/1.0']""" + + status=cloudwatch_conn.put_metric_alarm(alarm) + log.debug ("Alarm Configured Succesfully") + self.alarm_resp['schema_version'] = str(create_info['schema_version']) + self.alarm_resp['schema_type'] = 'create_alarm_response' + + inner_dict['correlation_id'] = str(alarm_info['correlation_id']) + inner_dict['alarm_uuid'] = str(alarm_id) + inner_dict['status'] = status + + self.alarm_resp['alarm_create_response'] = inner_dict + + if status == True: + return self.alarm_resp + else: + return None + else: + log.error("Operation not supported") + return None + else: + log.error("Statistic not supported") + return None except Exception as e: log.error("Alarm Configuration Failed: " + str(e)) @@ -120,37 +141,45 @@ class MetricAlarm(): return alarm_id else: try: - alarm = boto.ec2.cloudwatch.alarm.MetricAlarm( - connection = cloudwatch_conn, - name = status['info'].name , - metric = alarm_info['metric_name'], - namespace = "AWS/EC2", - statistic = alarm_info['statistic'], - comparison = alarm_info['operation'], - threshold = alarm_info['threshold_value'], - period = 60, - evaluation_periods = 1, - unit=alarm_info['unit'], - description = alarm_info['severity'] + ";" + alarm_id + ";" + alarm_info['description'], - dimensions = {'InstanceId':str(status['info'].dimensions['InstanceId']).split("'")[1]}, - alarm_actions = None, - ok_actions = None, - insufficient_data_actions = None) - - """Setting Alarm Actions : - alarm_actions = ['arn:aws:swf:us-west-2:465479087178:action/actions/AWS_EC2.InstanceId.Stop/1.0']""" - - status=cloudwatch_conn.put_metric_alarm(alarm) - log.debug("Alarm %s Updated ",alarm.name) - self.alarm_resp['schema_version'] = str(update_info['schema_version']) - self.alarm_resp['schema_type'] = 'update_alarm_response' - - inner_dict['correlation_id'] = str(alarm_info['correlation_id']) - inner_dict['alarm_uuid'] = str(alarm_id) - inner_dict['status'] = status - - self.alarm_resp['alarm_update_response'] = inner_dict - return self.alarm_resp + if alarm_info['statistic'] in STATISTICS: + if alarm_info['operation'] in OPERATIONS: + alarm = boto.ec2.cloudwatch.alarm.MetricAlarm( + connection = cloudwatch_conn, + name = status['info'].name , + metric = alarm_info['metric_name'], + namespace = "AWS/EC2", + statistic = STATISTICS[alarm_info['statistic']], + comparison = OPERATIONS[alarm_info['operation']], + threshold = alarm_info['threshold_value'], + period = 60, + evaluation_periods = 1, + unit=alarm_info['unit'], + description = alarm_info['severity'] + ";" + alarm_id + ";" + alarm_info['description'], + dimensions = {'InstanceId':str(status['info'].dimensions['InstanceId']).split("'")[1]}, + alarm_actions = None, + ok_actions = None, + insufficient_data_actions = None) + + """Setting Alarm Actions : + alarm_actions = ['arn:aws:swf:us-west-2:465479087178:action/actions/AWS_EC2.InstanceId.Stop/1.0']""" + + status=cloudwatch_conn.put_metric_alarm(alarm) + log.debug("Alarm %s Updated ",alarm.name) + self.alarm_resp['schema_version'] = str(update_info['schema_version']) + self.alarm_resp['schema_type'] = 'update_alarm_response' + + inner_dict['correlation_id'] = str(alarm_info['correlation_id']) + inner_dict['alarm_uuid'] = str(alarm_id) + inner_dict['status'] = status + + self.alarm_resp['alarm_update_response'] = inner_dict + return self.alarm_resp + else: + log.error("Operation not supported") + return None + else: + log.error("Statistic not supported") + return None except Exception as e: log.error ("Error in Updating Alarm " + str(e)) @@ -180,17 +209,49 @@ class MetricAlarm(): """Get a list of alarms that are present on a particular VIM type""" alarm_list = [] alarm_info = dict() + inner_dict = list_info['alarm_list_request'] try: #id vim alarms = cloudwatch_conn.describe_alarms() itr = 0 for alarm in alarms: list_info['alarm_list_request']['alarm_uuid'] = str(alarm.description).split(';')[1] - alarm_list.insert(itr,self.alarm_details(cloudwatch_conn,list_info)) - itr += 1 - + + #Severity = alarm_name = resource_uuid = "" + if inner_dict['severity'] == "" and inner_dict['alarm_name'] == "" and inner_dict['resource_uuid'] == "": + alarm_list.insert(itr,self.alarm_details(cloudwatch_conn,list_info)) + itr += 1 + #alarm_name = resource_uuid = "" + if inner_dict['severity'] == str(alarm.description).split(';')[0] and inner_dict['alarm_name'] == "" and inner_dict['resource_uuid'] == "": + alarm_list.insert(itr,self.alarm_details(cloudwatch_conn,list_info)) + itr += 1 + #severity = resource_uuid = "" + if inner_dict['severity'] == "" and inner_dict['alarm_name'] in alarm.name and inner_dict['resource_uuid'] == "": + alarm_list.insert(itr,self.alarm_details(cloudwatch_conn,list_info)) + itr += 1 + #severity = alarm_name = "" + if inner_dict['severity'] == "" and inner_dict['alarm_name'] == "" and inner_dict['resource_uuid'] == str(alarm.dimensions['InstanceId']).split("'")[1]: + alarm_list.insert(itr,self.alarm_details(cloudwatch_conn,list_info)) + itr += 1 + #resource_uuid = "" + if inner_dict['severity'] == str(alarm.description).split(';')[0] and inner_dict['alarm_name'] in alarm.name and inner_dict['resource_uuid'] == "": + alarm_list.insert(itr,self.alarm_details(cloudwatch_conn,list_info)) + itr += 1 + #alarm_name = "" + if inner_dict['severity'] == str(alarm.description).split(';')[0] and inner_dict['alarm_name'] == "" and inner_dict['resource_uuid'] == str(alarm.dimensions['InstanceId']).split("'")[1]: + alarm_list.insert(itr,self.alarm_details(cloudwatch_conn,list_info)) + itr += 1 + #severity = "" + if inner_dict['severity'] == "" and inner_dict['alarm_name'] in alarm.name and inner_dict['resource_uuid'] == str(alarm.dimensions['InstanceId']).split("'")[1]: + alarm_list.insert(itr,self.alarm_details(cloudwatch_conn,list_info)) + itr += 1 + #Everything provided + if inner_dict['severity'] == str(alarm.description).split(';')[0] and inner_dict['alarm_name'] in alarm.name and inner_dict['resource_uuid'] == str(alarm.dimensions['InstanceId']).split("'")[1]: + alarm_list.insert(itr,self.alarm_details(cloudwatch_conn,list_info)) + itr += 1 + alarm_info['schema_version'] = str(list_info['schema_version']) alarm_info['schema_type'] = 'list_alarm_response' - alarm_info['list_alarm_resp'] = json.dumps(alarm_list) + alarm_info['list_alarm_resp'] = alarm_list return alarm_info except Exception as e: diff --git a/plugins/CloudWatch/plugin_alarm.py b/plugins/CloudWatch/plugin_alarm.py index b3446bd..eb48208 100644 --- a/plugins/CloudWatch/plugin_alarm.py +++ b/plugins/CloudWatch/plugin_alarm.py @@ -43,16 +43,10 @@ class Plugin(): self.conn = Connection() self.metricAlarm = MetricAlarm() self.metric = Metrics() - server = {'server': 'localhost:9092', 'topic': 'alarm_request'} - self._consumer = KafkaConsumer(server['topic'], bootstrap_servers=server['server']) self._consumer.subscribe(['alarm_request']) - - #self.producer = KafkaProducer('create_alarm_request') - self.producer = KafkaProducer('') - - + self.producer = KafkaProducer('') #--------------------------------------------------------------------------------------------------------------------------- def connection(self): """Connecting instances with CloudWatch""" @@ -94,8 +88,7 @@ class Plugin(): if message.key == "create_alarm_request": if alarm_info['vim_type'] == 'AWS': alarm_inner_dict = alarm_info['alarm_create_request'] - metric_status = self.check_metric(alarm_inner_dict['metric_name']) - + metric_status = self.check_metric(alarm_inner_dict['metric_name']) if self.check_resource(alarm_inner_dict['resource_uuid']) == True and metric_status['status'] == True: log.debug ("Resource and Metrics exists") @@ -128,7 +121,7 @@ class Plugin(): ack_details = self.get_ack_details(alarm_info) payload = json.dumps(ack_details) file = open('../../core/models/notify_alarm.json','wb').write((payload)) - self.producer.update_alarm_response(key='notify_alarm',message=payload,topic = 'alarm_response') + self.producer.notify_alarm(key='notify_alarm',message=payload,topic = 'alarm_response') log.info("Acknowledge sent: %s", ack_details) else: log.error("Resource ID is Incorrect") @@ -168,7 +161,7 @@ class Plugin(): del_resp = self.delete_alarm(del_info) payload = json.dumps(del_resp) file = open('../../core/models/delete_alarm_resp.json','wb').write((payload)) - self.producer.update_alarm_response(key='delete_alarm_response',message=payload,topic = 'alarm_response') + self.producer.delete_alarm_response(key='delete_alarm_response',message=payload,topic = 'alarm_response') log.info("Alarm Deleted with alarm info: %s", del_resp) else: log.error(" VIM type Incorrect ") @@ -176,12 +169,12 @@ class Plugin(): elif message.key == "alarm_list_request": alarm_inner_dict = alarm_info['alarm_list_request'] if alarm_info['vim_type'] == 'AWS': - if self.check_resource(alarm_inner_dict['resource_uuid']) == True: + if self.check_resource(alarm_inner_dict['resource_uuid']) == True or alarm_inner_dict['resource_uuid'] == "": #Generate a valid response message, send via producer list_resp = self.get_alarms_list(alarm_info)#['alarm_names'] payload = json.dumps(list_resp) file = open('../../core/models/list_alarm_resp.json','wb').write((payload)) - self.producer.update_alarm_response(key='list_alarm_response',message=payload,topic = 'alarm_response') + self.producer.list_alarm_response(key='list_alarm_response',message=payload,topic = 'alarm_response') else: log.error("Resource ID is Incorrect") else: