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