Advertisement
Guest User

Untitled

a guest
Feb 4th, 2016
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.35 KB | None | 0 0
  1. //log4net declaration
  2. private static readonly ILog log = LogManager.GetLogger(typeof(SFDC_MonitoringTool));
  3.  
  4. //These stuff for registering service state in Event Viewer
  5. public enum ServiceState
  6. {
  7. SERVICE_STOPPED = 0x00000001,
  8. SERVICE_START_PENDING = 0x00000002,
  9. SERVICE_STOP_PENDING = 0x00000003,
  10. SERVICE_RUNNING = 0x00000004,
  11. SERVICE_CONTINUE_PENDING = 0x00000005,
  12. SERVICE_PAUSE_PENDING = 0x00000006,
  13. SERVICE_PAUSED = 0x00000007,
  14. }
  15. [StructLayout(LayoutKind.Sequential)]
  16. public struct ServiceStatus
  17. {
  18. public long dwServiceType;
  19. public ServiceState dwCurrentState;
  20. public long dwControlsAccepted;
  21. public long dwWin32ExitCode;
  22. public long dwServiceSpecificExitCode;
  23. public long dwCheckPoint;
  24. public long dwWaitHint;
  25. };
  26. [DllImport("advapi32.dll", SetLastError = true)]
  27. private static extern bool SetServiceStatus(IntPtr handle, ref ServiceStatus serviceStatus);
  28.  
  29.  
  30. private static void SetBasicTimer()
  31. {
  32. System.Timers.Timer basicTimer = new System.Timers.Timer(4000); //BasicCheck repetition in milliseconds
  33. basicTimer.Elapsed += BasicCheck;
  34. basicTimer.AutoReset = true;
  35. basicTimer.Enabled = true;
  36. }
  37.  
  38. private static void BasicCheck(Object source, ElapsedEventArgs e)
  39. {
  40. //CPU load Check
  41. foreach(var element in Config.globalHosts)
  42. {
  43. var cpuResult = Checks.cpuCheck(element);
  44. if (cpuResult.IsError == true)
  45. {
  46. log.Error("CPU load is [" + cpuResult.Value + "%] on " + cpuResult.MachineName + ".");
  47. //implement mail sender
  48. if (mailAlert.CheckLastSentMail(cpuResult.TypeOfError, cpuResult.MachineName, DateTime.Now) == true)
  49. {
  50. MailSender.SendMail(cpuResult.TypeOfError, cpuResult.MachineName, cpuResult.ErrorMessage);
  51. mailAlert.addToList(cpuResult.TypeOfError, cpuResult.MachineName, DateTime.Now);
  52. }
  53.  
  54. }
  55. else
  56. {
  57. log.Debug("CPU load is [" + cpuResult.Value + "%] below the threshold on " + cpuResult.MachineName + ".");
  58. }
  59. }
  60.  
  61. //Mem Load check
  62. foreach (var element in Config.globalHosts)
  63. {
  64. var memResult = Checks.memCheck(element);
  65. if(memResult.IsError == true)
  66. {
  67. log.Error("Memory load is " + memResult.Value + "%");
  68. //implement mail sender
  69. if (mailAlert.CheckLastSentMail(memResult.TypeOfError, memResult.MachineName, DateTime.Now) == true)
  70. {
  71. MailSender.SendMail(memResult.TypeOfError, memResult.MachineName, memResult.ErrorMessage);
  72. mailAlert.addToList(memResult.TypeOfError, memResult.MachineName, DateTime.Now);
  73. }
  74. }
  75. else
  76. {
  77. log.Debug("Memory load [" + memResult.Value + "%] is below the threshhold on " + memResult.MachineName + ".");
  78. }
  79. }
  80.  
  81.  
  82. //Free space check?
  83. /*
  84.  
  85. foreach (var element in Config.globalHosts)
  86. {
  87. ReturnType driveResult = Checks.freeSpaceCheck(element);
  88.  
  89. if (driveResult.IsError == true)
  90. {
  91. log.Error(driveResult.ErrorMessage);
  92. //implement mail sender
  93. if (mailAlert.CheckLastSentMail(driveResult.TypeOfError, driveResult.MachineName, DateTime.Now) == true)
  94. {
  95. MailSender.SendMail(driveResult.TypeOfError, driveResult.MachineName, driveResult.ErrorMessage);
  96. mailAlert.addToList(driveResult.TypeOfError, driveResult.MachineName, DateTime.Now);
  97. }
  98. }
  99. else
  100. {
  101. log.Debug(element + " check is successful. No error found.");
  102. }
  103. }
  104. */
  105.  
  106. //Ping check
  107. foreach(var element in Config.globalHosts)
  108. {
  109. var pingResult = Checks.pingCheck(element);
  110. if (pingResult.IsError == true)
  111. {
  112. log.Error(pingResult.MachineName + " is not responding within [" + Config.pingTimeout +" sec].");
  113. //implement mail sender
  114. if (mailAlert.CheckLastSentMail(pingResult.TypeOfError, pingResult.MachineName, DateTime.Now) == true)
  115. {
  116. MailSender.SendMail(pingResult.TypeOfError, pingResult.MachineName, pingResult.ErrorMessage);
  117. mailAlert.addToList(pingResult.TypeOfError, pingResult.MachineName, DateTime.Now);
  118. }
  119. }
  120. else
  121. {
  122. log.Debug(pingResult.MachineName + " is responding within the timeout [" + Config.pingTimeout + " sec].");
  123. }
  124. }
  125.  
  126. //DB Connection Check
  127.  
  128. var dbResult = Checks.dbCheck();
  129. if (dbResult.IsError == true)
  130. {
  131. log.Error(dbResult.MachineName + " is not responding to SQL queries. Please check immediately!");
  132.  
  133. if (mailAlert.CheckLastSentMail(dbResult.TypeOfError, dbResult.MachineName, DateTime.Now) == true)
  134. {
  135. MailSender.SendMail(dbResult.TypeOfError, dbResult.MachineName, dbResult.ErrorMessage);
  136. mailAlert.addToList(dbResult.TypeOfError, dbResult.MachineName, DateTime.Now);
  137. }
  138. }
  139. else
  140. {
  141. log.Debug(Config.connectionString + " responding properly to SQL queries.");
  142. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement