104 lines
4.6 KiB
Python
104 lines
4.6 KiB
Python
# backend/script_groups/example_group/x2.py
|
|
from backend.script_groups.base_script import BaseScript
|
|
import psutil
|
|
import json
|
|
from datetime import datetime
|
|
from pathlib import Path
|
|
|
|
class SystemInfo(BaseScript):
|
|
"""
|
|
System Monitor
|
|
Collects and analyzes system performance metrics
|
|
"""
|
|
|
|
def run(self, work_dir: str, profile: dict) -> dict:
|
|
try:
|
|
# Get configuration from the same config.json
|
|
config = self.get_config(work_dir, "example_group")
|
|
save_report = config.get("save_report", True)
|
|
report_format = config.get("report_format", "json")
|
|
|
|
# Collect system information
|
|
cpu_freq = psutil.cpu_freq()
|
|
memory = psutil.virtual_memory()
|
|
disk = psutil.disk_usage(work_dir)
|
|
|
|
info = {
|
|
"timestamp": datetime.now().isoformat(),
|
|
"cpu": {
|
|
"cores": psutil.cpu_count(),
|
|
"physical_cores": psutil.cpu_count(logical=False),
|
|
"frequency": {
|
|
"current": round(cpu_freq.current, 2) if cpu_freq else None,
|
|
"min": round(cpu_freq.min, 2) if cpu_freq else None,
|
|
"max": round(cpu_freq.max, 2) if cpu_freq else None
|
|
},
|
|
"usage_percent": psutil.cpu_percent(interval=1)
|
|
},
|
|
"memory": {
|
|
"total": memory.total,
|
|
"available": memory.available,
|
|
"used": memory.used,
|
|
"percent": memory.percent
|
|
},
|
|
"disk": {
|
|
"total": disk.total,
|
|
"used": disk.used,
|
|
"free": disk.free,
|
|
"percent": disk.percent
|
|
},
|
|
"network": {
|
|
"interfaces": list(psutil.net_if_addrs().keys()),
|
|
"connections": len(psutil.net_connections())
|
|
}
|
|
}
|
|
|
|
# Save report if configured
|
|
if save_report:
|
|
report_path = Path(work_dir) / f"system_info.{report_format}"
|
|
if report_format == "json":
|
|
with open(report_path, 'w') as f:
|
|
json.dump(info, f, indent=2)
|
|
elif report_format == "csv":
|
|
with open(report_path, 'w', newline='') as f:
|
|
writer = csv.writer(f)
|
|
writer.writerow(["Metric", "Value"])
|
|
writer.writerow(["CPU Cores", info["cpu"]["cores"]])
|
|
writer.writerow(["CPU Usage", f"{info['cpu']['usage_percent']}%"])
|
|
writer.writerow(["Memory Total", f"{info['memory']['total']:,} bytes"])
|
|
writer.writerow(["Memory Used", f"{info['memory']['percent']}%"])
|
|
writer.writerow(["Disk Total", f"{info['disk']['total']:,} bytes"])
|
|
writer.writerow(["Disk Used", f"{info['disk']['percent']}%"])
|
|
else: # txt
|
|
with open(report_path, 'w') as f:
|
|
f.write(f"System Information Report\n")
|
|
f.write(f"Generated: {info['timestamp']}\n\n")
|
|
f.write(f"CPU:\n")
|
|
f.write(f" Cores: {info['cpu']['cores']}\n")
|
|
f.write(f" Usage: {info['cpu']['usage_percent']}%\n\n")
|
|
f.write(f"Memory:\n")
|
|
f.write(f" Total: {info['memory']['total']:,} bytes\n")
|
|
f.write(f" Used: {info['memory']['percent']}%\n\n")
|
|
f.write(f"Disk:\n")
|
|
f.write(f" Total: {info['disk']['total']:,} bytes\n")
|
|
f.write(f" Used: {info['disk']['percent']}%\n")
|
|
|
|
# Format output
|
|
output = f"""System Information:
|
|
CPU: {info['cpu']['cores']} cores ({info['cpu']['usage_percent']}% usage)
|
|
Memory: {info['memory']['percent']}% used ({info['memory']['available']:,} bytes available)
|
|
Disk: {info['disk']['percent']}% used ({info['disk']['free']:,} bytes free)
|
|
Network Interfaces: {', '.join(info['network']['interfaces'])}
|
|
Active Connections: {info['network']['connections']}"""
|
|
|
|
return {
|
|
"status": "success",
|
|
"data": info,
|
|
"output": output
|
|
}
|
|
|
|
except Exception as e:
|
|
return {
|
|
"status": "error",
|
|
"error": str(e)
|
|
} |