1 ##############################################################################
2 # Copyright (c) 2015 Huawei Technologies Co.,Ltd. and others
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 ##############################################################################
10 import multiprocessing
15 LOG = logging.getLogger(__name__)
18 def _execute_shell_command(command):
19 '''execute shell script with error handling'''
23 output = subprocess.check_output(command, shell=True)
26 output = traceback.format_exc()
27 LOG.error("exec command '%s' error:\n " % command)
28 LOG.error(traceback.format_exc())
30 return exitcode, output
33 def _monitor_process(config, queue, event):
42 wait_time = config.get("duration", 0)
43 cmd = config.get("monitor_cmd", None)
45 LOG.error("There are no monitor cmd!")
50 begin_time = time.time()
53 total_count = total_count + 1
55 one_check_begin_time = time.time()
56 exit_status, stdout = _execute_shell_command(cmd)
57 one_check_end_time = time.time()
59 LOG.info("the exit_status:%s stdout:%s" % (exit_status, stdout))
61 outage_count = outage_count + 1
63 outage_time = outage_time + (
64 one_check_end_time - one_check_begin_time)
67 first_outage = one_check_begin_time
69 last_outage = one_check_end_time
72 LOG.debug("the monitor process stop")
78 end_time = time.time()
79 total_time = end_time - begin_time
81 queue.put({"total_time": total_time,
82 "outage_time": last_outage-first_outage,
83 "total_count": total_count,
84 "outage_count": outage_count})
91 self._monitor_process = []
93 def setup(self, config):
97 self._queue = multiprocessing.Queue()
98 self._event = multiprocessing.Event()
99 self._monitor_process = multiprocessing.Process(
100 target=_monitor_process, name="Monitor",
101 args=(self._config, self._queue, self._event))
103 self._monitor_process.start()
104 ret = self._queue.get()
106 LOG.debug("monitor process started!")
110 self._result = self._queue.get()
111 LOG.debug("stop the monitor process. the result:%s" % self._result)
113 def get_result(self):