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