64e12f1dd37d806dee434ebb8bbaeded00dc2322
[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         key_filename = host.get("key_filename", "~/.ssh/id_rsa")
27
28         self.connection = ssh.SSH(user, ip, key_filename=key_filename)
29         self.connection.wait(timeout=600)
30         LOG.debug("ssh host success!")
31         self.check_script = self.get_script_fullpath(
32             "script_tools/check_service.bash")
33         self.process_name = self._config["process_name"]
34
35     def monitor_func(self):
36         exit_status, stdout, stderr = self.connection.execute(
37             "/bin/sh -s {0}".format(self.process_name),
38             stdin=open(self.check_script, "r"))
39
40         if stdout and "running" in stdout:
41             LOG.info("check the envrioment success!")
42             return True
43         else:
44             LOG.error(
45                 "the host envrioment is error, stdout:%s, stderr:%s" %
46                 (stdout, stderr))
47         return False
48
49     def verify_SLA(self):
50         outage_time = self._result.get('outage_time', None)
51         max_outage_time = self._config["sla"]["max_recover_time"]
52         if outage_time > max_outage_time:
53             LOG.error("SLA failure: %f > %f" % (outage_time, max_outage_time))
54             return False
55         else:
56             return True
57
58
59 def _test():    # pragma: no cover
60     host = {
61         "ip": "10.20.0.5",
62         "user": "root",
63         "key_filename": "/root/.ssh/id_rsa"
64     }
65     context = {"node1": host}
66     monitor_configs = []
67     config = {
68         'monitor_type': 'process',
69         'process_name': 'nova-api',
70         'host': "node1",
71         'monitor_time': 1,
72         'SLA': {'max_recover_time': 5}
73     }
74     monitor_configs.append(config)
75
76     p = basemonitor.MonitorMgr()
77     p.init_monitors(monitor_configs, context)
78     p.start_monitors()
79     p.wait_monitors()
80     p.verify()
81
82
83 if __name__ == '__main__':    # pragma: no cover
84     _test()