Advertisement
Guest User

Untitled

a guest
Feb 4th, 2016
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.97 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.Diagnostics;
  7. using System.Threading;
  8. using System.Net.NetworkInformation;
  9. using System.Data.SqlClient;
  10. using System.Management;
  11. using log4net;
  12.  
  13. namespace SFDC_MonitoringTool
  14. {
  15.  
  16. class Checks
  17. {
  18. private static readonly ILog log = LogManager.GetLogger(typeof(Checks));
  19.  
  20. //CPU load check
  21. public static ReturnType cpuCheck(String passedMachineName)
  22. {
  23. ReturnType instance = new ReturnType();
  24. instance.IsError = false;
  25. instance.MachineName = passedMachineName;
  26. instance.TypeOfError = "CPULoadHigh";
  27. var cpuload = new PerformanceCounter("Processor", "% Processor Time", "_Total", passedMachineName);
  28. //Calculating the avarage of 3 measures. First is always 0 so it's left out
  29. var first = cpuload.NextValue(); Thread.Sleep(200);
  30. var second = cpuload.NextValue(); Thread.Sleep(200);
  31. var third = cpuload.NextValue(); Thread.Sleep(200);
  32. var fourth = cpuload.NextValue();
  33.  
  34. var cpuLoadValue = (second + third + fourth) / 3; //the avarage of 3 of them
  35. int cpuLoadInteger = (int)Math.Ceiling(cpuLoadValue);
  36.  
  37. if (cpuLoadInteger < Config.cpuThreshhold) //for debugging its < 80
  38. {
  39. instance.IsError = false;
  40. instance.Value = (int)Math.Ceiling(cpuLoadValue);
  41. return instance;
  42. }
  43. else
  44. {
  45. instance.IsError = true;
  46. instance.ErrorMessage = "CPU load is " + (int)Math.Ceiling(cpuLoadValue) + "% on " + instance.MachineName;
  47. return instance;
  48. }
  49.  
  50. }
  51.  
  52. //MEM load check
  53. public static ReturnType memCheck(String passedMachineName)
  54. {
  55. ReturnType instance = new ReturnType();
  56. instance.IsError = false;
  57. instance.MachineName = passedMachineName;
  58. instance.TypeOfError = "MEMLoadHigh";
  59. var ramCounter = new PerformanceCounter("Memory", "% Committed Bytes In Use", String.Empty, passedMachineName);
  60.  
  61. //Calculate avarage load
  62. var first = Convert.ToInt32(ramCounter.NextValue()); Thread.Sleep(200);
  63. var second = Convert.ToInt32(ramCounter.NextValue()); Thread.Sleep(200);
  64. var third = Convert.ToInt32(ramCounter.NextValue());
  65.  
  66. int percentage = (first + second + third) / 3; //the avarage of 3 of them
  67.  
  68. if (percentage < Config.memThreshhold)
  69. {
  70. instance.IsError = false;
  71. instance.Value = percentage;
  72. return instance;
  73. }
  74. else
  75. {
  76. instance.IsError = true;
  77. instance.ErrorMessage = "MEM load is " + percentage + "% on " + passedMachineName;
  78. return instance;
  79. }
  80.  
  81. }
  82.  
  83. //Ping check -> accessebility
  84. public static ReturnType pingCheck(String passedMachineName)
  85. {
  86. ReturnType instance = new ReturnType();
  87.  
  88. instance.IsError = false;
  89. instance.MachineName = passedMachineName;
  90. instance.TypeOfError = "PingCheck";
  91. Ping ping = new Ping();
  92. try
  93. {
  94. ping.Send(passedMachineName, Config.pingTimeout*1000); //convert sec to millisec
  95. instance.IsError = false;
  96. return instance;
  97. }
  98. catch (Exception ex)
  99. {
  100. instance.IsError = true;
  101. instance.ErrorMessage = passedMachineName +" does not respond within "+ Config.pingTimeout + "sec!!! Errortrace: " + ex.Message;
  102. return instance;
  103. }
  104.  
  105. }
  106.  
  107. //DB check
  108. public static ReturnType dbCheck()
  109. {
  110. ReturnType instance = new ReturnType();
  111. instance.IsError = false;
  112. instance.TypeOfError = "DBerror";
  113. SqlConnection connection = new SqlConnection(Config.connectionString);
  114. try
  115. {
  116. connection.Open();
  117. SqlCommand cmd = new SqlCommand("SELECT 1",connection);
  118. cmd.CommandTimeout = Config.dbTimeout;
  119. cmd.ExecuteNonQuery();
  120. connection.Close();
  121. //Successful
  122. instance.IsError = false;
  123. return instance;
  124. }
  125. catch (Exception ex)
  126. {
  127. //ERROR
  128. instance.IsError = true;
  129. instance.ErrorMessage = Config.connectionString + " does not respond within " + Config.dbTimeout + "sec. Errortrace: " + ex.Message;
  130. return instance;
  131. }
  132. }
  133.  
  134.  
  135.  
  136.  
  137. }
  138. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement