diff --git a/dockerplugin.db b/dockerplugin.db index 94d05d1..0c83621 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 +custom.open_sockets value:GAUGE:0:U diff --git a/dockerplugin.py b/dockerplugin.py index d77e583..57aa8a9 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('/mnt/proc', 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, 'custom.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