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 ##############################################################################
12 import yardstick.ssh as ssh
13 import basemonitor as basemonitor
15 LOG = logging.getLogger(__name__)
18 def _execute_shell_command(command):
19 '''execute shell script with error handling'''
23 output = subprocess.check_output(command, shell=True)
26 output = traceback.format_exc()
27 LOG.error("exec command '%s' error:\n ", command)
28 LOG.error(traceback.format_exc())
30 return exitcode, output
33 class MonitorOpenstackCmd(basemonitor.BaseMonitor):
34 """docstring for MonitorApi"""
36 __monitor_type__ = "openstack-cmd"
39 self.connection = None
40 node_name = self._config.get("host", None)
42 host = self._context[node_name]
43 ip = host.get("ip", None)
44 user = host.get("user", "root")
45 ssh_port = host.get("ssh_port", ssh.DEFAULT_PORT)
46 key_filename = host.get("key_filename", "~/.ssh/id_rsa")
48 self.connection = ssh.SSH(user, ip, key_filename=key_filename,
50 self.connection.wait(timeout=600)
51 LOG.debug("ssh host success!")
53 self.check_script = self.get_script_fullpath(
54 "ha_tools/check_openstack_cmd.bash")
56 self.cmd = self._config["command_name"]
58 def monitor_func(self):
61 with open(self.check_script, "r") as stdin_file:
62 exit_status, stdout, stderr = self.connection.execute(
63 "/bin/bash -s '{0}'".format(self.cmd),
66 LOG.debug("the ret stats: %s stdout: %s stderr: %s",
67 exit_status, stdout, stderr)
69 exit_status, stdout = _execute_shell_command(self.cmd)
75 outage_time = self._result.get('outage_time', None)
76 LOG.debug("the _result:%s", self._result)
77 max_outage_time = self._config["sla"]["max_outage_time"]
78 if outage_time > max_outage_time:
79 LOG.info("SLA failure: %f > %f", outage_time, max_outage_time)
82 LOG.info("the sla is passed")
86 def _test(): # pragma: no cover
88 "ip": "192.168.235.22",
90 "key_filename": "/root/.ssh/id_rsa"
92 context = {"node1": host}
95 'monitor_type': 'openstack-cmd',
96 'command_name': 'nova image-list',
99 'sla': {'max_outage_time': 5}
101 monitor_configs.append(config)
103 p = basemonitor.MonitorMgr()
104 p.init_monitors(monitor_configs, context)
110 if __name__ == '__main__': # pragma: no cover