diff --git a/checks.yaml b/checks.yaml index cb26892..e8fac34 100644 --- a/checks.yaml +++ b/checks.yaml @@ -40,7 +40,7 @@ - name: Dummy Postgres Database type: port - host: ec2-54-173-89-248.compute-1.amazonaws.com + host: ec2-54-173-89-228.compute-1.amazonaws.net port: 5432 - name: Dummy MySQL Database diff --git a/tinystatus.py b/tinystatus.py index 327ff76..95ef784 100644 --- a/tinystatus.py +++ b/tinystatus.py @@ -13,9 +13,11 @@ import logging import platform + # Load environment variables load_dotenv() + # Configuration MONITOR_CONTINOUSLY = os.getenv('MONITOR_CONTINOUSLY', 'True') == 'True' CHECK_INTERVAL = int(os.getenv('CHECK_INTERVAL', 30)) @@ -26,11 +28,13 @@ TEMPLATE_FILE = os.getenv('TEMPLATE_FILE', 'index.html.theme') HISTORY_TEMPLATE_FILE = os.getenv('HISTORY_TEMPLATE_FILE', 'history.html.theme') STATUS_HISTORY_FILE = os.getenv('STATUS_HISTORY_FILE', 'history.json') -HTML_OUTPUT_DIRECTORY = os.getenv('HTML_OUTPUT_DIRECTORY', os.getcwd()) +HTML_OUTPUT_DIRECTORY = os.getenv('HTML_OUTPUT_DIRECTORY',f"{os.getcwd()}/web") + # Platform Idendifier PLATFORM = platform.system().lower() + # Service check functions async def check_http(url, expected_code, selfsigned): async with aiohttp.ClientSession() as session: @@ -97,7 +101,10 @@ async def run_checks(checks): def load_history(): if os.path.exists(STATUS_HISTORY_FILE): with open(STATUS_HISTORY_FILE, 'r') as f: - return json.load(f) + try: + return json.load(f) + except json.JSONDecodeError: + return {} return {} @@ -119,10 +126,38 @@ def update_history(results): save_history(history) +async def download_file(url, filename): + async with aiohttp.ClientSession() as session: + print(f"Starting download file from {url}") + async with session.get(url) as response: + assert response.status == 200 + with open(filename, "wb") as f: + while True: + chunk = await response.content.readany() + if not chunk: + break + f.write(chunk) + print(f"Downloaded {filename} from {url}") + + +async def get_missing_files(): + # Downloads (latest version of) missing files + if not os.path.exists(CHECKS_FILE): + await download_file("https://raw.githubusercontent.com/harsxv/tinystatus/refs/heads/master/checks.yaml", CHECKS_FILE) + if not os.path.exists(INCIDENTS_FILE): + await download_file("https://raw.githubusercontent.com/harsxv/tinystatus/refs/heads/master/incidents.md", INCIDENTS_FILE) + if not os.path.exists(TEMPLATE_FILE): + await download_file("https://raw.githubusercontent.com/harsxv/tinystatus/refs/heads/master/index.html.theme", TEMPLATE_FILE) + if not os.path.exists(HISTORY_TEMPLATE_FILE): + await download_file("https://raw.githubusercontent.com/harsxv/tinystatus/refs/heads/master/history.html.theme", HISTORY_TEMPLATE_FILE) + + # Main monitoring loop async def monitor_services(): os.makedirs(HTML_OUTPUT_DIRECTORY, exist_ok=True) + await get_missing_files() + while True: start_ts = time.monotonic() down_services = [] @@ -190,6 +225,7 @@ def main(): with open(os.path.join(HTML_OUTPUT_DIRECTORY, 'index.html'), 'w') as f: f.write(html) + if __name__ == "__main__": logging.basicConfig(level=getattr(logging, LOG_LEVEL), format='%(asctime)s - %(levelname)s - %(message)s') asyncio.run(monitor_services())