From a777d2c1592c2a0583f1ee0660c6773005ae9b1c Mon Sep 17 00:00:00 2001 From: Rafael Garcia Date: Thu, 4 May 2017 23:41:17 +0000 Subject: [PATCH 1/4] capture open sockets --- dockerplugin.py | 18 +++++++++++++++++- requirements.txt | 1 + 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/dockerplugin.py b/dockerplugin.py index d77e583..892b988 100755 --- a/dockerplugin.py +++ b/dockerplugin.py @@ -33,6 +33,7 @@ import time from calendar import timegm from distutils.version import StrictVersion +import psutil COLLECTION_INTERVAL = 10 @@ -114,6 +115,20 @@ def emit(container, dimensions, point_type, value, t=None, val.values = value val.dispatch() +def read_open_sockets(container, dimensions, stats, t): + cpid = container["Inspect"]['State']['Pid'] + cpids = psutil.Process(cpid).children(recursive=True) + s = 0 + for pid in cpids: + fd_dir = "{}/{}/fd".format(psutil.PROCFS_PATH, pid.pid) + for fd in os.listdir(fd_dir): + # fd can be closed between listdir and readlink + try: + if "socket" in os.readlink("{}/{}".format(fd_dir, fd)): + s += 1 + except OSError as e: + continue + emit(container, dimensions, 'open_sockets', [s]) def read_blkio_stats(container, dimensions, stats, t): """Process block I/O stats for a container.""" @@ -392,7 +407,7 @@ class DockerPlugin: # TODO: add support for 'networks' from API >= 1.20 to get by-iface stats. METHODS = [read_network_stats, read_blkio_stats, read_cpu_stats, - read_memory_stats] + read_memory_stats, read_open_sockets] def __init__(self, docker_url=None): self.docker_url = docker_url or DockerPlugin.DEFAULT_BASE_URL @@ -514,6 +529,7 @@ def read_callback(self): cstats = self.stats[container['Id']] stats = cstats.stats + container["Inspect"] = self.client.inspect_container(container['Id']) read_at = stats.get('read') if stats else None if not read_at: # No stats available yet; skipping container. diff --git a/requirements.txt b/requirements.txt index 2728bdf..88fc11d 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,3 +1,4 @@ py-dateutil docker-py>=1.0.0 jsonpath_rw +psutil==5.2.2 From efda76004b20cdcbe13f1a5507c38b0a3a96f1d7 Mon Sep 17 00:00:00 2001 From: Rafael Garcia Date: Fri, 5 May 2017 00:07:18 +0000 Subject: [PATCH 2/4] add entry for open_sockets to db --- dockerplugin.db | 1 + 1 file changed, 1 insertion(+) diff --git a/dockerplugin.db b/dockerplugin.db index 94d05d1..0072b47 100644 --- a/dockerplugin.db +++ b/dockerplugin.db @@ -8,3 +8,4 @@ network.usage rx_bytes:COUNTER:0:U, rx_dropped:COUNTER:0:U, rx_errors: memory.usage limit:GAUGE:0:U, max:GAUGE:0:U, total:GAUGE:0:U memory.stats value:GAUGE:0:U memory.percent value:GAUGE:0:150 +open_sockets value:GAUGE:0:U From 1431c7c6d8a792852e32afc4d93424ddcc06bce8 Mon Sep 17 00:00:00 2001 From: Rafael Garcia Date: Fri, 5 May 2017 00:23:43 +0000 Subject: [PATCH 3/4] try namespacing --- dockerplugin.db | 2 +- dockerplugin.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/dockerplugin.db b/dockerplugin.db index 0072b47..0c83621 100644 --- a/dockerplugin.db +++ b/dockerplugin.db @@ -8,4 +8,4 @@ network.usage rx_bytes:COUNTER:0:U, rx_dropped:COUNTER:0:U, rx_errors: memory.usage limit:GAUGE:0:U, max:GAUGE:0:U, total:GAUGE:0:U memory.stats value:GAUGE:0:U memory.percent value:GAUGE:0:150 -open_sockets value:GAUGE:0:U +custom.open_sockets value:GAUGE:0:U diff --git a/dockerplugin.py b/dockerplugin.py index 892b988..7ee4140 100755 --- a/dockerplugin.py +++ b/dockerplugin.py @@ -128,7 +128,7 @@ def read_open_sockets(container, dimensions, stats, t): s += 1 except OSError as e: continue - emit(container, dimensions, 'open_sockets', [s]) + emit(container, dimensions, 'custom.open_sockets', [s]) def read_blkio_stats(container, dimensions, stats, t): """Process block I/O stats for a container.""" From 22dda66732e45dc4e1e0e88ce018d1aa5fc2b62d Mon Sep 17 00:00:00 2001 From: Rafael Garcia Date: Fri, 5 May 2017 00:44:26 +0000 Subject: [PATCH 4/4] use /mnt/proc --- dockerplugin.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dockerplugin.py b/dockerplugin.py index 7ee4140..57aa8a9 100755 --- a/dockerplugin.py +++ b/dockerplugin.py @@ -120,7 +120,7 @@ def read_open_sockets(container, dimensions, stats, t): cpids = psutil.Process(cpid).children(recursive=True) s = 0 for pid in cpids: - fd_dir = "{}/{}/fd".format(psutil.PROCFS_PATH, pid.pid) + fd_dir = "{}/{}/fd".format('/mnt/proc', pid.pid) for fd in os.listdir(fd_dir): # fd can be closed between listdir and readlink try: