#!/usr/bin/env node /** * Example: Monitoring system metrics in real-time using SSE */ const EventSource = require('eventsource'); const SSE_URL = 'http://localhost:3000/mcp/sse'; const API_URL = 'http://localhost:3000'; // Connect to SSE endpoint const eventSource = new EventSource(SSE_URL); eventSource.onopen = () => { console.log('Connected to MCP ProcFS Server\n'); startMonitoring(); }; eventSource.addEventListener('connected', (event) => { const data = JSON.parse(event.data); console.log('Connection confirmed:', data.clientId); }); eventSource.onerror = (error) => { console.error('SSE Error:', error); }; async function fetchMetric(endpoint) { try { const response = await fetch(`${API_URL}${endpoint}`); const data = await response.json(); return data.data; } catch (error) { console.error(`Error fetching ${endpoint}:`, error.message); return null; } } async function displayMetrics() { console.clear(); console.log('=== System Monitoring Dashboard ===\n'); // CPU Info const cpu = await fetchMetric('/api/cpu'); if (cpu) { console.log('CPU:'); console.log(` Model: ${cpu.model}`); console.log(` Cores: ${cpu.cores}`); console.log(` Speed: ${cpu.mhz.toFixed(2)} MHz`); } console.log(); // Memory Info const mem = await fetchMetric('/api/memory'); if (mem) { const usedPercent = ((mem.total - mem.available) / mem.total * 100).toFixed(1); console.log('Memory:'); console.log(` Total: ${(mem.total / 1024 / 1024).toFixed(2)} GB`); console.log(` Available: ${(mem.available / 1024 / 1024).toFixed(2)} GB`); console.log(` Used: ${usedPercent}%`); } console.log(); // Load Average const load = await fetchMetric('/api/load'); if (load) { console.log('Load Average:'); console.log(` 1min: ${load.one.toFixed(2)}`); console.log(` 5min: ${load.five.toFixed(2)}`); console.log(` 15min: ${load.fifteen.toFixed(2)}`); console.log(` Processes: ${load.runningProcesses}/${load.totalProcesses}`); } console.log(); // Network Stats const net = await fetchMetric('/api/network'); if (net && net.length > 0) { console.log('Network Interfaces:'); net.slice(0, 3).forEach(iface => { console.log(` ${iface.interface}:`); console.log(` RX: ${(iface.rxBytes / 1024 / 1024).toFixed(2)} MB | TX: ${(iface.txBytes / 1024 / 1024).toFixed(2)} MB`); }); } console.log(); console.log('Press Ctrl+C to exit'); console.log(`Last update: ${new Date().toLocaleTimeString()}`); } function startMonitoring() { // Initial display displayMetrics(); // Update every 2 seconds setInterval(displayMetrics, 2000); } // Handle cleanup process.on('SIGINT', () => { console.log('\nClosing connection...'); eventSource.close(); process.exit(0); });