1 ##############################################################################
2 # Copyright (c) 2017 ZTE Corporation and others.
4 # All rights reserved. This program and the accompanying materials
5 # are made available under the terms of the Apache License, Version 2.0
6 # which accompanies this distribution, and is available at
7 # http://www.apache.org/licenses/LICENSE-2.0
8 ##############################################################################
9 from oslo_config import cfg
11 from doctor_tests.identity_auth import get_identity_auth
12 from doctor_tests.identity_auth import get_session
13 from doctor_tests.os_clients import aodh_client
14 from doctor_tests.os_clients import nova_client
17 cfg.StrOpt('alarm_basename',
18 default='doctor_alarm',
19 help='the base name of alarm',
26 def __init__(self, conf, log):
29 self.auth = get_identity_auth(project=self.conf.doctor_project)
31 aodh_client(conf.aodh_version,
32 get_session(auth=self.auth))
34 nova_client(conf.nova_version,
35 get_session(auth=self.auth))
36 self._init_alarm_name()
38 def _init_alarm_name(self):
40 for i in range(0, self.conf.instance_count):
41 alarm_name = '%s%d' % (self.conf.alarm_basename, i)
42 self.alarm_names.append(alarm_name)
45 self.log.info('alarm create start......')
47 alarms = {alarm['name']: alarm for alarm in self.aodh.alarm.list()}
49 {getattr(server, 'name'): server
50 for server in self.nova.servers.list()}
52 for i in range(0, self.conf.instance_count):
53 alarm_name = self.alarm_names[i]
54 if alarm_name in alarms:
56 vm_name = '%s%d' % (self.conf.instance_basename, i)
57 vm_id = getattr(servers[vm_name], 'id')
60 description=u'VM failure',
62 alarm_actions=[u'http://%s:%d/failure'
63 % (self.conf.consumer.ip,
64 self.conf.consumer.port)],
69 event_type=u'compute.instance.update',
71 dict(field=u'traits.instance_id',
75 dict(field=u'traits.state',
79 self.aodh.alarm.create(alarm_request)
81 self.log.info('alarm create end......')
84 self.log.info('alarm delete start.......')
86 alarms = {alarm['name']: alarm for alarm in self.aodh.alarm.list()}
87 for alarm_name in self.alarm_names:
88 if alarm_name in alarms:
89 self.aodh.alarm.delete(alarms[alarm_name]['alarm_id'])
91 del self.alarm_names[:]
93 self.log.info('alarm delete end.......')