mirror of
https://github.com/StefBuwalda/dashboard_test.git
synced 2025-11-02 04:39:58 +00:00
Creating my own system because down with AI
This commit is contained in:
21
test.py
21
test.py
@@ -7,41 +7,49 @@ from flask import Flask, jsonify
|
|||||||
from concurrent.futures import ThreadPoolExecutor, as_completed
|
from concurrent.futures import ThreadPoolExecutor, as_completed
|
||||||
from typing import Any
|
from typing import Any
|
||||||
|
|
||||||
|
# Start background checker
|
||||||
|
import threading
|
||||||
|
|
||||||
# Disable warnings for self-signed certificates
|
# Disable warnings for self-signed certificates
|
||||||
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
|
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
|
||||||
|
|
||||||
services = [
|
services = [
|
||||||
"https://truenas.local/",
|
"https://truenas.local/",
|
||||||
"https://example.com/",
|
"https://example.com/",
|
||||||
"https://git.ihatemen.uk/"
|
"https://git.ihatemen.uk/",
|
||||||
]
|
]
|
||||||
|
|
||||||
services_status = {}
|
services_status = {}
|
||||||
|
|
||||||
|
|
||||||
# Determine SSL verification based on hostname
|
# Determine SSL verification based on hostname
|
||||||
def should_verify_ssl(url: str):
|
def should_verify_ssl(url: str):
|
||||||
hostname = urlparse(url).hostname
|
hostname = urlparse(url).hostname
|
||||||
return not (hostname and hostname.endswith(".local"))
|
return not (hostname and hostname.endswith(".local"))
|
||||||
|
|
||||||
|
|
||||||
# Function to check a single service
|
# Function to check a single service
|
||||||
def check_service(url: str) -> tuple[str, dict[str, Any]]:
|
def check_service(url: str) -> tuple[str, dict[str, Any]]:
|
||||||
try:
|
try:
|
||||||
start = time.time()
|
start = time.time()
|
||||||
r = requests.head(url, allow_redirects=True, timeout=5, verify=should_verify_ssl(url))
|
r = requests.head(
|
||||||
|
url, allow_redirects=True, timeout=5, verify=should_verify_ssl(url)
|
||||||
|
)
|
||||||
latency = int((time.time() - start) * 1000)
|
latency = int((time.time() - start) * 1000)
|
||||||
return url, {
|
return url, {
|
||||||
"status": "up" if r.ok else "down",
|
"status": "up" if r.ok else "down",
|
||||||
"latency": latency,
|
"latency": latency,
|
||||||
"error": None
|
"error": None,
|
||||||
}
|
}
|
||||||
except requests.exceptions.RequestException as e:
|
except requests.exceptions.RequestException as e:
|
||||||
return url, {
|
return url, {
|
||||||
"status": "down",
|
"status": "down",
|
||||||
"latency": None,
|
"latency": None,
|
||||||
"error": str(e),
|
"error": str(e),
|
||||||
"trace": traceback.format_exc()
|
"trace": traceback.format_exc(),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
# Background thread that checks all services in parallel
|
# Background thread that checks all services in parallel
|
||||||
def check_services_periodically(interval: int = 5):
|
def check_services_periodically(interval: int = 5):
|
||||||
while True:
|
while True:
|
||||||
@@ -52,16 +60,17 @@ def check_services_periodically(interval: int=5):
|
|||||||
services_status[url] = result
|
services_status[url] = result
|
||||||
time.sleep(interval)
|
time.sleep(interval)
|
||||||
|
|
||||||
# Start background checker
|
|
||||||
import threading
|
|
||||||
threading.Thread(target=check_services_periodically, daemon=True).start()
|
threading.Thread(target=check_services_periodically, daemon=True).start()
|
||||||
|
|
||||||
# Flask app to serve status
|
# Flask app to serve status
|
||||||
app = Flask(__name__)
|
app = Flask(__name__)
|
||||||
|
|
||||||
|
|
||||||
@app.route("/status")
|
@app.route("/status")
|
||||||
def status():
|
def status():
|
||||||
return jsonify(services_status)
|
return jsonify(services_status)
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
app.run(debug=True)
|
app.run(debug=True)
|
||||||
|
|||||||
48
test2.py
Normal file
48
test2.py
Normal file
@@ -0,0 +1,48 @@
|
|||||||
|
# import requests as r
|
||||||
|
from flask import jsonify, Flask
|
||||||
|
from typing import Any, Optional
|
||||||
|
|
||||||
|
|
||||||
|
class service:
|
||||||
|
url: str
|
||||||
|
status: Optional[int]
|
||||||
|
online: bool
|
||||||
|
private: bool
|
||||||
|
|
||||||
|
def __init__(self, url: str = "", private: bool = False):
|
||||||
|
self.url = url
|
||||||
|
self.private = private
|
||||||
|
|
||||||
|
self.online = False
|
||||||
|
self.status = None
|
||||||
|
|
||||||
|
def to_dict(self) -> dict[str, Any]:
|
||||||
|
return {
|
||||||
|
"url": self.url,
|
||||||
|
"status": self.status,
|
||||||
|
"private": self.private,
|
||||||
|
"online": self.online,
|
||||||
|
}
|
||||||
|
|
||||||
|
def set_status(self, status: int):
|
||||||
|
self.status = status
|
||||||
|
|
||||||
|
|
||||||
|
services: list[service] = [
|
||||||
|
service("https://git.ihatemen.uk"),
|
||||||
|
service("https://plex.ihatemen.uk"),
|
||||||
|
service("https://truenas.local", True),
|
||||||
|
]
|
||||||
|
|
||||||
|
# Flask app to serve status
|
||||||
|
app = Flask(__name__)
|
||||||
|
|
||||||
|
|
||||||
|
@app.route("/")
|
||||||
|
def status():
|
||||||
|
return jsonify([s.to_dict() for s in services])
|
||||||
|
|
||||||
|
|
||||||
|
# Only run if directly running file
|
||||||
|
if __name__ == "__main__":
|
||||||
|
app.run(debug=True)
|
||||||
Reference in New Issue
Block a user