Skip to content

Commit

Permalink
[docker_deamon] [ecs] try to build the ecs introspection url.
Browse files Browse the repository at this point in the history
[docker_deamon] [ecs] add the ability to add the ecs (docker) gateway.

[docker_daemon] [ecs] no need to raise, implicit in json() call.

[docker_deamon] [ecs] get docker gw from /proc.
  • Loading branch information
truthbk committed Aug 10, 2016
1 parent 6b07d09 commit 5f1c6f3
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 6 deletions.
27 changes: 21 additions & 6 deletions checks.d/docker_daemon.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,8 @@
FILTERED = "filtered"
IMAGE = "image"

ECS_INTROSPECT_DEFAULT_PORT = 51678


def get_filters(include, exclude):
# The reasoning is to check exclude first, so we can skip if there is no exclude
Expand Down Expand Up @@ -158,6 +160,8 @@ def init(self):
# Just needs to be done once
self.docker_util = DockerUtil()
self.docker_client = self.docker_util.client
self.docker_gateway = DockerUtil.get_gateway()

if self.is_k8s():
self.kubeutil = KubeUtil()
self._mountpoints = self.docker_util.get_mountpoints(CGROUP_METRICS)
Expand Down Expand Up @@ -427,13 +431,24 @@ def refresh_ecs_tags(self):
ip = ecs_config.get('NetworkSettings', {}).get('IPAddress')
ports = ecs_config.get('NetworkSettings', {}).get('Ports')
port = ports.keys()[0].split('/')[0] if ports else None
if not ip and DockerUtil.is_dockerized():
if self.docker_gateway:
ip = self.docker_gateway
port = ECS_INTROSPECT_DEFAULT_PORT
elif not ip:
ip = "localhost"
port = ECS_INTROSPECT_DEFAULT_PORT

ecs_tags = {}
if ip and port:
tasks = requests.get('http://%s:%s/v1/tasks' % (ip, port)).json()
for task in tasks.get('Tasks', []):
for container in task.get('Containers', []):
tags = ['task_name:%s' % task['Family'], 'task_version:%s' % task['Version']]
ecs_tags[container['DockerId']] = tags
try:
if ip and port:
tasks = requests.get('http://%s:%s/v1/tasks' % (ip, port)).json()
for task in tasks.get('Tasks', []):
for container in task.get('Containers', []):
tags = ['task_name:%s' % task['Family'], 'task_version:%s' % task['Version']]
ecs_tags[container['DockerId']] = tags
except requests.exceptions.HTTPError as e:
self.log.warning("Unable to collect ECS task names: %s" % e)

self.ecs_tags = ecs_tags

Expand Down
19 changes: 19 additions & 0 deletions utils/dockerutil.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
import logging
import os
import time
import socket
import struct

# 3rd party
from docker import Client
Expand Down Expand Up @@ -34,6 +36,7 @@ class DockerUtil:
__metaclass__ = Singleton

DEFAULT_SETTINGS = {"version": DEFAULT_VERSION}
DEFAULT_PROCFS_GW_PATH = "proc/net/route"

def __init__(self, **kwargs):
self._docker_root = None
Expand Down Expand Up @@ -113,6 +116,22 @@ def get_events(self):

return self.events, should_reload_conf

@classmethod
def get_gateway(cls, proc_prefix=""):
procfs_route = os.path.join("/", proc_prefix, cls.DEFAULT_PROCFS_GW_PATH)

try:
with open(procfs_route) as f:
for line in f.readlines():
fields = line.strip().split()
if fields[1] == '00000000':
return socket.inet_ntoa(struct.pack('<L', int(fields[2], 16)))
except IOError, e:
log.error('Unable to open {}: %s'.format(procfs_route), e)

return None


def get_hostname(self):
"""Return the `Name` param from `docker info` to use as the hostname"""
try:
Expand Down

0 comments on commit 5f1c6f3

Please sign in to comment.