Repository: ambari Updated Branches: refs/heads/trunk ac240927a -> 373afaad7
AMBARI-9278. Allow hostname customization for metric monitor. (swagle) Project: http://git-wip-us.apache.org/repos/asf/ambari/repo Commit: http://git-wip-us.apache.org/repos/asf/ambari/commit/373afaad Tree: http://git-wip-us.apache.org/repos/asf/ambari/tree/373afaad Diff: http://git-wip-us.apache.org/repos/asf/ambari/diff/373afaad Branch: refs/heads/trunk Commit: 373afaad7ff826ab7e1a284527e399fd033611bb Parents: ac24092 Author: Siddharth Wagle <swa...@hortonworks.com> Authored: Thu Jan 22 12:10:10 2015 -0800 Committer: Siddharth Wagle <swa...@hortonworks.com> Committed: Thu Jan 22 12:10:10 2015 -0800 ---------------------------------------------------------------------- .../src/main/python/core/config_reader.py | 3 ++ .../src/main/python/core/controller.py | 4 +-- .../src/main/python/core/host_info.py | 29 ++++++++++++++++++-- .../src/main/python/core/metric_collector.py | 4 +-- .../src/main/python/main.py | 3 +- .../src/test/python/core/TestHostInfo.py | 10 +++---- .../src/test/python/core/TestMetricCollector.py | 2 +- 7 files changed, 40 insertions(+), 15 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/ambari/blob/373afaad/ambari-metrics/ambari-metrics-host-monitoring/src/main/python/core/config_reader.py ---------------------------------------------------------------------- diff --git a/ambari-metrics/ambari-metrics-host-monitoring/src/main/python/core/config_reader.py b/ambari-metrics/ambari-metrics-host-monitoring/src/main/python/core/config_reader.py index 463c98c..f43f512 100644 --- a/ambari-metrics/ambari-metrics-host-monitoring/src/main/python/core/config_reader.py +++ b/ambari-metrics/ambari-metrics-host-monitoring/src/main/python/core/config_reader.py @@ -182,6 +182,9 @@ class Configuration: def get_server_address(self): return self.get("default", "metrics_server") + def get_hostname_script(self): + return self.get("default", "hostname_script") + def get_log_level(self): return self.get("default", "debug_level", "INFO") http://git-wip-us.apache.org/repos/asf/ambari/blob/373afaad/ambari-metrics/ambari-metrics-host-monitoring/src/main/python/core/controller.py ---------------------------------------------------------------------- diff --git a/ambari-metrics/ambari-metrics-host-monitoring/src/main/python/core/controller.py b/ambari-metrics/ambari-metrics-host-monitoring/src/main/python/core/controller.py index e0ef804..1713501 100644 --- a/ambari-metrics/ambari-metrics-host-monitoring/src/main/python/core/controller.py +++ b/ambari-metrics/ambari-metrics-host-monitoring/src/main/python/core/controller.py @@ -41,11 +41,11 @@ class Controller(threading.Thread): self.config = config self.metrics_config = config.getMetricGroupConfig() self.events_cache = [] - hostinfo = HostInfo() + hostinfo = HostInfo(config) self.application_metric_map = ApplicationMetricMap(hostinfo.get_hostname(), hostinfo.get_ip_address()) self.event_queue = Queue(config.get_max_queue_size()) - self.metric_collector = MetricsCollector(self.event_queue, self.application_metric_map) + self.metric_collector = MetricsCollector(self.event_queue, self.application_metric_map, hostinfo) self.server_url = config.get_server_address() self.sleep_interval = config.get_collector_sleep_interval() self._stop_handler = stop_handler http://git-wip-us.apache.org/repos/asf/ambari/blob/373afaad/ambari-metrics/ambari-metrics-host-monitoring/src/main/python/core/host_info.py ---------------------------------------------------------------------- diff --git a/ambari-metrics/ambari-metrics-host-monitoring/src/main/python/core/host_info.py b/ambari-metrics/ambari-metrics-host-monitoring/src/main/python/core/host_info.py index 020b84e..37c55cb 100644 --- a/ambari-metrics/ambari-metrics-host-monitoring/src/main/python/core/host_info.py +++ b/ambari-metrics/ambari-metrics-host-monitoring/src/main/python/core/host_info.py @@ -26,8 +26,11 @@ import platform import socket import time import threading +import socket +import subprocess logger = logging.getLogger() +cached_hostname = None def bytes2human(n): bytes = float(n) @@ -35,13 +38,14 @@ def bytes2human(n): return '%.2f' % gigabytes pass - class HostInfo(): - def __init__(self): + + def __init__(self, config): self.__last_network_io_time = 0 self.__last_network_data = {} self.__last_network_lock = threading.Lock() self.__host_static_info = self.get_host_static_info() + self.__config = config def get_cpu_times(self): """ @@ -245,7 +249,26 @@ class HostInfo(): pass def get_hostname(self): - return socket.getfqdn() + global cached_hostname + if cached_hostname is not None: + return cached_hostname + + try: + hostname_script = self.__config.get_hostname_script() + logger.info('hostname_script: %s' % hostname_script) + try: + osStat = subprocess.Popen([hostname_script], stdout=subprocess.PIPE, stderr=subprocess.PIPE) + out, err = osStat.communicate() + if (0 == osStat.returncode and 0 != len(out.strip())): + cached_hostname = out.strip() + else: + cached_hostname = socket.getfqdn().lower() + except: + cached_hostname = socket.getfqdn().lower() + except: + cached_hostname = socket.getfqdn().lower() + logger.info('Cached hostname: %s' % cached_hostname) + return cached_hostname def get_ip_address(self): return socket.gethostbyname(socket.getfqdn()) http://git-wip-us.apache.org/repos/asf/ambari/blob/373afaad/ambari-metrics/ambari-metrics-host-monitoring/src/main/python/core/metric_collector.py ---------------------------------------------------------------------- diff --git a/ambari-metrics/ambari-metrics-host-monitoring/src/main/python/core/metric_collector.py b/ambari-metrics/ambari-metrics-host-monitoring/src/main/python/core/metric_collector.py index 05e94be..a830a1f 100644 --- a/ambari-metrics/ambari-metrics-host-monitoring/src/main/python/core/metric_collector.py +++ b/ambari-metrics/ambari-metrics-host-monitoring/src/main/python/core/metric_collector.py @@ -34,10 +34,10 @@ class MetricsCollector(): not required if Timer class is used for metric groups. """ - def __init__(self, emit_queue, application_metric_map): + def __init__(self, emit_queue, application_metric_map, host_info): self.emit_queue = emit_queue self.application_metric_map = application_metric_map - self.host_info = HostInfo() + self.host_info = host_info pass def process_event(self, event): http://git-wip-us.apache.org/repos/asf/ambari/blob/373afaad/ambari-metrics/ambari-metrics-host-monitoring/src/main/python/main.py ---------------------------------------------------------------------- diff --git a/ambari-metrics/ambari-metrics-host-monitoring/src/main/python/main.py b/ambari-metrics/ambari-metrics-host-monitoring/src/main/python/main.py index 37e77e5..ce8ffc6 100644 --- a/ambari-metrics/ambari-metrics-host-monitoring/src/main/python/main.py +++ b/ambari-metrics/ambari-metrics-host-monitoring/src/main/python/main.py @@ -61,9 +61,8 @@ def server_process_main(stop_handler, scmStatus=None): save_pid(os.getpid(), PID_OUT_FILE) config = Configuration() - controller = Controller(config, stop_handler) - _init_logging(config) + controller = Controller(config, stop_handler) logger.info('Starting Server RPC Thread: %s' % ' '.join(sys.argv)) controller.start() http://git-wip-us.apache.org/repos/asf/ambari/blob/373afaad/ambari-metrics/ambari-metrics-host-monitoring/src/test/python/core/TestHostInfo.py ---------------------------------------------------------------------- diff --git a/ambari-metrics/ambari-metrics-host-monitoring/src/test/python/core/TestHostInfo.py b/ambari-metrics/ambari-metrics-host-monitoring/src/test/python/core/TestHostInfo.py index 4e63d6a..102ea44 100644 --- a/ambari-metrics/ambari-metrics-host-monitoring/src/test/python/core/TestHostInfo.py +++ b/ambari-metrics/ambari-metrics-host-monitoring/src/test/python/core/TestHostInfo.py @@ -22,7 +22,7 @@ import logging from host_info import HostInfo import platform from unittest import TestCase -from mock.mock import patch +from mock.mock import patch, MagicMock logger = logging.getLogger() @@ -41,7 +41,7 @@ class TestHostInfo(TestCase): cp.iowait = 0 cp.irq = 0 cp.softirq = 0 - hostinfo = HostInfo() + hostinfo = HostInfo(MagicMock()) if platform.system() != "Windows": with patch("os.getloadavg") as avg_mock: @@ -77,7 +77,7 @@ class TestHostInfo(TestCase): sw = sw_mock.return_value sw.free = 2341234 - hostinfo = HostInfo() + hostinfo = HostInfo(MagicMock()) cpu = hostinfo.get_mem_info() @@ -109,7 +109,7 @@ class TestHostInfo(TestCase): process_iter_mock.return_value = processes - hostinfo = HostInfo() + hostinfo = HostInfo(MagicMock()) procs = hostinfo.get_process_info() @@ -122,7 +122,7 @@ class TestHostInfo(TestCase): dp_mock.__iter__.return_value = ['a', 'b', 'c'] - hostinfo = HostInfo() + hostinfo = HostInfo(MagicMock()) cdu = hostinfo.get_combined_disk_usage() self.assertEqual(cdu['disk_total'], "0.00") http://git-wip-us.apache.org/repos/asf/ambari/blob/373afaad/ambari-metrics/ambari-metrics-host-monitoring/src/test/python/core/TestMetricCollector.py ---------------------------------------------------------------------- diff --git a/ambari-metrics/ambari-metrics-host-monitoring/src/test/python/core/TestMetricCollector.py b/ambari-metrics/ambari-metrics-host-monitoring/src/test/python/core/TestMetricCollector.py index 26a3edc..30cc023 100644 --- a/ambari-metrics/ambari-metrics-host-monitoring/src/test/python/core/TestMetricCollector.py +++ b/ambari-metrics/ambari-metrics-host-monitoring/src/test/python/core/TestMetricCollector.py @@ -36,7 +36,7 @@ class TestMetricCollector(TestCase): amm_mock.return_value = None host_info_mock.return_value = {'metric_name' : 'metric_value'} - metric_collector = MetricsCollector(None, amm_mock) + metric_collector = MetricsCollector(None, amm_mock, host_info_mock) group_config = {'collect_every' : 1, 'metrics' : 'cpu'}