fix referring neutron option in inspector
[doctor.git] / doctor_tests / alarm.py
1 ##############################################################################
2 # Copyright (c) 2017 ZTE Corporation and others.
3 #
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
10
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
15
16 OPTS = [
17     cfg.StrOpt('alarm_basename',
18                default='doctor_alarm',
19                help='the base name of alarm',
20                required=True),
21 ]
22
23
24 class Alarm(object):
25
26     def __init__(self, conf, log):
27         self.conf = conf
28         self.log = log
29         self.auth = get_identity_auth(project=self.conf.doctor_project)
30         self.aodh = \
31             aodh_client(conf.aodh_version,
32                         get_session(auth=self.auth))
33         self.nova = \
34             nova_client(conf.nova_version,
35                         get_session(auth=self.auth))
36         self._init_alarm_name()
37
38     def _init_alarm_name(self):
39         self.alarm_names = []
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)
43
44     def create(self):
45         self.log.info('alarm create start......')
46
47         alarms = {alarm['name']: alarm for alarm in self.aodh.alarm.list()}
48         servers = \
49             {getattr(server, 'name'): server
50              for server in self.nova.servers.list()}
51
52         for i in range(0, self.conf.instance_count):
53             alarm_name = self.alarm_names[i]
54             if alarm_name in alarms:
55                 continue;
56             vm_name = '%s%d' % (self.conf.instance_basename, i)
57             vm_id = getattr(servers[vm_name], 'id')
58             alarm_request = dict(
59                 name=alarm_name,
60                 description=u'VM failure',
61                 enabled=True,
62                 alarm_actions=[u'http://%s:%d/failure'
63                                % (self.conf.consumer.ip,
64                                   self.conf.consumer.port)],
65                 repeat_actions=False,
66                 severity=u'moderate',
67                 type=u'event',
68                 event_rule=dict(
69                     event_type=u'compute.instance.update',
70                     query=[
71                         dict(field=u'traits.instance_id',
72                              type='',
73                              op=u'eq',
74                              value=vm_id),
75                         dict(field=u'traits.state',
76                              type='',
77                              op=u'eq',
78                              value=u'error')]))
79             self.aodh.alarm.create(alarm_request)
80
81         self.log.info('alarm create end......')
82
83     def delete(self):
84         self.log.info('alarm delete start.......')
85
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'])
90
91         del self.alarm_names[:]
92
93         self.log.info('alarm delete end.......')