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