X-Git-Url: https://osm.etsi.org/gitweb/?a=blobdiff_plain;f=plugins%2FvRealiseOps%2Fmon_plugin_vrops.py;h=309cec9a51c25dfd7f1a85c04341214fe2766a47;hb=cea9af8fa73e2442415b31ef6dcd6ac45d80fe86;hp=79b1ea74f03b11c0976fdb356be47f4883dbd25f;hpb=409dc1c833c31efa10df51de88adf198a36121de;p=osm%2FMON.git diff --git a/plugins/vRealiseOps/mon_plugin_vrops.py b/plugins/vRealiseOps/mon_plugin_vrops.py index 79b1ea7..309cec9 100644 --- a/plugins/vRealiseOps/mon_plugin_vrops.py +++ b/plugins/vRealiseOps/mon_plugin_vrops.py @@ -1,21 +1,61 @@ # -*- coding: utf-8 -*- + +## +# Copyright 2016-2017 VMware Inc. +# This file is part of ETSI OSM +# All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. You may obtain +# a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. +# +# For those usages not covered by the Apache License, Version 2.0 please +# contact: osslegalrouting@vmware.com +## + """ Montoring metrics & creating Alarm definations in vROPs """ import requests -import logging as log +import logging from pyvcloud.vcloudair import VCA from xml.etree import ElementTree as XmlElementTree import traceback import time import json from OpenSSL.crypto import load_certificate, FILETYPE_PEM +import os +import datetime +from socket import getfqdn + +from requests.packages.urllib3.exceptions import InsecureRequestWarning +requests.packages.urllib3.disable_warnings(InsecureRequestWarning) OPERATION_MAPPING = {'GE':'GT_EQ', 'LE':'LT_EQ', 'GT':'GT', 'LT':'LT', 'EQ':'EQ'} -severity_mano2vrops = {'WARNING':'WARNING', 'MINOR':'WARNING', 'MAJOR':"IMMEDIATE", 'CRITICAL':'CRITICAL'} +severity_mano2vrops = {'WARNING':'WARNING', 'MINOR':'WARNING', 'MAJOR':"IMMEDIATE",\ + 'CRITICAL':'CRITICAL', 'INDETERMINATE':'UNKNOWN'} PERIOD_MSEC = {'HR':3600000,'DAY':86400000,'WEEK':604800000,'MONTH':2678400000,'YEAR':31536000000} -DEFAULT_CONFIG_FILE = 'vrops_config.xml' + +#To Do - Add actual webhook url & certificate +#SSL_CERTIFICATE_FILE_NAME = 'vROPs_Webservice/SSL_certificate/www.vrops_webservice.com.cert' +#webhook_url = "https://mano-dev-1:8080/notify/" #for testing +webhook_url = "https://" + getfqdn() + ":8080/notify/" +SSL_CERTIFICATE_FILE_NAME = ('vROPs_Webservice/SSL_certificate/' + getfqdn() + ".cert") +#SSL_CERTIFICATE_FILE_NAME = 'vROPs_Webservice/SSL_certificate/10.172.137.214.cert' #for testing + +MODULE_DIR = os.path.dirname(__file__) +CONFIG_FILE_NAME = 'vrops_config.xml' +CONFIG_FILE_PATH = os.path.join(MODULE_DIR, CONFIG_FILE_NAME) +SSL_CERTIFICATE_FILE_PATH = os.path.join(MODULE_DIR, SSL_CERTIFICATE_FILE_NAME) class MonPlugin(): """MON Plugin class for vROPs telemetry plugin @@ -37,15 +77,27 @@ class MonPlugin(): Returns: Raise an exception if some needed parameter is missing, but it must not do any connectivity check against the VIM """ + self.logger = logging.getLogger('PluginReceiver.MonPlugin') + self.logger.setLevel(logging.DEBUG) + access_config = self.get_default_Params('Access_Config') self.access_config = access_config - self.vrops_site = access_config['vrops_site'] - self.vrops_user = access_config['vrops_user'] - self.vrops_password = access_config['vrops_password'] - self.vcloud_site = access_config['vcloud-site'] - self.admin_username = access_config['admin_username'] - self.admin_password = access_config['admin_password'] - self.tenant_id = access_config['tenant_id'] + if not bool(access_config): + self.logger.error("Access configuration not provided in vROPs Config file") + raise KeyError("Access configuration not provided in vROPs Config file") + + try: + self.vrops_site = access_config['vrops_site'] + self.vrops_user = access_config['vrops_user'] + self.vrops_password = access_config['vrops_password'] + self.vcloud_site = access_config['vcloud-site'] + self.admin_username = access_config['admin_username'] + self.admin_password = access_config['admin_password'] + self.tenant_id = access_config['tenant_id'] + except KeyError as exp: + self.logger.error("Check Access configuration in vROPs Config file: {}".format(exp)) + raise KeyError("Check Access configuration in vROPs Config file: {}".format(exp)) + def configure_alarm(self, config_dict = {}): """Configures or creates a new alarm using the input parameters in config_dict @@ -73,14 +125,14 @@ class MonPlugin(): #1) get alarm & metrics parameters from plugin specific file def_a_params = self.get_default_Params(config_dict['alarm_name']) if not def_a_params: - log.warn("Alarm not supported: {}".format(config_dict['alarm_name'])) + self.logger.warn("Alarm not supported: {}".format(config_dict['alarm_name'])) return None metric_key_params = self.get_default_Params(config_dict['metric_name']) if not metric_key_params: - log.warn("Metric not supported: {}".format(config_dict['metric_name'])) + self.logger.warn("Metric not supported: {}".format(config_dict['metric_name'])) return None #2) create symptom definition - vrops_alarm_name = def_a_params['vrops_alarm']+ '-' +config_dict['resource_uuid'] + vrops_alarm_name = def_a_params['vrops_alarm']+ '-' + config_dict['resource_uuid'] symptom_params ={'cancel_cycles': (def_a_params['cancel_period']/300)*def_a_params['cancel_cycles'], 'wait_cycles': (def_a_params['period']/300)*def_a_params['evaluation'], 'resource_kind_key': def_a_params['resource_kind'], @@ -92,9 +144,9 @@ class MonPlugin(): 'threshold_value':config_dict['threshold_value']} symptom_uuid = self.create_symptom(symptom_params) if symptom_uuid is not None: - log.info("Symptom defined: {} with ID: {}".format(symptom_params['symptom_name'],symptom_uuid)) + self.logger.info("Symptom defined: {} with ID: {}".format(symptom_params['symptom_name'],symptom_uuid)) else: - log.warn("Failed to create Symptom: {}".format(symptom_params['symptom_name'])) + self.logger.warn("Failed to create Symptom: {}".format(symptom_params['symptom_name'])) return None #3) create alert definition #To Do - Get type & subtypes for all 5 alarms @@ -111,30 +163,30 @@ class MonPlugin(): alarm_def = self.create_alarm_definition(alarm_params) if alarm_def is None: - log.warn("Failed to create Alert: {}".format(alarm_params['name'])) + self.logger.warn("Failed to create Alert: {}".format(alarm_params['name'])) return None - log.info("Alarm defined: {} with ID: {}".format(alarm_params['name'],alarm_def)) + self.logger.info("Alarm defined: {} with ID: {}".format(alarm_params['name'],alarm_def)) #4) Find vm_moref_id from vApp uuid in vCD vm_moref_id = self.get_vm_moref_id(config_dict['resource_uuid']) if vm_moref_id is None: - log.warn("Failed to find vm morefid for vApp in vCD: {}".format(config_dict['resource_uuid'])) + self.logger.warn("Failed to find vm morefid for vApp in vCD: {}".format(config_dict['resource_uuid'])) return None #5) Based on vm_moref_id, find VM's corresponding resource_id in vROPs to set notification resource_id = self.get_vm_resource_id(vm_moref_id) if resource_id is None: - log.warn("Failed to find resource in vROPs: {}".format(config_dict['resource_uuid'])) + self.logger.warn("Failed to find resource in vROPs: {}".format(config_dict['resource_uuid'])) return None #6) Configure alarm notification for a particular VM using it's resource_id - notification_id = self.create_alarm_notification(vrops_alarm_name, alarm_def, resource_id) + notification_id = self.create_alarm_notification_rule(vrops_alarm_name, alarm_def, resource_id) if notification_id is None: return None else: alarm_def_uuid = alarm_def.split('-', 1)[1] - log.info("Alarm defination created with notification: {} with ID: {}"\ + self.logger.info("Alarm defination created with notification: {} with ID: {}"\ .format(alarm_params['name'],alarm_def_uuid)) #Return alarm defination UUID by removing 'AlertDefinition' from UUID return (alarm_def_uuid) @@ -145,9 +197,17 @@ class MonPlugin(): Params: metric_alarm_name: Name of the alarm, whose congif params to be read from the config file. """ - tree = XmlElementTree.parse(DEFAULT_CONFIG_FILE) - alarms = tree.getroot() a_params = {} + try: + source = open(CONFIG_FILE_PATH, 'r') + except IOError as exp: + msg = ("Could not read Config file: {}, \nException: {}"\ + .format(CONFIG_FILE_PATH, exp)) + self.logger.error(msg) + raise IOError(msg) + + tree = XmlElementTree.parse(source) + alarms = tree.getroot() for alarm in alarms: if alarm.tag == metric_alarm_name: for param in alarm: @@ -161,7 +221,7 @@ class MonPlugin(): a_params[param.tag] = False else: a_params[param.tag] = param.text - + source.close() return a_params @@ -216,7 +276,7 @@ class MonPlugin(): data=data) if resp.status_code != 201: - log.warn("Failed to create Symptom definition: {}, response {}"\ + self.logger.warn("Failed to create Symptom definition: {}, response {}"\ .format(symptom_params['symptom_name'], resp.content)) return None @@ -227,7 +287,7 @@ class MonPlugin(): return symptom_id except Exception as exp: - log.warn("Error creating symptom definition : {}\n{}"\ + self.logger.warn("Error creating symptom definition : {}\n{}"\ .format(exp, traceback.format_exc())) @@ -297,7 +357,7 @@ class MonPlugin(): data=data) if resp.status_code != 201: - log.warn("Failed to create Alarm definition: {}, response {}"\ + self.logger.warn("Failed to create Alarm definition: {}, response {}"\ .format(alarm_params['name'], resp.content)) return None @@ -309,7 +369,7 @@ class MonPlugin(): return alarm_uuid except Exception as exp: - log.warn("Error creating alarm definition : {}\n{}".format(exp, traceback.format_exc())) + self.logger.warn("Error creating alarm definition : {}\n{}".format(exp, traceback.format_exc())) def configure_rest_plugin(self): @@ -326,9 +386,12 @@ class MonPlugin(): if plugin_id is not None: return plugin_id else: - #To Do - Add actual webhook url - webhook_url = "https://mano-dev-1:8080/notify/" #for testing - cert_file_string = open("10.172.137.214.cert", "rb").read() #for testing + try: + cert_file_string = open(SSL_CERTIFICATE_FILE_PATH, "rb").read() + except IOError as exp: + msg = ("Could not read SSL certificate file: {}".format(SSL_CERTIFICATE_FILE_PATH)) + self.logger.error(msg) + raise IOError(msg) cert = load_certificate(FILETYPE_PEM, cert_file_string) certificate = cert.digest("sha1") api_url = '/suite-api/api/alertplugins' @@ -341,7 +404,7 @@ class MonPlugin(): {0:s} {1:s} - application/xml + application/json {2:s} 20 @@ -354,7 +417,7 @@ class MonPlugin(): data=data) if resp.status_code is not 201: - log.warn("Failed to create REST Plugin: {} for url: {}, \nresponse code: {},"\ + self.logger.warn("Failed to create REST Plugin: {} for url: {}, \nresponse code: {},"\ "\nresponse content: {}".format(plugin_name, webhook_url,\ resp.status_code, resp.content)) return None @@ -366,16 +429,16 @@ class MonPlugin(): plugin_id = plugin_xmlroot.find('{http://webservice.vmware.com/vRealizeOpsMgr/1.0/}pluginId').text if plugin_id is None: - log.warn("Failed to get REST Plugin ID for {}, url: {}".format(plugin_name, webhook_url)) + self.logger.warn("Failed to get REST Plugin ID for {}, url: {}".format(plugin_name, webhook_url)) return None else: - log.info("Created REST Plugin: {} with ID : {} for url: {}".format(plugin_name, plugin_id, webhook_url)) + self.logger.info("Created REST Plugin: {} with ID : {} for url: {}".format(plugin_name, plugin_id, webhook_url)) status = self.enable_rest_plugin(plugin_id, plugin_name) if status is False: - log.warn("Failed to enable created REST Plugin: {} for url: {}".format(plugin_name, webhook_url)) + self.logger.warn("Failed to enable created REST Plugin: {} for url: {}".format(plugin_name, webhook_url)) return None else: - log.info("Enabled REST Plugin: {} for url: {}".format(plugin_name, webhook_url)) + self.logger.info("Enabled REST Plugin: {} for url: {}".format(plugin_name, webhook_url)) return plugin_id def check_if_plugin_configured(self, plugin_name): @@ -393,7 +456,7 @@ class MonPlugin(): verify = False, headers = headers) if resp.status_code is not 200: - log.warn("Failed to REST GET Alarm plugin details \nResponse code: {}\nResponse content: {}"\ + self.logger.warn("Failed to REST GET Alarm plugin details \nResponse code: {}\nResponse content: {}"\ .format(resp.status_code, resp.content)) return None @@ -406,10 +469,10 @@ class MonPlugin(): plugin_id = notify_plugin.find('params:pluginId',namespace).text if plugin_id is None: - log.warn("REST plugin {} not found".format('MON_module_REST_Plugin')) + self.logger.warn("REST plugin {} not found".format('MON_module_REST_Plugin')) return None else: - log.info("Found REST Plugin: {}".format(plugin_name)) + self.logger.info("Found REST Plugin: {}".format(plugin_name)) return plugin_id @@ -421,7 +484,7 @@ class MonPlugin(): """ if plugin_id is None or plugin_name is None: - log.debug("enable_rest_plugin() : Plugin ID or plugin_name not provided for {} plugin"\ + self.logger.debug("enable_rest_plugin() : Plugin ID or plugin_name not provided for {} plugin"\ .format(plugin_name)) return False @@ -433,20 +496,20 @@ class MonPlugin(): verify = False) if resp.status_code is not 204: - log.warn("Failed to enable REST plugin {}. \nResponse code {}\nResponse Content: {}"\ + self.logger.warn("Failed to enable REST plugin {}. \nResponse code {}\nResponse Content: {}"\ .format(plugin_name, resp.status_code, resp.content)) return False - log.info("Enabled REST plugin {}.".format(plugin_name)) + self.logger.info("Enabled REST plugin {}.".format(plugin_name)) return True except Exception as exp: - log.warn("Error enabling REST plugin for {} plugin: Exception: {}\n{}"\ + self.logger.warn("Error enabling REST plugin for {} plugin: Exception: {}\n{}"\ .format(plugin_name, exp, traceback.format_exc())) - def create_alarm_notification(self, alarm_name, alarm_id, resource_id): + def create_alarm_notification_rule(self, alarm_name, alarm_id, resource_id): """ - Create notification for each alarm + Create notification rule for each alarm Params: alarm_name alarm_id @@ -462,7 +525,7 @@ class MonPlugin(): #1) Find the REST Plugin id details for - MON_module_REST_Plugin plugin_id = self.check_if_plugin_configured(plugin_name) if plugin_id is None: - log.warn("Failed to get REST plugin_id for : {}".format('MON_module_REST_Plugin')) + self.logger.warn("Failed to get REST plugin_id for : {}".format('MON_module_REST_Plugin')) return None #2) Create Alarm notification rule @@ -490,7 +553,7 @@ class MonPlugin(): data=data) if resp.status_code is not 201: - log.warn("Failed to create Alarm notification {} for {} alarm."\ + self.logger.warn("Failed to create Alarm notification rule {} for {} alarm."\ "\nResponse code: {}\nResponse content: {}"\ .format(notification_name, alarm_name, resp.status_code, resp.content)) return None @@ -500,7 +563,7 @@ class MonPlugin(): if xmlroot_resp is not None and 'id' in xmlroot_resp.attrib: notification_id = xmlroot_resp.attrib.get('id') - log.info("Created Alarm notification rule {} for {} alarm.".format(notification_name, alarm_name)) + self.logger.info("Created Alarm notification rule {} for {} alarm.".format(notification_name, alarm_name)) return notification_id def get_vm_moref_id(self, vapp_uuid): @@ -513,11 +576,11 @@ class MonPlugin(): if vm_details and "vm_vcenter_info" in vm_details: vm_moref_id = vm_details["vm_vcenter_info"].get("vm_moref_id", None) - log.info("Found vm_moref_id: {} for vApp UUID: {}".format(vm_moref_id, vapp_uuid)) + self.logger.info("Found vm_moref_id: {} for vApp UUID: {}".format(vm_moref_id, vapp_uuid)) return vm_moref_id except Exception as exp: - log.warn("Error occurred while getting VM moref ID for VM : {}\n{}"\ + self.logger.warn("Error occurred while getting VM moref ID for VM : {}\n{}"\ .format(exp, traceback.format_exc())) @@ -538,7 +601,7 @@ class MonPlugin(): vca = self.connect_as_admin() if not vca: - log.warn("connect() to vCD is failed") + self.logger.warn("connect() to vCD is failed") if vapp_uuid is None: return None @@ -551,7 +614,7 @@ class MonPlugin(): verify=vca.verify) if response.status_code != 200: - log.warn("REST API call {} failed. Return status code {}"\ + self.logger.warn("REST API call {} failed. Return status code {}"\ .format(get_vapp_restcall, response.content)) return parsed_respond @@ -576,7 +639,7 @@ class MonPlugin(): parsed_respond["vm_vcenter_info"]= vm_vcenter_info except Exception as exp : - log.warn("Error occurred calling rest api for getting vApp details: {}\n{}"\ + self.logger.warn("Error occurred calling rest api for getting vApp details: {}\n{}"\ .format(exp, traceback.format_exc())) return parsed_respond @@ -591,7 +654,7 @@ class MonPlugin(): The return vca object that letter can be used to connect to vcloud direct as admin for provider vdc """ - log.info("Logging in to a VCD org as admin.") + self.logger.info("Logging in to a VCD org as admin.") vca_admin = VCA(host=self.vcloud_site, username=self.admin_username, @@ -601,10 +664,10 @@ class MonPlugin(): log=False) result = vca_admin.login(password=self.admin_password, org='System') if not result: - log.warn("Can't connect to a vCloud director as: {}".format(self.admin_username)) + self.logger.warn("Can't connect to a vCloud director as: {}".format(self.admin_username)) result = vca_admin.login(token=vca_admin.token, org='System', org_url=vca_admin.vcloud_session.org_url) if result is True: - log.info("Successfully logged to a vcloud direct org: {} as user: {}"\ + self.logger.info("Successfully logged to a vcloud direct org: {} as user: {}"\ .format('System', self.admin_username)) return vca_admin @@ -625,7 +688,7 @@ class MonPlugin(): verify = False, headers = headers) if resp.status_code is not 200: - log.warn("Failed to get resource details from vROPs for {}\nResponse code:{}\nResponse Content: {}"\ + self.logger.warn("Failed to get resource details from vROPs for {}\nResponse code:{}\nResponse Content: {}"\ .format(vm_moref_id, resp.status_code, resp.content)) return None @@ -642,11 +705,11 @@ class MonPlugin(): resourceIdentifiers = child for r_id in resourceIdentifiers: if r_id.find('params:value',namespace).text == vm_moref_id: - log.info("Found Resource ID : {} in vROPs for {}"\ + self.logger.info("Found Resource ID : {} in vROPs for {}"\ .format(resource.attrib['identifier'], vm_moref_id)) return resource.attrib['identifier'] except Exception as exp: - log.warn("Error in parsing {}\n{}".format(exp, traceback.format_exc())) + self.logger.warn("Error in parsing {}\n{}".format(exp, traceback.format_exc())) def get_metrics_data(self, metric={}): @@ -671,22 +734,25 @@ class MonPlugin(): return_data['schema_version'] = 1.0 return_data['schema_type'] = 'read_metric_data_response' return_data['metric_name'] = metric['metric_name'] - #To do - No metric_uuid in vROPs, thus returning metric_name - return_data['metric_uuid'] = metric['metric_name'] + #To do - No metric_uuid in vROPs, thus returning '0' + return_data['metric_uuid'] = '0' return_data['correlation_id'] = metric['correlation_id'] return_data['resource_uuid'] = metric['resource_uuid'] return_data['metrics_data'] = {'time_series':[], 'metric_series':[]} #To do - Need confirmation about uuid & id - return_data['tenant_uuid'] = metric['tenant_uuid'] + if 'tenant_uuid' in metric and metric['tenant_uuid'] is not None: + return_data['tenant_uuid'] = metric['tenant_uuid'] + else: + return_data['tenant_uuid'] = None return_data['unit'] = None #return_data['tenant_id'] = self.tenant_id - #log.warn("return_data: {}".format(return_data)) + #self.logger.warn("return_data: {}".format(return_data)) #1) Get metric details from plugin specific file & format it into vROPs metrics metric_key_params = self.get_default_Params(metric['metric_name']) if not metric_key_params: - log.warn("Metric not supported: {}".format(metric['metric_name'])) + self.logger.warn("Metric not supported: {}".format(metric['metric_name'])) #To Do: Return message return return_data @@ -696,12 +762,12 @@ class MonPlugin(): #2.a) Find vm_moref_id from vApp uuid in vCD vm_moref_id = self.get_vm_moref_id(metric['resource_uuid']) if vm_moref_id is None: - log.warn("Failed to find vm morefid for vApp in vCD: {}".format(config_dict['resource_uuid'])) + self.logger.warn("Failed to find vm morefid for vApp in vCD: {}".format(config_dict['resource_uuid'])) return return_data #2.b) Based on vm_moref_id, find VM's corresponding resource_id in vROPs to set notification resource_id = self.get_vm_resource_id(vm_moref_id) if resource_id is None: - log.warn("Failed to find resource in vROPs: {}".format(config_dict['resource_uuid'])) + self.logger.warn("Failed to find resource in vROPs: {}".format(config_dict['resource_uuid'])) return return_data #3) Calculate begin & end time for period & period unit @@ -713,8 +779,8 @@ class MonPlugin(): begin_time = end_time - time_diff #4) Get the metrics data - log.info("metric_key_params['metric_key'] = {}".format(metric_key_params['metric_key'])) - log.info("end_time: {}, begin_time: {}".format(end_time, begin_time)) + self.logger.info("metric_key_params['metric_key'] = {}".format(metric_key_params['metric_key'])) + self.logger.info("end_time: {}, begin_time: {}".format(end_time, begin_time)) url_list = ['/suite-api/api/resources/', resource_id, '/stats?statKey=',\ metric_key_params['metric_key'], '&begin=', str(begin_time),'&end=',str(end_time)] @@ -726,7 +792,7 @@ class MonPlugin(): verify = False, headers = headers) if resp.status_code is not 200: - log.warn("Failed to retrive Metric data from vROPs for {}\nResponse code:{}\nResponse Content: {}"\ + self.logger.warn("Failed to retrive Metric data from vROPs for {}\nResponse code:{}\nResponse Content: {}"\ .format(metric['metric_name'], resp.status_code, resp.content)) return return_data @@ -750,8 +816,405 @@ class MonPlugin(): return return_data - def reconfigure_alarm(self, config_dict): + 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: + self.logger.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: + self.logger.info("Symptom Defination details not found for {}"\ + .format(new_alarm_config['alarm_uuid'])) + 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: + self.logger.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: + self.logger.warn("get_alarm_defination_details: Alarm UUID not provided") + return None, 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: + self.logger.warn("Alarm to be updated not found: {}\nResponse code:{}\nResponse Content: {}"\ + .format(alarm_uuid, resp.status_code, resp.content)) + return None, 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'] = json_data['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: + self.logger.warn("Exception while retriving alarm defination details: {}".format(exp)) + return None, None + + 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: + self.logger.warn("Metric not supported: {}".format(config_dict['metric_name'])) + return None + symptom_details['state']['condition']['key'] = metric_key_params['metric_key'] + """ + self.logger.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: + self.logger.warn("Failed to update Symptom definition: {}, response {}"\ + .format(symptom_uuid, resp.content)) + return None + + + if symptom_uuid is not None: + self.logger.info("Symptom defination updated {} for alarm: {}"\ + .format(symptom_uuid, new_alarm_config['alarm_uuid'])) + return symptom_uuid + else: + self.logger.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: + self.logger.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: + self.logger.warn("Symptom defination not found {} \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'] + if new_alarm_config.has_key('description') and new_alarm_config['description'] is not None: + alarm_details_json['description'] = new_alarm_config['description'] + + 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: + self.logger.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] + self.logger.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: + self.logger.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: + self.logger.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: + self.logger.warn("Failed to delete notification rules for {}".format(alarm_name)) + return None + else: + self.logger.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: + self.logger.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'] + self.logger.info("Found Notification id to be deleted: {} for {}"\ + .format(notification_id, alarm_name)) + return notification_id + + self.logger.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: + self.logger.warn("Failed to delete alarm definition {}".format(alarm_id)) + return None + else: + self.logger.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: + self.logger.warn("Failed to delete symptom definition {}".format(symptom_id)) + return None + else: + self.logger.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 """ - return None + status = False + if 'metric_name' not in metric_info: + self.logger.debug("Metric name not provided: {}".format(metric_info)) + return status + metric_key_params = self.get_default_Params(metric_info['metric_name']) + if not metric_key_params: + self.logger.warn("Metric not supported: {}".format(metric_info['metric_name'])) + return status + else: + #If Metric is supported, verify optional metric unit & return status + if 'metric_unit' in metric_info: + if metric_key_params.get('unit') == metric_info['metric_unit']: + self.logger.info("Metric is supported with unit: {}".format(metric_info['metric_name'])) + status = True + else: + self.logger.debug("Metric supported but there is unit mismatch for: {}."\ + "Supported unit: {}"\ + .format(metric_info['metric_name'],metric_key_params['unit'])) + status = True + return status + + def get_triggered_alarms_list(self, list_alarm_input): + """Get list of triggered alarms on a resource based on alarm input request. + """ + #TO Do - Need to add filtering of alarms based on Severity & alarm name + + triggered_alarms_list = [] + if list_alarm_input['resource_uuid'] is None: + return triggered_alarms_list + + #1)Find vROPs resource ID using RO resource UUID + vrops_resource_id = self.get_vrops_resourceid_from_ro_uuid(list_alarm_input['resource_uuid']) + if vrops_resource_id is None: + return triggered_alarms_list + + #2)Get triggered alarms on particular resource + triggered_alarms_list = self.get_triggered_alarms_on_resource(list_alarm_input['resource_uuid'], vrops_resource_id) + return triggered_alarms_list + + def get_vrops_resourceid_from_ro_uuid(self, ro_resource_uuid): + """Fetch vROPs resource ID using resource UUID from RO/SO + """ + #1) Find vm_moref_id from vApp uuid in vCD + vm_moref_id = self.get_vm_moref_id(ro_resource_uuid) + if vm_moref_id is None: + self.logger.warn("Failed to find vm morefid for vApp in vCD: {}".format(ro_resource_uuid)) + return None + + #2) Based on vm_moref_id, find VM's corresponding resource_id in vROPs to set notification + vrops_resource_id = self.get_vm_resource_id(vm_moref_id) + if vrops_resource_id is None: + self.logger.warn("Failed to find resource in vROPs: {}".format(ro_resource_uuid)) + return None + return vrops_resource_id + + + def get_triggered_alarms_on_resource(self, ro_resource_uuid, vrops_resource_id): + """Get triggered alarms on particular resource & return list of dictionary of alarms + """ + resource_alarms = [] + api_url = '/suite-api/api/alerts?resourceId=' + headers = {'Accept':'application/json'} + resp = requests.get(self.vrops_site + api_url + vrops_resource_id, + auth=(self.vrops_user, self.vrops_password), + verify = False, headers = headers) + + if resp.status_code is not 200: + self.logger.warn("Failed to get notification rules details for {}"\ + .format(delete_alarm_req_dict['alarm_name'])) + return None + + all_alerts = json.loads(resp.content) + if all_alerts.has_key('alerts'): + if not all_alerts['alerts']: + self.logger.info("No alarms present on resource {}".format(ro_resource_uuid)) + return resource_alarms + all_alerts_list = all_alerts['alerts'] + for alarm in all_alerts_list: + #self.logger.info("Triggered Alarm {}".format(alarm)) + if alarm['alertDefinitionName'] is not None and\ + len(alarm['alertDefinitionName'].split('-', 1)) == 2: + if alarm['alertDefinitionName'].split('-', 1)[1] == ro_resource_uuid: + alarm_instance = {} + alarm_instance['alarm_uuid'] = alarm['alertDefinitionId'].split('-', 1)[1] + alarm_instance['resource_uuid'] = ro_resource_uuid + alarm_instance['alarm_instance_uuid'] = alarm['alertId'] + alarm_instance['vim_type'] = 'VMware' + #find severity of alarm + severity = None + for key,value in severity_mano2vrops.iteritems(): + if value == alarm['alertLevel']: + severity = key + if severity is None: + severity = 'INDETERMINATE' + alarm_instance['severity'] = severity + alarm_instance['status'] = alarm['status'] + alarm_instance['start_date'] = self.convert_date_time(alarm['startTimeUTC']) + alarm_instance['update_date'] = self.convert_date_time(alarm['updateTimeUTC']) + alarm_instance['cancel_date'] = self.convert_date_time(alarm['cancelTimeUTC']) + self.logger.info("Triggered Alarm on resource {}".format(alarm_instance)) + resource_alarms.append(alarm_instance) + if not resource_alarms: + self.logger.info("No alarms present on resource {}".format(ro_resource_uuid)) + return resource_alarms + + def convert_date_time(self, date_time): + """Convert the input UTC time in msec to OSM date time format + """ + date_time_formatted = '0000-00-00T00:00:00' + if date_time != 0: + complete_datetime = datetime.datetime.fromtimestamp(date_time/1000.0).isoformat('T') + date_time_formatted = complete_datetime.split('.',1)[0] + return date_time_formatted +