import psutil
import platform
import requests
import socket
from datetime import datetime

SERVER_URL = "http://localhost:5000/collect"

def collect_metrics():
    data = {
        "hostname": socket.gethostname(),
        "os": platform.system(),
        "cpu_percent": psutil.cpu_percent(interval=1),
        "memory_percent": psutil.virtual_memory().percent,
        "disk_usage": psutil.disk_usage('/').percent,
        "time": datetime.now().isoformat()
    }
    return data

def send_data(data):
    try:
        response = requests.post(SERVER_URL, json=data, timeout=5)
        if response.status_code == 200:
            print("Data sent successfully")
        else:
            print("Server returned:", response.status_code)
    except Exception as e:
        print("Error sending data:", e)

if __name__ == "__main__":
    metrics = collect_metrics()
    send_data(metrics)