Add system monitoring with real-time metrics to admin dashboard
All checks were successful
Build And Push Image / docker (push) Successful in 2m54s

- Add systeminformation package for system metrics collection
- Create system-metrics utility for CPU, memory, disk monitoring
- Update admin stats API to return real system health data
- Replace mock data with live system metrics in admin dashboard
- Update @vite-pwa/nuxt to v0.10.8
This commit is contained in:
2025-08-07 15:51:16 +02:00
parent d0c9c02bf9
commit ec6958375c
5 changed files with 270 additions and 16 deletions

View File

@@ -17,15 +17,38 @@ export default defineEventHandler(async (event) => {
console.log('✅ Admin access verified for user:', session.user.email);
// For now, return improved mock data - TODO: integrate with real data sources
// Get real system metrics
const { getSystemMetrics, getSystemHealthStatus, formatBytes } = await import('~/server/utils/system-metrics');
const systemMetrics = await getSystemMetrics();
const systemHealth = getSystemHealthStatus(systemMetrics);
// Build comprehensive stats with real data
const stats = {
// Real system metrics
totalUsers: 156, // TODO: Get from Keycloak API
activeUsers: 45, // TODO: Get from session store
activeUsers: 45, // TODO: Get from session store
totalSessions: 67, // TODO: Get from session store
systemHealth: 'healthy',
systemHealth,
lastBackup: new Date().toISOString(),
diskUsage: '45%',
memoryUsage: '62%',
diskUsage: `${systemMetrics.disk.usagePercent}%`,
memoryUsage: `${systemMetrics.memory.usagePercent}%`,
// Enhanced system metrics with real data
systemMetrics: {
cpu: systemMetrics.cpu.usage,
memory: systemMetrics.memory.usagePercent,
disk: systemMetrics.disk.usagePercent,
uptime: systemMetrics.uptime,
processes: systemMetrics.processes.total,
cpuCores: systemMetrics.cpu.cores,
cpuModel: systemMetrics.cpu.model,
memoryTotal: formatBytes(systemMetrics.memory.total),
memoryUsed: formatBytes(systemMetrics.memory.used),
diskTotal: formatBytes(systemMetrics.disk.total),
diskUsed: formatBytes(systemMetrics.disk.used)
},
// Mock data for features not yet implemented
recentActivity: [
{
action: 'User login',
@@ -46,12 +69,7 @@ export default defineEventHandler(async (event) => {
type: 'success'
}
],
systemMetrics: {
cpu: 45,
memory: 62,
disk: 38,
uptime: '5d 12h 30m'
},
securityAlerts: [
{
id: 1,
@@ -70,7 +88,7 @@ export default defineEventHandler(async (event) => {
]
};
console.log('✅ Admin stats retrieved successfully');
console.log('✅ Admin stats retrieved successfully with real system metrics');
return stats;
} catch (error: any) {

View File

@@ -0,0 +1,173 @@
import si from 'systeminformation';
interface SystemMetrics {
cpu: {
usage: number;
cores: number;
model: string;
};
memory: {
total: number;
used: number;
free: number;
usagePercent: number;
};
disk: {
total: number;
used: number;
free: number;
usagePercent: number;
};
uptime: string;
processes: {
total: number;
running: number;
sleeping: number;
};
}
// Cache system metrics to avoid excessive system calls
let metricsCache: SystemMetrics | null = null;
let lastCacheTime = 0;
const CACHE_DURATION = 30000; // 30 seconds
export async function getSystemMetrics(): Promise<SystemMetrics> {
const now = Date.now();
// Return cached data if still valid
if (metricsCache && (now - lastCacheTime) < CACHE_DURATION) {
return metricsCache;
}
try {
console.log('🔄 Fetching fresh system metrics...');
// Get all metrics in parallel for better performance
const [cpuData, memData, fsData, currentLoad, processData, uptimeData] = await Promise.all([
si.cpu(),
si.mem(),
si.fsSize(),
si.currentLoad(),
si.processes(),
si.time()
]);
// Calculate disk totals across all mounted filesystems
const diskTotals = fsData.reduce((acc, fs) => {
// Skip special filesystems and focus on main storage
if (fs.type && !['tmpfs', 'devtmpfs', 'proc', 'sysfs'].includes(fs.type.toLowerCase())) {
acc.total += fs.size || 0;
acc.used += fs.used || 0;
}
return acc;
}, { total: 0, used: 0 });
const diskFree = diskTotals.total - diskTotals.used;
const diskUsagePercent = diskTotals.total > 0 ? Math.round((diskTotals.used / diskTotals.total) * 100) : 0;
// Format uptime
const formatUptime = (seconds: number): string => {
const days = Math.floor(seconds / 86400);
const hours = Math.floor((seconds % 86400) / 3600);
const minutes = Math.floor((seconds % 3600) / 60);
if (days > 0) {
return `${days}d ${hours}h ${minutes}m`;
} else if (hours > 0) {
return `${hours}h ${minutes}m`;
} else {
return `${minutes}m`;
}
};
// Build metrics object
const metrics: SystemMetrics = {
cpu: {
usage: Math.round(currentLoad.currentLoad || 0),
cores: cpuData.cores || 0,
model: cpuData.brand || 'Unknown'
},
memory: {
total: memData.total || 0,
used: memData.used || 0,
free: memData.free || 0,
usagePercent: memData.total > 0 ? Math.round((memData.used / memData.total) * 100) : 0
},
disk: {
total: diskTotals.total,
used: diskTotals.used,
free: diskFree,
usagePercent: diskUsagePercent
},
uptime: formatUptime(uptimeData.uptime || 0),
processes: {
total: processData.all || 0,
running: processData.running || 0,
sleeping: processData.sleeping || 0
}
};
// Cache the results
metricsCache = metrics;
lastCacheTime = now;
console.log('✅ System metrics fetched successfully:', {
cpu: `${metrics.cpu.usage}%`,
memory: `${metrics.memory.usagePercent}%`,
disk: `${metrics.disk.usagePercent}%`,
uptime: metrics.uptime
});
return metrics;
} catch (error) {
console.error('❌ Failed to fetch system metrics:', error);
// Return fallback metrics on error
const fallbackMetrics: SystemMetrics = {
cpu: { usage: 0, cores: 0, model: 'Unknown' },
memory: { total: 0, used: 0, free: 0, usagePercent: 0 },
disk: { total: 0, used: 0, free: 0, usagePercent: 0 },
uptime: '0m',
processes: { total: 0, running: 0, sleeping: 0 }
};
return fallbackMetrics;
}
}
// Helper function to format bytes to human readable format
export function formatBytes(bytes: number): string {
if (bytes === 0) return '0 B';
const k = 1024;
const sizes = ['B', 'KB', 'MB', 'GB', 'TB'];
const i = Math.floor(Math.log(bytes) / Math.log(k));
return parseFloat((bytes / Math.pow(k, i)).toFixed(1)) + ' ' + sizes[i];
}
// Helper function to get system health status
export function getSystemHealthStatus(metrics: SystemMetrics): 'healthy' | 'degraded' | 'unhealthy' {
const { cpu, memory, disk } = metrics;
// Define thresholds
const criticalThresholds = { cpu: 90, memory: 95, disk: 95 };
const warningThresholds = { cpu: 70, memory: 80, disk: 85 };
// Check for critical issues
if (cpu.usage > criticalThresholds.cpu ||
memory.usagePercent > criticalThresholds.memory ||
disk.usagePercent > criticalThresholds.disk) {
return 'unhealthy';
}
// Check for warning issues
if (cpu.usage > warningThresholds.cpu ||
memory.usagePercent > warningThresholds.memory ||
disk.usagePercent > warningThresholds.disk) {
return 'degraded';
}
return 'healthy';
}