98 líneas
2.4 KiB
Python
98 líneas
2.4 KiB
Python
#!/usr/bin/env python3
|
|
"""
|
|
Example: Using MCP ProcFS Server HTTP API from Python
|
|
"""
|
|
|
|
import requests
|
|
import json
|
|
from typing import Dict, Any
|
|
|
|
BASE_URL = "http://localhost:3000"
|
|
|
|
def pretty_print(data: Any) -> None:
|
|
"""Pretty print JSON data"""
|
|
print(json.dumps(data, indent=2))
|
|
|
|
def get_cpu_info() -> Dict:
|
|
"""Get CPU information"""
|
|
response = requests.get(f"{BASE_URL}/api/cpu")
|
|
return response.json()
|
|
|
|
def get_memory_info() -> Dict:
|
|
"""Get memory information"""
|
|
response = requests.get(f"{BASE_URL}/api/memory")
|
|
return response.json()
|
|
|
|
def get_load_average() -> Dict:
|
|
"""Get system load average"""
|
|
response = requests.get(f"{BASE_URL}/api/load")
|
|
return response.json()
|
|
|
|
def get_network_stats(interface: str = None) -> Dict:
|
|
"""Get network statistics"""
|
|
params = {"interface": interface} if interface else {}
|
|
response = requests.get(f"{BASE_URL}/api/network", params=params)
|
|
return response.json()
|
|
|
|
def get_process_info(pid: int) -> Dict:
|
|
"""Get process information"""
|
|
response = requests.get(f"{BASE_URL}/api/processes/{pid}")
|
|
return response.json()
|
|
|
|
def read_sysctl(key: str) -> Dict:
|
|
"""Read sysctl parameter"""
|
|
response = requests.get(f"{BASE_URL}/api/sysctl/{key}")
|
|
return response.json()
|
|
|
|
def write_sysctl(key: str, value) -> Dict:
|
|
"""Write sysctl parameter"""
|
|
response = requests.post(
|
|
f"{BASE_URL}/api/sysctl",
|
|
json={"key": key, "value": value}
|
|
)
|
|
return response.json()
|
|
|
|
def main():
|
|
print("=== MCP ProcFS Server Python Client Example ===\n")
|
|
|
|
# Get CPU info
|
|
print("CPU Information:")
|
|
cpu_info = get_cpu_info()
|
|
pretty_print(cpu_info)
|
|
print()
|
|
|
|
# Get memory info
|
|
print("Memory Information:")
|
|
mem_info = get_memory_info()
|
|
pretty_print(mem_info)
|
|
print()
|
|
|
|
# Get load average
|
|
print("Load Average:")
|
|
load_avg = get_load_average()
|
|
pretty_print(load_avg)
|
|
print()
|
|
|
|
# Get network stats for eth0
|
|
print("Network Statistics:")
|
|
net_stats = get_network_stats()
|
|
pretty_print(net_stats)
|
|
print()
|
|
|
|
# Get process info for PID 1
|
|
print("Process Info (PID 1):")
|
|
proc_info = get_process_info(1)
|
|
pretty_print(proc_info)
|
|
print()
|
|
|
|
# Read sysctl parameter
|
|
print("Sysctl Parameter (kernel.hostname):")
|
|
sysctl_info = read_sysctl("kernel.hostname")
|
|
pretty_print(sysctl_info)
|
|
print()
|
|
|
|
print("=== Example Complete ===")
|
|
|
|
if __name__ == "__main__":
|
|
main()
|