typescriptbeginner
System Information with OS Module
Retrieve system information including CPU, memory, network interfaces, and platform details using os module.
typescriptPress ⌘/Ctrl + Shift + C to copy
import os from 'os';
function getSystemInfo() {
return {
platform: os.platform(),
arch: os.arch(),
hostname: os.hostname(),
release: os.release(),
uptime: `${(os.uptime() / 3600).toFixed(1)} hours`,
nodeVersion: process.version,
memory: {
total: `${(os.totalmem() / 1e9).toFixed(1)} GB`,
free: `${(os.freemem() / 1e9).toFixed(1)} GB`,
usage: `${((1 - os.freemem() / os.totalmem()) * 100).toFixed(1)}%`,
},
cpus: {
model: os.cpus()[0]?.model,
cores: os.cpus().length,
speed: `${os.cpus()[0]?.speed} MHz`,
},
network: Object.entries(os.networkInterfaces())
.flatMap(([name, addrs]) =>
(addrs ?? []).filter(a => !a.internal).map(a => ({
interface: name,
address: a.address,
family: a.family,
}))
),
loadAverage: os.loadavg().map(l => l.toFixed(2)),
tmpdir: os.tmpdir(),
homedir: os.homedir(),
};
}
function printTable(obj: Record<string, any>, indent = 0) {
for (const [key, val] of Object.entries(obj)) {
const pad = ' '.repeat(indent);
if (typeof val === 'object' && !Array.isArray(val)) {
console.log(`${pad}${key}:`);
printTable(val, indent + 2);
} else {
console.log(`${pad}${key}: ${JSON.stringify(val)}`);
}
}
}
printTable(getSystemInfo());Use Cases
- Server health dashboards
- System monitoring scripts
- Environment diagnostics
Tags
Related Snippets
Similar patterns you can reuse in the same workflow.
typescriptbeginner
Health Check Endpoint Pattern
Implement comprehensive health checks with dependency status, uptime, and readiness probes.
Best for: Kubernetes liveness probes
#nodejs#health-check
typescriptadvanced
Application Metrics Collection
Collect and expose application metrics like request counts, latency histograms, and custom gauges.
Best for: Prometheus metrics endpoint
#nodejs#monitoring
typescriptadvanced
Node.js Worker Threads for Parallel Processing
Use Worker Threads to run CPU-intensive tasks in parallel without blocking the event loop.
Best for: CPU-intensive data processing without blocking
#nodejs#worker-threads
typescriptintermediate
Node.js Stream Pipeline with Transform
Build efficient data processing pipelines using Node.js streams for large file handling.
Best for: Processing large CSV files without loading into memory
#nodejs#streams