aboutsummaryrefslogtreecommitdiffstats
path: root/yardstick/benchmark/scenarios/availability/monitor/monitor_process.py
blob: 280e5811d8baee003e8f2907d4bfcc712e78c762 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
##############################################################################
# Copyright (c) 2015 Huawei Technologies Co.,Ltd. and others
#
# All rights reserved. This program and the accompanying materials
# are made available under the terms of the Apache License, Version 2.0
# which accompanies this distribution, and is available at
# http://www.apache.org/licenses/LICENSE-2.0
##############################################################################
from __future__ import absolute_import
import logging
import yardstick.ssh as ssh

from yardstick.benchmark.scenarios.availability.monitor import basemonitor

LOG = logging.getLogger(__name__)


class MonitorProcess(basemonitor.BaseMonitor):
    """docstring for MonitorApi"""

    __monitor_type__ = "process"

    def setup(self):
        host = self._context[self._config["host"]]

        self.connection = ssh.SSH.from_node(host, defaults={"user": "root"})
        self.connection.wait(timeout=600)
        LOG.debug("ssh host (%s) success!", str(host))
        self.check_script = self.get_script_fullpath(
            "ha_tools/check_process_python.bash")
        self.process_name = self._config["process_name"]

    def monitor_func(self):
        with open(self.check_script, "r") as stdin_file:
            _, stdout, _ = self.connection.execute(
                "sudo /bin/sh -s {0}".format(self.process_name),
                stdin=stdin_file)

        if not stdout or int(stdout) < self.monitor_data[self.process_name]:
            LOG.info("the (%s) processes are in recovery!", self.process_name)
            return False

        LOG.info("the (%s) processes have been fully recovered!",
                 self.process_name)
        return True

    def verify_SLA(self):
        outage_time = self._result.get('outage_time', None)
        if self._config.get("sla"):
            max_outage_time = self._config["sla"]["max_recover_time"]
            if outage_time > max_outage_time:
                LOG.info("SLA failure: %f > %f", outage_time, max_outage_time)
                return False
        return True


def _test():    # pragma: no cover
    host = {
        "ip": "10.20.0.5",
        "user": "root",
        "key_filename": "/root/.ssh/id_rsa"
    }
    context = {"node1": host}
    monitor_configs = []
    config = {
        'monitor_type': 'process',
        'process_name': 'nova-api',
        'host': "node1",
        'monitor_time': 1,
        'sla': {'max_recover_time': 5}
    }
    monitor_configs.append(config)

    p = basemonitor.MonitorMgr({})
    p.init_monitors(monitor_configs, context)
    p.start_monitors()
    p.wait_monitors()
    p.verify_SLA()


if __name__ == '__main__':    # pragma: no cover
    _test()