From d601773bf379c47c41feb834352ca6a05349d2a7 Mon Sep 17 00:00:00 2001 From: Marc Aymerich Date: Fri, 27 Feb 2015 22:24:36 +0000 Subject: [PATCH] Fixes on mailman traffic monitor backend --- orchestra/apps/lists/backends.py | 9 ++++----- orchestra/apps/orchestration/middlewares.py | 11 +++++------ orchestra/apps/orchestration/models.py | 2 ++ orchestra/apps/resources/tasks.py | 8 +++++--- orchestra/conf/base_settings.py | 2 +- orchestra/utils/humanize.py | 15 +++++++++++++++ 6 files changed, 32 insertions(+), 15 deletions(-) diff --git a/orchestra/apps/lists/backends.py b/orchestra/apps/lists/backends.py index 8e20b17e..8fc2c864 100644 --- a/orchestra/apps/lists/backends.py +++ b/orchestra/apps/lists/backends.py @@ -160,26 +160,25 @@ class MailmanTraffic(ServiceMonitor): MAILMAN_LOG="$4" SUBSCRIBERS=$(list_members ${LIST_NAME} | wc -l) - SIZE=$(grep ' post to ${LIST_NAME} ' "${MAILMAN_LOG}" \\ + SIZE=$(grep " post to ${LIST_NAME} " "${MAILMAN_LOG}" \\ | awk '"$LAST_DATE"<=$0 && $0<="%s"' \\ | sed 's/.*size=\([0-9]*\).*/\\1/' \\ | tr '\\n' '+' \\ - | xargs -i echo {} ) + | xargs -i echo {}0 ) echo ${OBJECT_ID} $(( ${SIZE}*${SUBSCRIBERS} )) }""") % current_date) def monitor(self, mail_list): context = self.get_context(mail_list) self.append( - 'monitor %(object_id)i %(last_date)s "%(list_name)s" "%(mailman_log)s{,.1}"' % context) + 'monitor %(object_id)i "%(last_date)s" "%(list_name)s" %(mailman_log)s{,.1}' % context) def get_context(self, mail_list): - last_date = timezone.localtime(self.get_last_date(mail_list.pk)) return { 'mailman_log': settings.LISTS_MAILMAN_POST_LOG_PATH, 'list_name': mail_list.name, 'object_id': mail_list.pk, - 'last_date': last_date.strftime("%b %d %H:%M:%S"), + 'last_date': self.get_last_date(mail_list.pk).strftime("%b %d %H:%M:%S"), } diff --git a/orchestra/apps/orchestration/middlewares.py b/orchestra/apps/orchestration/middlewares.py index 081d7990..82280e49 100644 --- a/orchestra/apps/orchestration/middlewares.py +++ b/orchestra/apps/orchestration/middlewares.py @@ -25,13 +25,12 @@ def pre_delete_collector(sender, *args, **kwargs): @receiver(m2m_changed, dispatch_uid='orchestration.m2m_collector') def m2m_collector(sender, *args, **kwargs): - # m2m relations without intermediary models are shit - # model.post_save is not sent and by the time related.post_save is sent - # the objects are not accessible with RelatedManager.all() + # m2m relations without intermediary models are shit. Model.post_save is not sent and + # by the time related.post_save is sent rel objects are not accessible via RelatedManager.all() # We have to use this inefficient technique of collecting the instances via m2m_changed.post_add - if kwargs.pop('action') == 'post_add': - for pk in kwargs['pk_set']: - kwargs['instance'] = kwargs['model'].objects.get(pk=pk) + if kwargs.pop('action') == 'post_add' and kwargs['pk_set']: + for instance in kwargs['model'].objects.filter(pk__in=kwargs['pk_set']): + kwargs['instance'] = instance OperationsMiddleware.collect(Operation.SAVE, **kwargs) diff --git a/orchestra/apps/orchestration/models.py b/orchestra/apps/orchestration/models.py index d34293de..797a10b4 100644 --- a/orchestra/apps/orchestration/models.py +++ b/orchestra/apps/orchestration/models.py @@ -99,6 +99,8 @@ class BackendOperation(models.Model): DELETE = 'delete' SAVE = 'save' MONITOR = 'monitor' + EXCEEDED = 'exceeded' + RECOVERY = 'recovery' log = models.ForeignKey('orchestration.BackendLog', related_name='operations') backend = models.CharField(_("backend"), max_length=256) diff --git a/orchestra/apps/resources/tasks.py b/orchestra/apps/resources/tasks.py index 196ec182..6d3d2b02 100644 --- a/orchestra/apps/resources/tasks.py +++ b/orchestra/apps/resources/tasks.py @@ -41,10 +41,12 @@ def monitor(resource_id, ids=None, async=True): data = ResourceData.get_or_create(obj, resource) data.update() if not resource.disable_trigger: - if data.used > data.allocated: - op = Operation.create(backend, obj, Operation.EXCEED) + a = data.used + b = data.allocated + if data.used > (data.allocated or 0): + op = Operation.create(backend, obj, Operation.EXCEEDED) triggers.append(op) - elif data.used < data.allocated: + elif data.used < (data.allocated or 0): op = Operation.create(backend, obj, Operation.RECOVERY) triggers.append(op) Operation.execute(triggers) diff --git a/orchestra/conf/base_settings.py b/orchestra/conf/base_settings.py index 49b874b4..4c594510 100644 --- a/orchestra/conf/base_settings.py +++ b/orchestra/conf/base_settings.py @@ -95,7 +95,7 @@ INSTALLED_APPS = ( 'admin_tools', 'admin_tools.theming', 'admin_tools.menu', -# 'admin_tools.dashboard', + 'admin_tools.dashboard', 'rest_framework', 'rest_framework.authtoken', 'passlib.ext.django', diff --git a/orchestra/utils/humanize.py b/orchestra/utils/humanize.py index 2794a313..c8b0c5cd 100644 --- a/orchestra/utils/humanize.py +++ b/orchestra/utils/humanize.py @@ -164,3 +164,18 @@ def text2int(textnum, numwords={}): return result + current + +UNITS_CONVERSIONS = { + 1024**4: ['TB', 'TiB', 'TERABYTES'], + 1024**3: ['GB', 'GiB', 'GYGABYTES'], + 1024**2: ['MB', 'MiB', 'MEGABYTES'], + 1024: ['KB', 'KiB', 'KYLOBYTES'], + 1: ['B', 'BYTES'], +} + +def unit_to_bytes(unit): + unit = unit.upper() + for bytes, units in UNITS_CONVERSIONS.iteritems(): + if unit in units: + return bytes + raise KeyError("%s is not a valid unit." % unit)