X-Git-Url: https://osm.etsi.org/gitweb/?a=blobdiff_plain;f=osm_mon%2Fplugins%2FvRealiseOps%2Fmon_plugin_vrops.py;h=6ca3d40e6a040a38a539c669f8e826effbfc4be8;hb=e80db311a29dc8562dc84ae3336af167bac2ec5b;hp=bf1dfceb646f656ee9431c2c67c85630e839bec8;hpb=dc5050c02102eab6ee32149cd5945086b1e21280;p=osm%2FMON.git diff --git a/osm_mon/plugins/vRealiseOps/mon_plugin_vrops.py b/osm_mon/plugins/vRealiseOps/mon_plugin_vrops.py index bf1dfce..6ca3d40 100644 --- a/osm_mon/plugins/vRealiseOps/mon_plugin_vrops.py +++ b/osm_mon/plugins/vRealiseOps/mon_plugin_vrops.py @@ -22,23 +22,32 @@ ## """ -Montoring metrics & creating Alarm definations in vROPs +Monitoring metrics & creating Alarm definitions in vROPs """ - +import pytz import requests import logging -from pyvcloud.vcloudair import VCA + +import six +from pyvcloud.vcd.client import BasicLoginCredentials +from pyvcloud.vcd.client import Client +API_VERSION = '5.9' + from xml.etree import ElementTree as XmlElementTree import traceback import time import json from OpenSSL.crypto import load_certificate, FILETYPE_PEM import os +import sys import datetime from socket import getfqdn -from requests.packages.urllib3.exceptions import InsecureRequestWarning -requests.packages.urllib3.disable_warnings(InsecureRequestWarning) +import urllib3 +urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning) + +sys.path.append(os.path.join(os.path.dirname(os.path.abspath(__file__)), '..', '..', '..')) +from osm_mon.core.database import DatabaseManager OPERATION_MAPPING = {'GE':'GT_EQ', 'LE':'LT_EQ', 'GT':'GT', 'LT':'LT', 'EQ':'EQ'} severity_mano2vrops = {'WARNING':'WARNING', 'MINOR':'WARNING', 'MAJOR':"IMMEDIATE",\ @@ -60,7 +69,7 @@ SSL_CERTIFICATE_FILE_PATH = os.path.join(MODULE_DIR, SSL_CERTIFICATE_FILE_NAME) class MonPlugin(): """MON Plugin class for vROPs telemetry plugin """ - def __init__(self): + def __init__(self, access_config=None): """Constructor of MON plugin Params: 'access_config': dictionary with VIM access information based on VIM type. @@ -77,26 +86,35 @@ 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') + if access_config is None: + self.logger.error("VIM Access Configuration not provided") + raise KeyError("VIM Access Configuration not provided") + + self.database_manager = DatabaseManager() + self.access_config = access_config 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") + self.logger.error("VIM Account details are not added. Please add a VIM account") + raise KeyError("VIM Account details are not added. Please add a VIM account") 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.vcloud_site = access_config['vim_url'] self.admin_username = access_config['admin_username'] self.admin_password = access_config['admin_password'] - self.tenant_id = access_config['tenant_id'] + #self.tenant_id = access_config['tenant_id'] + self.vim_uuid = access_config['vim_uuid'] + 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)) + self.logger.error("Required VIM account details not provided: {}".format(exp)) + raise KeyError("Required VIM account details not provided: {}".format(exp)) + def configure_alarm(self, config_dict = {}): @@ -111,7 +129,6 @@ class MonPlugin(): "operation": One of ('GE', 'LE', 'GT', 'LT', 'EQ') "threshold_value": Defines the threshold (up to 2 fraction digits) that, if crossed, will trigger the alarm. - "unit": Unit of measurement in string format "statistic": AVERAGE, MINIMUM, MAXIMUM, COUNT, SUM Default parameters for each alarm are read from the plugin specific config file. @@ -125,18 +142,18 @@ 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: - self.logger.warn("Alarm not supported: {}".format(config_dict['alarm_name'])) + self.logger.warning("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: - self.logger.warn("Metric not supported: {}".format(config_dict['metric_name'])) + self.logger.warning("Metric not supported: {}".format(config_dict['metric_name'])) return None #1.2) Check if alarm definition already exists vrops_alarm_name = def_a_params['vrops_alarm']+ '-' + config_dict['resource_uuid'] alert_def_list = self.get_alarm_defination_by_name(vrops_alarm_name) if alert_def_list: - self.logger.warn("Alarm already exists: {}. Try updating by update_alarm_request"\ + self.logger.warning("Alarm already exists: {}. Try updating by update_alarm_request"\ .format(vrops_alarm_name)) return None @@ -146,32 +163,33 @@ class MonPlugin(): 'resource_kind_key': def_a_params['resource_kind'], 'adapter_kind_key': def_a_params['adapter_kind'], 'symptom_name':vrops_alarm_name, - 'severity': severity_mano2vrops[config_dict['severity']], + 'severity': severity_mano2vrops[config_dict['severity'].upper()], 'metric_key':metric_key_params['metric_key'], 'operation':OPERATION_MAPPING[config_dict['operation']], 'threshold_value':config_dict['threshold_value']} + symptom_uuid = self.create_symptom(symptom_params) if symptom_uuid is not None: self.logger.info("Symptom defined: {} with ID: {}".format(symptom_params['symptom_name'],symptom_uuid)) else: - self.logger.warn("Failed to create Symptom: {}".format(symptom_params['symptom_name'])) + self.logger.warning("Failed to create Symptom: {}".format(symptom_params['symptom_name'])) return None #3) create alert definition #To Do - Get type & subtypes for all 5 alarms alarm_params = {'name':vrops_alarm_name, 'description':config_dict['description']\ - if config_dict.has_key('description') and config_dict['description'] is not None else config_dict['alarm_name'], + if 'description' in config_dict and config_dict['description'] is not None else config_dict['alarm_name'], 'adapterKindKey':def_a_params['adapter_kind'], 'resourceKindKey':def_a_params['resource_kind'], 'waitCycles':1, 'cancelCycles':1, 'type':def_a_params['alarm_type'], 'subType':def_a_params['alarm_subType'], - 'severity':severity_mano2vrops[config_dict['severity']], + 'severity':severity_mano2vrops[config_dict['severity'].upper()], 'symptomDefinitionId':symptom_uuid, 'impact':def_a_params['impact']} alarm_def = self.create_alarm_definition(alarm_params) if alarm_def is None: - self.logger.warn("Failed to create Alert: {}".format(alarm_params['name'])) + self.logger.warning("Failed to create Alert: {}".format(alarm_params['name'])) return None self.logger.info("Alarm defined: {} with ID: {}".format(alarm_params['name'],alarm_def)) @@ -179,13 +197,13 @@ class MonPlugin(): #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: - self.logger.warn("Failed to find vm morefid for vApp in vCD: {}".format(config_dict['resource_uuid'])) + self.logger.warning("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: - self.logger.warn("Failed to find resource in vROPs: {}".format(config_dict['resource_uuid'])) + self.logger.warning("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 @@ -194,16 +212,28 @@ class MonPlugin(): return None else: alarm_def_uuid = alarm_def.split('-', 1)[1] - self.logger.info("Alarm defination created with notification: {} with ID: {}"\ + self.logger.info("Alarm definition created with notification: {} with ID: {}"\ .format(alarm_params['name'],alarm_def_uuid)) - #Return alarm defination UUID by removing 'AlertDefinition' from UUID + ##self.database_manager.save_alarm(alarm_def_uuid, alarm_params['name'], self.vim_uuid) + self.database_manager.save_alarm(alarm_def_uuid, + self.vim_uuid, + ##alarm_params['name'], + config_dict['threshold_value'], + config_dict['operation'], + config_dict['metric_name'].lower(), + config_dict['vdu_name'].lower(), + config_dict['vnf_member_index'].lower(), + config_dict['ns_id'].lower() + ) + + #Return alarm definition UUID by removing 'AlertDefinition' from UUID return (alarm_def_uuid) def get_default_Params(self, metric_alarm_name): """ Read the default config parameters from plugin specific file stored with plugin file. Params: - metric_alarm_name: Name of the alarm, whose congif params to be read from the config file. + metric_alarm_name: Name of the alarm, whose config parameters to be read from the config file. """ a_params = {} try: @@ -217,7 +247,7 @@ class MonPlugin(): tree = XmlElementTree.parse(source) alarms = tree.getroot() for alarm in alarms: - if alarm.tag == metric_alarm_name: + if alarm.tag.lower() == metric_alarm_name.lower(): for param in alarm: if param.tag in ("period", "evaluation", "cancel_period", "alarm_type",\ "cancel_cycles", "alarm_subType"): @@ -252,50 +282,47 @@ class MonPlugin(): try: api_url = '/suite-api/api/symptomdefinitions' - headers = {'Content-Type': 'application/xml'} - data = """ - - {4:s} - - - {6:s} - {7:s} - {8:s} - NUMERIC - false - STATIC - - - """\ - .format(str(symptom_params['cancel_cycles']),str(symptom_params['wait_cycles']), - symptom_params['resource_kind_key'], symptom_params['adapter_kind_key'], - symptom_params['symptom_name'],symptom_params['severity'], - symptom_params['metric_key'],symptom_params['operation'], - str(symptom_params['threshold_value'])) + headers = {'Content-Type': 'application/json','Accept': 'application/json'} + data = { + "id": None, + "name": symptom_params['symptom_name'], + "adapterKindKey": symptom_params['adapter_kind_key'], + "resourceKindKey": symptom_params['resource_kind_key'], + "waitCycles": symptom_params['wait_cycles'], + "cancelCycles": symptom_params['cancel_cycles'], + "state": { + "severity": symptom_params['severity'], + "condition": { + "type": "CONDITION_HT", + "key": symptom_params['metric_key'], + "operator": symptom_params['operation'], + "value": symptom_params['threshold_value'], + "valueType": "NUMERIC", + "instanced": False, + "thresholdType": "STATIC" + } + } + } resp = requests.post(self.vrops_site + api_url, auth=(self.vrops_user, self.vrops_password), headers=headers, verify = False, - data=data) + data=json.dumps(data)) if resp.status_code != 201: - self.logger.warn("Failed to create Symptom definition: {}, response {}"\ + self.logger.warning("Failed to create Symptom definition: {}, response {}"\ .format(symptom_params['symptom_name'], resp.content)) return None - symptom_xmlroot = XmlElementTree.fromstring(resp.content) - if symptom_xmlroot is not None and 'id' in symptom_xmlroot.attrib: - symptom_id = symptom_xmlroot.attrib['id'] + resp_data = json.loads(resp.content) + if resp_data.get('id') is not None: + symptom_id = resp_data['id'] return symptom_id except Exception as exp: - self.logger.warn("Error creating symptom definition : {}\n{}"\ + self.logger.warning("Error creating symptom definition : {}\n{}"\ .format(exp, traceback.format_exc())) @@ -322,62 +349,54 @@ class MonPlugin(): try: api_url = '/suite-api/api/alertdefinitions' - headers = {'Content-Type': 'application/xml'} - data = """ - - {0:s} - {1:s} - {2:s} - {3:s} - 1 - 1 - {4:s} - {5:s} - - - - - {7:s} - - SELF - ALL - AND - - - BADGE - {8:s} - - - - """\ - .format(alarm_params['name'],alarm_params['description'], - alarm_params['adapterKindKey'],alarm_params['resourceKindKey'], - str(alarm_params['type']),str(alarm_params['subType']), - alarm_params['severity'],alarm_params['symptomDefinitionId'], - alarm_params['impact']) + headers = {'Content-Type': 'application/json', 'Accept': 'application/json'} + data = { + "name": alarm_params['name'], + "description": alarm_params['description'], + "adapterKindKey": alarm_params['adapterKindKey'], + "resourceKindKey": alarm_params['resourceKindKey'], + "waitCycles": 1, + "cancelCycles": 1, + "type": alarm_params['type'], + "subType": alarm_params['subType'], + "states": [ + { + "severity": alarm_params['severity'], + "base-symptom-set": + { + "type": "SYMPTOM_SET", + "relation": "SELF", + "aggregation": "ALL", + "symptomSetOperator": "AND", + "symptomDefinitionIds": [alarm_params['symptomDefinitionId']] + }, + "impact": { + "impactType": "BADGE", + "detail": alarm_params['impact'] + } + } + ] + } resp = requests.post(self.vrops_site + api_url, auth=(self.vrops_user, self.vrops_password), headers=headers, verify = False, - data=data) + data=json.dumps(data)) if resp.status_code != 201: - self.logger.warn("Failed to create Alarm definition: {}, response {}"\ + self.logger.warning("Failed to create Alarm definition: {}, response {}"\ .format(alarm_params['name'], resp.content)) return None - alarm_xmlroot = XmlElementTree.fromstring(resp.content) - for child in alarm_xmlroot: - if child.tag.split("}")[1] == 'id': - alarm_uuid = child.text + resp_data = json.loads(resp.content) + if resp_data.get('id') is not None: + alarm_uuid = resp_data['id'] return alarm_uuid except Exception as exp: - self.logger.warn("Error creating alarm definition : {}\n{}".format(exp, traceback.format_exc())) + self.logger.warning("Error creating alarm definition : {}\n{}".format(exp, traceback.format_exc())) def configure_rest_plugin(self): @@ -403,47 +422,54 @@ class MonPlugin(): cert = load_certificate(FILETYPE_PEM, cert_file_string) certificate = cert.digest("sha1") api_url = '/suite-api/api/alertplugins' - headers = {'Content-Type': 'application/xml'} - data = """ - - RestPlugin - {0:s} - - {1:s} - application/json - {2:s} - 20 - - """.format(plugin_name, webhook_url, certificate) + headers = {'Content-Type': 'application/json', 'Accept': 'application/json'} + data = { + "pluginTypeId": "RestPlugin", + "name": plugin_name, + "configValues": [ + { + "name": "Url", + "value": webhook_url + }, + { + "name": "Content-type", + "value": "application/json" + }, + { + "name": "Certificate", + "value": certificate + }, + { + "name": "ConnectionCount", + "value": "20" + } + ] + } resp = requests.post(self.vrops_site + api_url, auth=(self.vrops_user, self.vrops_password), headers=headers, verify = False, - data=data) + data=json.dumps(data)) if resp.status_code is not 201: - self.logger.warn("Failed to create REST Plugin: {} for url: {}, \nresponse code: {},"\ + self.logger.warning("Failed to create REST Plugin: {} for url: {}, \nresponse code: {},"\ "\nresponse content: {}".format(plugin_name, webhook_url,\ resp.status_code, resp.content)) return None - plugin_xmlroot = XmlElementTree.fromstring(resp.content) - if plugin_xmlroot is not None: - for child in plugin_xmlroot: - if child.tag.split("}")[1] == 'pluginId': - plugin_id = plugin_xmlroot.find('{http://webservice.vmware.com/vRealizeOpsMgr/1.0/}pluginId').text + resp_data = json.loads(resp.content) + if resp_data.get('pluginId') is not None: + plugin_id = resp_data['pluginId'] if plugin_id is None: - self.logger.warn("Failed to get REST Plugin ID for {}, url: {}".format(plugin_name, webhook_url)) + self.logger.warning("Failed to get REST Plugin ID for {}, url: {}".format(plugin_name, webhook_url)) return None else: 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: - self.logger.warn("Failed to enable created REST Plugin: {} for url: {}".format(plugin_name, webhook_url)) + self.logger.warning("Failed to enable created REST Plugin: {} for url: {}".format(plugin_name, webhook_url)) return None else: self.logger.info("Enabled REST Plugin: {} for url: {}".format(plugin_name, webhook_url)) @@ -456,28 +482,26 @@ class MonPlugin(): plugin_id = None #Find the REST Plugin id details for - MON_module_REST_Plugin api_url = '/suite-api/api/alertplugins' - headers = {'Accept': 'application/xml'} - namespace = {'params':"http://webservice.vmware.com/vRealizeOpsMgr/1.0/"} + headers = {'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 REST GET Alarm plugin details \nResponse code: {}\nResponse content: {}"\ + self.logger.warning("Failed to REST GET Alarm plugin details \nResponse code: {}\nResponse content: {}"\ .format(resp.status_code, resp.content)) return None # Look for specific plugin & parse pluginId for 'MON_module_REST_Plugin' - xmlroot_resp = XmlElementTree.fromstring(resp.content) - for notify_plugin in xmlroot_resp.findall('params:notification-plugin',namespace): - if notify_plugin.find('params:name',namespace) is not None and\ - notify_plugin.find('params:pluginId',namespace) is not None: - if notify_plugin.find('params:name',namespace).text == plugin_name: - plugin_id = notify_plugin.find('params:pluginId',namespace).text + plugins_list = json.loads(resp.content) + if plugins_list.get('notificationPluginInstances') is not None: + for notify_plugin in plugins_list['notificationPluginInstances']: + if notify_plugin.get('name') is not None and notify_plugin['name'] == plugin_name: + plugin_id = notify_plugin.get('pluginId') if plugin_id is None: - self.logger.warn("REST plugin {} not found".format('MON_module_REST_Plugin')) + self.logger.warning("REST plugin {} not found".format(plugin_name)) return None else: self.logger.info("Found REST Plugin: {}".format(plugin_name)) @@ -504,7 +528,7 @@ class MonPlugin(): verify = False) if resp.status_code is not 204: - self.logger.warn("Failed to enable REST plugin {}. \nResponse code {}\nResponse Content: {}"\ + self.logger.warning("Failed to enable REST plugin {}. \nResponse code {}\nResponse Content: {}"\ .format(plugin_name, resp.status_code, resp.content)) return False @@ -512,7 +536,7 @@ class MonPlugin(): return True except Exception as exp: - self.logger.warn("Error enabling REST plugin for {} plugin: Exception: {}\n{}"\ + self.logger.warning("Error enabling REST plugin for {} plugin: Exception: {}\n{}"\ .format(plugin_name, exp, traceback.format_exc())) def create_alarm_notification_rule(self, alarm_name, alarm_id, resource_id): @@ -533,43 +557,40 @@ 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: - self.logger.warn("Failed to get REST plugin_id for : {}".format('MON_module_REST_Plugin')) + self.logger.warning("Failed to get REST plugin_id for : {}".format('MON_module_REST_Plugin')) return None #2) Create Alarm notification rule api_url = '/suite-api/api/notifications/rules' - headers = {'Content-Type': 'application/xml'} - data = """ - - {0:s} - {1:s} - - true - - - {3:s} - - """\ - .format(notification_name, plugin_id, resource_id, alarm_id) + headers = {'Content-Type': 'application/json', 'Accept': 'application/json'} + data = { + "name" : notification_name, + "pluginId" : plugin_id, + "resourceFilter": { + "matchResourceIdOnly": True, + "resourceId": resource_id + }, + "alertDefinitionIdFilters" : { + "values" : [ alarm_id ] + } + } resp = requests.post(self.vrops_site + api_url, auth=(self.vrops_user, self.vrops_password), headers=headers, verify = False, - data=data) + data=json.dumps(data)) if resp.status_code is not 201: - self.logger.warn("Failed to create Alarm notification rule {} for {} alarm."\ + self.logger.warning("Failed to create Alarm notification rule {} for {} alarm."\ "\nResponse code: {}\nResponse content: {}"\ .format(notification_name, alarm_name, resp.status_code, resp.content)) return None #parse notification id from response - xmlroot_resp = XmlElementTree.fromstring(resp.content) - if xmlroot_resp is not None and 'id' in xmlroot_resp.attrib: - notification_id = xmlroot_resp.attrib.get('id') + resp_data = json.loads(resp.content) + if resp_data.get('id') is not None: + notification_id = resp_data['id'] self.logger.info("Created Alarm notification rule {} for {} alarm.".format(notification_name, alarm_name)) return notification_id @@ -588,7 +609,7 @@ class MonPlugin(): return vm_moref_id except Exception as exp: - self.logger.warn("Error occurred while getting VM moref ID for VM : {}\n{}"\ + self.logger.warning("Error occurred while getting VM moref ID for VM : {}\n{}"\ .format(exp, traceback.format_exc())) @@ -606,23 +627,26 @@ class MonPlugin(): parsed_respond = {} vca = None - vca = self.connect_as_admin() + if vapp_uuid is None: + return parsed_respond + vca = self.connect_as_admin() if not vca: - self.logger.warn("connect() to vCD is failed") - if vapp_uuid is None: - return None + self.logger.warning("Failed to connect to vCD") + return parsed_respond - url_list = [vca.host, '/api/vApp/vapp-', vapp_uuid] + url_list = [self.vcloud_site, '/api/vApp/vapp-', vapp_uuid] get_vapp_restcall = ''.join(url_list) - if vca.vcloud_session and vca.vcloud_session.organization: + if vca._session: + headers = {'Accept':'application/*+xml;version=' + API_VERSION, + 'x-vcloud-authorization': vca._session.headers['x-vcloud-authorization']} response = requests.get(get_vapp_restcall, - headers=vca.vcloud_session.get_vcloud_headers(), - verify=vca.verify) + headers=headers, + verify=False) if response.status_code != 200: - self.logger.warn("REST API call {} failed. Return status code {}"\ + self.logger.warning("REST API call {} failed. Return status code {}"\ .format(get_vapp_restcall, response.content)) return parsed_respond @@ -647,7 +671,7 @@ class MonPlugin(): parsed_respond["vm_vcenter_info"]= vm_vcenter_info except Exception as exp : - self.logger.warn("Error occurred calling rest api for getting vApp details: {}\n{}"\ + self.logger.warning("Error occurred calling rest api for getting vApp details: {}\n{}"\ .format(exp, traceback.format_exc())) return parsed_respond @@ -662,23 +686,19 @@ class MonPlugin(): The return vca object that letter can be used to connect to vcloud direct as admin for provider vdc """ - self.logger.info("Logging in to a VCD org as admin.") + self.logger.debug("Logging into vCD org as admin.") - vca_admin = VCA(host=self.vcloud_site, - username=self.admin_username, - service_type='standalone', - version='5.9', - verify=False, - log=False) - result = vca_admin.login(password=self.admin_password, org='System') - if not result: - 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: - self.logger.info("Successfully logged to a vcloud direct org: {} as user: {}"\ - .format('System', self.admin_username)) + try: + host = self.vcloud_site + org = 'System' + client_as_admin = Client(host, verify_ssl_certs=False) + client_as_admin.set_credentials(BasicLoginCredentials(self.admin_username, org,\ + self.admin_password)) + except Exception as e: + self.logger.warning("Can't connect to a vCloud director as: {} with exception {}"\ + .format(self.admin_username, e)) - return vca_admin + return client_as_admin def get_vm_resource_id(self, vm_moref_id): @@ -687,37 +707,42 @@ class MonPlugin(): if vm_moref_id is None: return None - api_url = '/suite-api/api/resources' - headers = {'Accept': 'application/xml'} - namespace = {'params':"http://webservice.vmware.com/vRealizeOpsMgr/1.0/"} + api_url = '/suite-api/api/resources?resourceKind=VirtualMachine' + headers = {'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 resource details from vROPs for {}\nResponse code:{}\nResponse Content: {}"\ - .format(vm_moref_id, resp.status_code, resp.content)) + self.logger.warning("Failed to get resource details from vROPs for {}"\ + "\nResponse code:{}\nResponse Content: {}"\ + .format(vm_moref_id, resp.status_code, resp.content)) return None + vm_resource_id = None try: - xmlroot_respond = XmlElementTree.fromstring(resp.content) - for resource in xmlroot_respond.findall('params:resource',namespace): - if resource is not None: - resource_key = resource.find('params:resourceKey',namespace) - if resource_key is not None: - if resource_key.find('params:adapterKindKey',namespace).text == 'VMWARE' and \ - resource_key.find('params:resourceKindKey',namespace).text == 'VirtualMachine': - for child in resource_key: - if child.tag.split('}')[1]=='resourceIdentifiers': - resourceIdentifiers = child - for r_id in resourceIdentifiers: - if r_id.find('params:value',namespace).text == vm_moref_id: - self.logger.info("Found Resource ID : {} in vROPs for {}"\ - .format(resource.attrib['identifier'], vm_moref_id)) - return resource.attrib['identifier'] + resp_data = json.loads(resp.content) + if resp_data.get('resourceList') is not None: + resource_list = resp_data.get('resourceList') + for resource in resource_list: + if resource.get('resourceKey') is not None: + resource_details = resource['resourceKey'] + if resource_details.get('resourceIdentifiers') is not None: + resource_identifiers = resource_details['resourceIdentifiers'] + for resource_identifier in resource_identifiers: + if resource_identifier['identifierType']['name']=='VMEntityObjectID': + if resource_identifier.get('value') is not None and \ + resource_identifier['value']==vm_moref_id: + vm_resource_id = resource['identifier'] + self.logger.info("Found VM resource ID: {} for vm_moref_id: {}"\ + .format(vm_resource_id, vm_moref_id)) + except Exception as exp: - self.logger.warn("Error in parsing {}\n{}".format(exp, traceback.format_exc())) + self.logger.warning("get_vm_resource_id: Error in parsing {}\n{}"\ + .format(exp, traceback.format_exc())) + + return vm_resource_id def get_metrics_data(self, metric={}): @@ -741,6 +766,7 @@ class MonPlugin(): return_data = {} return_data['schema_version'] = "1.0" return_data['schema_type'] = 'read_metric_data_response' + return_data['vim_uuid'] = metric['vim_uuid'] return_data['metric_name'] = metric['metric_name'] #To do - No metric_uuid in vROPs, thus returning '0' return_data['metric_uuid'] = '0' @@ -748,19 +774,19 @@ class MonPlugin(): return_data['resource_uuid'] = metric['resource_uuid'] return_data['metrics_data'] = {'time_series':[], 'metrics_series':[]} #To do - Need confirmation about uuid & id - 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 + ##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 - #self.logger.warn("return_data: {}".format(return_data)) + #self.logger.warning("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: - self.logger.warn("Metric not supported: {}".format(metric['metric_name'])) + self.logger.warning("Metric not supported: {}".format(metric['metric_name'])) #To Do: Return message return return_data @@ -770,12 +796,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: - self.logger.warn("Failed to find vm morefid for vApp in vCD: {}".format(config_dict['resource_uuid'])) + self.logger.warning("Failed to find vm morefid for vApp in vCD: {}".format(metric['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: - self.logger.warn("Failed to find resource in vROPs: {}".format(config_dict['resource_uuid'])) + self.logger.warning("Failed to find resource in vROPs: {}".format(metric['resource_uuid'])) return return_data #3) Calculate begin & end time for period & period unit @@ -800,21 +826,21 @@ class MonPlugin(): verify = False, headers = headers) if resp.status_code is not 200: - self.logger.warn("Failed to retrive Metric data from vROPs for {}\nResponse code:{}\nResponse Content: {}"\ + self.logger.warning("Failed to retrieve Metric data from vROPs for {}\nResponse code:{}\nResponse Content: {}"\ .format(metric['metric_name'], resp.status_code, resp.content)) return return_data #5) Convert to required format metrics_data = {} json_data = json.loads(resp.content) - for resp_key,resp_val in json_data.iteritems(): + for resp_key,resp_val in six.iteritems(json_data): if resp_key == 'values': data = json_data['values'][0] - for data_k,data_v in data.iteritems(): + for data_k,data_v in six.iteritems(data): if data_k == 'stat-list': stat_list = data_v - for stat_list_k,stat_list_v in stat_list.iteritems(): - for stat_keys,stat_vals in stat_list_v[0].iteritems(): + for stat_list_k,stat_list_v in six.iteritems(stat_list): + for stat_keys,stat_vals in six.iteritems(stat_list_v[0]): if stat_keys == 'timestamps': metrics_data['time_series'] = stat_list_v[0]['timestamps'] if stat_keys == 'data': @@ -828,26 +854,26 @@ class MonPlugin(): """Update alarm configuration (i.e. Symptom & alarm) as per request """ if new_alarm_config.get('alarm_uuid') is None: - self.logger.warn("alarm_uuid is required to update an Alarm") + self.logger.warning("alarm_uuid is required to update an Alarm") return None - #1) Get Alarm details from it's uuid & find the symptom defination + #1) Get Alarm details from it's uuid & find the symptom definition 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 + #2) Update the symptom definition 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'])) + self.logger.info("Symptom Definition 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 + #3) Update the alarm definition & Return UUID if successful update if symptom_uuid is None: - self.logger.info("Symptom Defination details not found for {}"\ + self.logger.info("Symptom Definition details not found for {}"\ .format(new_alarm_config['alarm_uuid'])) return None else: @@ -863,7 +889,7 @@ class MonPlugin(): """Get alarm details based on alarm UUID """ if alarm_uuid is None: - self.logger.warn("get_alarm_defination_details: Alarm UUID not provided") + self.logger.warning("get_alarm_defination_details: Alarm UUID not provided") return None, None alarm_details = {} @@ -876,7 +902,7 @@ class MonPlugin(): verify = False, headers = headers) if resp.status_code is not 200: - self.logger.warn("Alarm to be updated not found: {}\nResponse code:{}\nResponse Content: {}"\ + self.logger.warning("Alarm to be updated not found: {}\nResponse code:{}\nResponse Content: {}"\ .format(alarm_uuid, resp.status_code, resp.content)) return None, None @@ -891,7 +917,7 @@ class MonPlugin(): 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)) + self.logger.warning("Exception while retrieving alarm definition details: {}".format(exp)) return None, None return json_data, alarm_details @@ -904,7 +930,7 @@ class MonPlugin(): alert_match_list = [] if alarm_name is None: - self.logger.warn("get_alarm_defination_by_name: Alarm name not provided") + self.logger.warning("get_alarm_defination_by_name: Alarm name not provided") return alert_match_list json_data = {} @@ -916,7 +942,7 @@ class MonPlugin(): verify = False, headers = headers) if resp.status_code is not 200: - self.logger.warn("get_alarm_defination_by_name: Error in response: {}\nResponse code:{}"\ + self.logger.warning("get_alarm_defination_by_name: Error in response: {}\nResponse code:{}"\ "\nResponse Content: {}".format(alarm_name, resp.status_code, resp.content)) return alert_match_list @@ -924,38 +950,38 @@ class MonPlugin(): json_data = json.loads(resp.content) if json_data['alertDefinitions'] is not None: alerts_list = json_data['alertDefinitions'] - alert_match_list = filter(lambda alert: alert['name'] == alarm_name, alerts_list) + alert_match_list = list(filter(lambda alert: alert['name'] == alarm_name, alerts_list)) status = False if not alert_match_list else True #self.logger.debug("Found alert_match_list: {}for larm_name: {},\nstatus: {}".format(alert_match_list, alarm_name,status)) return alert_match_list except Exception as exp: - self.logger.warn("Exception while searching alarm defination: {}".format(exp)) + self.logger.warning("Exception while searching alarm definition: {}".format(exp)) return alert_match_list def update_symptom_defination(self, symptom_uuid, new_alarm_config): - """Update symptom defination based on new alarm input configuration + """Update symptom definition based on new alarm input configuration """ - #1) Get symptom defination details + #1) Get symptom definition 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: + if 'severity' in new_alarm_config 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: + if 'operation' in new_alarm_config 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: + if 'threshold_value' in new_alarm_config 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: + if 'metric_name' in new_alarm_config 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'])) + self.logger.warning("Metric not supported: {}".format(config_dict['metric_name'])) return None symptom_details['state']['condition']['key'] = metric_key_params['metric_key'] """ @@ -971,27 +997,27 @@ class MonPlugin(): data=data) if resp.status_code != 200: - self.logger.warn("Failed to update Symptom definition: {}, response {}"\ + self.logger.warning("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: {}"\ + self.logger.info("Symptom definition updated {} for alarm: {}"\ .format(symptom_uuid, new_alarm_config['alarm_uuid'])) return symptom_uuid else: - self.logger.warn("Failed to update Symptom Defination {} for : {}"\ + self.logger.warning("Failed to update Symptom Definition {} for : {}"\ .format(symptom_uuid, new_alarm_config['alarm_uuid'])) return None def get_symptom_defination_details(self, symptom_uuid): - """Get symptom defination details + """Get symptom definition details """ symptom_details = {} if symptom_uuid is None: - self.logger.warn("get_symptom_defination_details: Symptom UUID not provided") + self.logger.warning("get_symptom_defination_details: Symptom UUID not provided") return None api_url = '/suite-api/api/symptomdefinitions/' @@ -1002,7 +1028,7 @@ class MonPlugin(): verify = False, headers = headers) if resp.status_code is not 200: - self.logger.warn("Symptom defination not found {} \nResponse code:{}\nResponse Content: {}"\ + self.logger.warning("Symptom definition not found {} \nResponse code:{}\nResponse Content: {}"\ .format(symptom_uuid, resp.status_code, resp.content)) return None @@ -1012,11 +1038,11 @@ class MonPlugin(): def reconfigure_alarm(self, alarm_details_json, new_alarm_config): - """Reconfigure alarm defination as per input + """Reconfigure alarm definition as per input """ - if new_alarm_config.has_key('severity') and new_alarm_config['severity'] is not None: + if 'severity' in new_alarm_config 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: + if 'description' in new_alarm_config and new_alarm_config['description'] is not None: alarm_details_json['description'] = new_alarm_config['description'] api_url = '/suite-api/api/alertdefinitions' @@ -1029,13 +1055,13 @@ class MonPlugin(): 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)) + self.logger.warning("Failed to update Alarm definition: {}, response code {}, response content: {}"\ + .format(alarm_details_json['id'], 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)) + self.logger.info("Successfully updated Alarm definition: {}".format(alarm_def_uuid)) return alarm_def_uuid def delete_alarm_configuration(self, delete_alarm_req_dict): @@ -1044,17 +1070,17 @@ class MonPlugin(): 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 + #1)Get alarm & symptom definition 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 + #2) Delete alarm notification rule_id = self.delete_notification_rule(alarm_details['alarm_name']) if rule_id is None: return None - #3) Delete alarm configuraion + #3) Delete alarm configuration alarm_id = self.delete_alarm_defination(alarm_details['alarm_id']) if alarm_id is None: return None @@ -1081,7 +1107,7 @@ class MonPlugin(): 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)) + self.logger.warning("Failed to delete notification rules for {}".format(alarm_name)) return None else: self.logger.info("Deleted notification rules for {}".format(alarm_name)) @@ -1098,12 +1124,12 @@ class MonPlugin(): 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'])) + self.logger.warning("Failed to get notification rules details for {}"\ + .format(alarm_name)) return None notifications = json.loads(resp.content) - if notifications is not None and notifications.has_key('notification-rule'): + if notifications is not None and 'notification-rule' in notifications: notifications_list = notifications['notification-rule'] for dict in notifications_list: if dict['name'] is not None and dict['name'] == alarm_notify_id: @@ -1112,12 +1138,12 @@ class MonPlugin(): .format(notification_id, alarm_name)) return notification_id - self.logger.warn("Notification id to be deleted not found for {}"\ - .format(notification_id, alarm_name)) + self.logger.warning("Notification id to be deleted not found for {}"\ + .format(alarm_name)) return None def delete_alarm_defination(self, alarm_id): - """Delete created Alarm defination + """Delete created Alarm definition """ api_url = '/suite-api/api/alertdefinitions/' headers = {'Accept':'application/json'} @@ -1125,14 +1151,14 @@ class MonPlugin(): 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)) + self.logger.warning("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 + """Delete symptom definition """ api_url = '/suite-api/api/symptomdefinitions/' headers = {'Accept':'application/json'} @@ -1140,7 +1166,7 @@ class MonPlugin(): 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)) + self.logger.warning("Failed to delete symptom definition {}".format(symptom_id)) return None else: self.logger.info("Deleted symptom definition {}".format(symptom_id)) @@ -1156,9 +1182,9 @@ class MonPlugin(): 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']) + metric_key_params = self.get_default_Params(metric_info['metric_name'].lower()) if not metric_key_params: - self.logger.warn("Metric not supported: {}".format(metric_info['metric_name'])) + self.logger.warning("Metric not supported: {}".format(metric_info['metric_name'])) return status else: #If Metric is supported, verify optional metric unit & return status @@ -1180,7 +1206,7 @@ class MonPlugin(): triggered_alarms_list = [] if list_alarm_input.get('resource_uuid') is None: - self.logger.warn("Resource UUID is required to get triggered alarms list") + self.logger.warning("Resource UUID is required to get triggered alarms list") return triggered_alarms_list #1)Find vROPs resource ID using RO resource UUID @@ -1198,13 +1224,13 @@ class MonPlugin(): #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)) + self.logger.warning("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)) + self.logger.warning("Failed to find resource in vROPs: {}".format(ro_resource_uuid)) return None return vrops_resource_id @@ -1220,12 +1246,12 @@ class MonPlugin(): 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'])) + self.logger.warning("Failed to get triggered alarms for {}"\ + .format(ro_resource_uuid)) return None all_alerts = json.loads(resp.content) - if all_alerts.has_key('alerts'): + if 'alerts' in all_alerts: if not all_alerts['alerts']: self.logger.info("No alarms present on resource {}".format(ro_resource_uuid)) return resource_alarms @@ -1242,7 +1268,7 @@ class MonPlugin(): alarm_instance['vim_type'] = 'VMware' #find severity of alarm severity = None - for key,value in severity_mano2vrops.iteritems(): + for key,value in six.iteritems(severity_mano2vrops): if value == alarm['alertLevel']: severity = key if severity is None: @@ -1263,8 +1289,7 @@ class MonPlugin(): """ date_time_formatted = '0000-00-00T00:00:00' if date_time != 0: - complete_datetime = datetime.datetime.fromtimestamp(date_time/1000.0).isoformat('T') + complete_datetime = datetime.datetime.fromtimestamp(date_time/1000.0, tz=pytz.utc).isoformat('T') date_time_formatted = complete_datetime.split('.',1)[0] return date_time_formatted -