/QuickMon3/QuickMonCommonAgents/Collectors/WMIQueryCollector/WMIQueryEntry.cs

# · C# · 259 lines · 247 code · 8 blank · 4 comment · 82 complexity · ed8691c9e03645f76a9acd0dcbe5d83d MD5 · raw file

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Data;
  4. using System.Linq;
  5. using System.Management;
  6. using System.Text;
  7. namespace QuickMon.Collectors
  8. {
  9. public class WMIQueryEntry : ICollectorConfigEntry
  10. {
  11. #region ICollectorConfigEntry Members
  12. public string Description
  13. {
  14. get { return string.Format("{0}: {1}\\{2} - {3} -> S:{4},W:{5},E:{6}", Name, Machinename, Namespace, StateQuery, SuccessValue, WarningValue, ErrorValue); }
  15. }
  16. public string TriggerSummary
  17. {
  18. get
  19. {
  20. return string.Format("Success: {0}, Warn: {1}, Err: {2}", SuccessValue, WarningValue, ErrorValue);
  21. }
  22. }
  23. public List<ICollectorConfigSubEntry> SubItems { get; set; }
  24. #endregion
  25. #region Properties
  26. public string Name { get; set; }
  27. public string Namespace { get; set; }
  28. public string Machinename { get; set; }
  29. public string StateQuery { get; set; }
  30. public bool ReturnValueIsInt { get; set; }
  31. public bool ReturnValueInverted { get; set; }
  32. public string WarningValue { get; set; }
  33. public string ErrorValue { get; set; }
  34. public string SuccessValue { get; set; }
  35. public bool UseRowCountAsValue { get; set; }
  36. public string DetailQuery { get; set; }
  37. public List<string> ColumnNames { get; set; }
  38. #endregion
  39. internal object RunQuery()
  40. {
  41. object value = null;
  42. if (!ReturnValueIsInt)
  43. {
  44. value = RunQueryWithSingleResult();
  45. }
  46. else
  47. {
  48. if (UseRowCountAsValue)
  49. {
  50. value = RunQueryWithCountResult();
  51. }
  52. else
  53. {
  54. value = RunQueryWithSingleResult();
  55. }
  56. }
  57. return value;
  58. }
  59. internal CollectorState GetState(object value)
  60. {
  61. CollectorState currentState = CollectorState.Good;
  62. if (value == null)
  63. {
  64. if (ErrorValue == "[null]")
  65. currentState = CollectorState.Error;
  66. else if (WarningValue == "[null]")
  67. currentState = CollectorState.Warning;
  68. }
  69. else //non null value
  70. {
  71. if (!ReturnValueIsInt)
  72. {
  73. if (value.ToString() == ErrorValue)
  74. currentState = CollectorState.Error;
  75. else if (value.ToString() == WarningValue)
  76. currentState = CollectorState.Warning;
  77. else if (value.ToString() == SuccessValue || SuccessValue == "[any]")
  78. currentState = CollectorState.Good; //just to flag condition
  79. else if (WarningValue == "[any]")
  80. currentState = CollectorState.Warning;
  81. else if (ErrorValue == "[any]")
  82. currentState = CollectorState.Error;
  83. }
  84. else //now we know the value is not null and must be in a range
  85. {
  86. if (!value.IsIntegerTypeNumber()) //value must be a number!
  87. {
  88. currentState = CollectorState.Error;
  89. }
  90. else if (ErrorValue != "[any]" && ErrorValue != "[null]" &&
  91. (
  92. (!ReturnValueInverted && decimal.Parse(value.ToString()) >= decimal.Parse(ErrorValue)) ||
  93. (ReturnValueInverted && decimal.Parse(value.ToString()) <= decimal.Parse(ErrorValue))
  94. )
  95. )
  96. {
  97. currentState = CollectorState.Error;
  98. }
  99. else if (WarningValue != "[any]" && WarningValue != "[null]" &&
  100. (
  101. (!ReturnValueInverted && decimal.Parse(value.ToString()) >= decimal.Parse(WarningValue)) ||
  102. (ReturnValueInverted && decimal.Parse(value.ToString()) <= decimal.Parse(WarningValue))
  103. )
  104. )
  105. {
  106. currentState = CollectorState.Warning;
  107. }
  108. }
  109. }
  110. return currentState;
  111. }
  112. internal decimal RunQueryWithCountResult()
  113. {
  114. decimal result = 0;
  115. ManagementScope managementScope = new ManagementScope(new ManagementPath(Namespace) { Server = Machinename });
  116. using (ManagementObjectSearcher searcherInstance = new ManagementObjectSearcher(managementScope, new WqlObjectQuery(StateQuery), null))
  117. {
  118. if (searcherInstance != null)
  119. {
  120. using (ManagementObjectCollection results = searcherInstance.Get())
  121. {
  122. result += results.Count;
  123. }
  124. }
  125. }
  126. return result;
  127. }
  128. internal object RunQueryWithSingleResult()
  129. {
  130. ManagementScope managementScope = new ManagementScope(new ManagementPath(Namespace) { Server = Machinename });
  131. using (ManagementObjectSearcher searcherInstance = new ManagementObjectSearcher(managementScope, new WqlObjectQuery(StateQuery), null))
  132. {
  133. if (searcherInstance != null)
  134. {
  135. using (ManagementObjectCollection results = searcherInstance.Get())
  136. {
  137. int nItems = results.Count;
  138. if (nItems > 0)
  139. {
  140. foreach (ManagementObject objServiceInstance in results)
  141. {
  142. foreach (var prop in objServiceInstance.Properties)
  143. {
  144. return prop.Value; //return first value it encounters
  145. }
  146. }
  147. }
  148. }
  149. }
  150. }
  151. return null;
  152. }
  153. internal List<DataColumn> GetDetailQueryColumns()
  154. {
  155. List<DataColumn> columns = new List<DataColumn>();
  156. //columns.Add(new DataColumn("Machine", typeof(string)));
  157. //string firstMachineName = Machinename;
  158. ManagementScope managementScope = new ManagementScope(new ManagementPath(Namespace) { Server = Machinename });
  159. using (ManagementObjectSearcher searcherInstance = new ManagementObjectSearcher(managementScope, new WqlObjectQuery(DetailQuery), null))
  160. {
  161. if (searcherInstance != null)
  162. {
  163. using (ManagementObjectCollection results = searcherInstance.Get())
  164. {
  165. int nItems = results.Count;
  166. if (nItems > 0)
  167. {
  168. foreach (ManagementObject objServiceInstance in results)
  169. {
  170. foreach (var prop in objServiceInstance.Properties)
  171. {
  172. DataColumn newColum = new DataColumn(prop.Name);
  173. string typeStr = prop.Type.ToString().ToLower();
  174. if (typeStr == "string")
  175. newColum.DataType = typeof(string);
  176. else if (typeStr == "uint64")
  177. newColum.DataType = typeof(UInt64);
  178. else if (typeStr == "uint32")
  179. newColum.DataType = typeof(UInt32);
  180. else if (typeStr == "uint16")
  181. newColum.DataType = typeof(UInt16);
  182. else if (typeStr == "sint64")
  183. newColum.DataType = typeof(Int64);
  184. else if (typeStr == "sint32")
  185. newColum.DataType = typeof(Int32);
  186. else if (typeStr == "sint16")
  187. newColum.DataType = typeof(Int16);
  188. else if (typeStr == "boolean")
  189. newColum.DataType = typeof(bool);
  190. else if (typeStr == "datetime")
  191. newColum.DataType = typeof(DateTime);
  192. else
  193. newColum.DataType = typeof(string);
  194. newColum.AllowDBNull = true;
  195. columns.Add(newColum);
  196. }
  197. break;
  198. }
  199. }
  200. }
  201. }
  202. }
  203. return columns;
  204. }
  205. private List<DataRow> GetDetailQueryRows(DataTable dtab, string machineName)
  206. {
  207. List<DataRow> rows = new List<DataRow>();
  208. ManagementScope managementScope = new ManagementScope(new ManagementPath(Namespace) { Server = machineName });
  209. using (ManagementObjectSearcher searcherInstance = new ManagementObjectSearcher(managementScope, new WqlObjectQuery(DetailQuery), null))
  210. {
  211. if (searcherInstance != null)
  212. {
  213. using (ManagementObjectCollection results = searcherInstance.Get())
  214. {
  215. int nItems = results.Count;
  216. if (nItems > 0)
  217. {
  218. foreach (ManagementObject objServiceInstance in results)
  219. {
  220. DataRow row = dtab.NewRow();
  221. //row["Machine"] = machineName;
  222. int fieldIndex = 0;
  223. foreach (var prop in objServiceInstance.Properties)
  224. {
  225. if (prop.Value == null)
  226. row[fieldIndex] = DBNull.Value;
  227. else
  228. row[fieldIndex] = prop.Value;
  229. fieldIndex++;
  230. }
  231. rows.Add(row);
  232. }
  233. }
  234. }
  235. }
  236. }
  237. return rows;
  238. }
  239. internal DataSet RunDetailQuery()
  240. {
  241. DataSet results = new DataSet();
  242. //string firstMachineName = Machinename;
  243. DataTable dtab = new DataTable(Machinename);
  244. dtab.Columns.AddRange(GetDetailQueryColumns().ToArray());
  245. foreach (DataRow row in GetDetailQueryRows(dtab, Machinename))
  246. dtab.Rows.Add(row);
  247. results.Tables.Add(dtab);
  248. return results;
  249. }
  250. }
  251. }