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