2024-01-17 18:03:10 +00:00
|
|
|
from prometheus_client import start_http_server, Gauge
|
|
|
|
import random
|
|
|
|
|
|
|
|
from orchestra.contrib.accounts.models import Account
|
|
|
|
from orchestra.contrib.websites.models import Website
|
2024-01-24 17:58:54 +00:00
|
|
|
from orchestra.contrib.databases.models import Database
|
|
|
|
from orchestra.contrib.resources.models import ResourceData
|
2024-01-24 19:28:12 +00:00
|
|
|
from orchestra.contrib.mailboxes.models import Mailbox
|
|
|
|
from orchestra.contrib.lists.models import List
|
|
|
|
from orchestra.contrib.saas.models import SaaS
|
2024-01-17 18:03:10 +00:00
|
|
|
|
|
|
|
|
|
|
|
# Crear métricas de tipo Gauge con etiquetas
|
2024-01-24 17:58:54 +00:00
|
|
|
usuarios_metrica = Gauge('usuarios', 'Número total de usuarios', ['tipo', 'estado'])
|
2024-01-17 18:03:10 +00:00
|
|
|
websites_metrica = Gauge('websites_server', 'Número total de websites en server', ['target_server', 'estado'])
|
2024-01-24 17:58:54 +00:00
|
|
|
databases_metrica = Gauge('databases', 'Número total de websites en server', ['target_server'])
|
2024-01-24 19:28:12 +00:00
|
|
|
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'])
|
2024-01-25 18:34:54 +00:00
|
|
|
|
|
|
|
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)
|
2024-01-17 18:03:10 +00:00
|
|
|
|
2024-01-25 19:22:04 +00:00
|
|
|
def get_data_objects_with_active(model, field_type):
|
|
|
|
objects = model.objects.all()
|
2024-01-24 17:58:54 +00:00
|
|
|
|
2024-01-25 19:22:04 +00:00
|
|
|
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
|
2024-01-24 17:58:54 +00:00
|
|
|
else:
|
2024-01-25 19:22:04 +00:00
|
|
|
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')
|
2024-01-17 18:03:10 +00:00
|
|
|
|
2024-01-24 17:58:54 +00:00
|
|
|
# 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'])
|
2024-01-17 18:03:10 +00:00
|
|
|
|
2024-01-24 19:28:12 +00:00
|
|
|
|
2024-01-17 18:03:10 +00:00
|
|
|
def actualizar_metrica_websites():
|
2024-01-25 19:22:04 +00:00
|
|
|
website_dict = get_data_objects_with_active(Website, 'target_server')
|
2024-01-17 18:03:10 +00:00
|
|
|
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'])
|
|
|
|
|
2024-01-24 17:58:54 +00:00
|
|
|
|
|
|
|
def actualizar_metrica_databases():
|
2024-01-25 18:34:54 +00:00
|
|
|
get_size_resourcedata(5, databases_top_size_metrica)
|
2024-01-24 17:58:54 +00:00
|
|
|
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'])
|
|
|
|
|
2024-01-24 19:28:12 +00:00
|
|
|
|
|
|
|
def actualizar_metrica_mailboxes():
|
2024-01-25 18:34:54 +00:00
|
|
|
get_size_resourcedata(1, mailboxes_top_size_metrica)
|
2024-01-24 19:28:12 +00:00
|
|
|
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():
|
2024-01-25 18:34:54 +00:00
|
|
|
get_size_resourcedata(23, saas_top_size_metrica)
|
2024-01-25 19:22:04 +00:00
|
|
|
saas_dict = get_data_objects_with_active(SaaS, 'service')
|
2024-01-24 19:28:12 +00:00
|
|
|
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'])
|