Files
bgp-server/scripts/monitor.py
T
denozord 5d4d24b49f
Build and Push Docker Image / build (push) Has been cancelled
Sure! Pl
2025-07-11 13:23:21 +07:00

93 lines
3.2 KiB
Python

#!/usr/bin/env python3
"""
Скрипт мониторинга BGP сервера
"""
import subprocess
import json
import time
import sys
from datetime import datetime
def run_command(cmd):
"""Выполнение команды и возврат результата"""
try:
result = subprocess.run(cmd, shell=True, capture_output=True, text=True)
return result.returncode == 0, result.stdout, result.stderr
except Exception as e:
return False, "", str(e)
def check_container_status():
"""Проверка статуса Docker контейнера"""
success, output, error = run_command("docker ps --filter name=bgp-server --format '{{.Status}}'")
if success and output.strip():
return True, output.strip()
return False, "Container not running"
def check_bgp_session():
"""Проверка BGP сессии"""
success, output, error = run_command("docker exec bgp-server exabgpcli show neighbor")
if success:
return True, output
return False, "Cannot check BGP session"
def check_prefixes():
"""Проверка загруженных префиксов"""
try:
with open("/app/logs/prefixes.log", "r") as f:
lines = f.readlines()
return True, f"Found {len(lines)} log entries"
except FileNotFoundError:
return False, "Prefix log not found"
def check_network_connectivity():
"""Проверка сетевого подключения"""
success, output, error = run_command("docker exec bgp-server ping -c 1 192.168.1.1")
return success, "Network connectivity OK" if success else "Network connectivity failed"
def main():
"""Основная функция мониторинга"""
print("=== BGP Server Monitoring ===")
print(f"Time: {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}")
print()
# Проверка контейнера
print("1. Container Status:")
status, details = check_container_status()
print(f" Status: {'✓ Running' if status else '✗ Stopped'}")
print(f" Details: {details}")
print()
# Проверка сети
print("2. Network Connectivity:")
net_ok, net_details = check_network_connectivity()
print(f" Status: {'✓ OK' if net_ok else '✗ Failed'}")
print(f" Details: {net_details}")
print()
# Проверка BGP сессии
print("3. BGP Session:")
bgp_ok, bgp_details = check_bgp_session()
print(f" Status: {'✓ Active' if bgp_ok else '✗ Inactive'}")
if bgp_ok:
print(f" Details: {bgp_details[:200]}...")
else:
print(f" Details: {bgp_details}")
print()
# Проверка префиксов
print("4. Prefixes:")
prefix_ok, prefix_details = check_prefixes()
print(f" Status: {'✓ Loaded' if prefix_ok else '✗ Not loaded'}")
print(f" Details: {prefix_details}")
print()
# Общий статус
overall_status = status and net_ok and bgp_ok and prefix_ok
print("=== Overall Status ===")
print(f"Status: {'✓ HEALTHY' if overall_status else '✗ UNHEALTHY'}")
return 0 if overall_status else 1
if __name__ == "__main__":
sys.exit(main())