The secondi HA test case-shutdown controller
[yardstick.git] / yardstick / benchmark / scenarios / availability / monitor / basemonitor.py
1 ##############################################################################
2 # Copyright (c) 2015 Huawei Technologies Co.,Ltd. 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 import pkg_resources
10 import logging
11 import multiprocessing
12 import time
13 import os
14 import yardstick.common.utils as utils
15
16 LOG = logging.getLogger(__name__)
17
18 monitor_conf_path = pkg_resources.resource_filename(
19     "yardstick.benchmark.scenarios.availability",
20     "monitor_conf.yaml")
21
22
23 class MonitorMgr(object):
24     """docstring for MonitorMgr"""
25     def __init__(self):
26         self._monitor_list = []
27
28     def init_monitors(self, monitor_cfgs, context):
29         LOG.debug("monitorMgr config: %s" % monitor_cfgs)
30
31         for monitor_cfg in monitor_cfgs:
32             monitor_type = monitor_cfg["monitor_type"]
33             monitor_cls = BaseMonitor.get_monitor_cls(monitor_type)
34             monitor_ins = monitor_cls(monitor_cfg, context)
35
36             self._monitor_list.append(monitor_ins)
37
38     def start_monitors(self):
39         for _monotor_instace in self._monitor_list:
40             _monotor_instace.start_monitor()
41
42     def wait_monitors(self):
43         for monitor in self._monitor_list:
44             monitor.wait_monitor()
45
46     def verify_SLA(self):
47         sla_pass = True
48         for monitor in self._monitor_list:
49             sla_pass = sla_pass & monitor.verify_SLA()
50         return sla_pass
51
52
53 class BaseMonitor(multiprocessing.Process):
54     """docstring for BaseMonitor"""
55
56     def __init__(self, config, context):
57         multiprocessing.Process.__init__(self)
58         self._config = config
59         self._context = context
60         self._queue = multiprocessing.Queue()
61         self._event = multiprocessing.Event()
62         self.setup_done = False
63
64     @staticmethod
65     def get_monitor_cls(monitor_type):
66         '''return monitor class of specified type'''
67
68         for monitor in utils.itersubclasses(BaseMonitor):
69             if monitor_type == monitor.__monitor_type__:
70                 return monitor
71         raise RuntimeError("No such monitor_type %s" % monitor_type)
72
73     def get_script_fullpath(self, path):
74         base_path = os.path.dirname(monitor_conf_path)
75         return os.path.join(base_path, path)
76
77     def run(self):
78         LOG.debug("config:%s context:%s" % (self._config, self._context))
79
80         self.setup()
81         monitor_time = self._config.get("monitor_time", 0)
82
83         total_time = 0
84         outage_time = 0
85         total_count = 0
86         outage_count = 0
87         first_outage = 0
88         last_outage = 0
89
90         begin_time = time.time()
91         while True:
92             total_count = total_count + 1
93
94             one_check_begin_time = time.time()
95             exit_status = self.monitor_func()
96             one_check_end_time = time.time()
97
98             if exit_status is False:
99                 outage_count = outage_count + 1
100
101                 outage_time = outage_time + (
102                     one_check_end_time - one_check_begin_time)
103
104                 if not first_outage:
105                     first_outage = one_check_begin_time
106
107                 last_outage = one_check_end_time
108
109             if self._event.is_set():
110                 LOG.debug("the monitor process stop")
111                 break
112
113             if one_check_end_time - begin_time > monitor_time:
114                 LOG.debug("the monitor max_time finished and exit!")
115                 break
116
117         end_time = time.time()
118         total_time = end_time - begin_time
119
120         self._queue.put({"total_time": total_time,
121                          "outage_time": last_outage-first_outage,
122                          "total_count": total_count,
123                          "outage_count": outage_count})
124
125     def start_monitor(self):
126         self.start()
127
128     def wait_monitor(self):
129         self.join()
130         self._result = self._queue.get()
131         LOG.debug("the monitor result:%s" % self._result)
132
133     def setup(self):  # pragma: no cover
134         pass
135
136     def monitor_func(self):  # pragma: no cover
137         pass
138
139     def verify_SLA(self):
140         pass