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