Merge branch 'resources' into develmain

This commit is contained in:
Jorge Pastor 2024-03-03 11:06:11 +01:00
commit c47e5ac8f3
6 changed files with 51 additions and 9 deletions

View File

@ -452,12 +452,30 @@ class DovecotMaildirDisk(ServiceMonitor):
self.append(textwrap.dedent("""\ self.append(textwrap.dedent("""\
function monitor () { function monitor () {
SIZE=$(du -sb $1/Maildir/ 2> /dev/null || echo 0) && echo $SIZE | awk '{print $1}' SIZE=$(du -sb $1/Maildir/ 2> /dev/null || echo 0) && echo $SIZE | awk '{print $1}'
list=()
}""")) }"""))
def monitor(self, mailbox): def monitor(self, mailbox):
context = self.get_context(mailbox) context = self.get_context(mailbox)
# self.append("echo %(object_id)s $(monitor %(maildir_path)s)" % context) # self.append("echo %(object_id)s $(monitor %(maildir_path)s)" % context)
self.append("echo %(object_id)s $(monitor %(home)s)" % context) # self.append("echo %(object_id)s $(monitor %(home)s)" % context)
self.append("list[${#list[@]}]=\'echo %(object_id)s $(monitor %(home)s)\'" % context)
def commit(self):
self.append(textwrap.dedent("""\
proces=0
for cmd in "${list[@]}"
do
eval $cmd &
proces=$((proces+1))
if [ $proces -ge 10 ];then
wait
proces=0
fi
done
wait
exit $exit_code
"""))
def get_context(self, mailbox): def get_context(self, mailbox):
context = { context = {

View File

@ -2,6 +2,7 @@ import logging
import threading import threading
import traceback import traceback
from collections import OrderedDict from collections import OrderedDict
import random
from django.core.mail import mail_admins from django.core.mail import mail_admins
@ -111,6 +112,7 @@ def execute(scripts, serialize=False, run_async=None):
executions = [] executions = []
threads_to_join = [] threads_to_join = []
logs = [] logs = []
id_launch = random.randint(0, 2147483646)
for key, value in scripts.items(): for key, value in scripts.items():
route, __, async_action = key route, __, async_action = key
backend, operations = value backend, operations = value
@ -126,6 +128,7 @@ def execute(scripts, serialize=False, run_async=None):
with db.clone(model=BackendLog) as handle: with db.clone(model=BackendLog) as handle:
log = backend.create_log(*args, using=handle.target) log = backend.create_log(*args, using=handle.target)
log._state.db = handle.origin log._state.db = handle.origin
log.id_launch = id_launch
kwargs['log'] = log kwargs['log'] = log
task = keep_log(backend.execute, log, operations) task = keep_log(backend.execute, log, operations)
logger.debug('%s is going to be executed on %s.' % (backend, route.host)) logger.debug('%s is going to be executed on %s.' % (backend, route.host))

View File

@ -33,10 +33,11 @@ class Last(Aggregation):
def filter(self, dataset, date=None): def filter(self, dataset, date=None):
# dataset = dataset.order_by('object_id', '-id').distinct('monitor') lastdataset = dataset.order_by('-id').first()
now = timezone.now() dataset = dataset.filter( launch_id=lastdataset.launch_id)
epoch = now - datetime.timedelta(minutes=2) # now = timezone.now()
dataset = dataset.filter( created_at__range=(epoch, now )) # epoch = now - datetime.timedelta(minutes=2)
# dataset = dataset.filter( created_at__range=(epoch, now ))
if date is not None: if date is not None:
dataset = dataset.filter(created_at__lte=date) dataset = dataset.filter(created_at__lte=date)

View File

@ -72,6 +72,7 @@ class ServiceMonitor(ServiceBackend):
from .models import MonitorData from .models import MonitorData
name = self.get_name() name = self.get_name()
app_label, model_name = self.model.split('.') app_label, model_name = self.model.split('.')
id_launch = log.id_launch
ct = ContentType.objects.get_by_natural_key(app_label, model_name.lower()) ct = ContentType.objects.get_by_natural_key(app_label, model_name.lower())
for line in log.stdout.splitlines(): for line in log.stdout.splitlines():
line = line.strip() line = line.strip()
@ -83,7 +84,7 @@ class ServiceMonitor(ServiceBackend):
content_object = ct.get_object_for_this_type(pk=object_id) content_object = ct.get_object_for_this_type(pk=object_id)
MonitorData.objects.create( MonitorData.objects.create(
monitor=name, object_id=object_id, content_type=ct, value=value, state=state, monitor=name, object_id=object_id, content_type=ct, value=value, state=state,
created_at=self.current_date, content_object_repr=str(content_object), created_at=self.current_date, content_object_repr=str(content_object), launch_id=id_launch,
) )
def execute(self, *args, **kwargs): def execute(self, *args, **kwargs):

View File

@ -280,6 +280,7 @@ class MonitorData(models.Model):
content_object = GenericForeignKey() content_object = GenericForeignKey()
objects = MonitorDataQuerySet.as_manager() objects = MonitorDataQuerySet.as_manager()
launch_id = models.PositiveIntegerField(_("launch id"), blank=True, null=True)
class Meta: class Meta:
get_latest_by = 'id' get_latest_by = 'id'

View File

@ -287,13 +287,31 @@ class UNIXUserDisk(ServiceMonitor):
self.append(textwrap.dedent("""\ self.append(textwrap.dedent("""\
function monitor () { function monitor () {
{ SIZE=$(du -bs "$1") && echo $SIZE || echo 0; } | awk {'print $1'} { SIZE=$(du -bs "$1") && echo $SIZE || echo 0; } | awk {'print $1'}
}""" }
)) list=()
"""))
def monitor(self, user): def monitor(self, user):
context = self.get_context(user) context = self.get_context(user)
self.append("echo %(object_id)s $(monitor %(base_home)s)" % context) # self.append("echo %(object_id)s $(monitor %(base_home)s)" % context)
self.append("list[${#list[@]}]=\'echo %(object_id)s $(monitor %(base_home)s)\'" % context)
def commit(self):
self.append(textwrap.dedent("""\
proces=0
for cmd in "${list[@]}"
do
eval $cmd &
proces=$((proces+1))
if [ $proces -ge 5 ];then
wait
proces=0
fi
done
wait
exit $exit_code
"""))
def get_context(self, user): def get_context(self, user):
context = { context = {
'object_id': user.pk, 'object_id': user.pk,