Merge "Generate pod.yaml from current context"
[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         self.key = self._config["key"]
28         self.monitor_key = self._config["monitor_key"]
29         self.monitor_type = self._config["monitor_type"]
30
31         if "parameter" in self._config:
32             parameter = self._config['parameter']
33             str = buildshellparams(parameter)
34             l = list(item for item in parameter.values())
35             self.cmd_param = str.format(*l)
36
37         self.monitor_cfg = basemonitor.BaseMonitor.monitor_cfgs.get(
38             self.monitor_key)
39         self.monitor_script = self.get_script_fullpath(
40             self.monitor_cfg['monitor_script'])
41         self.connection = ssh.SSH.from_node(host, defaults={"user": "root"})
42         self.connection.wait(timeout=600)
43         LOG.debug("ssh host success!")
44
45     def monitor_func(self):
46         if "parameter" in self._config:
47             with open(self.monitor_script, "r") as stdin_file:
48                 exit_status, stdout, stderr = self.connection.execute(
49                     "sudo {}".format(self.cmd_param),
50                     stdin=stdin_file)
51         else:
52             with open(self.monitor_script, "r") as stdin_file:
53                 exit_status, stdout, stderr = self.connection.execute(
54                     "sudo /bin/bash -s ",
55                     stdin=stdin_file)
56
57         if exit_status:
58             return False
59         return True
60
61     def verify_SLA(self):
62         LOG.debug("the _result:%s", self._result)
63         outage_time = self._result.get('outage_time', None)
64         max_outage_time = self._config["sla"]["max_outage_time"]
65         if outage_time is None:
66             LOG.error("There is no outage_time in monitor result.")
67             return False
68         if outage_time > max_outage_time:
69             LOG.error("SLA failure: %f > %f", outage_time, max_outage_time)
70             return False
71         else:
72             return True