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

- 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:
Matt 2025-08-07 15:51:16 +02:00
parent d0c9c02bf9
commit ec6958375c
5 changed files with 270 additions and 16 deletions

29
package-lock.json generated
View File

@ -8,7 +8,7 @@
"hasInstallScript": true,
"dependencies": {
"@nuxt/ui": "^3.2.0",
"@vite-pwa/nuxt": "^0.10.6",
"@vite-pwa/nuxt": "^0.10.8",
"cookie": "^0.6.0",
"formidable": "^3.5.4",
"mime-types": "^3.0.1",
@ -16,6 +16,7 @@
"motion-v": "^1.6.1",
"nuxt": "^3.15.4",
"sharp": "^0.34.2",
"systeminformation": "^5.27.7",
"vue": "latest",
"vue-router": "latest",
"vuetify-nuxt-module": "^0.18.3"
@ -15941,6 +15942,32 @@
"url": "https://github.com/sponsors/sindresorhus"
}
},
"node_modules/systeminformation": {
"version": "5.27.7",
"resolved": "https://registry.npmjs.org/systeminformation/-/systeminformation-5.27.7.tgz",
"integrity": "sha512-saaqOoVEEFaux4v0K8Q7caiauRwjXC4XbD2eH60dxHXbpKxQ8kH9Rf7Jh+nryKpOUSEFxtCdBlSUx0/lO6rwRg==",
"license": "MIT",
"os": [
"darwin",
"linux",
"win32",
"freebsd",
"openbsd",
"netbsd",
"sunos",
"android"
],
"bin": {
"systeminformation": "lib/cli.js"
},
"engines": {
"node": ">=8.0.0"
},
"funding": {
"type": "Buy me a coffee",
"url": "https://www.buymeacoffee.com/systeminfo"
}
},
"node_modules/tailwind-merge": {
"version": "3.0.2",
"resolved": "https://registry.npmjs.org/tailwind-merge/-/tailwind-merge-3.0.2.tgz",

View File

@ -11,7 +11,7 @@
},
"dependencies": {
"@nuxt/ui": "^3.2.0",
"@vite-pwa/nuxt": "^0.10.6",
"@vite-pwa/nuxt": "^0.10.8",
"cookie": "^0.6.0",
"formidable": "^3.5.4",
"mime-types": "^3.0.1",
@ -19,6 +19,7 @@
"motion-v": "^1.6.1",
"nuxt": "^3.15.4",
"sharp": "^0.34.2",
"systeminformation": "^5.27.7",
"vue": "latest",
"vue-router": "latest",
"vuetify-nuxt-module": "^0.18.3"

View File

@ -390,19 +390,48 @@ const loadAdminStats = async () => {
totalSessions: number;
diskUsage: string;
memoryUsage: string;
systemMetrics: {
cpu: number;
memory: number;
disk: number;
uptime: string;
processes: number;
cpuCores: number;
cpuModel: string;
memoryTotal: string;
memoryUsed: string;
diskTotal: string;
diskUsed: string;
};
}>('/api/admin/stats');
// Update system stats with real data
systemStats.value = {
totalUsers: stats.totalUsers || 0,
activeUsers: stats.activeUsers || 0,
totalSessions: stats.totalSessions || 0,
diskUsage: stats.diskUsage || '0%',
memoryUsage: stats.memoryUsage || '0%',
uptime: '5d 12h'
uptime: stats.systemMetrics?.uptime || '0m'
};
// Update system health with real metrics
systemHealth.value = {
cpu: stats.systemMetrics?.cpu || 0,
memory: stats.systemMetrics?.memory || 0,
disk: stats.systemMetrics?.disk || 0
};
console.log('✅ Admin stats loaded with real system metrics:', {
cpu: `${systemHealth.value.cpu}%`,
memory: `${systemHealth.value.memory}%`,
disk: `${systemHealth.value.disk}%`,
uptime: systemStats.value.uptime
});
} catch (error) {
console.error('Failed to load admin stats:', error);
// Use mock data on error
// Use fallback data on error
systemStats.value = {
totalUsers: 156,
activeUsers: 45,
@ -411,6 +440,12 @@ const loadAdminStats = async () => {
memoryUsage: '62%',
uptime: '5d 12h'
};
systemHealth.value = {
cpu: 45,
memory: 62,
disk: 38
};
}
};

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
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';
}