ServiceCenterController.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. using System;
  2. using System.Diagnostics;
  3. using System.Linq;
  4. using Newtonsoft.Json.Linq;
  5. using Sers.Core.Module.Rpc;
  6. using Sers.Gover.Base;
  7. using Sers.Hardware.Usage;
  8. using Sers.SersLoader;
  9. using Sers.SersLoader.ApiDesc.Attribute.Valid;
  10. using Vit.Core.Module.Log;
  11. using Vit.Core.Util.ComponentModel.Api;
  12. using Vit.Core.Util.ComponentModel.Data;
  13. using Vit.Core.Util.ComponentModel.Model;
  14. using Vit.Extensions;
  15. namespace Sers.Gover.Controllers.ApiControllers
  16. {
  17. [SsStationName("_gover_")]
  18. public class ServerCenterController : IApiController
  19. {
  20. #region HealthInfo
  21. [SsRoute("serviceCenter/healthInfo")]
  22. [SsCallerSource(ECallerSource.Internal)]
  23. //[CallFromGover]
  24. [SsName("获取服务中心健康数据")]
  25. public ApiReturn<HealthInfoData> HealthInfo()
  26. {
  27. var info = new HealthInfoData();
  28. //(x.1) usageStatus
  29. info.usageStatus = UsageHelp.GetUsageInfo();
  30. //(x.2) Process信息
  31. try
  32. {
  33. var process = Process.GetCurrentProcess();
  34. //(x.x.1) ThreadCount
  35. info.Process.ThreadCount = process.Threads.Count;
  36. //(x.x.2) RunningThreadCount
  37. int n = 0;
  38. foreach (ProcessThread th in process.Threads)
  39. {
  40. if (th.ThreadState == ThreadState.Running)
  41. n++;
  42. }
  43. info.Process.RunningThreadCount = n;
  44. //(x.x.3) WorkingSet
  45. info.Process.WorkingSet = process.WorkingSet64 / 1024.0f / 1024;
  46. }
  47. catch (Exception ex)
  48. {
  49. Logger.Error(ex);
  50. }
  51. return info;
  52. }
  53. public class HealthInfoData
  54. {
  55. public UsageStatus usageStatus;
  56. public ProcessInfo Process = new ProcessInfo();
  57. public class ProcessInfo
  58. {
  59. /// <summary>
  60. /// 总线程数
  61. /// </summary>
  62. public int ThreadCount;
  63. /// <summary>
  64. /// 活动的线程数
  65. /// </summary>
  66. public int RunningThreadCount;
  67. /// <summary>
  68. /// 占用总内存(单位:MB)
  69. /// </summary>
  70. public float WorkingSet;
  71. }
  72. }
  73. #endregion
  74. #region Statistics
  75. [SsRoute("serviceCenter/statistics")]
  76. [SsCallerSource(ECallerSource.Internal)]
  77. //[CallFromGover]
  78. [SsName("获取服务中心统计信息")]
  79. public ApiReturn<StatisticsInfo> Statistics()
  80. {
  81. var info = new StatisticsInfo();
  82. //(x.1) qps
  83. try
  84. {
  85. var qps = GoverApiCenterService.Instance.ApiStation_GetAll().Sum(m => m.qps);
  86. info.qps= qps;
  87. }
  88. catch (Exception ex)
  89. {
  90. Logger.Error(ex);
  91. }
  92. return info;
  93. }
  94. public class StatisticsInfo
  95. {
  96. /// <summary>
  97. /// 总qps
  98. /// </summary>
  99. public float qps;
  100. }
  101. #endregion
  102. }
  103. }