90 líneas
2.8 KiB
TypeScript
90 líneas
2.8 KiB
TypeScript
/**
|
|
* Example: Using MCP ProcFS Server as a library
|
|
*/
|
|
|
|
import { ProcFSReader, ProcFSWriter } from '../src/index';
|
|
|
|
async function main() {
|
|
const reader = new ProcFSReader();
|
|
const writer = new ProcFSWriter();
|
|
|
|
console.log('=== System Information ===\n');
|
|
|
|
// Get CPU information
|
|
const cpuInfo = await reader.getCPUInfo();
|
|
console.log('CPU Information:');
|
|
console.log(` Model: ${cpuInfo.model}`);
|
|
console.log(` Cores: ${cpuInfo.cores}`);
|
|
console.log(` Processors: ${cpuInfo.processors}`);
|
|
console.log(` MHz: ${cpuInfo.mhz}`);
|
|
console.log();
|
|
|
|
// Get memory information
|
|
const memInfo = await reader.getMemInfo();
|
|
console.log('Memory Information:');
|
|
console.log(` Total: ${(memInfo.total / 1024).toFixed(2)} GB`);
|
|
console.log(` Free: ${(memInfo.free / 1024).toFixed(2)} GB`);
|
|
console.log(` Available: ${(memInfo.available / 1024).toFixed(2)} GB`);
|
|
console.log(` Cached: ${(memInfo.cached / 1024).toFixed(2)} GB`);
|
|
console.log();
|
|
|
|
// Get load average
|
|
const loadAvg = await reader.getLoadAvg();
|
|
console.log('Load Average:');
|
|
console.log(` 1 min: ${loadAvg.one}`);
|
|
console.log(` 5 min: ${loadAvg.five}`);
|
|
console.log(` 15 min: ${loadAvg.fifteen}`);
|
|
console.log(` Running/Total: ${loadAvg.runningProcesses}/${loadAvg.totalProcesses}`);
|
|
console.log();
|
|
|
|
// Get network statistics
|
|
const netStats = await reader.getNetDevStats();
|
|
console.log('Network Interfaces:');
|
|
for (const iface of netStats) {
|
|
console.log(` ${iface.interface}:`);
|
|
console.log(` RX: ${(iface.rxBytes / 1024 / 1024).toFixed(2)} MB`);
|
|
console.log(` TX: ${(iface.txBytes / 1024 / 1024).toFixed(2)} MB`);
|
|
}
|
|
console.log();
|
|
|
|
// Get disk statistics
|
|
const diskStats = await reader.getDiskStats();
|
|
console.log('Disk Devices:');
|
|
for (const disk of diskStats.slice(0, 5)) {
|
|
if (disk.readsCompleted > 0 || disk.writesCompleted > 0) {
|
|
console.log(` ${disk.device}:`);
|
|
console.log(` Reads: ${disk.readsCompleted}`);
|
|
console.log(` Writes: ${disk.writesCompleted}`);
|
|
}
|
|
}
|
|
console.log();
|
|
|
|
// List some processes
|
|
const pids = await reader.listPIDs();
|
|
console.log(`Total Processes: ${pids.length}`);
|
|
console.log('\nFirst 5 processes:');
|
|
for (const pid of pids.slice(0, 5)) {
|
|
try {
|
|
const procInfo = await reader.getProcessInfo(pid);
|
|
console.log(` PID ${procInfo.pid}: ${procInfo.name} (${procInfo.state})`);
|
|
} catch (error) {
|
|
// Process may have terminated
|
|
}
|
|
}
|
|
console.log();
|
|
|
|
// Read a sysctl parameter (example)
|
|
try {
|
|
const param = await writer.readSysctl('kernel.hostname');
|
|
console.log('Sysctl Parameter:');
|
|
console.log(` kernel.hostname = ${param.value}`);
|
|
console.log();
|
|
} catch (error) {
|
|
console.log('Could not read sysctl parameter');
|
|
}
|
|
|
|
console.log('=== Example Complete ===');
|
|
}
|
|
|
|
main().catch(console.error);
|