#!/usr/bin/env python3
"""Auto-generated by AGI Loop cycle #1077
Task: Write a Python function that checks disk usage of /Eden with os.statvfs and writes a warning to /Eden/DATA/alerts.json if usage exceeds 80 percent
Generated: 2026-02-12T19:24:11.404826
"""

import os
import json

def check_disk_usage():
    path = "/Eden"
    try:
        statvfs = os.statvfs(path)
        block_size = statvfs.f_frsize
        total_blocks = statvfs.f_blocks
        free_blocks = statvfs.f_bfree

        total_space = block_size * total_blocks
        free_space = block_size * free_blocks
        used_space = total_space - free_space
        usage_percent = (used_space / total_space) * 100

        print(f"Disk usage of {path}: {usage_percent:.2f}%")

        if usage_percent > 80:
            alert_file = "/Eden/DATA/alerts.json"
            alert_data = {
                "alert": "Disk usage exceeds 80%",
                "path": path,
                "usage_percent": round(usage_percent, 2)
            }

            try:
                with open(alert_file, 'w') as f:
                    json.dump(alert_data, f, indent=4)
                print(f"Warning written to {alert_file}")
            except Exception as e:
                print(f"Failed to write alert: {e}")
    except Exception as e:
        print(f"Error checking disk usage: {e}")

if __name__ == '__main__':
    check_disk_usage()