PageRenderTime 51ms CodeModel.GetById 23ms RepoModel.GetById 1ms app.codeStats 0ms

/trunk/App_Code/Configuration.cs

#
C# | 183 lines | 151 code | 25 blank | 7 comment | 7 complexity | 059addeed9015da34367edc21ef86731 MD5 | raw file
  1. using System;
  2. using System.Data;
  3. using System.Configuration;
  4. using System.Web;
  5. using System.Web.Security;
  6. using System.Web.UI;
  7. using System.Web.UI.HtmlControls;
  8. using System.Web.UI.WebControls;
  9. using System.Web.UI.WebControls.WebParts;
  10. using System.Xml;
  11. using System.Collections.Generic;
  12. using System.ServiceProcess;
  13. using System.Diagnostics;
  14. using System.IO;
  15. /// <summary>
  16. /// Summary description for Configuration
  17. /// </summary>
  18. public class Configuration
  19. {
  20. private ServiceController[] _services;
  21. private string[] _eventLogs;
  22. private DiskInfo[] _disks;
  23. private ProcessInfo[] _processes;
  24. private bool _showEventLogsOnDashboard;
  25. static private Configuration _instace;
  26. static private string _fileName = HttpContext.Current.Server.MapPath("~/dashboard.config");
  27. static public Configuration Instance
  28. {
  29. get
  30. {
  31. if (_instace == null)
  32. _instace = new Configuration();
  33. return _instace;
  34. }
  35. }
  36. public ServiceController[] Services
  37. {
  38. get { return _services; }
  39. }
  40. public string[] EventLogs
  41. {
  42. get { return _eventLogs; }
  43. }
  44. public DiskInfo[] Disks
  45. {
  46. get { return _disks; }
  47. }
  48. public ProcessInfo[] Processes
  49. {
  50. get { return _processes; }
  51. }
  52. public bool ShowEventLogsOnDashboard
  53. {
  54. get { return _showEventLogsOnDashboard; }
  55. }
  56. private Configuration()
  57. {
  58. XmlDocument doc = new XmlDocument();
  59. doc.Load(_fileName);
  60. //services
  61. RefreshServices(doc);
  62. //eventlogs
  63. RefreshEventLogs(doc);
  64. //disks
  65. RefreshDisks(doc);
  66. //processes
  67. RefreshProcesses(doc);
  68. }
  69. private void RefreshProcesses(XmlDocument doc)
  70. {
  71. XmlNodeList nodes = doc.SelectNodes("//processes/process");
  72. _processes = new ProcessInfo[nodes.Count];
  73. int i = 0;
  74. foreach (XmlNode node in nodes)
  75. {
  76. string name=node.Attributes["name"].Value;
  77. switch (node.Attributes["type"].Value)
  78. {
  79. case "Run":
  80. _processes[i] = new ProcessInfo(name, ProcessMonitoringType.Run);
  81. break;
  82. case "NotRun":
  83. _processes[i] = new ProcessInfo(name, ProcessMonitoringType.NotRun);
  84. break;
  85. case "MinMemory":
  86. _processes[i] = new ProcessInfo(name, ProcessMonitoringType.MinMemory,Int64.Parse(node.Attributes["memory"].Value));
  87. break;
  88. case "MaxMemory":
  89. _processes[i] = new ProcessInfo(name, ProcessMonitoringType.MaxMemory, Int64.Parse(node.Attributes["memory"].Value));
  90. break;
  91. }
  92. i++;
  93. }
  94. }
  95. private void RefreshDisks(XmlDocument doc)
  96. {
  97. XmlNodeList nodes = doc.SelectNodes("//disks/disk");
  98. _disks = new DiskInfo[nodes.Count];
  99. int i = 0;
  100. foreach (XmlNode node in nodes)
  101. {
  102. _disks[i] = new DiskInfo(node.Attributes["letter"].Value, Int64.Parse(node.Attributes["warn"].InnerXml), Int64.Parse(node.Attributes["fail"].InnerXml));
  103. i++;
  104. }
  105. }
  106. public void RefreshDisks()
  107. {
  108. XmlDocument doc = new XmlDocument();
  109. doc.Load(_fileName);
  110. RefreshDisks(doc);
  111. }
  112. private void RefreshEventLogs(XmlDocument doc)
  113. {
  114. XmlNode node = doc.SelectSingleNode("//eventlogs/@showOnDashboard");
  115. if ((node.Value == "1") || (node.Value == "true"))
  116. {
  117. this._showEventLogsOnDashboard = true;
  118. }
  119. XmlNodeList nodes = doc.SelectNodes("//eventlogs/eventlog");
  120. _eventLogs = new string[nodes.Count];
  121. int i = 0;
  122. foreach (XmlNode node2 in nodes)
  123. {
  124. _eventLogs[i] = node2.InnerXml;
  125. i++;
  126. }
  127. }
  128. public void RefreshEventLogs()
  129. {
  130. XmlDocument doc = new XmlDocument();
  131. doc.Load(_fileName);
  132. RefreshEventLogs(doc);
  133. }
  134. private void RefreshServices(XmlDocument doc)
  135. {
  136. int i = 0;
  137. XmlNodeList nodes = doc.SelectNodes("//services/service");
  138. _services = new ServiceController[nodes.Count];
  139. i = 0;
  140. foreach (XmlNode node in nodes)
  141. {
  142. _services[i] = new ServiceController(node.InnerXml);
  143. i++;
  144. }
  145. }
  146. public void RefreshServices()
  147. {
  148. XmlDocument doc = new XmlDocument();
  149. doc.Load(_fileName);
  150. RefreshServices(doc);
  151. }
  152. public void RefreshProcesses()
  153. {
  154. XmlDocument doc = new XmlDocument();
  155. doc.Load(_fileName);
  156. RefreshProcesses(doc);
  157. }
  158. }