PageRenderTime 25ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/QuickMon4/QuickMonLinuxAgents/Collectors/NIC/LinuxNICCollector.cs

#
C# | 316 lines | 278 code | 23 blank | 15 comment | 31 complexity | f2e34194193952f14a58c6996310a249 MD5 | raw file
  1. using QuickMon.Linux;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.ComponentModel;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Xml;
  8. namespace QuickMon.Collectors
  9. {
  10. [Description("Network IOs Collector"), Category("SSH")]
  11. public class LinuxNICCollector : CollectorAgentBase
  12. {
  13. public LinuxNICCollector()
  14. {
  15. AgentConfig = new LinuxNICCollectorConfig();
  16. }
  17. #region ICollector Members
  18. public override MonitorState RefreshState()
  19. {
  20. MonitorState returnState = new MonitorState();
  21. string lastAction = "";
  22. long highestVal = 0;
  23. int errors = 0;
  24. int warnings = 0;
  25. int success = 0;
  26. try
  27. {
  28. LinuxNICCollectorConfig currentConfig = (LinuxNICCollectorConfig)AgentConfig;
  29. foreach (LinuxNICEntry entry in currentConfig.Entries)
  30. {
  31. MonitorState entryState = new MonitorState()
  32. {
  33. ForAgent = entry.SSHConnection.ComputerName
  34. };
  35. List<NICState> diss = entry.GetStates();
  36. foreach (NICState dis in diss)
  37. {
  38. if (dis.State == CollectorState.Error)
  39. {
  40. errors++;
  41. }
  42. else if (dis.State == CollectorState.Warning)
  43. {
  44. warnings++;
  45. }
  46. else
  47. {
  48. success++;
  49. }
  50. entryState.ChildStates.Add(
  51. new MonitorState()
  52. {
  53. ForAgent = dis.NICInfo.Name,
  54. State = dis.State,
  55. CurrentValue = dis.NICInfo.RTxBytes,
  56. CurrentValueUnit = "Bytes"
  57. }
  58. );
  59. if (highestVal < dis.NICInfo.RTxBytes)
  60. highestVal = dis.NICInfo.RTxBytes;
  61. }
  62. returnState.ChildStates.Add(entryState);
  63. }
  64. returnState.CurrentValue = highestVal;
  65. returnState.CurrentValueUnit = "Bytes (highest)";
  66. if (errors > 0 && warnings == 0 && success == 0) // any errors
  67. returnState.State = CollectorState.Error;
  68. else if (errors > 0 || warnings > 0) //any warnings
  69. returnState.State = CollectorState.Warning;
  70. else
  71. returnState.State = CollectorState.Good;
  72. }
  73. catch (Exception ex)
  74. {
  75. returnState.RawDetails = ex.Message;
  76. returnState.HtmlDetails = string.Format("<p><b>Last action:</b> {0}</p><blockquote>{1}</blockquote>", lastAction, ex.Message);
  77. returnState.State = CollectorState.Error;
  78. }
  79. return returnState;
  80. }
  81. public override List<System.Data.DataTable> GetDetailDataTables()
  82. {
  83. List<System.Data.DataTable> tables = new List<System.Data.DataTable>();
  84. System.Data.DataTable dt = new System.Data.DataTable();
  85. try
  86. {
  87. dt.Columns.Add(new System.Data.DataColumn("Computer", typeof(string)));
  88. dt.Columns.Add(new System.Data.DataColumn("File System", typeof(string)));
  89. dt.Columns.Add(new System.Data.DataColumn("Bytes/Sec", typeof(double)));
  90. LinuxNICCollectorConfig currentConfig = (LinuxNICCollectorConfig)AgentConfig;
  91. foreach (LinuxNICEntry entry in currentConfig.Entries)
  92. {
  93. foreach (NICState diInfo in entry.GetNICInfos())
  94. {
  95. dt.Rows.Add(entry.SSHConnection.ComputerName, diInfo.NICInfo.Name, diInfo.NICInfo.RTxBytes);
  96. }
  97. }
  98. }
  99. catch (Exception ex)
  100. {
  101. dt = new System.Data.DataTable("Exception");
  102. dt.Columns.Add(new System.Data.DataColumn("Text", typeof(string)));
  103. dt.Rows.Add(ex.ToString());
  104. }
  105. tables.Add(dt);
  106. return tables;
  107. }
  108. #endregion
  109. }
  110. public class LinuxNICCollectorConfig : ICollectorConfig
  111. {
  112. public LinuxNICCollectorConfig()
  113. {
  114. Entries = new List<ICollectorConfigEntry>();
  115. }
  116. #region ICollectorConfig Members
  117. public bool SingleEntryOnly { get { return false; } }
  118. public List<ICollectorConfigEntry> Entries { get; set; }
  119. #endregion
  120. #region IAgentConfig Members
  121. public void FromXml(string configurationString)
  122. {
  123. if (configurationString == null || configurationString.Length == 0)
  124. return;
  125. XmlDocument config = new XmlDocument();
  126. config.LoadXml(configurationString);
  127. XmlElement root = config.DocumentElement;
  128. Entries.Clear();
  129. foreach (XmlElement pcNode in root.SelectNodes("linux/nics"))
  130. {
  131. LinuxNICEntry entry = new LinuxNICEntry();
  132. entry.SSHConnection = SSHConnectionDetails.FromXmlElement(pcNode);
  133. //entry.SSHConnection.SSHSecurityOption = SSHSecurityOptionTypeConverter.FromString(pcNode.ReadXmlElementAttr("sshSecOpt", "password"));
  134. //entry.SSHConnection.ComputerName = pcNode.ReadXmlElementAttr("machine", ".");
  135. //entry.SSHConnection.SSHPort = pcNode.ReadXmlElementAttr("sshPort", 22);
  136. //entry.SSHConnection.UserName = pcNode.ReadXmlElementAttr("userName", "");
  137. //entry.SSHConnection.Password = pcNode.ReadXmlElementAttr("password", "");
  138. //entry.SSHConnection.PrivateKeyFile = pcNode.ReadXmlElementAttr("privateKeyFile", "");
  139. //entry.SSHConnection.PassPhrase = pcNode.ReadXmlElementAttr("passPhrase", "");
  140. entry.SubItems = new List<ICollectorConfigSubEntry>();
  141. foreach (XmlElement fileSystemNode in pcNode.SelectNodes("nic"))
  142. {
  143. LinuxNICSubEntry fse = new LinuxNICSubEntry();
  144. fse.NICName = fileSystemNode.ReadXmlElementAttr("name", "");
  145. fse.WarningValueKB = fileSystemNode.ReadXmlElementAttr("warningValueKB", 1024);
  146. fse.ErrorValueKB = fileSystemNode.ReadXmlElementAttr("errorValueKB", 5120);
  147. entry.SubItems.Add(fse);
  148. }
  149. Entries.Add(entry);
  150. }
  151. }
  152. public string ToXml()
  153. {
  154. XmlDocument config = new XmlDocument();
  155. config.LoadXml(GetDefaultOrEmptyXml());
  156. XmlElement root = config.DocumentElement;
  157. XmlNode linuxDiskSpaceNode = root.SelectSingleNode("linux");
  158. linuxDiskSpaceNode.InnerXml = "";
  159. foreach (LinuxNICEntry entry in Entries)
  160. {
  161. XmlElement nicsNode = config.CreateElement("nics");
  162. entry.SSHConnection.SaveToXmlElementAttr(nicsNode);
  163. //nicsNode.SetAttributeValue("sshSecOpt", entry.SSHConnection.SSHSecurityOption.ToString());
  164. //nicsNode.SetAttributeValue("machine", entry.SSHConnection.ComputerName);
  165. //nicsNode.SetAttributeValue("sshPort", entry.SSHConnection.SSHPort);
  166. //nicsNode.SetAttributeValue("userName", entry.SSHConnection.UserName);
  167. //nicsNode.SetAttributeValue("password", entry.SSHConnection.Password);
  168. //nicsNode.SetAttributeValue("privateKeyFile", entry.SSHConnection.PrivateKeyFile);
  169. //nicsNode.SetAttributeValue("passPhrase", entry.SSHConnection.PassPhrase);
  170. foreach (LinuxNICSubEntry fse in entry.SubItems)
  171. {
  172. XmlElement fileSystemNode = config.CreateElement("nic");
  173. fileSystemNode.SetAttributeValue("name", fse.NICName);
  174. fileSystemNode.SetAttributeValue("warningValueKB", fse.WarningValueKB);
  175. fileSystemNode.SetAttributeValue("errorValueKB", fse.ErrorValueKB);
  176. nicsNode.AppendChild(fileSystemNode);
  177. }
  178. linuxDiskSpaceNode.AppendChild(nicsNode);
  179. }
  180. return config.OuterXml;
  181. }
  182. public string GetDefaultOrEmptyXml()
  183. {
  184. return "<config>\r\n<linux>\r\n</linux>\r\n</config>";
  185. }
  186. public string ConfigSummary
  187. {
  188. get
  189. {
  190. StringBuilder sb = new StringBuilder();
  191. sb.Append(string.Format("{0} entry(s): ", Entries.Count));
  192. if (Entries.Count == 0)
  193. sb.Append("None");
  194. else
  195. {
  196. foreach (ICollectorConfigEntry entry in Entries)
  197. {
  198. sb.Append(entry.Description + ", ");
  199. }
  200. }
  201. return sb.ToString().TrimEnd(' ', ',');
  202. }
  203. }
  204. #endregion
  205. }
  206. public class LinuxNICEntry : LinuxBaseEntry
  207. {
  208. public List<NICState> GetNICInfos()
  209. {
  210. List<NICState> nicEntries = new List<NICState>();
  211. Renci.SshNet.SshClient sshClient = SSHConnection.GetConnection();
  212. //First see if ANY subentry is for all
  213. bool addAll = (from LinuxNICSubEntry d in SubItems
  214. where d.NICName == "*"
  215. select d).Count() > 0;
  216. if (addAll)
  217. {
  218. LinuxNICSubEntry alertDef = (from LinuxNICSubEntry d in SubItems
  219. where d.NICName == "*"
  220. select d).FirstOrDefault();
  221. foreach (Linux.NicInfo ni in NicInfo.GetCurrentNicStats(sshClient, 250))
  222. {
  223. NICState nis = new NICState() { NICInfo = ni, State = CollectorState.NotAvailable, AlertDefinition = alertDef };
  224. nicEntries.Add(nis);
  225. }
  226. }
  227. else
  228. {
  229. foreach (Linux.NicInfo di in NicInfo.GetCurrentNicStats(sshClient, 250))
  230. {
  231. LinuxNICSubEntry alertDef = (from LinuxNICSubEntry d in SubItems
  232. where d.NICName.ToLower() == di.Name.ToLower()
  233. select d).FirstOrDefault();
  234. if (alertDef != null)
  235. {
  236. if (!nicEntries.Any(f => f.NICInfo.Name.ToLower() == di.Name.ToLower()))
  237. {
  238. NICState dis = new NICState() { NICInfo = di, State = CollectorState.NotAvailable, AlertDefinition = alertDef };
  239. nicEntries.Add(dis);
  240. }
  241. }
  242. }
  243. SSHConnection.CloseConnection();
  244. }
  245. return nicEntries;
  246. }
  247. public List<NICState> GetStates()
  248. {
  249. List<NICState> states = new List<NICState>();
  250. foreach (NICState dis in GetNICInfos())
  251. {
  252. dis.State = CollectorState.Good;
  253. if (dis.NICInfo.RTxBytesPerSec >= (dis.AlertDefinition.ErrorValueKB * 1024))
  254. {
  255. dis.State = CollectorState.Error;
  256. }
  257. else if (dis.NICInfo.RTxBytesPerSec >= (dis.AlertDefinition.WarningValueKB * 1024))
  258. {
  259. dis.State = CollectorState.Warning;
  260. }
  261. states.Add(dis);
  262. }
  263. return states;
  264. }
  265. public override string TriggerSummary
  266. {
  267. get { return string.Format("{0} Network interface(s)", SubItems.Count); }
  268. }
  269. }
  270. public class LinuxNICSubEntry : ICollectorConfigSubEntry
  271. {
  272. public LinuxNICSubEntry()
  273. {
  274. WarningValueKB = 1024;
  275. ErrorValueKB = 5120;
  276. }
  277. public string NICName { get; set; }
  278. public long WarningValueKB { get; set; }
  279. public long ErrorValueKB { get; set; }
  280. #region ICollectorConfigSubEntry Members
  281. public string Description
  282. {
  283. get { return string.Format("{0} Warn:{1} Err:{2}", NICName, WarningValueKB, ErrorValueKB); }
  284. }
  285. #endregion
  286. }
  287. public class NICState
  288. {
  289. public Linux.NicInfo NICInfo { get; set; }
  290. public LinuxNICSubEntry AlertDefinition { get; set; }
  291. public CollectorState State { get; set; }
  292. }
  293. }