42 lines
1.1 KiB
Python
42 lines
1.1 KiB
Python
# checks.py
|
|
import socket
|
|
import urllib.request
|
|
import urllib.error
|
|
|
|
|
|
class SystemChecks:
|
|
|
|
def __init__(self, network_targets, ziti_target):
|
|
self.network_targets = network_targets
|
|
self.ziti_target = ziti_target
|
|
|
|
def check_network_status(self):
|
|
"""Loops through configured network targets and returns True if any succeed."""
|
|
for host, port in self.network_targets:
|
|
if self._tcp_ping(host, int(port), timeout=1.5):
|
|
return True
|
|
return False
|
|
|
|
def check_ziti_status(self):
|
|
"""Makes an HTTP request to the configured address."""
|
|
if not self.ziti_target:
|
|
return False
|
|
|
|
try:
|
|
request = urllib.request.Request(
|
|
self.ziti_target,
|
|
method="GET",
|
|
)
|
|
|
|
with urllib.request.urlopen(request, timeout=2.0) as response:
|
|
return 200 <= response.status < 300
|
|
|
|
except (urllib.error.URLError, TimeoutError, OSError):
|
|
return False
|
|
|
|
def _tcp_ping(self, host, port, timeout=1):
|
|
try:
|
|
with socket.create_connection((host, port), timeout=timeout):
|
|
return True
|
|
except OSError:
|
|
return False |