Add a new monitor type: MultiMonitor that can run any number of other monitors at...
[yardstick.git] / yardstick / benchmark / scenarios / availability / monitor / monitor_multi.py
1 ##############################################################################
2 # Copyright (c) 2017 Huan Li and others
3 # lihuansse@tongji.edu.cn
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
10 from __future__ import absolute_import
11 import logging
12 import time
13
14 from yardstick.benchmark.scenarios.availability.monitor import basemonitor
15
16 LOG = logging.getLogger(__name__)
17
18
19 class MultiMonitor(basemonitor.BaseMonitor):
20
21     __monitor_type__ = "multi-monitor"
22
23     def __init__(self, config, context):
24         super(MultiMonitor, self).__init__(config, context)
25
26         self.monitors = []
27         monitor_type = self._config["monitor_type"]
28         monitor_cls = basemonitor.BaseMonitor.get_monitor_cls(monitor_type)
29
30         monitor_number = self._config.get("monitor_number", 1)
31         for i in range(monitor_number):
32             monitor_ins = monitor_cls(self._config, self._context)
33             self.monitors.append(monitor_ins)
34
35     def start_monitor(self):
36         for monitor in self.monitors:
37             monitor.start_monitor()
38
39     def wait_monitor(self):
40         for monitor in self.monitors:
41             monitor.wait_monitor()
42
43     def verify_SLA(self):
44         first_outage = time.time()
45         last_outage = 0
46
47         for monitor in self.monitors:
48             monitor_result = monitor.result()
49             monitor_first_outage = monitor_result.get('first_outage', None)
50             monitor_last_outage = monitor_result.get('last_outage', None)
51
52             if monitor_first_outage is None or monitor_last_outage is None:
53                 continue
54
55             if monitor_first_outage < first_outage:
56                 first_outage = monitor_first_outage
57
58             if monitor_last_outage > last_outage:
59                 last_outage = monitor_last_outage
60         LOG.debug("multi monitor result: %f , %f", first_outage, last_outage)
61
62         outage_time = last_outage - first_outage
63         max_outage_time = self._config["sla"]["max_outage_time"]
64         if outage_time > max_outage_time:
65             LOG.error("SLA failure: %f > %f", outage_time, max_outage_time)
66             return False
67         else:
68             return True