В этой части:
Проверка доступности: ping и порты
Ping показывает, что хост жив, а проверка TCP-порта — что работает сервис. Для админа важнее второе: сервер может пинговаться, а служба 1С или SQL — лежать.
# src/pyreports/checks.py
import platform
import socket
import subprocess
from concurrent.futures import ThreadPoolExecutor
def ping(host: str, timeout_s: int = 1) -> bool:
if platform.system() == "Windows":
cmd = ["ping", "-n", "1", "-w", str(timeout_s * 1000), host]
else:
cmd = ["ping", "-c", "1", "-W", str(timeout_s), host]
return subprocess.run(cmd, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL).returncode == 0
def port_open(host: str, port: int, timeout_s: float = 2) -> bool:
try:
with socket.create_connection((host, port), timeout=timeout_s):
return True
except OSError:
return False
TARGETS = [
("srv-1c.corp.local", 1541), # менеджер кластера 1С
("srv-sql.corp.local", 5432), # PostgreSQL
("dc01.corp.local", 636), # LDAPS
("mail.company.kz", 587), # SMTP
]
def check_all(targets=TARGETS) -> list[tuple[str, int, bool]]:
with ThreadPoolExecutor(max_workers=20) as pool:
results = pool.map(lambda t: (t[0], t[1], port_open(*t)), targets)
return list(results)
if __name__ == "__main__":
for host, port, ok in check_all():
print(f"{'OK ' if ok else 'FAIL'} {host}:{port}")

