/csharp/MonitisTop/MonitisTop/Program.cs

https://github.com/monitisexchange/Windows-Monitoring-Scripts · C# · 361 lines · 239 code · 62 blank · 60 comment · 30 complexity · ecd51062e0ca3617328d520bc507e3ab MD5 · raw file

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.IO;
  6. using Microsoft.Http;
  7. using System.Xml.Linq;
  8. namespace MonitisTop
  9. {
  10. class Program
  11. {
  12. // API access
  13. private static string apiKey = GetAPIKey();
  14. private static HttpClient client = new HttpClient("http://www.monitis.com/");
  15. // specify which agent monitor types we can support
  16. private static string[] SupportedAgentTypes = { "int", "ext", "full" };
  17. // specify if we need to show just monitors or process monitors
  18. private static bool useMonitors = false;
  19. private static bool useProcesses = false;
  20. // command line parameters
  21. private static string argCommand = "";
  22. private static string argApiKey = "";
  23. private static string argSecretKey = "";
  24. private static List<string> argAgentTypes = new List<string>();
  25. private static List<string> argProcesses = new List<string>();
  26. private static List<string> argMonitors = new List<string>();
  27. private static Dictionary<string, string> options = new Dictionary<string, string>();
  28. private static Dictionary<string, Agent> agents = new Dictionary<string, Agent>();
  29. private static Dictionary<string, Monitor> monitors = new Dictionary<string, Monitor>();
  30. static void Main(string[] args)
  31. {
  32. if (ParseCommands(args))
  33. ProcessCommands();
  34. }
  35. /// <summary>
  36. /// Collect command line parameters and split them into named arguments
  37. /// </summary>
  38. /// <param name="args"></param>
  39. /// <returns></returns>
  40. private static bool ParseCommands(string[] args)
  41. {
  42. // collect command line parameters and split them into named arguments
  43. foreach (string arg in args)
  44. {
  45. string[] pieces = arg.Split(':');
  46. options[pieces[0]] = pieces.Length > 1 ? pieces[1] : "";
  47. }
  48. // parse command line options
  49. if (options.Count > 0)
  50. {
  51. // check for help
  52. if (options.ContainsKey("/help"))
  53. {
  54. showUsage("Available Options:");
  55. return false;
  56. }
  57. // get the command to execute
  58. if (options.ContainsKey("/cmd"))
  59. argCommand = options["/cmd"];
  60. else
  61. {
  62. showUsage("Invalid or missing command");
  63. return false;
  64. }
  65. switch (argCommand)
  66. {
  67. case "listagents":
  68. // get the agent monitor types to query
  69. if (options.ContainsKey("/type"))
  70. {
  71. // are we show all agents?
  72. if (options["/type"].ToLower() == "all")
  73. argAgentTypes.AddRange(SupportedAgentTypes);
  74. else
  75. argAgentTypes.AddRange(options["/type"].Split(',').ToList());
  76. // validate the agent types entered
  77. foreach (string s in argAgentTypes)
  78. {
  79. if (!SupportedAgentTypes.Contains(s.ToLower()))
  80. {
  81. showUsage("Invalid monitor type speficied");
  82. return false;
  83. }
  84. }
  85. }
  86. else
  87. {
  88. showUsage("Invalid or missing monitor type");
  89. return false;
  90. }
  91. // get the monitors to query
  92. if (options.ContainsKey("/monitors"))
  93. {
  94. useMonitors = true;
  95. if (options["/monitors"] == "")
  96. {
  97. showUsage("Please specify the names of the monitors to query");
  98. return false;
  99. }
  100. else if (options["/monitors"].ToLower() == "all")
  101. {
  102. // Do nothing here. We'll pass an empty list of showMonitors
  103. // which will return the results for ALL monitors
  104. ;
  105. }
  106. else
  107. {
  108. // Add the list of entered monitors to the list of monitors to show
  109. argMonitors.AddRange(options["/monitors"].Split(',').ToList());
  110. }
  111. }
  112. // get the processes to query
  113. if (options.ContainsKey("/processes"))
  114. {
  115. useProcesses = true;
  116. argProcesses.AddRange(options["/processes"].Split(',').ToList());
  117. if (argProcesses.Count == 0)
  118. {
  119. showUsage("Please specify the names of the proceses to query");
  120. return false;
  121. }
  122. }
  123. // make sure there are monitors or processes provided on the commandline
  124. if (argCommand == "listagents" && (!useMonitors && !useProcesses))
  125. {
  126. showUsage("please specify monitors or processes to query");
  127. return false;
  128. }
  129. break;
  130. case "setkeys":
  131. // get the keys from the commandline
  132. if (options.ContainsKey("/apikey"))
  133. argApiKey = options["/apikey"];
  134. else
  135. {
  136. showUsage("Please provide the APIKey value");
  137. return false;
  138. }
  139. if (options.ContainsKey("/secretkey"))
  140. argSecretKey = options["/secretkey"];
  141. else
  142. {
  143. showUsage("Please provide the SecretKey value");
  144. return false;
  145. }
  146. break;
  147. }
  148. // all command line options check out
  149. return true;
  150. }
  151. // no command line options provided at all
  152. showUsage("Missing commandline option");
  153. return false;
  154. }
  155. /// <summary>
  156. /// Process the commands
  157. /// </summary>
  158. private static void ProcessCommands()
  159. {
  160. // process the command
  161. switch (argCommand)
  162. {
  163. case "listagents":
  164. // create the Agent list
  165. AgentList agentList = new AgentList();
  166. if (argAgentTypes.Contains("int"))
  167. ListInternalAgents(agentList);
  168. if (argAgentTypes.Contains("ext"))
  169. ListExternalAgents(agentList);
  170. if (argAgentTypes.Contains("full"))
  171. ListFullpageAgents(agentList);
  172. // Display the results
  173. agentList.DisplayAgents();
  174. break;
  175. case "setkeys":
  176. SetKeys(argApiKey, argSecretKey);
  177. break;
  178. case "getkeys":
  179. Console.WriteLine("APIKey: {0}", GetAPIKey());
  180. Console.WriteLine("SecretKey: {0}", GetSecretKey());
  181. break;
  182. }
  183. }
  184. /// <summary>
  185. /// List agents and collect monitor information
  186. /// </summary>
  187. private static void ListInternalAgents(AgentList agentList)
  188. {
  189. // Retrieve all the agents defined
  190. agentList.GetAgents(apiKey, client);
  191. // Process each Agent
  192. foreach (Agent agent in agentList)
  193. {
  194. if (useMonitors)
  195. agent.GetGlobalMonitors(apiKey, client, argMonitors);
  196. if (useProcesses)
  197. agent.GetProcessMonitors(apiKey, client, argProcesses);
  198. }
  199. }
  200. /// <summary>
  201. /// List the external agent monitors
  202. /// </summary>
  203. private static void ListExternalAgents(AgentList agentList)
  204. {
  205. // Add a dummy agent
  206. Agent dummy = new Agent();
  207. dummy.Id = "9999";
  208. dummy.Name = "EXTERNAL";
  209. agentList.Add(dummy);
  210. dummy.GetExternalMonitors(apiKey, client, argMonitors);
  211. }
  212. /// <summary>
  213. /// List the fullpage monitors
  214. /// </summary>
  215. private static void ListFullpageAgents(AgentList agentList)
  216. {
  217. // Add a dummy agent
  218. Agent dummy = new Agent();
  219. dummy.Id = "9999";
  220. dummy.Name = "FULLPAGE";
  221. agentList.Add(dummy);
  222. dummy.GetFullpageMonitors(apiKey, client, argMonitors);
  223. }
  224. /// <summary>
  225. /// Retrieve the API key from config.xml
  226. /// </summary>
  227. /// <returns></returns>
  228. private static string GetAPIKey()
  229. {
  230. string key = "";
  231. try
  232. {
  233. XElement root = XElement.Load("config.xml");
  234. key = (string)(from el in root.Descendants("APIKey")
  235. select el).First();
  236. return key;
  237. }
  238. catch
  239. {
  240. showUsage("CONFIG.XML not found");
  241. }
  242. return key;
  243. }
  244. /// <summary>
  245. /// Retrieve the Secret key from config.xml
  246. /// </summary>
  247. /// <returns></returns>
  248. private static string GetSecretKey()
  249. {
  250. string key = "";
  251. try
  252. {
  253. XElement root = XElement.Load("config.xml");
  254. key = (string)(from el in root.Descendants("SecretKey")
  255. select el).First();
  256. return key;
  257. }
  258. catch
  259. {
  260. showUsage("CONFIG.XML not found");
  261. }
  262. return key;
  263. }
  264. /// <summary>
  265. /// Set the ASI and Secretkey
  266. /// </summary>
  267. /// <returns></returns>
  268. private static void SetKeys(string apiKey, string secretKey)
  269. {
  270. FileInfo fi = new FileInfo("config.xml");
  271. String s = "<monitis><APIKey>" + apiKey + "</APIKey><SecretKey>" + secretKey + "</SecretKey></monitis>";
  272. TextWriter writer = new StreamWriter("config.xml");
  273. writer.WriteLine(s);
  274. writer.Close();
  275. }
  276. /// <summary>
  277. /// Show Usage
  278. /// </summary>
  279. /// <param name="strError"></param>
  280. private static void showUsage(string strInfo)
  281. {
  282. Console.WriteLine("Error: {0}\n", strInfo);
  283. Console.WriteLine("See usage examples below\n");
  284. Console.WriteLine("Show command line help:");
  285. Console.WriteLine("/help\n");
  286. Console.WriteLine("View APIKey and SecretKey values:");
  287. Console.WriteLine("/cmd:getkeys\n");
  288. Console.WriteLine("Set APIKey and SecretKey:");
  289. Console.WriteLine("/cmd:setkeys /apikey:<apikey> /secretkey:<secretKey>\n");
  290. Console.WriteLine("Show global monitor results for defined agents:");
  291. Console.WriteLine("/cmd:listagents /type:<int>|<ext>|<fullpage>|<all> [/monitors:<all><name>,<name>,...]\n");
  292. Console.WriteLine("Show monitor results for one or more specific processes:");
  293. Console.WriteLine("/cmd:listagents /type:<int>|<ext>|<all> [/process:<name>,<name>,...]\n");
  294. Console.WriteLine("Example:");
  295. Console.WriteLine("Show cpu and memory monitors for all internal agents");
  296. Console.WriteLine("/cmd:listagents /type:int /monitors:cpu,memory\n");
  297. }
  298. }
  299. }