Fix ping latency to use actual RTT instead of subprocess time
Parse the real RTT from ping output instead of measuring subprocess execution time, which included process startup overhead. Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
@@ -4,8 +4,8 @@ Checker Ping.
|
|||||||
Vérifie la disponibilité d'un hôte via ICMP ping.
|
Vérifie la disponibilité d'un hôte via ICMP ping.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
import re
|
||||||
import subprocess
|
import subprocess
|
||||||
import time
|
|
||||||
import platform
|
import platform
|
||||||
|
|
||||||
from .base import BaseChecker, CheckResult
|
from .base import BaseChecker, CheckResult
|
||||||
@@ -41,7 +41,6 @@ class PingChecker(BaseChecker):
|
|||||||
else:
|
else:
|
||||||
cmd = ["ping", "-c", str(count), "-W", str(timeout), host]
|
cmd = ["ping", "-c", str(count), "-W", str(timeout), host]
|
||||||
|
|
||||||
start = time.time()
|
|
||||||
try:
|
try:
|
||||||
result = subprocess.run(
|
result = subprocess.run(
|
||||||
cmd,
|
cmd,
|
||||||
@@ -49,9 +48,12 @@ class PingChecker(BaseChecker):
|
|||||||
text=True,
|
text=True,
|
||||||
timeout=timeout + 5
|
timeout=timeout + 5
|
||||||
)
|
)
|
||||||
response_time = (time.time() - start) * 1000 # ms
|
|
||||||
|
|
||||||
if result.returncode == 0:
|
if result.returncode == 0:
|
||||||
|
# Parse RTT from ping output (e.g., "time=0.32 ms" or "time=0.32ms")
|
||||||
|
match = re.search(r"time[=<]([\d.]+)\s*ms", result.stdout)
|
||||||
|
response_time = float(match.group(1)) if match else None
|
||||||
|
|
||||||
return CheckResult(
|
return CheckResult(
|
||||||
success=True,
|
success=True,
|
||||||
message="Host is reachable",
|
message="Host is reachable",
|
||||||
|
|||||||
Reference in New Issue
Block a user