new features

This commit is contained in:
Mateusz Gruszczyński
2026-04-14 15:43:25 +02:00
parent 1a2ae0d607
commit 92a0f99fb3
17 changed files with 580 additions and 154 deletions

View File

@@ -1,6 +1,10 @@
from concurrent.futures import ThreadPoolExecutor
from datetime import datetime
import io
from pathlib import Path
import platform
import re
import subprocess
import paramiko
from sqlalchemy.orm import Session
@@ -11,6 +15,30 @@ from app.services.swos_beta_service import swos_beta_service
class RouterService:
def ping(self, router: Router):
if getattr(router, 'disable_ping', False):
return {'router_id': router.id, 'reachable': False, 'latency_ms': None, 'disabled': True}
count_flag = '-n' if platform.system().lower().startswith('win') else '-c'
timeout_flag = '-w' if platform.system().lower().startswith('win') else '-W'
command = ['ping', count_flag, '1', timeout_flag, '1', router.host]
try:
completed = subprocess.run(command, capture_output=True, text=True, timeout=3, check=False)
output = completed.stdout + "\n" + completed.stderr
if completed.returncode != 0:
return {'router_id': router.id, 'reachable': False, 'latency_ms': None, 'disabled': False}
match = re.search(r'time[=<]\s*([0-9]+(?:[.,][0-9]+)?)\s*ms', output, re.IGNORECASE)
latency = float(match.group(1).replace(',', '.')) if match else None
return {'router_id': router.id, 'reachable': True, 'latency_ms': latency, 'disabled': False}
except Exception:
return {'router_id': router.id, 'reachable': False, 'latency_ms': None, 'disabled': False}
def ping_many(self, routers: list[Router]):
if not routers:
return []
max_workers = min(8, max(1, len(routers)))
with ThreadPoolExecutor(max_workers=max_workers) as executor:
return list(executor.map(self.ping, routers))
def _load_pkey(self, ssh_key_str: str):
key_str = (ssh_key_str or "").strip()
key_buffer = io.StringIO(key_str)