The secondi HA test case-shutdown controller
[yardstick.git] / yardstick / benchmark / scenarios / availability / monitor / monitor_command.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 subprocess
11 import traceback
12 import yardstick.ssh as ssh
13 import basemonitor as basemonitor
14
15 LOG = logging.getLogger(__name__)
16
17
18 def _execute_shell_command(command):
19     '''execute shell script with error handling'''
20     exitcode = 0
21     output = []
22     try:
23         output = subprocess.check_output(command, shell=True)
24     except Exception:
25         exitcode = -1
26         output = traceback.format_exc()
27         LOG.error("exec command '%s' error:\n " % command)
28         LOG.error(traceback.format_exc())
29
30     return exitcode, output
31
32
33 class MonitorOpenstackCmd(basemonitor.BaseMonitor):
34     """docstring for MonitorApi"""
35
36     __monitor_type__ = "openstack-cmd"
37
38     def setup(self):
39         self.connection = None
40         node_name = self._config.get("host", None)
41         if node_name:
42             host = self._context[node_name]
43             ip = host.get("ip", None)
44             user = host.get("user", "root")
45             key_filename = host.get("key_filename", "~/.ssh/id_rsa")
46
47             self.connection = ssh.SSH(user, ip, key_filename=key_filename)
48             self.connection.wait(timeout=600)
49             LOG.debug("ssh host success!")
50
51         self.check_script = self.get_script_fullpath(
52             "ha_tools/check_openstack_cmd.bash")
53
54         self.cmd = self._config["command_name"]
55
56     def monitor_func(self):
57         exit_status = 0
58         if self.connection:
59             exit_status, stdout, stderr = self.connection.execute(
60                 "/bin/bash -s '{0}'".format(self.cmd),
61                 stdin=open(self.check_script, "r"))
62
63             LOG.debug("the ret stats: %s stdout: %s stderr: %s" %
64                       (exit_status, stdout, stderr))
65         else:
66             exit_status, stdout = _execute_shell_command(self.cmd)
67         if exit_status:
68             return False
69         return True
70
71     def verify_SLA(self):
72         outage_time = self._result.get('outage_time', None)
73         LOG.debug("the _result:%s" % self._result)
74         max_outage_time = self._config["sla"]["max_outage_time"]
75         if outage_time > max_outage_time:
76             LOG.info("SLA failure: %f > %f" % (outage_time, max_outage_time))
77             return False
78         else:
79             LOG.info("the sla is passed")
80             return True
81
82
83 def _test():    # pragma: no cover
84     host = {
85         "ip": "192.168.235.22",
86         "user": "root",
87         "key_filename": "/root/.ssh/id_rsa"
88     }
89     context = {"node1": host}
90     monitor_configs = []
91     config = {
92         'monitor_type': 'openstack-cmd',
93         'command_name': 'nova image-list',
94         'monitor_time': 1,
95         'host': 'node1',
96         'sla': {'max_outage_time': 5}
97     }
98     monitor_configs.append(config)
99
100     p = basemonitor.MonitorMgr()
101     p.init_monitors(monitor_configs, context)
102     p.start_monitors()
103     p.wait_monitors()
104     p.verify_SLA()
105
106
107 if __name__ == '__main__':    # pragma: no cover
108     _test()