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

53 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-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'
actions = ('monitor', 'resource_exceeded', 'resource_recovery')
@classmethod
def get_backends(cls):
""" filter monitor classes """
return [plugin for plugin in cls.plugins if ServiceMonitor in plugin.__mro__]
2014-07-10 15:19:06 +00:00
@cached
def get_last_date(self, obj):
from .models import MonitorData
try:
# TODO replace
#return MonitorData.objects.filter(content_object=obj).latest().date
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):
return datetime.datetime.now()
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