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

55 lines
1.8 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-10 15:19:06 +00:00
2014-07-09 16:17:43 +00:00
from orchestra.apps.orchestration import ServiceBackend
2014-07-10 15:19:06 +00:00
from orchestra.utils.functional import cached
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-09 16:17:43 +00:00
actions = ('monitor', 'resource_exceeded', 'resource_recovery')
@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-10 15:19:06 +00:00
@cached
def get_last_date(self, obj):
from .models import MonitorData
try:
2014-07-10 17:34:23 +00:00
ct = ContentType.objects.get_for_model(type(obj))
2014-07-10 15:19:06 +00:00
return MonitorData.objects.filter(content_type=ct, object_id=obj.pk).latest().date
except MonitorData.DoesNotExist:
return self.get_current_date() - datetime.timedelta(days=1)
@cached
def get_current_date(self):
2014-07-11 14:48:46 +00:00
return timezone.now()
2014-07-10 15:19:06 +00:00
def store(self, log):
2014-07-09 16:17:43 +00:00
""" object_id value """
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()
object_id, value = line.split()
2014-07-10 15:19:06 +00:00
MonitorData.objects.create(monitor=name, object_id=object_id,
content_type=ct, value=value, date=self.get_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