SystemMonitor.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. using dodohold.core;
  2. using Microsoft.Extensions.Logging;
  3. using System.Diagnostics;
  4. using System.Text;
  5. namespace molilian.core
  6. {
  7. public static class SystemMonitor
  8. {
  9. private static Timer _monitorTimer;
  10. public static void StartMonitoring(int intervalSeconds = 30)
  11. {
  12. _monitorTimer = new Timer(
  13. CollectMetrics,
  14. null,
  15. TimeSpan.Zero,
  16. TimeSpan.FromSeconds(intervalSeconds)
  17. );
  18. }
  19. private static void CollectMetrics(object state)
  20. {
  21. try
  22. {
  23. var metrics = new StringBuilder();
  24. // 线程池信息
  25. ThreadPool.GetMaxThreads(out int maxWorkerThreads, out int maxIoThreads);
  26. ThreadPool.GetAvailableThreads(out int availWorkerThreads, out int availIoThreads);
  27. ThreadPool.GetMinThreads(out int minWorkerThreads, out int minIoThreads);
  28. var usedWorkerThreads = maxWorkerThreads - availWorkerThreads;
  29. var usedIoThreads = maxIoThreads - availIoThreads;
  30. metrics.AppendLine("=== Thread Pool Stats ===");
  31. metrics.AppendLine($"Worker Threads: {usedWorkerThreads}/{maxWorkerThreads} (used/max)");
  32. metrics.AppendLine($"IO Threads: {usedIoThreads}/{maxIoThreads} (used/max)");
  33. metrics.AppendLine($"Min Worker Threads: {minWorkerThreads}");
  34. metrics.AppendLine($"Min IO Threads: {minIoThreads}");
  35. metrics.AppendLine($"Thread Pool Queue Length: {ThreadPool.PendingWorkItemCount}");
  36. metrics.AppendLine();
  37. // 内存信息
  38. var process = Process.GetCurrentProcess();
  39. var gcInfo = GC.GetGCMemoryInfo();
  40. metrics.AppendLine("=== Memory Stats ===");
  41. metrics.AppendLine($"Working Set: {process.WorkingSet64 / 1024 / 1024} MB");
  42. metrics.AppendLine($"Private Memory: {process.PrivateMemorySize64 / 1024 / 1024} MB");
  43. metrics.AppendLine($"GC Heap Size: {GC.GetTotalMemory(false) / 1024 / 1024} MB");
  44. metrics.AppendLine($"Gen 0 Collections: {GC.CollectionCount(0)}");
  45. metrics.AppendLine($"Gen 1 Collections: {GC.CollectionCount(1)}");
  46. metrics.AppendLine($"Gen 2 Collections: {GC.CollectionCount(2)}");
  47. metrics.AppendLine($"Memory Load: {gcInfo.MemoryLoadBytes / 1024 / 1024} MB");
  48. // 记录日志
  49. _ = new LoggerLibrary("debug", "metrics").Info(metrics.ToString()).SaveAsync();
  50. // 检查是否需要报警
  51. CheckAlertConditions(
  52. usedWorkerThreads, maxWorkerThreads,
  53. process.WorkingSet64,
  54. ThreadPool.PendingWorkItemCount
  55. );
  56. }
  57. catch (Exception ex)
  58. {
  59. _ = new LoggerLibrary("debug", "metrics_error")
  60. .Info($"Error collecting system metrics: {ex}").SaveAsync();
  61. }
  62. }
  63. private static void CheckAlertConditions(
  64. int usedWorkerThreads,
  65. int maxWorkerThreads,
  66. long workingSet,
  67. long pendingWork)
  68. {
  69. var alerts = new List<string>();
  70. // 线程池使用率超过80%
  71. if ((double)usedWorkerThreads / maxWorkerThreads > 0.8)
  72. {
  73. alerts.Add($"High thread pool usage: {usedWorkerThreads}/{maxWorkerThreads}");
  74. }
  75. // 内存使用超过2GB
  76. if (workingSet > 2L * 1024 * 1024 * 1024)
  77. {
  78. alerts.Add($"High memory usage: {workingSet / 1024 / 1024} MB");
  79. }
  80. // 线程池队列堆积
  81. if (pendingWork > 100)
  82. {
  83. alerts.Add($"High thread pool queue length: {pendingWork}");
  84. }
  85. if (alerts.Any())
  86. {
  87. NotifyCore.Notify($"System alerts:\n{string.Join("\n", alerts)}");
  88. }
  89. }
  90. public static void StopMonitoring()
  91. {
  92. _monitorTimer?.Dispose();
  93. }
  94. }
  95. }