diff --git a/checks.d/kubernetes.py b/checks.d/kubernetes.py index 7419e6df6f..3fe6841f06 100644 --- a/checks.d/kubernetes.py +++ b/checks.d/kubernetes.py @@ -46,6 +46,7 @@ RATE: {True: HISTORATE, False: RATE} } + class Kubernetes(AgentCheck): """ Collect metrics and events from kubelet """ diff --git a/tests/checks/mock/test_kubernetes.py b/tests/checks/mock/test_kubernetes.py index 82afbbbbbd..b76e4a1376 100644 --- a/tests/checks/mock/test_kubernetes.py +++ b/tests/checks/mock/test_kubernetes.py @@ -49,6 +49,7 @@ def test_fail_1_1(self): } with mock.patch('utils.kubeutil.KubeUtil.retrieve_pods_list', side_effect=lambda: json.loads(Fixtures.read_file("pods_list_1.1.json", string_escape=False))): + with mock.patch('utils.dockerutil.DockerUtil.get_hostname', side_effect=lambda: 'foo'): # Can't use run_check_twice due to specific metrics self.run_check(config, mocks=mocks, force_reload=True) self.assertServiceCheck("kubernetes.kubelet.check", status=AgentCheck.CRITICAL, tags=None, count=1) @@ -69,6 +70,7 @@ def test_metrics_1_1(self): } # parts of the json returned by the kubelet api is escaped, keep it untouched with mock.patch('utils.kubeutil.KubeUtil.retrieve_pods_list', side_effect=lambda: json.loads(Fixtures.read_file("pods_list_1.1.json", string_escape=False))): + with mock.patch('utils.dockerutil.DockerUtil.get_hostname', side_effect=lambda: 'foo'): # Can't use run_check_twice due to specific metrics self.run_check_twice(config, mocks=mocks, force_reload=True) @@ -125,6 +127,7 @@ def test_historate_1_1(self): # parts of the json returned by the kubelet api is escaped, keep it untouched with mock.patch('utils.kubeutil.KubeUtil.retrieve_pods_list', side_effect=lambda: json.loads(Fixtures.read_file("pods_list_1.1.json", string_escape=False))): + with mock.patch('utils.dockerutil.DockerUtil.get_hostname', side_effect=lambda: 'foo'): # Can't use run_check_twice due to specific metrics self.run_check_twice(config, mocks=mocks, force_reload=True) @@ -163,6 +166,7 @@ def test_fail_1_2(self): } with mock.patch('utils.kubeutil.KubeUtil.retrieve_pods_list', side_effect=lambda: json.loads(Fixtures.read_file("pods_list_1.2.json", string_escape=False))): + with mock.patch('utils.dockerutil.DockerUtil.get_hostname', side_effect=lambda: 'foo'): # Can't use run_check_twice due to specific metrics self.run_check(config, mocks=mocks, force_reload=True) self.assertServiceCheck("kubernetes.kubelet.check", status=AgentCheck.CRITICAL) @@ -183,6 +187,7 @@ def test_metrics_1_2(self): } # parts of the json returned by the kubelet api is escaped, keep it untouched with mock.patch('utils.kubeutil.KubeUtil.retrieve_pods_list', side_effect=lambda: json.loads(Fixtures.read_file("pods_list_1.2.json", string_escape=False))): + with mock.patch('utils.dockerutil.DockerUtil.get_hostname', side_effect=lambda: 'foo'): # Can't use run_check_twice due to specific metrics self.run_check_twice(config, mocks=mocks, force_reload=True) @@ -235,6 +240,7 @@ def test_historate_1_2(self): # parts of the json returned by the kubelet api is escaped, keep it untouched with mock.patch('utils.kubeutil.KubeUtil.retrieve_pods_list', side_effect=lambda: json.loads(Fixtures.read_file("pods_list_1.2.json", string_escape=False))): + with mock.patch('utils.dockerutil.DockerUtil.get_hostname', side_effect=lambda: 'foo'): # Can't use run_check_twice due to specific metrics self.run_check_twice(config, mocks=mocks, force_reload=True) diff --git a/utils/dockerutil.py b/utils/dockerutil.py index 3f42356a0d..f22f5da633 100644 --- a/utils/dockerutil.py +++ b/utils/dockerutil.py @@ -108,12 +108,10 @@ def get_events(self): def get_hostname(self): """Return the `Name` param from `docker info` to use as the hostname""" - if self.is_dockerized(): - try: - return self.client.info().get("Name") - except Exception: - log.critical("Unable to find docker host hostname") - + try: + return self.client.info().get("Name") + except Exception: + log.critical("Unable to find docker host hostname") return None @property diff --git a/utils/kubeutil.py b/utils/kubeutil.py index 7922561ec1..130e80ced3 100644 --- a/utils/kubeutil.py +++ b/utils/kubeutil.py @@ -6,8 +6,6 @@ from collections import defaultdict import logging import os -import socket -import struct from urlparse import urljoin # project @@ -15,6 +13,7 @@ from utils.checkfiles import get_conf_path from utils.http import retrieve_json from utils.singleton import Singleton +from utils.dockerutil import DockerUtil log = logging.getLogger('collector') @@ -39,6 +38,7 @@ class KubeUtil(): NAMESPACE_LABEL = "io.kubernetes.pod.namespace" def __init__(self): + self.docker_util = DockerUtil() try: config_file_path = get_conf_path(KUBERNETES_CHECK_NAME) check_config = check_yaml(config_file_path) @@ -53,7 +53,7 @@ def __init__(self): instance = {} self.method = instance.get('method', KubeUtil.DEFAULT_METHOD) - self.host = instance.get("host") or self._get_default_router() + self.host = instance.get("host") or self.docker_util.get_hostname() self.cadvisor_port = instance.get('port', KubeUtil.DEFAULT_CADVISOR_PORT) self.kubelet_port = instance.get('kubelet_port', KubeUtil.DEFAULT_KUBELET_PORT) @@ -85,7 +85,7 @@ def extract_kube_labels(self, pods_list, excluded_keys=None): if name and labels and namespace: key = "%s/%s" % (namespace, name) - for k,v in labels.iteritems(): + for k, v in labels.iteritems(): if k in excluded_keys: continue @@ -95,16 +95,3 @@ def extract_kube_labels(self, pods_list, excluded_keys=None): def retrieve_pods_list(self): return retrieve_json(self.pods_list_url) - - @classmethod - def _get_default_router(cls): - try: - with open('/proc/net/route') as f: - for line in f.readlines(): - fields = line.strip().split() - if fields[1] == '00000000': - return socket.inet_ntoa(struct.pack('