metrics lists and saas

This commit is contained in:
Jorge Pastor 2024-01-24 20:28:12 +01:00
parent 3d8fddfd9e
commit dc2927bc88
2 changed files with 66 additions and 4 deletions

View File

@ -5,14 +5,22 @@ from orchestra.contrib.accounts.models import Account
from orchestra.contrib.websites.models import Website from orchestra.contrib.websites.models import Website
from orchestra.contrib.databases.models import Database from orchestra.contrib.databases.models import Database
from orchestra.contrib.resources.models import ResourceData from orchestra.contrib.resources.models import ResourceData
from orchestra.contrib.mailboxes.models import Mailbox
from orchestra.contrib.lists.models import List
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', 'Número total de websites en server', ['database']) 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_top_size_metrica = Gauge('mailboxes_top_size', 'Top 10 mailboxes ocupan espacio', ['mailbox'])
lists_metrica = Gauge('lists', 'Número total de listas')
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'])
def actualizar_metrica_usuarios(): def actualizar_metrica_usuarios():
# Generar una lista de usuarios aleatorios para el ejemplo # Generar una lista de usuarios aleatorios para el ejemplo
@ -33,6 +41,11 @@ 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()
@ -66,4 +79,43 @@ def actualizar_metrica_databases():
top_resources = ResourceData.objects.filter(resource_id=5, used__isnull=False).order_by('-used')[:10] top_resources = ResourceData.objects.filter(resource_id=5, used__isnull=False).order_by('-used')[:10]
for resourcedata in top_resources: for resourcedata in top_resources:
databases_top_size_metrica.labels(database=resourcedata.content_object_repr).set(resourcedata.used) databases_top_size_metrica.labels(database=resourcedata.content_object_repr).set(resourcedata.used)
def actualizar_metrica_mailboxes():
mailboxes = Mailbox.objects.all()
mailbox_activos = sum(1 for mailbox in mailboxes if mailbox.is_active)
mailbox_inactivos = len(mailboxes) - mailbox_activos
mailboxes_metrica.labels(estado='activo').set(mailbox_activos)
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():
lists = List.objects.all()
lists_metrica.set(len(lists))
def actualizar_metrica_saas():
saases = SaaS.objects.all()
saas_dict = {}
for saas in saases:
if saas.service not in saas_dict.keys():
saas_dict[saas.service] = {'activo':0, 'inactivo':0}
if saas.is_active:
saas_dict[saas.service]['activo'] += 1
else:
saas_dict[saas.service]['inactivo'] += 1
for servicio, value in saas_dict.items():
saas_metrica.labels(service=servicio, estado='activo').set(value['activo'])
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)

View File

@ -3,7 +3,14 @@ from django.http import HttpResponse
from django.views.decorators.http import require_GET from django.views.decorators.http import require_GET
from prometheus_client.exposition import generate_latest from prometheus_client.exposition import generate_latest
from prometheus_client import REGISTRY, CONTENT_TYPE_LATEST from prometheus_client import REGISTRY, CONTENT_TYPE_LATEST
from .custom_metrics import actualizar_metrica_usuarios, actualizar_metrica_websites, actualizar_metrica_databases from .custom_metrics import (
actualizar_metrica_usuarios,
actualizar_metrica_websites,
actualizar_metrica_databases,
actualizar_metrica_mailboxes,
actualizar_metrica_lists,
actualizar_metrica_saas,
)
@require_GET @require_GET
def metrics_view(request): def metrics_view(request):
@ -11,6 +18,9 @@ def metrics_view(request):
actualizar_metrica_usuarios() actualizar_metrica_usuarios()
actualizar_metrica_websites() actualizar_metrica_websites()
actualizar_metrica_databases() actualizar_metrica_databases()
actualizar_metrica_mailboxes()
actualizar_metrica_lists()
actualizar_metrica_saas()
# Devolver las métricas exportadas como respuesta HTTP # Devolver las métricas exportadas como respuesta HTTP
output = generate_latest(REGISTRY) output = generate_latest(REGISTRY)