django-orchestra-test/orchestra/contrib/resources/backends.py

79 lines
2.6 KiB
Python
Raw Normal View History

2014-07-10 15:19:06 +00:00
import datetime
from django.contrib.contenttypes.models import ContentType
2014-07-11 14:48:46 +00:00
from django.utils import timezone
2014-07-14 14:56:48 +00:00
from django.utils.functional import cached_property
2014-10-11 16:21:51 +00:00
from django.utils.translation import ugettext_lazy as _
2014-07-10 15:19:06 +00:00
2015-04-05 10:46:24 +00:00
from orchestra.contrib.orchestration import ServiceBackend
2014-07-09 16:17:43 +00:00
class ServiceMonitor(ServiceBackend):
TRAFFIC = 'traffic'
DISK = 'disk'
MEMORY = 'memory'
CPU = 'cpu'
2014-07-11 21:09:17 +00:00
# TODO UNITS
2014-07-16 15:20:16 +00:00
actions = ('monitor', 'exceeded', 'recovery')
2014-10-11 16:21:51 +00:00
abstract = True
2014-07-09 16:17:43 +00:00
@classmethod
def get_plugins(cls):
2014-10-11 16:21:51 +00:00
""" filter controller classes """
return [
plugin for plugin in cls.plugins if ServiceMonitor in plugin.__mro__
]
@classmethod
def get_verbose_name(cls):
return _("[M] %s") % super(ServiceMonitor, cls).get_verbose_name()
2014-07-09 16:17:43 +00:00
2014-07-14 14:56:48 +00:00
@cached_property
def current_date(self):
return timezone.now()
@cached_property
def content_type(self):
app_label, model = self.model.split('.')
model = model.lower()
2014-07-21 12:20:04 +00:00
return ContentType.objects.get_by_natural_key(app_label, model)
2014-07-14 14:56:48 +00:00
def get_last_data(self, object_id):
2014-07-10 15:19:06 +00:00
from .models import MonitorData
try:
2014-07-14 14:56:48 +00:00
return MonitorData.objects.filter(content_type=self.content_type,
object_id=object_id).latest()
2014-07-10 15:19:06 +00:00
except MonitorData.DoesNotExist:
2014-07-14 14:56:48 +00:00
return None
def get_last_date(self, object_id):
data = self.get_last_data(object_id)
if data is None:
return self.current_date - datetime.timedelta(days=1)
2014-10-27 13:29:02 +00:00
return data.created_at
2014-07-10 15:19:06 +00:00
2014-07-16 15:20:16 +00:00
def process(self, line):
""" line -> object_id, value """
return line.split()
2014-07-10 15:19:06 +00:00
def store(self, log):
2014-09-22 15:59:53 +00:00
""" stores monitored values from stdout """
2014-07-10 15:19:06 +00:00
from .models import MonitorData
name = self.get_name()
app_label, model_name = self.model.split('.')
2014-07-21 12:20:04 +00:00
ct = ContentType.objects.get_by_natural_key(app_label, model_name.lower())
2014-07-10 15:19:06 +00:00
for line in log.stdout.splitlines():
2014-07-09 16:17:43 +00:00
line = line.strip()
try:
object_id, value = self.process(line)
except ValueError:
cls_name = self.__class__.__name__
raise ValueError("%s expected '<id> <value>' got '%s'" % (cls_name, line))
2014-07-10 15:19:06 +00:00
MonitorData.objects.create(monitor=name, object_id=object_id,
2014-10-27 13:29:02 +00:00
content_type=ct, value=value, created_at=self.current_date)
2014-07-09 16:17:43 +00:00
def execute(self, server, async=False):
log = super(ServiceMonitor, self).execute(server, async=async)
2014-07-10 15:19:06 +00:00
self.store(log)
2014-07-09 16:17:43 +00:00
return log