2 Commits

Author SHA1 Message Date
Phoenix f2a0818d0e Update service port from 5000 to 2414 and adjust related configurations 2025-08-07 17:53:54 +08:00
Phoenix d6efa9163b Add Telegram bot integration for real-time alert notifications
- Implemented ITelegramNotificationService and TelegramNotificationService for sending alerts via Telegram.
- Updated MonitoringSettings to include Telegram configuration options.
- Enhanced AlertService to send alerts and resolutions through Telegram.
- Added API endpoints for checking Telegram status and sending test alerts.
- Updated README and TELEGRAM_SETUP.md with setup instructions and features.
- Included example configuration in appsettings.telegram.example.json.
2025-08-07 17:30:02 +08:00
21 changed files with 621 additions and 108 deletions
+14
View File
@@ -38,6 +38,8 @@ namespace ResourceMonitorService.Configuration
new() { Component = "CPUTemp", WarningThreshold = 75, CriticalThreshold = 85, DurationSeconds = 60 }, new() { Component = "CPUTemp", WarningThreshold = 75, CriticalThreshold = 85, DurationSeconds = 60 },
new() { Component = "GPUTemp", WarningThreshold = 80, CriticalThreshold = 90, DurationSeconds = 60 } new() { Component = "GPUTemp", WarningThreshold = 80, CriticalThreshold = 90, DurationSeconds = 60 }
}; };
public TelegramSettings Telegram { get; set; } = new();
} }
public class AlertThresholdConfig public class AlertThresholdConfig
@@ -73,4 +75,16 @@ namespace ResourceMonitorService.Configuration
public bool EnableConsoleLogging { get; set; } = true; public bool EnableConsoleLogging { get; set; } = true;
public bool EnablePerformanceLogging { get; set; } = false; public bool EnablePerformanceLogging { get; set; } = false;
} }
public class TelegramSettings
{
public bool IsEnabled { get; set; } = false;
public string BotToken { get; set; } = string.Empty;
public List<long> ChatIds { get; set; } = new();
public bool SendWarningAlerts { get; set; } = true;
public bool SendCriticalAlerts { get; set; } = true;
public bool SendResolutionNotifications { get; set; } = true;
public string MessageTemplate { get; set; } = "🚨 *{Level} Alert*\n\n📊 *{Component}*\n💬 {Message}\n⏰ {Timestamp:yyyy-MM-dd HH:mm:ss}";
public string ResolutionTemplate { get; set; } = "✅ *Alert Resolved*\n\n📊 *{Component}*\n💬 {Message}\n⏰ Resolved at {ResolvedAt:yyyy-MM-dd HH:mm:ss}";
}
} }
+1
View File
@@ -51,6 +51,7 @@ namespace ResourceMonitorService
services.AddSingleton<ISystemInfoService, SystemInfoService>(); services.AddSingleton<ISystemInfoService, SystemInfoService>();
services.AddSingleton<IResourceMonitorService, Services.ResourceMonitorService>(); services.AddSingleton<IResourceMonitorService, Services.ResourceMonitorService>();
services.AddSingleton<IGameDetectionService, GameDetectionService>(); services.AddSingleton<IGameDetectionService, GameDetectionService>();
services.AddSingleton<ITelegramNotificationService, TelegramNotificationService>();
services.AddSingleton<IAlertService, AlertService>(); services.AddSingleton<IAlertService, AlertService>();
// Register the main worker service // Register the main worker service
+39
View File
@@ -22,6 +22,7 @@ A comprehensive system monitoring service specifically designed for Windows VMs
- **Game Detection**: Multi-platform game detection with fullscreen monitoring and configurable root folders - **Game Detection**: Multi-platform game detection with fullscreen monitoring and configurable root folders
- **Process Management**: View top processes with CPU/memory percentages, terminate processes via API - **Process Management**: View top processes with CPU/memory percentages, terminate processes via API
- **Smart Alerting**: Duration-based alerting to prevent false positives - **Smart Alerting**: Duration-based alerting to prevent false positives
- **Telegram Bot Integration**: Real-time alerts via Telegram bot with customizable notifications
- **System Control**: Remote shutdown/restart capabilities - **System Control**: Remote shutdown/restart capabilities
- **Health Monitoring**: Comprehensive health checks and uptime tracking - **Health Monitoring**: Comprehensive health checks and uptime tracking
- **Real-time Metrics**: CPU usage calculation and memory percentage tracking for processes - **Real-time Metrics**: CPU usage calculation and memory percentage tracking for processes
@@ -57,6 +58,10 @@ The service runs on `http://localhost:5000` by default and provides the followin
- `POST /api/alerts/{alertId}/resolve` - Manually resolve an alert - `POST /api/alerts/{alertId}/resolve` - Manually resolve an alert
- `GET /api/alerts/enabled` - Check if alerting is enabled - `GET /api/alerts/enabled` - Check if alerting is enabled
### Telegram Bot Integration
- `GET /api/telegram/status` - Check Telegram bot status and connection
- `POST /api/telegram/test` - Send a test alert to verify bot configuration
### System Control ### System Control
- `POST /api/process/kill` - Terminate a process (requires process ID and optional force flag) - `POST /api/process/kill` - Terminate a process (requires process ID and optional force flag)
- `POST /api/system/shutdown` - Shutdown, restart, or cancel system operations - `POST /api/system/shutdown` - Shutdown, restart, or cancel system operations
@@ -180,6 +185,40 @@ D:\Games\The Witcher 3\witcher3.exe
- Use absolute paths (e.g., `C:\Games`, not `Games`) - Use absolute paths (e.g., `C:\Games`, not `Games`)
- Root folders are checked in order, so prioritize most common locations first - Root folders are checked in order, so prioritize most common locations first
### Telegram Bot Alerts
The service supports real-time alert notifications via Telegram bot. To set up Telegram alerts:
1. **Create a Telegram Bot** - Contact `@BotFather` on Telegram and create a new bot
2. **Get Chat ID** - Send a message to your bot, then visit `https://api.telegram.org/bot<TOKEN>/getUpdates`
3. **Configure Settings** - Add Telegram configuration to your `appsettings.json`:
```json
{
"MonitoringSettings": {
"Telegram": {
"IsEnabled": true,
"BotToken": "123456789:ABCdefGHIjklMNOpqrSTUvwxyz",
"ChatIds": [123456789, -987654321],
"SendWarningAlerts": true,
"SendCriticalAlerts": true,
"SendResolutionNotifications": true,
"MessageTemplate": "🚨 *{Level} Alert*\n\n📊 *{Component}*\n💬 {Message}\n⏰ {Timestamp}",
"ResolutionTemplate": "✅ *Alert Resolved*\n\n📊 *{Component}*\n💬 {Message}\n⏰ Resolved at {ResolvedAt}"
}
}
}
```
**Features:**
- **Multiple Chats**: Send alerts to multiple users/groups by adding chat IDs
- **Customizable Templates**: Modify message format with placeholders for alert data
- **Alert Filtering**: Choose which alert levels to send (Warning/Critical)
- **Silent Notifications**: Warning alerts are sent silently, critical alerts with sound
- **Resolution Notifications**: Optional notifications when alerts are resolved
📋 For detailed setup instructions, see [TELEGRAM_SETUP.md](TELEGRAM_SETUP.md)
## 📊 Example API Responses ## 📊 Example API Responses
### Health Check ### Health Check
+1
View File
@@ -17,5 +17,6 @@
<PackageReference Include="Serilog.Extensions.Hosting" Version="8.0.0" /> <PackageReference Include="Serilog.Extensions.Hosting" Version="8.0.0" />
<PackageReference Include="Serilog.Sinks.Console" Version="5.0.1" /> <PackageReference Include="Serilog.Sinks.Console" Version="5.0.1" />
<PackageReference Include="Serilog.Sinks.File" Version="5.0.0" /> <PackageReference Include="Serilog.Sinks.File" Version="5.0.0" />
<PackageReference Include="Telegram.Bot" Version="22.6.0" />
</ItemGroup> </ItemGroup>
</Project> </Project>
+29 -1
View File
@@ -21,6 +21,7 @@ namespace ResourceMonitorService.Services
{ {
private readonly ILogger<AlertService> _logger; private readonly ILogger<AlertService> _logger;
private readonly MonitoringSettings _settings; private readonly MonitoringSettings _settings;
private readonly ITelegramNotificationService _telegramService;
private readonly ConcurrentDictionary<string, Alert> _activeAlerts; private readonly ConcurrentDictionary<string, Alert> _activeAlerts;
private readonly ConcurrentQueue<Alert> _alertHistory; private readonly ConcurrentQueue<Alert> _alertHistory;
private readonly Dictionary<string, DateTime> _lastAlertTime; private readonly Dictionary<string, DateTime> _lastAlertTime;
@@ -29,10 +30,11 @@ namespace ResourceMonitorService.Services
public event EventHandler<Alert>? AlertTriggered; public event EventHandler<Alert>? AlertTriggered;
public event EventHandler<Alert>? AlertResolved; public event EventHandler<Alert>? AlertResolved;
public AlertService(ILogger<AlertService> logger, IOptions<MonitoringSettings> settings) public AlertService(ILogger<AlertService> logger, IOptions<MonitoringSettings> settings, ITelegramNotificationService telegramService)
{ {
_logger = logger; _logger = logger;
_settings = settings.Value; _settings = settings.Value;
_telegramService = telegramService;
_activeAlerts = new ConcurrentDictionary<string, Alert>(); _activeAlerts = new ConcurrentDictionary<string, Alert>();
_alertHistory = new ConcurrentQueue<Alert>(); _alertHistory = new ConcurrentQueue<Alert>();
_lastAlertTime = new Dictionary<string, DateTime>(); _lastAlertTime = new Dictionary<string, DateTime>();
@@ -183,6 +185,19 @@ namespace ResourceMonitorService.Services
_logger.LogWarning("Alert triggered: {Message}", alert.Message); _logger.LogWarning("Alert triggered: {Message}", alert.Message);
AlertTriggered?.Invoke(this, alert); AlertTriggered?.Invoke(this, alert);
// Send Telegram notification
_ = Task.Run(async () =>
{
try
{
await _telegramService.SendAlertAsync(alert);
}
catch (Exception ex)
{
_logger.LogError(ex, "Failed to send Telegram alert notification");
}
});
} }
else else
{ {
@@ -264,6 +279,19 @@ namespace ResourceMonitorService.Services
_logger.LogInformation("Alert resolved: {Message}", resolvedAlert.Message); _logger.LogInformation("Alert resolved: {Message}", resolvedAlert.Message);
AlertResolved?.Invoke(this, resolvedAlert); AlertResolved?.Invoke(this, resolvedAlert);
// Send Telegram resolution notification
_ = Task.Run(async () =>
{
try
{
await _telegramService.SendAlertResolvedAsync(resolvedAlert);
}
catch (Exception ex)
{
_logger.LogError(ex, "Failed to send Telegram resolution notification");
}
});
} }
} }
} }
+5 -2
View File
@@ -154,8 +154,11 @@ namespace ResourceMonitorService.Services
return false; return false;
// Get screen dimensions // Get screen dimensions
var screenWidth = System.Windows.Forms.Screen.PrimaryScreen.Bounds.Width; var primaryScreen = System.Windows.Forms.Screen.PrimaryScreen;
var screenHeight = System.Windows.Forms.Screen.PrimaryScreen.Bounds.Height; if (primaryScreen == null)
return false;
var screenWidth = primaryScreen.Bounds.Width;
var screenHeight = primaryScreen.Bounds.Height;
// Check if window covers the entire screen // Check if window covers the entire screen
var windowWidth = rect.Right - rect.Left; var windowWidth = rect.Right - rect.Left;
+206
View File
@@ -0,0 +1,206 @@
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using ResourceMonitorService.Configuration;
using ResourceMonitorService.Models;
using Telegram.Bot;
using Telegram.Bot.Exceptions;
using Telegram.Bot.Types.Enums;
namespace ResourceMonitorService.Services
{
public interface ITelegramNotificationService
{
Task SendAlertAsync(Alert alert);
Task SendAlertResolvedAsync(Alert alert);
Task<bool> IsEnabledAsync();
Task<bool> TestConnectionAsync();
}
public class TelegramNotificationService : ITelegramNotificationService
{
private readonly ILogger<TelegramNotificationService> _logger;
private readonly TelegramSettings _telegramSettings;
private readonly ITelegramBotClient? _botClient;
public TelegramNotificationService(
ILogger<TelegramNotificationService> logger,
IOptions<MonitoringSettings> settings)
{
_logger = logger;
_telegramSettings = settings.Value.Telegram;
if (_telegramSettings.IsEnabled && !string.IsNullOrEmpty(_telegramSettings.BotToken))
{
try
{
_botClient = new TelegramBotClient(_telegramSettings.BotToken);
}
catch (Exception ex)
{
_logger.LogError(ex, "Failed to initialize Telegram bot client");
}
}
}
public async Task<bool> IsEnabledAsync()
{
return await Task.FromResult(_telegramSettings.IsEnabled && _botClient != null);
}
public async Task<bool> TestConnectionAsync()
{
if (_botClient == null || !_telegramSettings.IsEnabled)
return false;
try
{
var me = await _botClient.GetMe();
_logger.LogInformation("Telegram bot connected successfully: @{Username}", me.Username);
return true;
}
catch (Exception ex)
{
_logger.LogError(ex, "Failed to connect to Telegram bot");
return false;
}
}
public async Task SendAlertAsync(Alert alert)
{
if (_botClient == null || !_telegramSettings.IsEnabled)
return;
// Check if we should send this type of alert
if ((alert.Level == "Warning" && !_telegramSettings.SendWarningAlerts) ||
(alert.Level == "Critical" && !_telegramSettings.SendCriticalAlerts))
{
return;
}
var message = FormatAlertMessage(alert, _telegramSettings.MessageTemplate);
foreach (var chatId in _telegramSettings.ChatIds)
{
try
{
await _botClient.SendMessage(
chatId: chatId,
text: message,
parseMode: ParseMode.Markdown,
disableNotification: alert.Level == "Warning" // Don't ping for warnings
);
_logger.LogInformation("Telegram alert sent to chat {ChatId}: {AlertLevel} - {Component}",
chatId, alert.Level, alert.Component);
}
catch (ApiRequestException ex)
{
_logger.LogError(ex, "Failed to send Telegram alert to chat {ChatId}: {ErrorCode} - {Description}",
chatId, ex.ErrorCode, ex.Message);
}
catch (Exception ex)
{
_logger.LogError(ex, "Unexpected error sending Telegram alert to chat {ChatId}", chatId);
}
}
}
public async Task SendAlertResolvedAsync(Alert alert)
{
if (_botClient == null || !_telegramSettings.IsEnabled || !_telegramSettings.SendResolutionNotifications)
return;
var message = FormatAlertMessage(alert, _telegramSettings.ResolutionTemplate);
foreach (var chatId in _telegramSettings.ChatIds)
{
try
{
await _botClient.SendMessage(
chatId: chatId,
text: message,
parseMode: ParseMode.Markdown,
disableNotification: true // Don't ping for resolutions
);
_logger.LogInformation("Telegram resolution notification sent to chat {ChatId}: {Component}",
chatId, alert.Component);
}
catch (ApiRequestException ex)
{
_logger.LogError(ex, "Failed to send Telegram resolution to chat {ChatId}: {ErrorCode} - {Description}",
chatId, ex.ErrorCode, ex.Message);
}
catch (Exception ex)
{
_logger.LogError(ex, "Unexpected error sending Telegram resolution to chat {ChatId}", chatId);
}
}
}
private string FormatAlertMessage(Alert alert, string template)
{
var levelIcon = alert.Level switch
{
"Critical" => "🔴",
"Warning" => "⚠️",
_ => "️"
};
var componentIcon = alert.Component switch
{
"CPU" => "🖥️",
"CPUTemp" => "🌡️",
"Memory" => "💾",
"GPU" => "🎮",
"GPUTemp" => "🌡️",
var disk when disk.StartsWith("Disk") => "💽",
var process when process.StartsWith("ProcessMemory") => "⚙️",
_ => "📊"
};
// Replace template placeholders
var message = template
.Replace("{Level}", alert.Level)
.Replace("{Component}", alert.Component)
.Replace("{Message}", EscapeMarkdown(alert.Message))
.Replace("{Timestamp}", alert.Timestamp.ToString("yyyy-MM-dd HH:mm:ss"))
.Replace("{CurrentValue}", alert.CurrentValue.ToString("F1"))
.Replace("{ThresholdValue}", alert.ThresholdValue.ToString("F1"));
if (alert.ResolvedAt.HasValue)
{
message = message.Replace("{ResolvedAt}", alert.ResolvedAt.Value.ToString("yyyy-MM-dd HH:mm:ss"));
}
// Add icons
message = $"{levelIcon} {componentIcon} {message}";
return message;
}
private static string EscapeMarkdown(string text)
{
// Escape special Markdown characters
return text
.Replace("_", "\\_")
.Replace("*", "\\*")
.Replace("[", "\\[")
.Replace("]", "\\]")
.Replace("(", "\\(")
.Replace(")", "\\)")
.Replace("~", "\\~")
.Replace("`", "\\`")
.Replace(">", "\\>")
.Replace("#", "\\#")
.Replace("+", "\\+")
.Replace("-", "\\-")
.Replace("=", "\\=")
.Replace("|", "\\|")
.Replace("{", "\\{")
.Replace("}", "\\}")
.Replace(".", "\\.")
.Replace("!", "\\!");
}
}
}
+196
View File
@@ -0,0 +1,196 @@
# Telegram Bot Alert Setup Guide
The Resource Monitor Service supports sending alerts via Telegram bot. This allows you to receive real-time notifications about system resource warnings and critical alerts directly to your Telegram chat.
## Prerequisites
1. **Create a Telegram Bot**:
- Open Telegram and search for `@BotFather`
- Send `/newbot` command
- Follow the instructions to create a new bot
- Save the Bot Token (format: `123456789:ABCdefGHIjklMNOpqrSTUvwxyz`)
2. **Get Your Chat ID**:
- Send a message to your bot
- Visit: `https://api.telegram.org/bot<YourBOTToken>/getUpdates`
- Find your chat ID in the response (it's a number, can be negative for groups)
## Configuration
### 1. Edit appsettings.json
Add or update the Telegram configuration in your `appsettings.json`:
```json
{
"MonitoringSettings": {
// ... other settings ...
"Telegram": {
"IsEnabled": true,
"BotToken": "123456789:ABCdefGHIjklMNOpqrSTUvwxyz",
"ChatIds": [12345678, -987654321],
"SendWarningAlerts": true,
"SendCriticalAlerts": true,
"SendResolutionNotifications": true,
"MessageTemplate": "🚨 *{Level} Alert*\n\n📊 *{Component}*\n💬 {Message}\n⏰ {Timestamp}",
"ResolutionTemplate": "✅ *Alert Resolved*\n\n📊 *{Component}*\n💬 {Message}\n⏰ Resolved at {ResolvedAt}"
}
}
}
```
### 2. Configuration Options
| Setting | Description | Default |
|---------|-------------|---------|
| `IsEnabled` | Enable/disable Telegram notifications | `false` |
| `BotToken` | Your Telegram bot token from BotFather | `""` |
| `ChatIds` | Array of chat IDs to send alerts to | `[]` |
| `SendWarningAlerts` | Send warning level alerts | `true` |
| `SendCriticalAlerts` | Send critical level alerts | `true` |
| `SendResolutionNotifications` | Send alert resolution notifications | `true` |
| `MessageTemplate` | Template for alert messages | See above |
| `ResolutionTemplate` | Template for resolution messages | See above |
### 3. Message Templates
Templates support the following placeholders:
- `{Level}` - Alert level (Warning, Critical)
- `{Component}` - Component name (CPU, Memory, GPU, etc.)
- `{Message}` - Full alert message
- `{Timestamp}` - Alert timestamp
- `{CurrentValue}` - Current resource value
- `{ThresholdValue}` - Threshold that was exceeded
- `{ResolvedAt}` - Resolution timestamp (resolution template only)
## API Endpoints
### Check Telegram Status
```
GET /api/telegram/status
```
Returns the current status of Telegram integration:
```json
{
"enabled": true,
"connected": true
}
```
### Send Test Alert
```
POST /api/telegram/test
```
Sends a test alert to verify the Telegram bot is working correctly.
## Features
### Alert Types
The bot sends different types of alerts:
1. **Warning Alerts** ⚠️
- Sent when thresholds are exceeded but not critical
- Notifications are silent (no sound/vibration)
2. **Critical Alerts** 🔴
- Sent when critical thresholds are exceeded
- Normal notifications (with sound/vibration)
3. **Resolution Notifications**
- Sent when alerts are resolved
- Always silent notifications
### Icons and Formatting
The bot automatically adds relevant icons:
- 🖥️ CPU usage
- 🌡️ Temperature alerts
- 💾 Memory usage
- 🎮 GPU usage
- 💽 Disk usage
- ⚙️ Process alerts
Messages are formatted using Telegram's Markdown formatting for better readability.
### Multiple Chat Support
You can send alerts to multiple chats:
- Personal chats
- Group chats
- Channels (if bot is admin)
Just add multiple chat IDs to the `ChatIds` array.
## Troubleshooting
### Common Issues
1. **Bot not responding**:
- Verify bot token is correct
- Ensure bot is not blocked
- Check `/api/telegram/status` endpoint
2. **Messages not received**:
- Verify chat ID is correct
- Ensure you've sent at least one message to the bot
- Check bot has permission to send messages
3. **Connection errors**:
- Check internet connectivity
- Verify Telegram API is accessible
- Check firewall settings
### Testing
1. Set `IsEnabled: true` in configuration
2. Restart the service
3. Call `POST /api/telegram/test` to send a test message
4. Check if the message is received in Telegram
### Logs
Monitor the service logs for Telegram-related errors:
```
grep -i telegram logs/resourcemonitor-*.txt
```
## Security Considerations
1. **Keep bot token secure** - Never commit it to version control
2. **Use environment variables** for sensitive configuration in production
3. **Limit chat IDs** to trusted users/groups only
4. **Regular token rotation** if compromised
## Example Alert Messages
### Warning Alert
```
⚠️ 🖥️ 🚨 Warning Alert
📊 CPU
💬 CPU Usage is warning: 85.2% (threshold: 80.0%)
⏰ 2025-08-07 14:30:15
```
### Critical Alert
```
🔴 💾 🚨 Critical Alert
📊 Memory
💬 Memory Usage is critical: 96.8% (threshold: 95.0%)
⏰ 2025-08-07 14:35:22
```
### Resolution
```
✅ 🖥️ Alert Resolved
📊 CPU
💬 CPU Usage is warning: 85.2% (threshold: 80.0%)
⏰ Resolved at 2025-08-07 14:32:45
```
+34
View File
@@ -20,6 +20,7 @@ namespace ResourceMonitorService
private readonly IResourceMonitorService _resourceMonitorService; private readonly IResourceMonitorService _resourceMonitorService;
private readonly IGameDetectionService _gameDetectionService; private readonly IGameDetectionService _gameDetectionService;
private readonly IAlertService _alertService; private readonly IAlertService _alertService;
private readonly ITelegramNotificationService _telegramService;
private readonly ApiSettings _apiSettings; private readonly ApiSettings _apiSettings;
private readonly MonitoringSettings _monitoringSettings; private readonly MonitoringSettings _monitoringSettings;
@@ -30,6 +31,7 @@ namespace ResourceMonitorService
IResourceMonitorService resourceMonitorService, IResourceMonitorService resourceMonitorService,
IGameDetectionService gameDetectionService, IGameDetectionService gameDetectionService,
IAlertService alertService, IAlertService alertService,
ITelegramNotificationService telegramService,
IOptions<ApiSettings> apiSettings, IOptions<ApiSettings> apiSettings,
IOptions<MonitoringSettings> monitoringSettings) IOptions<MonitoringSettings> monitoringSettings)
{ {
@@ -39,6 +41,7 @@ namespace ResourceMonitorService
_resourceMonitorService = resourceMonitorService; _resourceMonitorService = resourceMonitorService;
_gameDetectionService = gameDetectionService; _gameDetectionService = gameDetectionService;
_alertService = alertService; _alertService = alertService;
_telegramService = telegramService;
_apiSettings = apiSettings.Value; _apiSettings = apiSettings.Value;
_monitoringSettings = monitoringSettings.Value; _monitoringSettings = monitoringSettings.Value;
} }
@@ -148,6 +151,37 @@ namespace ResourceMonitorService
app.MapGet($"{basePath}/alerts/enabled", async () => app.MapGet($"{basePath}/alerts/enabled", async () =>
Results.Ok(new { Enabled = await _alertService.IsAlertingEnabledAsync() })); Results.Ok(new { Enabled = await _alertService.IsAlertingEnabledAsync() }));
// Telegram endpoints
app.MapGet($"{basePath}/telegram/status", async () =>
Results.Ok(new {
Enabled = await _telegramService.IsEnabledAsync(),
Connected = await _telegramService.TestConnectionAsync()
}));
app.MapPost($"{basePath}/telegram/test", async () =>
{
try
{
var testAlert = new Alert
{
Timestamp = DateTime.Now,
Component = "Test",
Level = "Warning",
Message = "This is a test alert from Resource Monitor Service",
CurrentValue = 100,
ThresholdValue = 90,
IsResolved = false
};
await _telegramService.SendAlertAsync(testAlert);
return Results.Ok(new { Message = "Test alert sent successfully" });
}
catch (Exception ex)
{
return Results.Problem($"Failed to send test alert: {ex.Message}");
}
});
// Process management endpoints (enhanced) // Process management endpoints (enhanced)
app.MapPost($"{basePath}/process/kill", async (HttpContext context) => app.MapPost($"{basePath}/process/kill", async (HttpContext context) =>
{ {
+12 -2
View File
@@ -12,7 +12,7 @@
"Kestrel": { "Kestrel": {
"Endpoints": { "Endpoints": {
"Http": { "Http": {
"Url": "http://*:5000" "Url": "http://*:2414"
} }
} }
}, },
@@ -88,7 +88,17 @@
"DurationSeconds": 60, "DurationSeconds": 60,
"IsEnabled": true "IsEnabled": true
} }
] ],
"Telegram": {
"IsEnabled": true,
"BotToken": "7705627522:AAHDTVMF1uPJW7qm-Di0g_BmefAVWdOrS2U",
"ChatIds": [398126624],
"SendWarningAlerts": true,
"SendCriticalAlerts": true,
"SendResolutionNotifications": true,
"MessageTemplate": "🚨 *{Level} Alert*\n\n📊 *{Component}*\n💬 {Message}\n⏰ {Timestamp}",
"ResolutionTemplate": "✅ *Alert Resolved*\n\n📊 *{Component}*\n💬 {Message}\n⏰ Resolved at {ResolvedAt}"
}
}, },
"LoggingSettings": { "LoggingSettings": {
"LogLevel": "Information", "LogLevel": "Information",
+74
View File
@@ -0,0 +1,74 @@
{
"MonitoringSettings": {
"UpdateIntervalMs": 5000,
"DataRetentionDays": 7,
"EnableGpuMonitoring": true,
"EnableDiskMonitoring": true,
"EnableNetworkMonitoring": true,
"EnableTemperatureMonitoring": true,
"EnableProcessMonitoring": true,
"EnableGameDetection": true,
"EnableAlerts": true,
"MaxProcessesToTrack": 10,
"MaxHistoryPoints": 1000,
"GamePlatformPaths": [
"\\steamapps\\common\\",
"\\Epic Games\\",
"\\GOG Galaxy\\Games\\",
"\\Origin Games\\",
"\\Ubisoft Game Launcher\\games\\"
],
"GameRootFolders": [
"C:\\Games",
"D:\\Games",
"E:\\Games"
],
"AlertThresholds": [
{
"Component": "CPU",
"WarningThreshold": 80,
"CriticalThreshold": 95,
"DurationSeconds": 30,
"IsEnabled": true
},
{
"Component": "Memory",
"WarningThreshold": 85,
"CriticalThreshold": 95,
"DurationSeconds": 30,
"IsEnabled": true
},
{
"Component": "GPU",
"WarningThreshold": 85,
"CriticalThreshold": 95,
"DurationSeconds": 30,
"IsEnabled": true
},
{
"Component": "CPUTemp",
"WarningThreshold": 75,
"CriticalThreshold": 85,
"DurationSeconds": 60,
"IsEnabled": true
},
{
"Component": "GPUTemp",
"WarningThreshold": 80,
"CriticalThreshold": 90,
"DurationSeconds": 60,
"IsEnabled": true
}
],
"Telegram": {
"IsEnabled": true,
"BotToken": "123456789:ABCdefGHIjklMNOpqrSTUvwxyz",
"ChatIds": [123456789],
"SendWarningAlerts": true,
"SendCriticalAlerts": true,
"SendResolutionNotifications": true,
"MessageTemplate": "🚨 *{Level} Alert*\n\n📊 *{Component}*\n💬 {Message}\n⏰ {Timestamp}",
"ResolutionTemplate": "✅ *Alert Resolved*\n\n📊 *{Component}*\n💬 {Message}\n⏰ Resolved at {ResolvedAt}"
}
}
}
+7 -7
View File
@@ -138,11 +138,11 @@ try {
# Configure firewall rule # Configure firewall rule
Write-Host "Configuring Windows Firewall..." Write-Host "Configuring Windows Firewall..."
try { try {
New-NetFirewallRule -DisplayName "Resource Monitor Service" -Direction Inbound -Protocol TCP -LocalPort 5000 -Action Allow -Profile Any -ErrorAction Stop New-NetFirewallRule -DisplayName "Resource Monitor Service" -Direction Inbound -Protocol TCP -LocalPort 2414 -Action Allow -Profile Any -ErrorAction Stop
Write-Host "Firewall rule created" -ForegroundColor Green Write-Host "Firewall rule created" -ForegroundColor Green
} catch { } catch {
Write-Host "WARNING: Failed to create firewall rule. You may need to configure manually." -ForegroundColor Yellow Write-Host "WARNING: Failed to create firewall rule. You may need to configure manually." -ForegroundColor Yellow
Write-Host "Manual command: New-NetFirewallRule -DisplayName 'Resource Monitor Service' -Direction Inbound -Protocol TCP -LocalPort 5000 -Action Allow" -ForegroundColor Gray Write-Host "Manual command: New-NetFirewallRule -DisplayName 'Resource Monitor Service' -Direction Inbound -Protocol TCP -LocalPort 2414 -Action Allow" -ForegroundColor Gray
} }
# Start the service # Start the service
@@ -165,8 +165,8 @@ Write-Host
Write-Host "=== Installation Complete ===" -ForegroundColor Cyan Write-Host "=== Installation Complete ===" -ForegroundColor Cyan
Write-Host "Service Name: $SERVICE_NAME" -ForegroundColor White Write-Host "Service Name: $SERVICE_NAME" -ForegroundColor White
Write-Host "Installation Path: $INSTALL_PATH" -ForegroundColor White Write-Host "Installation Path: $INSTALL_PATH" -ForegroundColor White
Write-Host "Service URL: http://localhost:5000" -ForegroundColor White Write-Host "Service URL: http://localhost:2414" -ForegroundColor White
Write-Host "API Health Check: http://localhost:5000/api/health" -ForegroundColor White Write-Host "API Health Check: http://localhost:2414/api/health" -ForegroundColor White
Write-Host Write-Host
Write-Host "The service is now running and will start automatically with Windows." -ForegroundColor Green Write-Host "The service is now running and will start automatically with Windows." -ForegroundColor Green
Write-Host "You can manage it through Services.msc or using PowerShell commands:" -ForegroundColor White Write-Host "You can manage it through Services.msc or using PowerShell commands:" -ForegroundColor White
@@ -182,12 +182,12 @@ Write-Host
Write-Host "Testing API endpoint..." -ForegroundColor Yellow Write-Host "Testing API endpoint..." -ForegroundColor Yellow
Start-Sleep -Seconds 5 Start-Sleep -Seconds 5
try { try {
$response = Invoke-RestMethod -Uri "http://localhost:5000/api/health" -TimeoutSec 10 $response = Invoke-RestMethod -Uri "http://localhost:2414/" -TimeoutSec 10
Write-Host "API Test Result: SUCCESS" -ForegroundColor Green Write-Host "API Test Result: SUCCESS" -ForegroundColor Green
Write-Host "Service Version: $($response.Service)" -ForegroundColor White Write-Host "Service Version: $($response.Service) v$($response.Version)" -ForegroundColor White
Write-Host "Status: $($response.Status)" -ForegroundColor White Write-Host "Status: $($response.Status)" -ForegroundColor White
} catch { } catch {
Write-Host "API Test Result: FAILED" -ForegroundColor Red Write-Host "API Test Result: FAILED" -ForegroundColor Red
Write-Host "The service may still be starting up. Wait a few minutes and try accessing:" -ForegroundColor Yellow Write-Host "The service may still be starting up. Wait a few minutes and try accessing:" -ForegroundColor Yellow
Write-Host "http://localhost:5000/api/health" -ForegroundColor White Write-Host "http://localhost:2414/api/health" -ForegroundColor White
} }
+3 -3
View File
@@ -58,7 +58,7 @@ sc failure "$SERVICE_NAME" reset=300 actions=restart/5000/restart/5000/restart/1
# Configure firewall rule # Configure firewall rule
echo "Configuring Windows Firewall..." echo "Configuring Windows Firewall..."
powershell -Command "New-NetFirewallRule -DisplayName 'Resource Monitor Service' -Direction Inbound -Protocol TCP -LocalPort 5000 -Action Allow -Profile Any" 2>/dev/null powershell -Command "New-NetFirewallRule -DisplayName 'Resource Monitor Service' -Direction Inbound -Protocol TCP -LocalPort 2414 -Action Allow -Profile Any" 2>/dev/null
# Start the service # Start the service
echo "Starting service..." echo "Starting service..."
@@ -69,8 +69,8 @@ if [[ $? -eq 0 ]]; then
echo "=== Installation Complete ===" echo "=== Installation Complete ==="
echo "Service Name: $SERVICE_NAME" echo "Service Name: $SERVICE_NAME"
echo "Installation Path: $INSTALL_PATH" echo "Installation Path: $INSTALL_PATH"
echo "Service URL: http://localhost:5000" echo "Service URL: http://localhost:2414"
echo "API Health Check: http://localhost:5000/api/health" echo "API Health Check: http://localhost:2414/api/health"
echo echo
echo "The service is now running and will start automatically with Windows." echo "The service is now running and will start automatically with Windows."
echo "You can manage it through Services.msc or using sc commands:" echo "You can manage it through Services.msc or using sc commands:"
-8
View File
@@ -1,8 +0,0 @@
# PowerShell script to create a new inbound rule in Windows Firewall
$port = 5000
$ruleName = "ResourceMonitorServicePublish"
if (Get-NetFirewallRule -DisplayName $ruleName) {
Write-Host "Rule already exists, not creating a new one"
} else {
New-NetFirewallRule -DisplayName $ruleName -Direction Inbound -Protocol TCP -LocalPort $port -Action Allow -Profile Any
}
-4
View File
@@ -1,4 +0,0 @@
sc create ResourceMonitorService binPath="%~dp0ResourceMonitorService.exe --windows-service" start= auto
sc description ResourceMonitorService "A service that monitors system resource usage and exposes it via a web API."
@@ -1,8 +0,0 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.Hosting.Lifetime": "Information"
}
}
}
-19
View File
@@ -1,19 +0,0 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.Hosting.Lifetime": "Information"
}
},
"RunAsWindowsService": true,
"Kestrel": {
"Endpoints": {
"Http": {
"Url": "http://*:5000"
}
}
},
"ApiSettings": {
"ApiKey": "b7f3e8a1-4c2d-4d9f-9a6e-2a1c5d7f8e9a"
}
}
@@ -1,8 +0,0 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.Hosting.Lifetime": "Information"
}
}
}
-19
View File
@@ -1,19 +0,0 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.Hosting.Lifetime": "Information"
}
},
"RunAsWindowsService": true,
"Kestrel": {
"Endpoints": {
"Http": {
"Url": "http://*:5000"
}
}
},
"ApiSettings": {
"ApiKey": "b7f3e8a1-4c2d-4d9f-9a6e-2a1c5d7f8e9a"
}
}
@@ -1,8 +0,0 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.Hosting.Lifetime": "Information"
}
}
}
@@ -1,19 +0,0 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.Hosting.Lifetime": "Information"
}
},
"RunAsWindowsService": true,
"Kestrel": {
"Endpoints": {
"Http": {
"Url": "http://*:5000"
}
}
},
"ApiSettings": {
"ApiKey": "b7f3e8a1-4c2d-4d9f-9a6e-2a1c5d7f8e9a"
}
}