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