The next generation of the Teknik Services. Written in ASP.NET. https://www.teknik.io/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ServerUsageTicker.cs 6.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. using Microsoft.AspNet.SignalR;
  2. using Microsoft.AspNet.SignalR.Hubs;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Diagnostics;
  6. using System.Linq;
  7. using System.Threading;
  8. using System.Timers;
  9. using System.Web;
  10. using Teknik.Configuration;
  11. using Teknik.Hubs;
  12. using static Teknik.Hubs.ServerUsageHub;
  13. namespace Teknik.SignalR
  14. {
  15. public class ServerUsageTicker
  16. {
  17. // Singleton instance
  18. private readonly static Lazy<ServerUsageTicker> _instance = new Lazy<ServerUsageTicker>(() => new ServerUsageTicker(GlobalHost.ConnectionManager.GetHubContext<ServerUsageHub>().Clients));
  19. private readonly ServerUsage _serverUsage = new ServerUsage();
  20. private readonly object _updateServerUsageLock = new object();
  21. private readonly double _updateInterval = 100;
  22. private readonly System.Timers.Timer _timer;
  23. private volatile bool _updatingServerUpdates = false;
  24. public ServerUsageTicker(IHubConnectionContext<dynamic> clients)
  25. {
  26. Clients = clients;
  27. _timer = new System.Timers.Timer(_updateInterval);
  28. _timer.Elapsed += UpdateServerUsage;
  29. _timer.AutoReset = false;
  30. _timer.Enabled = true;
  31. }
  32. public static ServerUsageTicker Instance
  33. {
  34. get
  35. {
  36. return _instance.Value;
  37. }
  38. }
  39. private IHubConnectionContext<dynamic> Clients
  40. {
  41. get;
  42. set;
  43. }
  44. private void UpdateServerUsage(object source, ElapsedEventArgs e)
  45. {
  46. lock (_updateServerUsageLock)
  47. {
  48. if (!_updatingServerUpdates)
  49. {
  50. _updatingServerUpdates = true;
  51. // Update Server Usage
  52. if (TryUpdateServerUsage())
  53. {
  54. BroadcastServerUsage(_serverUsage);
  55. }
  56. _updatingServerUpdates = false;
  57. // Restart the timer
  58. _timer.Enabled = true;
  59. }
  60. }
  61. }
  62. private void BroadcastServerUsage(ServerUsage serverUsage)
  63. {
  64. Clients.All.updateServerUsage(serverUsage);
  65. }
  66. private bool TryUpdateServerUsage()
  67. {
  68. try
  69. {
  70. Config config = Config.Load();
  71. // CPU
  72. PerformanceCounter totalCPU = new PerformanceCounter();
  73. PerformanceCounter webCPU = new PerformanceCounter();
  74. PerformanceCounter dbCPU = new PerformanceCounter();
  75. // Memory
  76. PerformanceCounter totalAvailMem = new PerformanceCounter();
  77. PerformanceCounter webMem = new PerformanceCounter();
  78. PerformanceCounter dbMem = new PerformanceCounter();
  79. // Network
  80. PerformanceCounter sentPerf = new PerformanceCounter();
  81. PerformanceCounter receivedPerf = new PerformanceCounter();
  82. string processName = Process.GetCurrentProcess().ProcessName;
  83. // CPU
  84. totalCPU = new PerformanceCounter("Processor", "% Processor Time", "_Total", true);
  85. webCPU = new PerformanceCounter("Process", "% Processor Time", processName, true);
  86. if (config.StatusConfig.ShowDatabaseStatus)
  87. {
  88. dbCPU = new PerformanceCounter("Process", "% Processor Time", config.StatusConfig.DatabaseProcessName, true);
  89. }
  90. // Memory
  91. totalAvailMem = new PerformanceCounter("Memory", "Available Bytes", true);
  92. webMem = new PerformanceCounter("Process", "Private Bytes", processName, true);
  93. if (config.StatusConfig.ShowDatabaseStatus)
  94. {
  95. dbMem = new PerformanceCounter("Process", "Private Bytes", config.StatusConfig.DatabaseProcessName, true);
  96. }
  97. // Network
  98. if (config.StatusConfig.ShowNetworkStatus)
  99. {
  100. sentPerf = new PerformanceCounter("Network Interface", "Bytes Sent/sec", config.StatusConfig.NetworkInterface, true);
  101. receivedPerf = new PerformanceCounter("Network Interface", "Bytes Received/sec", config.StatusConfig.NetworkInterface, true);
  102. }
  103. // CPU Sample
  104. totalCPU.NextValue();
  105. if (config.StatusConfig.ShowWebStatus)
  106. {
  107. webCPU.NextValue();
  108. }
  109. if (config.StatusConfig.ShowDatabaseStatus)
  110. {
  111. dbCPU.NextValue();
  112. }
  113. // Network Sample
  114. sentPerf.NextValue();
  115. receivedPerf.NextValue();
  116. // Wait the sample time
  117. Thread.Sleep(1000);
  118. // CPU Values
  119. _serverUsage.CPU.Total = totalCPU.NextValue();
  120. if (config.StatusConfig.ShowWebStatus)
  121. {
  122. _serverUsage.CPU.Website = webCPU.NextValue();
  123. }
  124. if (config.StatusConfig.ShowDatabaseStatus)
  125. {
  126. _serverUsage.CPU.Database = dbCPU.NextValue();
  127. }
  128. // Memory Values
  129. _serverUsage.Memory.Total = config.StatusConfig.TotalMemory;
  130. _serverUsage.Memory.Available = totalAvailMem.NextValue();
  131. _serverUsage.Memory.Used = _serverUsage.Memory.Total - _serverUsage.Memory.Available;
  132. if (config.StatusConfig.ShowWebStatus)
  133. {
  134. _serverUsage.Memory.WebsiteUsed = webMem.NextValue();
  135. }
  136. if (config.StatusConfig.ShowDatabaseStatus)
  137. {
  138. _serverUsage.Memory.DatabaseUsed = dbMem.NextValue();
  139. }
  140. // Network Values
  141. if (config.StatusConfig.ShowNetworkStatus)
  142. {
  143. _serverUsage.Network.Sent = sentPerf.NextValue();
  144. _serverUsage.Network.Received = receivedPerf.NextValue();
  145. }
  146. return true;
  147. }
  148. catch (Exception ex)
  149. {
  150. Logging.Logger.WriteEntry(ex);
  151. }
  152. return false;
  153. }
  154. }
  155. }