Merge "Disable spoof check on vfs in sriov setup"
[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 \
15     import build_shell_command, execute_shell_command
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.get(self._config.get('host', None), None)
27         self.connection = None
28         if host:
29             self.connection = ssh.SSH.from_node(
30                 host, defaults={"user": "root"})
31             self.connection.wait(timeout=600)
32             LOG.debug("ssh host success!")
33         self.key = self._config["key"]
34         self.monitor_key = self._config["monitor_key"]
35         self.monitor_type = self._config["monitor_type"]
36         if "parameter" in self._config:
37             self.parameter_config = self._config['parameter']
38         self.monitor_cfg = basemonitor.BaseMonitor.monitor_cfgs.get(
39             self.monitor_key)
40         self.monitor_script = self.get_script_fullpath(
41             self.monitor_cfg['monitor_script'])
42         LOG.debug("ssh host success!")
43
44     def monitor_func(self):
45         if "parameter" in self._config:
46             self.cmd_param = \
47                 build_shell_command(
48                     self.parameter_config,
49                     bool(self.connection),
50                     self.intermediate_variables)
51             cmd_remote = "sudo {}".format(self.cmd_param)
52             cmd_local = "/bin/bash {0} {1}".format(self.monitor_script, self.cmd_param)
53         else:
54             cmd_remote = "sudo /bin/sh -s "
55             cmd_local = "/bin/bash {0}".format(self.monitor_script)
56         if self.connection:
57             with open(self.monitor_script, "r") as stdin_file:
58                 exit_status, stdout, stderr = self.connection.execute(cmd_remote, stdin=stdin_file)
59         else:
60             exit_status, stdout = execute_shell_command(cmd_local)
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