Host Metrics plugin Modmon hosts
$ dotnet add package ModMon.Modules.HostMetricsComprehensive host system metrics monitoring module for the ModMon framework. Provides real-time collection and analysis of CPU, memory, disk, network, and process metrics with configurable alerts, health checks, and background processing.
/api/host-metrics/current - Get current system metrics/api/host-metrics/history - Historical metrics with time range filtering/api/host-metrics/system-info - Static system information/api/host-metrics/performance - Performance summary and health indicators/api/host-metrics/health - Module health status/api/host-metrics/config - Current configuration/api/host-metrics/collect - Manual metrics collection triggerdotnet add package ModMon.HostMetrics
using ModMon.Hosting;
using ModMon.HostMetrics;
var app = ModMonApplication.CreateBuilder(args)
.AddModuleDefaults()
.AddModule<HostMetricsModule>()
.Build();
app.Run();
Add to your appsettings.json:
{
"Modules": {
"HostMetrics": {
"Collection": {
"EnableCollection": true,
"EnableBackgroundCollection": true,
"CollectionInterval": "00:01:00"
},
"Storage": {
"EnableInMemoryCache": true,
"MaxCacheSize": 1440,
"RetentionPeriod": "7.00:00:00"
},
"Alerts": {
"CpuUsageThreshold": 80.0,
"MemoryUsageThreshold": 85.0,
"DiskUsageThreshold": 90.0
}
}
}
}
# Get current metrics
curl http://localhost:5000/api/host-metrics/current
# Get historical data
curl "http://localhost:5000/api/host-metrics/history?from=2025-01-01T00:00:00Z&to=2025-01-01T23:59:59Z&intervalMinutes=5"
# Check health
curl http://localhost:5000/api/host-metrics/health
{
"Collection": {
"EnableCollection": true,
"EnableBackgroundCollection": true,
"CollectionInterval": "00:01:00",
"CollectionTimeout": "00:00:30",
"EnableCpuMetrics": true,
"EnableMemoryMetrics": true,
"EnableDiskMetrics": true,
"EnableNetworkMetrics": true,
"EnableProcessMetrics": true,
"EnableGcMetrics": true,
"EnableThreadPoolMetrics": true,
"MonitoredDrives": [],
"MonitoredNetworkInterfaces": [],
"MaxRetries": 3,
"RetryDelay": "00:00:05"
}
}
{
"Storage": {
"EnableInMemoryCache": true,
"MaxCacheSize": 1440,
"RetentionPeriod": "7.00:00:00",
"EnableAutoCleanup": true,
"CleanupInterval": "1.00:00:00",
"EnableCompression": false
}
}
{
"Alerts": {
"EnableAlerts": true,
"CpuUsageThreshold": 80.0,
"MemoryUsageThreshold": 85.0,
"DiskUsageThreshold": 90.0,
"NetworkUsageThreshold": 104857600,
"GcPressureThreshold": 10.0,
"ThreadCountThreshold": 200,
"CustomThresholds": {},
"AlertCooldown": "00:05:00"
}
}
{
"Performance": {
"EnablePerformanceAnalysis": true,
"AnalysisWindowSize": 60,
"EnableRecommendations": true,
"EnableTrendAnalysis": true,
"MinTrendDataPoints": 10,
"EnableAnomalyDetection": false,
"AnomalyThreshold": 2.0
}
}
Request:
GET /api/host-metrics/current
Response:
{
"timestamp": "2025-08-22T16:30:00Z",
"machineName": "web-server-01",
"cpu": {
"usagePercent": 45.2,
"logicalProcessors": 8,
"loadAverages": [1.2, 1.5, 1.8],
"processCpuTime": "01:23:45",
"systemUptime": "5.12:34:56"
},
"memory": {
"totalBytes": 17179869184,
"availableBytes": 8589934592,
"usedBytes": 8589934592,
"usagePercent": 50.0,
"processWorkingSet": 134217728,
"processPrivateMemory": 268435456,
"managedMemory": 67108864,
"virtualMemoryUsage": 536870912
},
"disks": [
{
"driveName": "C:",
"fileSystem": "NTFS",
"totalBytes": 1099511627776,
"freeBytes": 549755813888,
"usedBytes": 549755813888,
"usagePercent": 50.0,
"readOpsPerSecond": 15.2,
"writeOpsPerSecond": 8.7,
"readBytesPerSecond": 1048576,
"writeBytesPerSecond": 524288
}
],
"network": {
"bytesReceivedPerSecond": 1048576,
"bytesSentPerSecond": 524288,
"totalBytesReceived": 1099511627776,
"totalBytesSent": 549755813888,
"activeConnections": 42,
"interfaces": [
{
"name": "Ethernet",
"description": "Intel Ethernet Controller",
"isUp": true,
"bytesReceived": 1099511627776,
"bytesSent": 549755813888,
"speed": 1000000000
}
]
},
"process": {
"processId": 1234,
"processName": "MyApp",
"startTime": "2025-08-22T12:00:00Z",
"uptime": "04:30:00",
"threadCount": 15,
"handleCount": 456,
"peakWorkingSet": 268435456,
"peakVirtualMemory": 1073741824
},
"garbageCollection": {
"totalCollections": 1250,
"collectionsByGeneration": [800, 350, 100],
"totalAllocatedBytes": 5368709120,
"heapSizeBytes": 134217728,
"timeInGcPercent": 2.5,
"largeObjectHeapSize": 16777216
},
"threadPool": {
"workerThreads": 12,
"completionPortThreads": 4,
"maxWorkerThreads": 32,
"maxCompletionPortThreads": 32,
"queuedWorkItems": 3,
"completedWorkItems": 98765
},
"customMetrics": {}
}
Request:
GET /api/host-metrics/performance
Response:
{
"generatedAt": "2025-08-22T16:30:00Z",
"overallStatus": "Good",
"indicators": [
{
"name": "CPU Usage",
"value": 45.2,
"unit": "%",
"status": "Good",
"description": "Current CPU utilization percentage",
"threshold": 80.0
},
{
"name": "Memory Usage",
"value": 50.0,
"unit": "%",
"status": "Good",
"description": "Current memory utilization percentage",
"threshold": 85.0
}
],
"alerts": [],
"keyMetrics": {
"CpuUsage": 45.2,
"MemoryUsage": 50.0,
"ProcessWorkingSet": 128.0,
"ThreadCount": 15,
"HandleCount": 456,
"GcCollections": 1250,
"GcHeapSize": 128.0,
"ActiveConnections": 42,
"ProcessUptime": 4.5
},
"recommendations": []
}
The module automatically registers background tasks:
Monitor task execution through the ModMon background task system.
The module registers with ASP.NET Core health checks:
# Check overall application health (includes HostMetrics)
curl http://localhost:5000/health
# Check detailed health with HostMetrics info
curl http://localhost:5000/health/detailed
Health check validates:
Metrics not collecting:
# Check module configuration
curl http://localhost:5000/api/host-metrics/config
# Check health status
curl http://localhost:5000/api/host-metrics/health
# Check logs for errors
High memory usage:
{
"Storage": {
"MaxCacheSize": 720, // Reduce cache size
"RetentionPeriod": "3.00:00:00" // Reduce retention
}
}
Performance impact:
{
"Collection": {
"CollectionInterval": "00:05:00", // Collect less frequently
"EnableNetworkMetrics": false, // Disable expensive metrics
"EnableDiskMetrics": false
}
}
Enable detailed logging:
{
"Logging": {
"LogLevel": {
"ModMon.HostMetrics": "Debug"
}
}
}
This module follows ModMon framework patterns:
Contracts folderModels folderImplementation folderOptions folderTasks folderMIT License - see the LICENSE file for details.