Skip to content
This repository has been archived by the owner on Mar 14, 2022. It is now read-only.

adding open sockets #19

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions dockerplugin.db
Original file line number Diff line number Diff line change
Expand Up @@ -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
18 changes: 17 additions & 1 deletion dockerplugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import time
from calendar import timegm
from distutils.version import StrictVersion
import psutil

COLLECTION_INTERVAL = 10

Expand Down Expand Up @@ -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."""
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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.
Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
py-dateutil
docker-py>=1.0.0
jsonpath_rw
psutil==5.2.2