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.

StatusController.cs 5.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. using System.Linq;
  5. using System.Threading;
  6. using System.Web;
  7. using System.Web.Mvc;
  8. using Teknik.Areas.Status.ViewModels;
  9. using Teknik.Attributes;
  10. using Teknik.Controllers;
  11. using Teknik.Filters;
  12. using Teknik.Models;
  13. using Teknik.Utilities;
  14. namespace Teknik.Areas.Status.Controllers
  15. {
  16. [TeknikAuthorize]
  17. public class StatusController : DefaultController
  18. {
  19. private TeknikEntities db = new TeknikEntities();
  20. [TrackPageView]
  21. [AllowAnonymous]
  22. public ActionResult Index()
  23. {
  24. ViewBag.Title = "Status Information - " + Config.Title;
  25. ViewBag.Description = "Current status information for the server and resources.";
  26. StatusViewModel model = new StatusViewModel();
  27. // Load initial status info
  28. return View(model);
  29. }
  30. [HttpGet]
  31. [AllowAnonymous]
  32. public ActionResult GetUsage()
  33. {
  34. try
  35. {
  36. float totalCPUValue = 0;
  37. float webCPUValue = 0;
  38. float dbCPUValue = 0;
  39. float totalMem = 0;
  40. float totalAvailMemValue = 0;
  41. float totalUsedMemValue = 0;
  42. float webMemValue = 0;
  43. float dbMemValue = 0;
  44. float bytesSent = 0;
  45. float bytesReceived = 0;
  46. // CPU
  47. using (PerformanceCounter totalCPU = new PerformanceCounter("Processor", "% Processor Time", "_Total", true))
  48. using (PerformanceCounter webCPU = new PerformanceCounter("Process", "% Processor Time", Process.GetCurrentProcess().ProcessName, true))
  49. using (PerformanceCounter dbCPU = new PerformanceCounter("Process", "% Processor Time", Config.StatusConfig.DatabaseProcessName, true))
  50. // Memory
  51. using (PerformanceCounter totalAvailMem = new PerformanceCounter("Memory", "Available Bytes", true))
  52. using (PerformanceCounter webMem = new PerformanceCounter("Process", "Private Bytes", Process.GetCurrentProcess().ProcessName, true))
  53. using (PerformanceCounter dbMem = new PerformanceCounter("Process", "Private Bytes", Config.StatusConfig.DatabaseProcessName, true))
  54. // Network
  55. using (PerformanceCounter sentPerf = new PerformanceCounter("Network Interface", "Bytes Sent/sec", Config.StatusConfig.NetworkInterface, true))
  56. using (PerformanceCounter receivedPerf = new PerformanceCounter("Network Interface", "Bytes Received/sec", Config.StatusConfig.NetworkInterface, true))
  57. {
  58. // CPU Sample
  59. totalCPU.NextValue();
  60. if (Config.StatusConfig.ShowWebStatus)
  61. {
  62. webCPU.NextValue();
  63. }
  64. if (Config.StatusConfig.ShowDatabaseStatus)
  65. {
  66. dbCPU.NextValue();
  67. }
  68. // Network Sample
  69. sentPerf.NextValue();
  70. receivedPerf.NextValue();
  71. // Wait the sample time
  72. Thread.Sleep(1000);
  73. // CPU Values
  74. totalCPUValue = totalCPU.NextValue();
  75. if (Config.StatusConfig.ShowWebStatus)
  76. {
  77. webCPUValue = webCPU.NextValue();
  78. }
  79. if (Config.StatusConfig.ShowDatabaseStatus)
  80. {
  81. dbCPUValue = dbCPU.NextValue();
  82. }
  83. // Memory Values
  84. totalMem = Config.StatusConfig.TotalMemory;
  85. totalAvailMemValue = totalAvailMem.NextValue();
  86. totalUsedMemValue = totalMem - totalAvailMemValue;
  87. if (Config.StatusConfig.ShowWebStatus)
  88. {
  89. webMemValue = webMem.NextValue();
  90. }
  91. if (Config.StatusConfig.ShowDatabaseStatus)
  92. {
  93. dbMemValue = dbMem.NextValue();
  94. }
  95. // Network Values
  96. bytesSent = sentPerf.NextValue();
  97. bytesReceived = receivedPerf.NextValue();
  98. // Return usage info
  99. return Json(new { result = new {
  100. cpu = new { total = totalCPUValue, web = webCPUValue, db = dbCPUValue },
  101. memory = new { total = totalMem, totalAvail = totalAvailMemValue, totalUsed = totalUsedMemValue, webUsed = webMemValue, dbUsed = dbMemValue },
  102. network = new { sent = bytesSent, received = bytesReceived }
  103. } }, JsonRequestBehavior.AllowGet);
  104. }
  105. }
  106. catch (Exception ex)
  107. {
  108. return Json(new { error = new { message = ex.GetFullMessage(true) } }, JsonRequestBehavior.AllowGet);
  109. }
  110. }
  111. }
  112. }