A comprehensive server monitoring package for Node.js applications with SQLite storage support.
This package helps monitor the health of your server running alongside your Node.js application. The main KPIs the package keeps track of are:
- MEMORY USAGE - Total, used, free memory and percentage
- TOP 20 MEMORY USAGE PROCESSES - List of processes consuming most memory
- CPU USAGE - CPU percentage and load average
- TOP 20 CPU USAGE PROCESSES - List of processes consuming most CPU
- FREE DISK SPACE - Total, used, free disk space and percentage
- TOP 20 FOLDERS DISK USAGE - List of folders consuming most disk space
- MONITOR PM2 LOGS - PM2 process monitoring and log access
npm install @igortrindade/server-monitorimport { ServerMonitor } from '@igortrindade/server-monitor';
const monitor = new ServerMonitor({
interval: 60000, // Collect metrics every 60 seconds
dbPath: './server-metrics.db', // SQLite database path
enableMemoryMonitoring: true,
enableCpuMonitoring: true,
enableDiskMonitoring: true,
enablePM2Monitoring: true
});
// Event listeners
monitor.on('memoryMetrics', (metrics) => {
console.log(`Memory usage: ${metrics.percentage.toFixed(2)}%`);
});
monitor.on('error', (error) => {
console.error('Monitor error:', error);
});
// Start monitoring
await monitor.initialize();
await monitor.start();
// Get current metrics
const currentMemory = await monitor.getMemoryUsage();
const currentCpu = await monitor.getCpuUsage();
const currentDisk = await monitor.getDiskUsage();
// Get historical data
const memoryHistory = await monitor.getHistoricalData('memory', 100);
// Stop monitoring
await monitor.stop();interface MonitorConfig {
interval: number; // Monitoring interval in milliseconds (default: 60000)
dbPath?: string; // SQLite database path (default: './server-monitor.db')
enableMemoryMonitoring: boolean; // Enable memory monitoring (default: true)
enableCpuMonitoring: boolean; // Enable CPU monitoring (default: true)
enableDiskMonitoring: boolean; // Enable disk monitoring (default: true)
enablePM2Monitoring: boolean; // Enable PM2 monitoring (default: true)
maxRecords?: number; // Maximum records to keep (default: 10000)
diskPaths?: string[]; // Disk paths to monitor (default: ['/'])
}new ServerMonitor(config?: Partial<MonitorConfig>)initialize(): Promise<void>- Initialize the monitor and databasestart(): Promise<void>- Start monitoringstop(): Promise<void>- Stop monitoringgetMemoryUsage(): Promise<MemoryUsage>- Get current memory usagegetCpuUsage(): Promise<CpuUsage>- Get current CPU usagegetDiskUsage(): Promise<DiskUsage>- Get current disk usagegetPM2Processes(): Promise<PM2ProcessInfo[]>- Get PM2 processesgetPM2Logs(appName?: string, lines?: number): Promise<string>- Get PM2 logsgetHistoricalData(type, limit?, startDate?, endDate?)- Get historical metricsgetLatestMetrics()- Get latest metrics from databaseupdateConfig(newConfig)- Update configurationisMonitoringActive(): boolean- Check if monitoring is active
initialized- Emitted when monitor is initializedstarted- Emitted when monitoring startsstopped- Emitted when monitoring stopsmemoryMetrics- Emitted when memory metrics are collectedcpuMetrics- Emitted when CPU metrics are collecteddiskMetrics- Emitted when disk metrics are collectedpm2Metrics- Emitted when PM2 metrics are collectedmetricsCollected- Emitted when all metrics are collectederror- Emitted when an error occurs
interface MemoryUsage {
total: number; // Total memory in bytes
used: number; // Used memory in bytes
free: number; // Free memory in bytes
percentage: number; // Usage percentage
topProcesses: ProcessInfo[];
}interface CpuUsage {
percentage: number; // CPU usage percentage
loadAverage: number[]; // Load average [1min, 5min, 15min]
topProcesses: ProcessInfo[];
}interface DiskUsage {
total: number; // Total disk space in bytes
used: number; // Used disk space in bytes
free: number; // Free disk space in bytes
percentage: number; // Usage percentage
topFolders: FolderInfo[];
}interface ProcessInfo {
pid: number; // Process ID
name: string; // Process name
memoryUsage: number; // Memory usage (MB or %)
cpuUsage: number; // CPU usage percentage
command: string; // Full command
}All metrics are automatically stored in an SQLite database. The database schema includes:
id- Auto-incrementing primary keytimestamp- ISO string timestamptype- Metric type ('memory', 'cpu', 'disk', 'pm2')data- JSON stringified metric data
This package supports:
- macOS - Full support for all monitoring features
- Linux - Full support for all monitoring features
- Windows - Partial support (some commands may differ)
- Node.js 14.0.0 or higher
- SQLite3
- PM2 (optional, for PM2 monitoring)
This package is written in TypeScript and includes full type definitions. No additional @types packages are needed.
Contributions are welcome! Please feel free to submit issues and pull requests.
MIT