Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Doesn't fix anything, just catches bad data #36

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
32 changes: 21 additions & 11 deletions dockerplugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,20 @@
#
# Requirements: docker-py

import dateutil.parser
from distutils.version import StrictVersion
import docker
import datetime
import os
import re
import sys
import threading
import time
import sys
import re
from distutils.version import StrictVersion

import dateutil.parser
import docker

STREAM_DOCKER_PY_VERSION = (1, 6, 0)

CACHE = {}

def _c(c):
"""A helper method for representing a container in messages. If the given
Expand All @@ -58,7 +61,8 @@ def emit(cls, container, type, value, t=None, type_instance=None):
val.type_instance = type_instance

if t:
val.time = time.mktime(dateutil.parser.parse(t).timetuple())
d = dateutil.parser.parse(t)
val.time = (d - datetime.datetime(1970, 1, 1, tzinfo=d.tzinfo)).total_seconds()
else:
val.time = time.time()

Expand All @@ -69,7 +73,11 @@ def emit(cls, container, type, value, t=None, type_instance=None):
val.meta = {'true': 'true'}

val.values = value
val.dispatch()

cache_identifier = val.plugin_instance + val.type + val.type_instance
if not CACHE.get(cache_identifier) == val.time:
CACHE[cache_identifier] = val.time
val.dispatch()

@classmethod
def read(cls, container, stats, t):
Expand Down Expand Up @@ -156,8 +164,7 @@ def read(cls, container, stats, t):
cls.emit(container, 'memory.usage', values, t=t)

for key, value in (mem_stats.get('stats') or {}).items():
cls.emit(container, 'memory.stats', [value],
type_instance=key, t=t)
cls.emit(container, 'memory.stats', [value], type_instance=key, t=t)

mem_percent = 100.0 * mem_stats['usage'] / mem_stats['limit']
cls.emit(container, 'memory.percent', ["%.2f" % mem_percent], t=t)
Expand Down Expand Up @@ -240,7 +247,7 @@ def stats(self):
"""Wait, if needed, for stats to be available and return the most
recently read stats data, parsed as JSON, for the container."""
while not self._stats:
pass
time.sleep(0.01)
return self._stats


Expand Down Expand Up @@ -351,7 +358,8 @@ def dispatch(self):
if getattr(self, 'type_instance', None):
identifier += '-' + self.type_instance
print 'PUTVAL', identifier, \
':'.join(map(str, [int(self.time)] + self.values))
':'.join(map(str, [int(self.time)] + self.values))


class ExecCollectd:
def Values(self):
Expand All @@ -366,6 +374,7 @@ def info(self, msg):
def register_read(self, docker_plugin):
pass


collectd = ExecCollectd()
plugin = DockerPlugin()
if len(sys.argv) > 1:
Expand All @@ -377,6 +386,7 @@ def register_read(self, docker_plugin):
# Normal plugin execution via CollectD
else:
import collectd

plugin = DockerPlugin()
collectd.register_config(plugin.configure_callback)
collectd.register_init(plugin.init_callback)