Files
prod/infra/monitoring/test_monitor.py

99 lines
4.0 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#!/usr/bin/env python3
"""
Тестовый скрипт для проверки работы модуля мониторинга
"""
import sys
import os
import logging
# Добавляем текущую директорию в путь для импорта
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
from server_monitor import ServerMonitor
# Настройка логирования
logging.basicConfig(
level=logging.INFO,
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s'
)
def main():
"""Основная функция тестирования"""
print("🚀 Тестирование модуля мониторинга сервера")
print("=" * 50)
try:
# Создаем экземпляр мониторинга
monitor = ServerMonitor()
# Получаем информацию о системе
print("📊 Получение информации о системе...")
system_info = monitor.get_system_info()
if system_info:
print("✅ Информация о системе получена успешно")
print(f" CPU: {system_info.get('cpu_percent', 'N/A')}%")
print(f" RAM: {system_info.get('ram_percent', 'N/A')}%")
print(f" Диск: {system_info.get('disk_percent', 'N/A')}%")
print(f" Хост: {system_info.get('server_hostname', 'N/A')}")
print(f" ОС: {monitor.os_type}")
else:
print("Не удалось получить информацию о системе")
return
# Проверяем статус процессов
print("\n🤖 Проверка статуса процессов...")
helper_status, helper_uptime = monitor.check_process_status('helper_bot')
print(f" Helper Bot: {helper_status} - {helper_uptime}")
# Получаем метрики для Prometheus
print("\n📈 Получение метрик для Prometheus...")
metrics = monitor.get_metrics_data()
if metrics:
print("✅ Метрики получены успешно")
for key, value in metrics.items():
print(f" {key}: {value}")
else:
print("Не удалось получить метрики")
# Проверяем алерты
print("\n🚨 Проверка алертов...")
alerts, recoveries = monitor.check_alerts(system_info)
if alerts:
print(f" Найдено алертов: {len(alerts)}")
for alert_type, value, details in alerts:
print(f" {alert_type}: {value}% - {details}")
else:
print(" Алертов не найдено")
if recoveries:
print(f" Найдено восстановлений: {len(recoveries)}")
for recovery_type, value in recoveries:
print(f" {recovery_type}: {value}%")
# Получаем сообщение о статусе
print("\n💬 Формирование сообщения о статусе...")
status_message = monitor.get_status_message(system_info)
if status_message:
print("✅ Сообщение о статусе сформировано")
print(" Первые 200 символов:")
print(f" {status_message[:200]}...")
else:
print("Не удалось сформировать сообщение о статусе")
print("\n🎉 Тестирование завершено успешно!")
except Exception as e:
print(f"❌ Ошибка при тестировании: {e}")
logging.error(f"Ошибка при тестировании: {e}", exc_info=True)
return 1
return 0
if __name__ == "__main__":
exit(main())