use context manager for stdin files and use _put_file_shell
[yardstick.git] / yardstick / benchmark / scenarios / availability / monitor / monitor_general.py
1 ##############################################################################
2 # Copyright (c) 2016 Juan Qiu and others
3 # juan_ qiu@tongji.edu.cn
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 yardstick.ssh as ssh
11
12 import basemonitor as basemonitor
13 from yardstick.benchmark.scenarios.availability.util import buildshellparams
14
15
16 LOG = logging.getLogger(__name__)
17
18
19 class GeneralMonitor(basemonitor.BaseMonitor):
20     """docstring for MonitorApi"""
21
22     __monitor_type__ = "general-monitor"
23
24     def setup(self):
25         host = self._context[self._config["host"]]
26         ip = host.get("ip", None)
27         user = host.get("user", "root")
28         ssh_port = host.get("ssh_port", ssh.DEFAULT_PORT)
29         key_filename = host.get("key_filename", "~/.ssh/id_rsa")
30         self.key = self._config["key"]
31         self.monitor_key = self._config["monitor_key"]
32         self.monitor_type = self._config["monitor_type"]
33
34         if "parameter" in self._config:
35             parameter = self._config['parameter']
36             str = buildshellparams(parameter)
37             l = list(item for item in parameter.values())
38             self.cmd_param = str.format(*l)
39
40         self.monitor_cfg = basemonitor.BaseMonitor.monitor_cfgs.get(
41             self.monitor_key)
42         self.monitor_script = self.get_script_fullpath(
43             self.monitor_cfg['monitor_script'])
44         self.connection = ssh.SSH(user, ip, key_filename=key_filename,
45                                   port=ssh_port)
46         self.connection.wait(timeout=600)
47         LOG.debug("ssh host success!")
48
49     def monitor_func(self):
50         if "parameter" in self._config:
51             with open(self.monitor_script, "r") as stdin_file:
52                 exit_status, stdout, stderr = self.connection.execute(
53                     self.cmd_param,
54                     stdin=stdin_file)
55         else:
56             with open(self.monitor_script, "r") as stdin_file:
57                 exit_status, stdout, stderr = self.connection.execute(
58                     "/bin/bash -s ",
59                     stdin=stdin_file)
60
61         if exit_status:
62             return False
63         return True
64
65     def verify_SLA(self):
66         LOG.debug("the _result:%s", self._result)
67         outage_time = self._result.get('outage_time', None)
68         max_outage_time = self._config["sla"]["max_outage_time"]
69         if outage_time is None:
70             LOG.error("There is no outage_time in monitor result.")
71             return False
72         if outage_time > max_outage_time:
73             LOG.error("SLA failure: %f > %f", outage_time, max_outage_time)
74             return False
75         else:
76             return True