metrics reduce code get top 10 resources

This commit is contained in:
Jorge Pastor 2024-01-25 19:34:54 +01:00
parent dc2927bc88
commit 4cfbf57428
1 changed files with 15 additions and 21 deletions

View File

@ -12,18 +12,25 @@ from orchestra.contrib.saas.models import SaaS
# Crear métricas de tipo Gauge con etiquetas # Crear métricas de tipo Gauge con etiquetas
usuarios_metrica = Gauge('usuarios', 'Número total de usuarios', ['tipo', 'estado']) usuarios_metrica = Gauge('usuarios', 'Número total de usuarios', ['tipo', 'estado'])
usuarios_top_size_metrica = Gauge('usuarios_top_size', 'Top 10 cuentas ocupan espacio', ['account'])
websites_metrica = Gauge('websites_server', 'Número total de websites en server', ['target_server', 'estado']) websites_metrica = Gauge('websites_server', 'Número total de websites en server', ['target_server', 'estado'])
databases_metrica = Gauge('databases', 'Número total de websites en server', ['target_server']) databases_metrica = Gauge('databases', 'Número total de websites en server', ['target_server'])
databases_top_size_metrica = Gauge('databases_top_size', 'Top 10 databases ocupan espacio', ['database'])
mailboxes_metrica = Gauge('mailbox', 'Número total de mailbox', ['estado']) mailboxes_metrica = Gauge('mailbox', 'Número total de mailbox', ['estado'])
mailboxes_top_size_metrica = Gauge('mailboxes_top_size', 'Top 10 mailboxes ocupan espacio', ['mailbox'])
lists_metrica = Gauge('lists', 'Número total de listas') lists_metrica = Gauge('lists', 'Número total de listas')
saas_metrica = Gauge('saas', 'Número total de saas', ['service', 'estado']) saas_metrica = Gauge('saas', 'Número total de saas', ['service', 'estado'])
saas_top_size_metrica = Gauge('saas_top_size', 'Top 10 saas ocupan espacio', ['saas'])
usuarios_top_size_metrica = Gauge('usuarios_top_size', 'Top 10 cuentas ocupan espacio', ['object'])
databases_top_size_metrica = Gauge('databases_top_size', 'Top 10 databases ocupan espacio', ['object'])
mailboxes_top_size_metrica = Gauge('mailboxes_top_size', 'Top 10 mailboxes ocupan espacio', ['object'])
saas_top_size_metrica = Gauge('saas_top_size', 'Top 10 saas ocupan espacio', ['object'])
def get_size_resourcedata(id_resource, metrica):
top_resources = ResourceData.objects.filter(resource_id=id_resource, used__isnull=False).order_by('-used')[:10]
for resourcedata in top_resources:
metrica.labels(object=resourcedata.content_object_repr).set(resourcedata.used)
def actualizar_metrica_usuarios(): def actualizar_metrica_usuarios():
# Generar una lista de usuarios aleatorios para el ejemplo get_size_resourcedata(4, usuarios_top_size_metrica)
usuarios = Account.objects.all() usuarios = Account.objects.all()
user_dict = {} user_dict = {}
@ -41,10 +48,6 @@ def actualizar_metrica_usuarios():
usuarios_metrica.labels(tipo=type, estado='activo').set(value['activo']) usuarios_metrica.labels(tipo=type, estado='activo').set(value['activo'])
usuarios_metrica.labels(tipo=type, estado='no_activo').set(value['inactivo']) usuarios_metrica.labels(tipo=type, estado='no_activo').set(value['inactivo'])
top_resources = ResourceData.objects.filter(resource_id=4, used__isnull=False).order_by('-used')[:10]
for resourcedata in top_resources:
usuarios_top_size_metrica.labels(account=resourcedata.content_object_repr).set(resourcedata.used)
def actualizar_metrica_websites(): def actualizar_metrica_websites():
websites = Website.objects.all() websites = Website.objects.all()
@ -65,6 +68,7 @@ def actualizar_metrica_websites():
def actualizar_metrica_databases(): def actualizar_metrica_databases():
get_size_resourcedata(5, databases_top_size_metrica)
databases = Database.objects.all() databases = Database.objects.all()
data = {} data = {}
@ -76,12 +80,9 @@ def actualizar_metrica_databases():
for server, value in data.items(): for server, value in data.items():
databases_metrica.labels(target_server=server).set(value['total']) databases_metrica.labels(target_server=server).set(value['total'])
top_resources = ResourceData.objects.filter(resource_id=5, used__isnull=False).order_by('-used')[:10]
for resourcedata in top_resources:
databases_top_size_metrica.labels(database=resourcedata.content_object_repr).set(resourcedata.used)
def actualizar_metrica_mailboxes(): def actualizar_metrica_mailboxes():
get_size_resourcedata(1, mailboxes_top_size_metrica)
mailboxes = Mailbox.objects.all() mailboxes = Mailbox.objects.all()
mailbox_activos = sum(1 for mailbox in mailboxes if mailbox.is_active) mailbox_activos = sum(1 for mailbox in mailboxes if mailbox.is_active)
@ -90,16 +91,13 @@ def actualizar_metrica_mailboxes():
mailboxes_metrica.labels(estado='activo').set(mailbox_activos) mailboxes_metrica.labels(estado='activo').set(mailbox_activos)
mailboxes_metrica.labels(estado='no_activo').set(mailbox_inactivos) mailboxes_metrica.labels(estado='no_activo').set(mailbox_inactivos)
top_resources = ResourceData.objects.filter(resource_id=1, used__isnull=False).order_by('-used')[:10]
for resourcedata in top_resources:
mailboxes_top_size_metrica.labels(mailbox=resourcedata.content_object_repr).set(resourcedata.used)
def actualizar_metrica_lists(): def actualizar_metrica_lists():
lists = List.objects.all() lists = List.objects.all()
lists_metrica.set(len(lists)) lists_metrica.set(len(lists))
def actualizar_metrica_saas(): def actualizar_metrica_saas():
get_size_resourcedata(23, saas_top_size_metrica)
saases = SaaS.objects.all() saases = SaaS.objects.all()
saas_dict = {} saas_dict = {}
@ -115,7 +113,3 @@ def actualizar_metrica_saas():
for servicio, value in saas_dict.items(): for servicio, value in saas_dict.items():
saas_metrica.labels(service=servicio, estado='activo').set(value['activo']) saas_metrica.labels(service=servicio, estado='activo').set(value['activo'])
saas_metrica.labels(service=servicio, estado='no_activo').set(value['inactivo']) saas_metrica.labels(service=servicio, estado='no_activo').set(value['inactivo'])
top_resources = ResourceData.objects.filter(resource_id=23, used__isnull=False).order_by('-used')[:10]
for resourcedata in top_resources:
saas_top_size_metrica.labels(saas=resourcedata.content_object_repr).set(resourcedata.used)