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

70 lines
2.2 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-07-10 15:19:06 +00:00
2014-07-09 16:17:43 +00:00
from orchestra.apps.orchestration import ServiceBackend
class ServiceMonitor(ServiceBackend):
TRAFFIC = 'traffic'
DISK = 'disk'
MEMORY = 'memory'
CPU = 'cpu'
2014-07-11 21:09:17 +00:00
# TODO UNITS
2014-07-09 16:17:43 +00:00
2014-07-16 15:20:16 +00:00
actions = ('monitor', 'exceeded', 'recovery')
2014-07-09 16:17:43 +00:00
@classmethod
def get_backends(cls):
""" filter monitor classes """
2014-07-11 14:48:46 +00:00
for backend in cls.plugins:
if backend != ServiceMonitor and ServiceMonitor in backend.__mro__:
yield backend
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()
return ContentType.objects.get(app_label=app_label, model=model)
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)
return data.date
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-07-16 15:20:16 +00:00
""" stores montirod 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('.')
ct = ContentType.objects.get(app_label=app_label, model=model_name.lower())
for line in log.stdout.splitlines():
2014-07-09 16:17:43 +00:00
line = line.strip()
2014-07-16 15:20:16 +00:00
object_id, value = self.process(line)
2014-07-10 15:19:06 +00:00
MonitorData.objects.create(monitor=name, object_id=object_id,
2014-07-14 14:56:48 +00:00
content_type=ct, value=value, date=self.current_date)
2014-07-09 16:17:43 +00:00
def execute(self, server):
2014-07-10 15:19:06 +00:00
log = super(ServiceMonitor, self).execute(server)
self.store(log)
2014-07-09 16:17:43 +00:00
return log