aboutsummaryrefslogtreecommitdiffstats
path: root/yardstick/benchmark/scenarios/availability/monitor/monitor_command.py
blob: d757bd88d163e1ef31f731b3cf9717a7ec4162a1 (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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
##############################################################################
# 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 os
import logging
import subprocess
import traceback

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

LOG = logging.getLogger(__name__)


def _execute_shell_command(command):
    """execute shell script with error handling"""
    exitcode = 0
    output = []
    try:
        output = subprocess.check_output(command, shell=True)
    except Exception:
        exitcode = -1
        output = traceback.format_exc()
        LOG.error("exec command '%s' error:\n ", command)
        LOG.error(traceback.format_exc())

    return exitcode, output


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

    __monitor_type__ = "openstack-cmd"

    def setup(self):
        self.connection = None
        node_name = self._config.get("host", None)
        if node_name:
            host = self._context[node_name]

            self.connection = ssh.SSH.from_node(host,
                                                defaults={"user": "root"})
            self.connection.wait(timeout=600)
            LOG.debug("ssh host success!")

        self.check_script = self.get_script_fullpath(
            "ha_tools/check_openstack_cmd.bash")

        self.cmd = self._config["command_name"]

        try:
            cacert = os.environ['OS_CACERT']
        except KeyError:
            pass
        else:
            if cacert.lower() == "false":
                self.cmd = self.cmd + " --insecure"

    def monitor_func(self):
        exit_status = 0
        exit_status, stdout = _execute_shell_command(self.cmd)
        if exit_status:
            return False
        return True

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


def _test():    # pragma: no cover
    host = {
        "ip": "192.168.235.22",
        "user": "root",
        "key_filename": "/root/.ssh/id_rsa"
    }
    context = {"node1": host}
    monitor_configs = []
    config = {
        'monitor_type': 'openstack-cmd',
        'command_name': 'nova image-list',
        'monitor_time': 1,
        'host': 'node1',
        'sla': {'max_outage_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()