Merge "operation_general: fix logging to use %s"
[yardstick.git] / yardstick / benchmark / scenarios / availability / monitor / monitor_process.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 from __future__ import absolute_import
10 import logging
11 import yardstick.ssh as ssh
12
13 from yardstick.benchmark.scenarios.availability.monitor import basemonitor
14
15 LOG = logging.getLogger(__name__)
16
17
18 class MonitorProcess(basemonitor.BaseMonitor):
19     """docstring for MonitorApi"""
20
21     __monitor_type__ = "process"
22
23     def setup(self):
24         host = self._context[self._config["host"]]
25
26         self.connection = ssh.SSH.from_node(host, defaults={"user": "root"})
27         self.connection.wait(timeout=600)
28         LOG.debug("ssh host success!")
29         self.check_script = self.get_script_fullpath(
30             "ha_tools/check_process_python.bash")
31         self.process_name = self._config["process_name"]
32
33     def monitor_func(self):
34         with open(self.check_script, "r") as stdin_file:
35             exit_status, stdout, stderr = self.connection.execute(
36                 "/bin/sh -s {0}".format(self.process_name),
37                 stdin=stdin_file)
38         if not stdout or int(stdout) <= 0:
39             LOG.info("the process (%s) is not running!", self.process_name)
40             return False
41
42         return True
43
44     def verify_SLA(self):
45         LOG.debug("the _result:%s", self._result)
46         outage_time = self._result.get('outage_time', None)
47         max_outage_time = self._config["sla"]["max_recover_time"]
48         if outage_time > max_outage_time:
49             LOG.error("SLA failure: %f > %f", outage_time, max_outage_time)
50             return False
51         else:
52             return True
53
54
55 def _test():    # pragma: no cover
56     host = {
57         "ip": "10.20.0.5",
58         "user": "root",
59         "key_filename": "/root/.ssh/id_rsa"
60     }
61     context = {"node1": host}
62     monitor_configs = []
63     config = {
64         'monitor_type': 'process',
65         'process_name': 'nova-api',
66         'host': "node1",
67         'monitor_time': 1,
68         'sla': {'max_recover_time': 5}
69     }
70     monitor_configs.append(config)
71
72     p = basemonitor.MonitorMgr()
73     p.init_monitors(monitor_configs, context)
74     p.start_monitors()
75     p.wait_monitors()
76     p.verify_SLA()
77
78
79 if __name__ == '__main__':    # pragma: no cover
80     _test()