79b1ea74f03b11c0976fdb356be47f4883dbd25f
1 # -*- coding: utf-8 -*-
3 Montoring metrics & creating Alarm definations in vROPs
8 from pyvcloud
.vcloudair
import VCA
9 from xml
.etree
import ElementTree
as XmlElementTree
13 from OpenSSL
.crypto
import load_certificate
, FILETYPE_PEM
15 OPERATION_MAPPING
= {'GE':'GT_EQ', 'LE':'LT_EQ', 'GT':'GT', 'LT':'LT', 'EQ':'EQ'}
16 severity_mano2vrops
= {'WARNING':'WARNING', 'MINOR':'WARNING', 'MAJOR':"IMMEDIATE", 'CRITICAL':'CRITICAL'}
17 PERIOD_MSEC
= {'HR':3600000,'DAY':86400000,'WEEK':604800000,'MONTH':2678400000,'YEAR':31536000000}
18 DEFAULT_CONFIG_FILE
= 'vrops_config.xml'
21 """MON Plugin class for vROPs telemetry plugin
24 """Constructor of MON plugin
26 'access_config': dictionary with VIM access information based on VIM type.
27 This contains a consolidate version of VIM & monitoring tool config at creation and
28 particular VIM config at their attachment.
29 For VIM type: 'vmware',
30 access_config - {'vrops_site':<>, 'vrops_user':<>, 'vrops_password':<>,
31 'vcloud-site':<>,'admin_username':<>,'admin_password':<>,
32 'nsx_manager':<>,'nsx_user':<>,'nsx_password':<>,
33 'vcenter_ip':<>,'vcenter_port':<>,'vcenter_user':<>,'vcenter_password':<>,
34 'vim_tenant_name':<>,'orgname':<>}
37 Returns: Raise an exception if some needed parameter is missing, but it must not do any connectivity
40 access_config
= self
.get_default_Params('Access_Config')
41 self
.access_config
= access_config
42 self
.vrops_site
= access_config
['vrops_site']
43 self
.vrops_user
= access_config
['vrops_user']
44 self
.vrops_password
= access_config
['vrops_password']
45 self
.vcloud_site
= access_config
['vcloud-site']
46 self
.admin_username
= access_config
['admin_username']
47 self
.admin_password
= access_config
['admin_password']
48 self
.tenant_id
= access_config
['tenant_id']
50 def configure_alarm(self
, config_dict
= {}):
51 """Configures or creates a new alarm using the input parameters in config_dict
53 "alarm_name": Alarm name in string format
54 "description": Description of alarm in string format
55 "resource_uuid": Resource UUID for which alarm needs to be configured. in string format
56 "Resource type": String resource type: 'VDU' or 'host'
57 "Severity": 'WARNING', 'MINOR', 'MAJOR', 'CRITICAL'
58 "metric_name": Metric key in string format
59 "operation": One of ('GE', 'LE', 'GT', 'LT', 'EQ')
60 "threshold_value": Defines the threshold (up to 2 fraction digits) that,
61 if crossed, will trigger the alarm.
62 "unit": Unit of measurement in string format
63 "statistic": AVERAGE, MINIMUM, MAXIMUM, COUNT, SUM
65 Default parameters for each alarm are read from the plugin specific config file.
66 Dict of default parameters is as follows:
67 default_params keys = {'cancel_cycles','wait_cycles','resource_kind','adapter_kind',
68 'alarm_type','alarm_subType',impact}
70 Returns the UUID of created alarm or None
73 #1) get alarm & metrics parameters from plugin specific file
74 def_a_params
= self
.get_default_Params(config_dict
['alarm_name'])
76 log
.warn("Alarm not supported: {}".format(config_dict
['alarm_name']))
78 metric_key_params
= self
.get_default_Params(config_dict
['metric_name'])
79 if not metric_key_params
:
80 log
.warn("Metric not supported: {}".format(config_dict
['metric_name']))
82 #2) create symptom definition
83 vrops_alarm_name
= def_a_params
['vrops_alarm']+ '-' +config_dict
['resource_uuid']
84 symptom_params
={'cancel_cycles': (def_a_params
['cancel_period']/300)*def_a_params
['cancel_cycles'],
85 'wait_cycles': (def_a_params
['period']/300)*def_a_params
['evaluation'],
86 'resource_kind_key': def_a_params
['resource_kind'],
87 'adapter_kind_key': def_a_params
['adapter_kind'],
88 'symptom_name':vrops_alarm_name
,
89 'severity': severity_mano2vrops
[config_dict
['severity']],
90 'metric_key':metric_key_params
['metric_key'],
91 'operation':OPERATION_MAPPING
[config_dict
['operation']],
92 'threshold_value':config_dict
['threshold_value']}
93 symptom_uuid
= self
.create_symptom(symptom_params
)
94 if symptom_uuid
is not None:
95 log
.info("Symptom defined: {} with ID: {}".format(symptom_params
['symptom_name'],symptom_uuid
))
97 log
.warn("Failed to create Symptom: {}".format(symptom_params
['symptom_name']))
99 #3) create alert definition
100 #To Do - Get type & subtypes for all 5 alarms
101 alarm_params
= {'name':vrops_alarm_name
,
102 'description':config_dict
['description']\
103 if config_dict
['description'] is not None else config_dict
['alarm_name'],
104 'adapterKindKey':def_a_params
['adapter_kind'],
105 'resourceKindKey':def_a_params
['resource_kind'],
106 'waitCycles':1, 'cancelCycles':1,
107 'type':def_a_params
['alarm_type'], 'subType':def_a_params
['alarm_subType'],
108 'severity':severity_mano2vrops
[config_dict
['severity']],
109 'symptomDefinitionId':symptom_uuid
,
110 'impact':def_a_params
['impact']}
112 alarm_def
= self
.create_alarm_definition(alarm_params
)
113 if alarm_def
is None:
114 log
.warn("Failed to create Alert: {}".format(alarm_params
['name']))
117 log
.info("Alarm defined: {} with ID: {}".format(alarm_params
['name'],alarm_def
))
119 #4) Find vm_moref_id from vApp uuid in vCD
120 vm_moref_id
= self
.get_vm_moref_id(config_dict
['resource_uuid'])
121 if vm_moref_id
is None:
122 log
.warn("Failed to find vm morefid for vApp in vCD: {}".format(config_dict
['resource_uuid']))
125 #5) Based on vm_moref_id, find VM's corresponding resource_id in vROPs to set notification
126 resource_id
= self
.get_vm_resource_id(vm_moref_id
)
127 if resource_id
is None:
128 log
.warn("Failed to find resource in vROPs: {}".format(config_dict
['resource_uuid']))
131 #6) Configure alarm notification for a particular VM using it's resource_id
132 notification_id
= self
.create_alarm_notification(vrops_alarm_name
, alarm_def
, resource_id
)
133 if notification_id
is None:
136 alarm_def_uuid
= alarm_def
.split('-', 1)[1]
137 log
.info("Alarm defination created with notification: {} with ID: {}"\
138 .format(alarm_params
['name'],alarm_def_uuid
))
139 #Return alarm defination UUID by removing 'AlertDefinition' from UUID
140 return (alarm_def_uuid
)
142 def get_default_Params(self
, metric_alarm_name
):
144 Read the default config parameters from plugin specific file stored with plugin file.
146 metric_alarm_name: Name of the alarm, whose congif params to be read from the config file.
148 tree
= XmlElementTree
.parse(DEFAULT_CONFIG_FILE
)
149 alarms
= tree
.getroot()
152 if alarm
.tag
== metric_alarm_name
:
154 if param
.tag
in ("period", "evaluation", "cancel_period", "alarm_type",\
155 "cancel_cycles", "alarm_subType"):
156 a_params
[param
.tag
] = int(param
.text
)
157 elif param
.tag
in ("enabled", "repeat"):
158 if(param
.text
.lower() == "true"):
159 a_params
[param
.tag
] = True
161 a_params
[param
.tag
] = False
163 a_params
[param
.tag
] = param
.text
168 def create_symptom(self
, symptom_params
):
169 """Create Symptom definition for an alarm
171 symptom_params: Dict of parameters required for defining a symptom as follows
174 resource_kind_key = "VirtualMachine"
175 adapter_kind_key = "VMWARE"
176 symptom_name = Test_Memory_Usage_TooHigh
181 Returns the uuid of Symptom definition
186 api_url
= '/suite-api/api/symptomdefinitions'
187 headers
= {'Content-Type': 'application/xml'}
188 data
= """<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
189 <ops:symptom-definition cancelCycles="{0:s}" waitCycles="{1:s}"
190 resourceKindKey="{2:s}" adapterKindKey="{3:s}"
191 xmlns:xs="http://www.w3.org/2001/XMLSchema"
192 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
193 xmlns:ops="http://webservice.vmware.com/vRealizeOpsMgr/1.0/">
194 <ops:name>{4:s}</ops:name>
195 <ops:state severity="{5:s}">
196 <ops:condition xsi:type="ops:htCondition">
197 <ops:key>{6:s}</ops:key>
198 <ops:operator>{7:s}</ops:operator>
199 <ops:value>{8:s}</ops:value>
200 <ops:valueType>NUMERIC</ops:valueType>
201 <ops:instanced>false</ops:instanced>
202 <ops:thresholdType>STATIC</ops:thresholdType>
205 </ops:symptom-definition>"""\
206 .format(str(symptom_params
['cancel_cycles']),str(symptom_params
['wait_cycles']),
207 symptom_params
['resource_kind_key'], symptom_params
['adapter_kind_key'],
208 symptom_params
['symptom_name'],symptom_params
['severity'],
209 symptom_params
['metric_key'],symptom_params
['operation'],
210 str(symptom_params
['threshold_value']))
212 resp
= requests
.post(self
.vrops_site
+ api_url
,
213 auth
=(self
.vrops_user
, self
.vrops_password
),
218 if resp
.status_code
!= 201:
219 log
.warn("Failed to create Symptom definition: {}, response {}"\
220 .format(symptom_params
['symptom_name'], resp
.content
))
223 symptom_xmlroot
= XmlElementTree
.fromstring(resp
.content
)
224 if symptom_xmlroot
is not None and 'id' in symptom_xmlroot
.attrib
:
225 symptom_id
= symptom_xmlroot
.attrib
['id']
229 except Exception as exp
:
230 log
.warn("Error creating symptom definition : {}\n{}"\
231 .format(exp
, traceback
.format_exc()))
234 def create_alarm_definition(self
, alarm_params
):
236 Create an alarm definition in vROPs
239 'description':Alarm description,
240 'adapterKindKey': Adapter type in vROPs "VMWARE",
241 'resourceKindKey':Resource type in vROPs "VirtualMachine",
242 'waitCycles': No of wait cycles,
243 'cancelCycles': No of cancel cycles,
245 'subType': Alarm subtype,
246 'severity': Severity in vROPs "CRITICAL",
247 'symptomDefinitionId':symptom Definition uuid,
248 'impact': impact 'risk'
250 'alarm_uuid': returns alarm uuid
256 api_url
= '/suite-api/api/alertdefinitions'
257 headers
= {'Content-Type': 'application/xml'}
258 data
= """<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
259 <ops:alert-definition xmlns:xs="http://www.w3.org/2001/XMLSchema"
260 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
261 xmlns:ops="http://webservice.vmware.com/vRealizeOpsMgr/1.0/">
262 <ops:name>{0:s}</ops:name>
263 <ops:description>{1:s}</ops:description>
264 <ops:adapterKindKey>{2:s}</ops:adapterKindKey>
265 <ops:resourceKindKey>{3:s}</ops:resourceKindKey>
266 <ops:waitCycles>1</ops:waitCycles>
267 <ops:cancelCycles>1</ops:cancelCycles>
268 <ops:type>{4:s}</ops:type>
269 <ops:subType>{5:s}</ops:subType>
271 <ops:state severity="{6:s}">
273 <ops:symptomDefinitionIds>
274 <ops:symptomDefinitionId>{7:s}</ops:symptomDefinitionId>
275 </ops:symptomDefinitionIds>
276 <ops:relation>SELF</ops:relation>
277 <ops:aggregation>ALL</ops:aggregation>
278 <ops:symptomSetOperator>AND</ops:symptomSetOperator>
281 <ops:impactType>BADGE</ops:impactType>
282 <ops:detail>{8:s}</ops:detail>
286 </ops:alert-definition>"""\
287 .format(alarm_params
['name'],alarm_params
['description'],
288 alarm_params
['adapterKindKey'],alarm_params
['resourceKindKey'],
289 str(alarm_params
['type']),str(alarm_params
['subType']),
290 alarm_params
['severity'],alarm_params
['symptomDefinitionId'],
291 alarm_params
['impact'])
293 resp
= requests
.post(self
.vrops_site
+ api_url
,
294 auth
=(self
.vrops_user
, self
.vrops_password
),
299 if resp
.status_code
!= 201:
300 log
.warn("Failed to create Alarm definition: {}, response {}"\
301 .format(alarm_params
['name'], resp
.content
))
304 alarm_xmlroot
= XmlElementTree
.fromstring(resp
.content
)
305 for child
in alarm_xmlroot
:
306 if child
.tag
.split("}")[1] == 'id':
307 alarm_uuid
= child
.text
311 except Exception as exp
:
312 log
.warn("Error creating alarm definition : {}\n{}".format(exp
, traceback
.format_exc()))
315 def configure_rest_plugin(self
):
317 Creates REST Plug-in for vROPs outbound alerts
322 plugin_name
= 'MON_module_REST_Plugin'
323 plugin_id
= self
.check_if_plugin_configured(plugin_name
)
325 #If REST plugin not configured, configure it
326 if plugin_id
is not None:
329 #To Do - Add actual webhook url
330 webhook_url
= "https://mano-dev-1:8080/notify/" #for testing
331 cert_file_string
= open("10.172.137.214.cert", "rb").read() #for testing
332 cert
= load_certificate(FILETYPE_PEM
, cert_file_string
)
333 certificate
= cert
.digest("sha1")
334 api_url
= '/suite-api/api/alertplugins'
335 headers
= {'Content-Type': 'application/xml'}
336 data
= """<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
337 <ops:notification-plugin version="0" xmlns:xs="http://www.w3.org/2001/XMLSchema"
338 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
339 xmlns:ops="http://webservice.vmware.com/vRealizeOpsMgr/1.0/">
340 <ops:pluginTypeId>RestPlugin</ops:pluginTypeId>
341 <ops:name>{0:s}</ops:name>
343 <ops:configValue name="Url">{1:s}</ops:configValue>
344 <ops:configValue name="Content-type">application/xml</ops:configValue>
345 <ops:configValue name="Certificate">{2:s}</ops:configValue>
346 <ops:configValue name="ConnectionCount">20</ops:configValue>
348 </ops:notification-plugin>""".format(plugin_name
, webhook_url
, certificate
)
350 resp
= requests
.post(self
.vrops_site
+ api_url
,
351 auth
=(self
.vrops_user
, self
.vrops_password
),
356 if resp
.status_code
is not 201:
357 log
.warn("Failed to create REST Plugin: {} for url: {}, \nresponse code: {},"\
358 "\nresponse content: {}".format(plugin_name
, webhook_url
,\
359 resp
.status_code
, resp
.content
))
362 plugin_xmlroot
= XmlElementTree
.fromstring(resp
.content
)
363 if plugin_xmlroot
is not None:
364 for child
in plugin_xmlroot
:
365 if child
.tag
.split("}")[1] == 'pluginId':
366 plugin_id
= plugin_xmlroot
.find('{http://webservice.vmware.com/vRealizeOpsMgr/1.0/}pluginId').text
368 if plugin_id
is None:
369 log
.warn("Failed to get REST Plugin ID for {}, url: {}".format(plugin_name
, webhook_url
))
372 log
.info("Created REST Plugin: {} with ID : {} for url: {}".format(plugin_name
, plugin_id
, webhook_url
))
373 status
= self
.enable_rest_plugin(plugin_id
, plugin_name
)
375 log
.warn("Failed to enable created REST Plugin: {} for url: {}".format(plugin_name
, webhook_url
))
378 log
.info("Enabled REST Plugin: {} for url: {}".format(plugin_name
, webhook_url
))
381 def check_if_plugin_configured(self
, plugin_name
):
382 """Check if the REST plugin is already created
383 Returns: plugin_id: if already created, None: if needs to be created
386 #Find the REST Plugin id details for - MON_module_REST_Plugin
387 api_url
= '/suite-api/api/alertplugins'
388 headers
= {'Accept': 'application/xml'}
389 namespace
= {'params':"http://webservice.vmware.com/vRealizeOpsMgr/1.0/"}
391 resp
= requests
.get(self
.vrops_site
+ api_url
,
392 auth
=(self
.vrops_user
, self
.vrops_password
),
393 verify
= False, headers
= headers
)
395 if resp
.status_code
is not 200:
396 log
.warn("Failed to REST GET Alarm plugin details \nResponse code: {}\nResponse content: {}"\
397 .format(resp
.status_code
, resp
.content
))
400 # Look for specific plugin & parse pluginId for 'MON_module_REST_Plugin'
401 xmlroot_resp
= XmlElementTree
.fromstring(resp
.content
)
402 for notify_plugin
in xmlroot_resp
.findall('params:notification-plugin',namespace
):
403 if notify_plugin
.find('params:name',namespace
) is not None and\
404 notify_plugin
.find('params:pluginId',namespace
) is not None:
405 if notify_plugin
.find('params:name',namespace
).text
== plugin_name
:
406 plugin_id
= notify_plugin
.find('params:pluginId',namespace
).text
408 if plugin_id
is None:
409 log
.warn("REST plugin {} not found".format('MON_module_REST_Plugin'))
412 log
.info("Found REST Plugin: {}".format(plugin_name
))
416 def enable_rest_plugin(self
, plugin_id
, plugin_name
):
418 Enable the REST plugin using plugin_id
419 Params: plugin_id: plugin ID string that is to be enabled
420 Returns: status (Boolean) - True for success, False for failure
423 if plugin_id
is None or plugin_name
is None:
424 log
.debug("enable_rest_plugin() : Plugin ID or plugin_name not provided for {} plugin"\
425 .format(plugin_name
))
429 api_url
= "/suite-api/api/alertplugins/{}/enable/True".format(plugin_id
)
431 resp
= requests
.put(self
.vrops_site
+ api_url
,
432 auth
=(self
.vrops_user
, self
.vrops_password
),
435 if resp
.status_code
is not 204:
436 log
.warn("Failed to enable REST plugin {}. \nResponse code {}\nResponse Content: {}"\
437 .format(plugin_name
, resp
.status_code
, resp
.content
))
440 log
.info("Enabled REST plugin {}.".format(plugin_name
))
443 except Exception as exp
:
444 log
.warn("Error enabling REST plugin for {} plugin: Exception: {}\n{}"\
445 .format(plugin_name
, exp
, traceback
.format_exc()))
447 def create_alarm_notification(self
, alarm_name
, alarm_id
, resource_id
):
449 Create notification for each alarm
456 notification_id: notification_id or None
458 notification_name
= 'notify_' + alarm_name
459 notification_id
= None
460 plugin_name
= 'MON_module_REST_Plugin'
462 #1) Find the REST Plugin id details for - MON_module_REST_Plugin
463 plugin_id
= self
.check_if_plugin_configured(plugin_name
)
464 if plugin_id
is None:
465 log
.warn("Failed to get REST plugin_id for : {}".format('MON_module_REST_Plugin'))
468 #2) Create Alarm notification rule
469 api_url
= '/suite-api/api/notifications/rules'
470 headers
= {'Content-Type': 'application/xml'}
471 data
= """<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
472 <ops:notification-rule xmlns:xs="http://www.w3.org/2001/XMLSchema"
473 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
474 xmlns:ops="http://webservice.vmware.com/vRealizeOpsMgr/1.0/">
475 <ops:name>{0:s}</ops:name>
476 <ops:pluginId>{1:s}</ops:pluginId>
477 <ops:resourceFilter resourceId="{2:s}">
478 <ops:matchResourceIdOnly>true</ops:matchResourceIdOnly>
479 </ops:resourceFilter>
480 <ops:alertDefinitionIdFilters>
481 <ops:values>{3:s}</ops:values>
482 </ops:alertDefinitionIdFilters>
483 </ops:notification-rule>"""\
484 .format(notification_name
, plugin_id
, resource_id
, alarm_id
)
486 resp
= requests
.post(self
.vrops_site
+ api_url
,
487 auth
=(self
.vrops_user
, self
.vrops_password
),
492 if resp
.status_code
is not 201:
493 log
.warn("Failed to create Alarm notification {} for {} alarm."\
494 "\nResponse code: {}\nResponse content: {}"\
495 .format(notification_name
, alarm_name
, resp
.status_code
, resp
.content
))
498 #parse notification id from response
499 xmlroot_resp
= XmlElementTree
.fromstring(resp
.content
)
500 if xmlroot_resp
is not None and 'id' in xmlroot_resp
.attrib
:
501 notification_id
= xmlroot_resp
.attrib
.get('id')
503 log
.info("Created Alarm notification rule {} for {} alarm.".format(notification_name
, alarm_name
))
504 return notification_id
506 def get_vm_moref_id(self
, vapp_uuid
):
508 Get the moref_id of given VM
512 vm_details
= self
.get_vapp_details_rest(vapp_uuid
)
513 if vm_details
and "vm_vcenter_info" in vm_details
:
514 vm_moref_id
= vm_details
["vm_vcenter_info"].get("vm_moref_id", None)
516 log
.info("Found vm_moref_id: {} for vApp UUID: {}".format(vm_moref_id
, vapp_uuid
))
519 except Exception as exp
:
520 log
.warn("Error occurred while getting VM moref ID for VM : {}\n{}"\
521 .format(exp
, traceback
.format_exc()))
524 def get_vapp_details_rest(self
, vapp_uuid
=None):
526 Method retrieve vapp detail from vCloud director
529 vapp_uuid - is vapp identifier.
532 Returns VM MOref ID or return None
538 vca
= self
.connect_as_admin()
541 log
.warn("connect() to vCD is failed")
542 if vapp_uuid
is None:
545 url_list
= [vca
.host
, '/api/vApp/vapp-', vapp_uuid
]
546 get_vapp_restcall
= ''.join(url_list
)
548 if vca
.vcloud_session
and vca
.vcloud_session
.organization
:
549 response
= requests
.get(get_vapp_restcall
,
550 headers
=vca
.vcloud_session
.get_vcloud_headers(),
553 if response
.status_code
!= 200:
554 log
.warn("REST API call {} failed. Return status code {}"\
555 .format(get_vapp_restcall
, response
.content
))
556 return parsed_respond
559 xmlroot_respond
= XmlElementTree
.fromstring(response
.content
)
561 namespaces
= {'vm': 'http://www.vmware.com/vcloud/v1.5',
562 "vmext":"http://www.vmware.com/vcloud/extension/v1.5",
563 "xmlns":"http://www.vmware.com/vcloud/v1.5"
566 # parse children section for other attrib
567 children_section
= xmlroot_respond
.find('vm:Children/', namespaces
)
568 if children_section
is not None:
569 vCloud_extension_section
= children_section
.find('xmlns:VCloudExtension', namespaces
)
570 if vCloud_extension_section
is not None:
572 vim_info
= vCloud_extension_section
.find('vmext:VmVimInfo', namespaces
)
573 vmext
= vim_info
.find('vmext:VmVimObjectRef', namespaces
)
574 if vmext
is not None:
575 vm_vcenter_info
["vm_moref_id"] = vmext
.find('vmext:MoRef', namespaces
).text
576 parsed_respond
["vm_vcenter_info"]= vm_vcenter_info
578 except Exception as exp
:
579 log
.warn("Error occurred calling rest api for getting vApp details: {}\n{}"\
580 .format(exp
, traceback
.format_exc()))
582 return parsed_respond
585 def connect_as_admin(self
):
586 """ Method connect as pvdc admin user to vCloud director.
587 There are certain action that can be done only by provider vdc admin user.
588 Organization creation / provider network creation etc.
591 The return vca object that letter can be used to connect to vcloud direct as admin for provider vdc
594 log
.info("Logging in to a VCD org as admin.")
596 vca_admin
= VCA(host
=self
.vcloud_site
,
597 username
=self
.admin_username
,
598 service_type
='standalone',
602 result
= vca_admin
.login(password
=self
.admin_password
, org
='System')
604 log
.warn("Can't connect to a vCloud director as: {}".format(self
.admin_username
))
605 result
= vca_admin
.login(token
=vca_admin
.token
, org
='System', org_url
=vca_admin
.vcloud_session
.org_url
)
607 log
.info("Successfully logged to a vcloud direct org: {} as user: {}"\
608 .format('System', self
.admin_username
))
613 def get_vm_resource_id(self
, vm_moref_id
):
614 """ Find resource ID in vROPs using vm_moref_id
616 if vm_moref_id
is None:
619 api_url
= '/suite-api/api/resources'
620 headers
= {'Accept': 'application/xml'}
621 namespace
= {'params':"http://webservice.vmware.com/vRealizeOpsMgr/1.0/"}
623 resp
= requests
.get(self
.vrops_site
+ api_url
,
624 auth
=(self
.vrops_user
, self
.vrops_password
),
625 verify
= False, headers
= headers
)
627 if resp
.status_code
is not 200:
628 log
.warn("Failed to get resource details from vROPs for {}\nResponse code:{}\nResponse Content: {}"\
629 .format(vm_moref_id
, resp
.status_code
, resp
.content
))
633 xmlroot_respond
= XmlElementTree
.fromstring(resp
.content
)
634 for resource
in xmlroot_respond
.findall('params:resource',namespace
):
635 if resource
is not None:
636 resource_key
= resource
.find('params:resourceKey',namespace
)
637 if resource_key
is not None:
638 if resource_key
.find('params:adapterKindKey',namespace
).text
== 'VMWARE' and \
639 resource_key
.find('params:resourceKindKey',namespace
).text
== 'VirtualMachine':
640 for child
in resource_key
:
641 if child
.tag
.split('}')[1]=='resourceIdentifiers':
642 resourceIdentifiers
= child
643 for r_id
in resourceIdentifiers
:
644 if r_id
.find('params:value',namespace
).text
== vm_moref_id
:
645 log
.info("Found Resource ID : {} in vROPs for {}"\
646 .format(resource
.attrib
['identifier'], vm_moref_id
))
647 return resource
.attrib
['identifier']
648 except Exception as exp
:
649 log
.warn("Error in parsing {}\n{}".format(exp
, traceback
.format_exc()))
652 def get_metrics_data(self
, metric
={}):
653 """Get an individual metric's data of a resource.
655 'metric_name': Normalized name of metric (string)
656 'resource_uuid': Resource UUID (string)
657 'period': Time period in Period Unit for which metrics data to be collected from
658 Monitoring tool from now.
659 'period_unit': Period measurement unit can be one of 'HR', 'DAY', 'MONTH', 'YEAR'
661 Return a dict that contains:
662 'metric_name': Normalized name of metric (string)
663 'resource_uuid': Resource UUID (string)
664 'tenant_id': tenent id name in which the resource is present in string format
665 'metrics_data': Dictionary containing time_series & metric_series data.
666 'time_series': List of individual time stamp values in msec
667 'metric_series': List of individual metrics data values
668 Raises an exception upon error or when network is not found
671 return_data
['schema_version'] = 1.0
672 return_data
['schema_type'] = 'read_metric_data_response'
673 return_data
['metric_name'] = metric
['metric_name']
674 #To do - No metric_uuid in vROPs, thus returning metric_name
675 return_data
['metric_uuid'] = metric
['metric_name']
676 return_data
['correlation_id'] = metric
['correlation_id']
677 return_data
['resource_uuid'] = metric
['resource_uuid']
678 return_data
['metrics_data'] = {'time_series':[], 'metric_series':[]}
679 #To do - Need confirmation about uuid & id
680 return_data
['tenant_uuid'] = metric
['tenant_uuid']
681 return_data
['unit'] = None
682 #return_data['tenant_id'] = self.tenant_id
683 #log.warn("return_data: {}".format(return_data))
685 #1) Get metric details from plugin specific file & format it into vROPs metrics
686 metric_key_params
= self
.get_default_Params(metric
['metric_name'])
688 if not metric_key_params
:
689 log
.warn("Metric not supported: {}".format(metric
['metric_name']))
690 #To Do: Return message
693 return_data
['unit'] = metric_key_params
['unit']
695 #2) Find the resource id in vROPs based on OSM resource_uuid
696 #2.a) Find vm_moref_id from vApp uuid in vCD
697 vm_moref_id
= self
.get_vm_moref_id(metric
['resource_uuid'])
698 if vm_moref_id
is None:
699 log
.warn("Failed to find vm morefid for vApp in vCD: {}".format(config_dict
['resource_uuid']))
701 #2.b) Based on vm_moref_id, find VM's corresponding resource_id in vROPs to set notification
702 resource_id
= self
.get_vm_resource_id(vm_moref_id
)
703 if resource_id
is None:
704 log
.warn("Failed to find resource in vROPs: {}".format(config_dict
['resource_uuid']))
707 #3) Calculate begin & end time for period & period unit
708 end_time
= int(round(time
.time() * 1000))
709 if metric
['collection_unit'] == 'YR':
710 time_diff
= PERIOD_MSEC
[metric
['collection_unit']]
712 time_diff
= metric
['collection_period']* PERIOD_MSEC
[metric
['collection_unit']]
713 begin_time
= end_time
- time_diff
715 #4) Get the metrics data
716 log
.info("metric_key_params['metric_key'] = {}".format(metric_key_params
['metric_key']))
717 log
.info("end_time: {}, begin_time: {}".format(end_time
, begin_time
))
719 url_list
= ['/suite-api/api/resources/', resource_id
, '/stats?statKey=',\
720 metric_key_params
['metric_key'], '&begin=', str(begin_time
),'&end=',str(end_time
)]
721 api_url
= ''.join(url_list
)
722 headers
= {'Accept': 'application/json'}
724 resp
= requests
.get(self
.vrops_site
+ api_url
,
725 auth
=(self
.vrops_user
, self
.vrops_password
),
726 verify
= False, headers
= headers
)
728 if resp
.status_code
is not 200:
729 log
.warn("Failed to retrive Metric data from vROPs for {}\nResponse code:{}\nResponse Content: {}"\
730 .format(metric
['metric_name'], resp
.status_code
, resp
.content
))
733 #5) Convert to required format
735 json_data
= json
.loads(resp
.content
)
736 for resp_key
,resp_val
in json_data
.iteritems():
737 if resp_key
== 'values':
738 data
= json_data
['values'][0]
739 for data_k
,data_v
in data
.iteritems():
740 if data_k
== 'stat-list':
742 for stat_list_k
,stat_list_v
in stat_list
.iteritems():
743 for stat_keys
,stat_vals
in stat_list_v
[0].iteritems():
744 if stat_keys
== 'timestamps':
745 metrics_data
['time_series'] = stat_list_v
[0]['timestamps']
746 if stat_keys
== 'data':
747 metrics_data
['metric_series'] = stat_list_v
[0]['data']
749 return_data
['metrics_data'] = metrics_data
753 def reconfigure_alarm(self
, config_dict
):