from prometheus_client import start_http_server, Gauge import random from orchestra.contrib.accounts.models import Account from orchestra.contrib.websites.models import Website from orchestra.contrib.databases.models import Database 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 usuarios_metrica = Gauge('usuarios', 'Número total de usuarios', ['tipo', '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']) mailboxes_metrica = Gauge('mailbox', 'Número total de mailbox', ['estado']) lists_metrica = Gauge('lists', 'Número total de listas') saas_metrica = Gauge('saas', 'Número total de saas', ['service', 'estado']) 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 get_data_objects_with_active(model, field_type): objects = model.objects.all() object_dict = {} for obj in objects: key = getattr(obj, field_type) if key not in object_dict: object_dict[key] = {'activo':0, 'inactivo':0} if getattr(obj, 'is_active'): object_dict[key]['activo'] += 1 else: object_dict[key]['inactivo'] += 1 return object_dict def actualizar_metrica_usuarios(): get_size_resourcedata(4, usuarios_top_size_metrica) user_dict = get_data_objects_with_active(Account, 'type') # envia metrica por cada tipo de usuario num de activos y inactivos for type, value in user_dict.items(): usuarios_metrica.labels(tipo=type, estado='activo').set(value['activo']) usuarios_metrica.labels(tipo=type, estado='no_activo').set(value['inactivo']) def actualizar_metrica_websites(): website_dict = get_data_objects_with_active(Website, 'target_server') for server, value in website_dict.items(): websites_metrica.labels(target_server=server, estado='activo').set(value['activo']) websites_metrica.labels(target_server=server, estado='no_activo').set(value['inactivo']) def actualizar_metrica_databases(): get_size_resourcedata(5, databases_top_size_metrica) databases = Database.objects.all() data = {} for database in databases: if database.target_server.name not in data.keys(): data[database.target_server.name] = {'total':0} data[database.target_server.name]['total'] += 1 for server, value in data.items(): databases_metrica.labels(target_server=server).set(value['total']) def actualizar_metrica_mailboxes(): get_size_resourcedata(1, mailboxes_top_size_metrica) 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) def actualizar_metrica_lists(): lists = List.objects.all() lists_metrica.set(len(lists)) def actualizar_metrica_saas(): get_size_resourcedata(23, saas_top_size_metrica) saas_dict = get_data_objects_with_active(SaaS, 'service') 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'])