Merge "Creating a generic monitor"
[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         key_filename = host.get("key_filename", "~/.ssh/id_rsa")
29         self.key = self._config["key"]
30         self.monitor_type = self._config["monitor_type"]
31
32         if "parameter" in self._config:
33             parameter = self._config['parameter']
34             str = buildshellparams(parameter)
35             l = list(item for item in parameter.values())
36             self.cmd_param = str.format(*l)
37
38         self.monitor_cfg = basemonitor.BaseMonitor.monitor_cfgs.get(self.key)
39         self.monitor_script = self.get_script_fullpath(
40             self.monitor_cfg['monitor_script'])
41         self.connection = ssh.SSH(user, ip, key_filename=key_filename)
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             exit_status, stdout, stderr = self.connection.execute(
48                 self.cmd_param,
49                 stdin=open(self.monitor_script, "r"))
50         else:
51             exit_status, stdout, stderr = self.connection.execute(
52                 "/bin/bash -s ",
53                 stdin=open(self.monitor_script, "r"))
54
55         if exit_status:
56             return False
57         return True
58
59     def verify_SLA(self):
60         LOG.debug("the _result:%s" % self._result)
61         outage_time = self._result.get('outage_time', None)
62         max_outage_time = self._config["sla"]["max_recover_time"]
63         if outage_time is None:
64             LOG.error("There is no outage_time in monitor result.")
65             return False
66         if outage_time > max_outage_time:
67             LOG.error("SLA failure: %f > %f" % (outage_time, max_outage_time))
68             return False
69         else:
70             return True