Files
ResourceUsageAPI/README.md
T
Phoenix 823e467078 Add start-service.bat script for Resource Monitor Service v2.0
- Introduced a batch script to simplify the startup process for the Resource Monitor Service.
- Included checks for .NET 9.0 Runtime installation.
- Added build and run commands for the service with appropriate error handling.
- Provided user instructions and API documentation links in the script output.
2025-08-07 02:39:54 +08:00

6.0 KiB

Resource Monitor Service v2.0

A comprehensive Windows VM monitoring service designed specifically for Unraid virtual machines. This service provides real-time system monitoring, alerting, and remote management capabilities through a REST API.

🚀 New Features in v2.0

Enhanced Monitoring

  • Multi-core CPU monitoring with per-core usage and frequency tracking
  • Advanced memory monitoring including paged/non-paged memory
  • Enhanced GPU monitoring with NVIDIA GPU support via NVML
  • Comprehensive disk monitoring with I/O performance metrics
  • Network monitoring with per-adapter statistics
  • Temperature monitoring for CPU, GPU, and storage devices
  • Process monitoring with detailed resource usage tracking

VM-Specific Features

  • Hypervisor detection (VMware, Hyper-V, QEMU/KVM, etc.)
  • VM information including boot time and uptime tracking
  • Virtual machine optimization for better performance in virtualized environments

Game Detection & Management

  • Multi-platform game detection (Steam, Epic Games, GOG, Origin, Ubisoft)
  • Fullscreen detection for gaming sessions
  • Game performance monitoring with memory and CPU usage
  • Enhanced process management with graceful termination options

Intelligent Alerting System

  • Configurable thresholds for CPU, memory, GPU, and temperature
  • Duration-based alerting to prevent false positives
  • Alert history and active alert management
  • Automatic alert resolution when conditions improve

Improved API

  • RESTful endpoints with structured responses

  • Enhanced error handling and logging

  • Configurable CORS and API key authentication

  • Health check endpoints for monitoring service status

    • RAM Usage
    • GPU Usage
    • Currently Running Steam Games (if any)
  • Process Management: Provides an API to kill processes by their process ID.

Directory Structure

ResourceUsageAPI/
├── Worker.cs
├── Program.cs
├── Startup.cs
└── NvmlWrapper.cs

Code Analysis

Worker.cs

This file contains the main logic for monitoring system resources and exposing APIs.

  • Dependencies: Uses System.Diagnostics, Microsoft.AspNetCore.Builder, Newtonsoft.Json among others.
  • Methods:
    • ExecuteAsync: Sets up the ASP.NET Core web application, defines routes for resource usage and process management, and runs the server.
    • GetComputerInfo: Retrieves basic system information.
    • GetCpuUsage: Fetches CPU usage and lists top three processes by CPU usage if usage is over 80%.
    • GetRamUsage: Calculates RAM usage percentage.
    • GetTotalPhysicalMemory: Retrieves total physical memory size.
    • GetGpuUsage: Uses NVIDIA Management Library (NVML) to fetch GPU usage, temperature, and fan speed.
    • GetCurrentlyRunningGame: Detects if a Steam game is running by checking process paths.
    • GetCurrentTime: Returns the current time.

Program.cs

This file sets up the hosting environment for the application.

  • Dependencies: Uses Microsoft.Extensions.DependencyInjection and Microsoft.Extensions.Hosting.
  • Methods:
    • Main: Entry point of the application, builds and runs the host.
    • CreateHostBuilder: Configures services and determines if the application should run as a Windows service based on command-line arguments or environment variables.

Startup.cs

This file is not used in the current implementation since all routing and configuration are done within Worker.cs.

  • Dependencies: Uses Microsoft.AspNetCore.Builder and Microsoft.AspNetCore.Hosting.
  • Methods:
    • ConfigureServices: Placeholder method for adding services.
    • Configure: Placeholder method for configuring application HTTP requests pipeline.

NvmlWrapper.cs

This file provides a C# wrapper for the NVIDIA Management Library (NVML) functions.

  • Dependencies: Uses System and System.Runtime.InteropServices.
  • Methods:
    • Importing NVML DLL functions to interact with GPU hardware.
    • Structures like NvmlUtilization are defined for handling utilization rates returned by NVML.

Usage

  1. Build the Project: Use your preferred .NET build tool (e.g., dotnet build) to compile the project.
  2. Run the Application:
    • To run as a console application, execute the compiled binary directly.
    • To run as a Windows service, use the command-line argument --windows-service or set the environment variable RUN_AS_SERVICE to "true".

APIs

  • Get Resource Usage:

    • URL: /api/resource-usage
    • Method: GET
    • Description: Retrieves current system resource usage.
  • Kill Process:

    • URL: /api/kill-process
    • Method: POST
    • Body: JSON with the process ID ({"id": "1234"})
    • Description: Kills the specified process.

Important Notes

  • Ensure that the NVIDIA Management Library (NVML) is installed on the system for GPU monitoring to work.
  • The application allows CORS from all origins, which should be configured securely in production environments.
  • Error handling and logging are minimal; consider adding robust error handling and logging mechanisms for a production-ready solution.

Contributing

Feel free to contribute by opening issues or submitting pull requests. Make sure to follow the project's coding style and best practices.

devnote

dotnet run
git add . git commit -m "Add steam running games"
git push origin master
dotnet publish -c Release -o ./publish

devtest

Invoke-WebRequest -Uri "http://localhost:5000/api/kill-process" -Method POST -Body "1234" -Headers @{ "X-API-KEY" = "b7f3e8a1-4c2d-4d9f-9a6e-2a1c5d7f8e9a" }

Invoke-WebRequest -Uri "http://192.168.50.52:5000/api/resource-usage" -Method GET -Headers @{ "X-API-KEY" = "b7f3e8a1-4c2d-4d9f-9a6e-2a1c5d7f8e9a" }

Use 'shutdown', 'restart', or 'cancel'. Invoke-WebRequest -Uri "http://192.168.50.52:5000/api/force-shutdown" -Method POST -Headers @{ "X-API-KEY" = "b7f3e8a1-4c2d-4d9f-9a6e-2a1c5d7f8e9a" } -Body '{"Action": "shutdown", "DelaySeconds": 120}' -ContentType "application/json"