diff --git a/oci-arm-monitor.py b/oci-arm-monitor.py index bddf8ec..f7c4594 100644 --- a/oci-arm-monitor.py +++ b/oci-arm-monitor.py @@ -19,6 +19,7 @@ SHAPE = "VM.Standard.A1.Flex" OCPUS = 4 MEMORY_GB = 24 INTERVAL = 60 +WEEKLY_REPORT_INTERVAL = 7 * 24 * 3600 EMAIL_TO = os.environ["EMAIL_TO"] EMAIL_FROM = os.environ["EMAIL_FROM"] @@ -37,6 +38,9 @@ config = oci.config.from_file() compute = oci.core.ComputeClient(config) print("Starting ARM instance monitor...", flush=True) +start_time = time.time() +last_report = start_time +attempts = 0 while True: try: resp = compute.launch_instance( @@ -58,9 +62,23 @@ while True: sys.exit(0) except oci.exceptions.ServiceError as e: if "capacity" in str(e.message).lower() or "host" in str(e.message).lower(): + attempts += 1 print(f"{time.strftime('%Y-%m-%d %H:%M:%S', time.gmtime(time.time() + 8*3600))} No capacity. Retrying in {INTERVAL}s...", flush=True) else: print(f"Unexpected error: {e.message}", flush=True) send_email("OCI ARM Monitor Error", str(e.message)) sys.exit(1) + now = time.time() + if now - last_report >= WEEKLY_REPORT_INTERVAL: + uptime_days = int((now - start_time) / 86400) + send_email( + "OCI ARM Monitor - Weekly Report", + f"Status: Still running, no capacity available.\n" + f"Uptime: {uptime_days} days\n" + f"Attempts since last report: {attempts}\n" + f"Shape: {SHAPE} ({OCPUS} OCPUs, {MEMORY_GB}GB RAM)", + ) + print(f"{time.strftime('%Y-%m-%d %H:%M:%S', time.gmtime(now + 8*3600))} Weekly report sent.", flush=True) + last_report = now + attempts = 0 time.sleep(INTERVAL)