Initial commit: LAN Checker
Network health monitoring script with MQTT reporting for Home Assistant. - Ping, HTTP, and SNMP checkers - MQTT Discovery for automatic entity creation - Configurable check intervals Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
53
checkers/http.py
Normal file
53
checkers/http.py
Normal file
@@ -0,0 +1,53 @@
|
||||
import time
|
||||
|
||||
import requests
|
||||
|
||||
from .base import BaseChecker, CheckResult
|
||||
|
||||
|
||||
class HttpChecker(BaseChecker):
|
||||
def check(self) -> CheckResult:
|
||||
url = self.config["url"]
|
||||
method = self.config.get("method", "GET").upper()
|
||||
timeout = self.config.get("timeout", 10)
|
||||
expected_status = self.config.get("expected_status", 200)
|
||||
verify_ssl = self.config.get("verify_ssl", True)
|
||||
headers = self.config.get("headers", {})
|
||||
|
||||
start = time.time()
|
||||
try:
|
||||
response = requests.request(
|
||||
method=method,
|
||||
url=url,
|
||||
timeout=timeout,
|
||||
verify=verify_ssl,
|
||||
headers=headers
|
||||
)
|
||||
response_time = (time.time() - start) * 1000 # ms
|
||||
|
||||
if response.status_code == expected_status:
|
||||
return CheckResult(
|
||||
success=True,
|
||||
message=f"HTTP {response.status_code}",
|
||||
response_time=response_time,
|
||||
details={"status_code": response.status_code}
|
||||
)
|
||||
else:
|
||||
return CheckResult(
|
||||
success=False,
|
||||
message=f"Unexpected status: {response.status_code} (expected {expected_status})",
|
||||
response_time=response_time,
|
||||
details={"status_code": response.status_code}
|
||||
)
|
||||
except requests.Timeout:
|
||||
return CheckResult(
|
||||
success=False,
|
||||
message="HTTP timeout",
|
||||
response_time=None
|
||||
)
|
||||
except requests.RequestException as e:
|
||||
return CheckResult(
|
||||
success=False,
|
||||
message=f"HTTP error: {e}",
|
||||
response_time=None
|
||||
)
|
||||
Reference in New Issue
Block a user