1 ##############################################################################
2 # Copyright (c) 2015 Huawei Technologies Co.,Ltd. and others
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
14 import yardstick.ssh as ssh
15 from yardstick.benchmark.scenarios.availability.monitor import basemonitor
17 LOG = logging.getLogger(__name__)
20 def _execute_shell_command(command):
21 """execute shell script with error handling"""
25 output = subprocess.check_output(command, shell=True)
28 output = traceback.format_exc()
29 LOG.error("exec command '%s' error:\n ", command)
30 LOG.error(traceback.format_exc())
32 return exitcode, output
35 class MonitorOpenstackCmd(basemonitor.BaseMonitor):
36 """docstring for MonitorApi"""
38 __monitor_type__ = "openstack-cmd"
41 self.connection = None
42 node_name = self._config.get("host", None)
44 host = self._context[node_name]
46 self.connection = ssh.SSH.from_node(host,
47 defaults={"user": "root"})
48 self.connection.wait(timeout=600)
49 LOG.debug("ssh host success!")
51 self.check_script = self.get_script_fullpath(
52 "ha_tools/check_openstack_cmd.bash")
54 self.cmd = self._config["command_name"]
56 def monitor_func(self):
59 with open(self.check_script, "r") as stdin_file:
60 exit_status, stdout, stderr = self.connection.execute(
61 "/bin/bash -s '{0}'".format(self.cmd),
64 LOG.debug("the ret stats: %s stdout: %s stderr: %s",
65 exit_status, stdout, stderr)
67 exit_status, stdout = _execute_shell_command(self.cmd)
73 outage_time = self._result.get('outage_time', None)
74 LOG.debug("the _result:%s", self._result)
75 max_outage_time = self._config["sla"]["max_outage_time"]
76 if outage_time > max_outage_time:
77 LOG.info("SLA failure: %f > %f", outage_time, max_outage_time)
80 LOG.info("the sla is passed")
84 def _test(): # pragma: no cover
86 "ip": "192.168.235.22",
88 "key_filename": "/root/.ssh/id_rsa"
90 context = {"node1": host}
93 'monitor_type': 'openstack-cmd',
94 'command_name': 'nova image-list',
97 'sla': {'max_outage_time': 5}
99 monitor_configs.append(config)
101 p = basemonitor.MonitorMgr()
102 p.init_monitors(monitor_configs, context)
108 if __name__ == '__main__': # pragma: no cover