Add server monitoring functionality and update Makefile and requirements
- Introduced a new server monitoring module in `run_helper.py` with graceful shutdown handling. - Updated `.gitignore` to include PID files. - Added `test-monitor` target in `Makefile` for testing the server monitoring module. - Included `psutil` in `requirements.txt` for system monitoring capabilities.
This commit is contained in:
81
tests/test_monitor.py
Normal file
81
tests/test_monitor.py
Normal file
@@ -0,0 +1,81 @@
|
||||
#!/usr/bin/env python3
|
||||
"""
|
||||
Тестовый скрипт для проверки модуля мониторинга сервера
|
||||
"""
|
||||
import asyncio
|
||||
import sys
|
||||
import os
|
||||
|
||||
# Добавляем путь к проекту
|
||||
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
|
||||
|
||||
from helper_bot.server_monitor import ServerMonitor
|
||||
|
||||
|
||||
class MockBot:
|
||||
"""Мок объект бота для тестирования"""
|
||||
|
||||
async def send_message(self, chat_id, text, parse_mode=None):
|
||||
print(f"\n{'='*60}")
|
||||
print(f"Отправка в чат: {chat_id}")
|
||||
print(f"Текст сообщения:")
|
||||
print(text)
|
||||
print(f"{'='*60}\n")
|
||||
|
||||
|
||||
async def test_monitor():
|
||||
"""Тестирование модуля мониторинга"""
|
||||
print("🧪 Тестирование модуля мониторинга сервера")
|
||||
print("=" * 60)
|
||||
|
||||
# Создаем мок бота
|
||||
mock_bot = MockBot()
|
||||
|
||||
# Создаем монитор
|
||||
monitor = ServerMonitor(
|
||||
bot=mock_bot,
|
||||
group_for_logs="-123456789",
|
||||
important_logs="-987654321"
|
||||
)
|
||||
|
||||
print("📊 Получение информации о системе...")
|
||||
system_info = monitor.get_system_info()
|
||||
|
||||
if system_info:
|
||||
print("✅ Информация о системе получена успешно")
|
||||
print(f"CPU: {system_info['cpu_percent']}%")
|
||||
print(f"RAM: {system_info['ram_percent']}%")
|
||||
print(f"Disk: {system_info['disk_percent']}%")
|
||||
print(f"Uptime: {system_info['system_uptime']}")
|
||||
|
||||
print("\n🤖 Проверка статуса процессов...")
|
||||
voice_status = monitor.check_process_status('voice_bot')
|
||||
helper_status = monitor.check_process_status('helper_bot')
|
||||
print(f"Voice Bot: {voice_status}")
|
||||
print(f"Helper Bot: {helper_status}")
|
||||
|
||||
print("\n📝 Тестирование отправки статуса...")
|
||||
await monitor.send_status_message(system_info)
|
||||
|
||||
print("\n🚨 Тестирование отправки алерта...")
|
||||
await monitor.send_alert_message(
|
||||
"Использование CPU",
|
||||
85.5,
|
||||
"Нагрузка за 1 мин: 2.5"
|
||||
)
|
||||
|
||||
print("\n✅ Тестирование отправки сообщения о восстановлении...")
|
||||
await monitor.send_recovery_message(
|
||||
"Использование CPU",
|
||||
70.0,
|
||||
85.5
|
||||
)
|
||||
|
||||
else:
|
||||
print("❌ Не удалось получить информацию о системе")
|
||||
|
||||
print("\n🎯 Тестирование завершено!")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
asyncio.run(test_monitor())
|
||||
Reference in New Issue
Block a user