public static async Task <Prtg> Read(RequestPrams request) { var client = new HttpClient { BaseAddress = new Uri("http://" + request.ServerAndPort + "/api/") }; client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", Convert.ToBase64String(Encoding.ASCII.GetBytes(request.User + ":" + request.Password))); using (var result = await client.GetAsync(string.Join("/", request.Type, request.Host, request.Name))) { result.EnsureSuccessStatusCode(); var serializer = new JsonSerializer(); using (var s = await result.Content.ReadAsStreamAsync()) { var reader = new JsonTextReader(new StreamReader(s)); var data = serializer.Deserialize(reader); if (request.Type.Equals("exchanges", StringComparison.OrdinalIgnoreCase)) { return(ReadExchange(data)); } else if (request.Type.Equals("queues", StringComparison.OrdinalIgnoreCase)) { return(ReadQueue(data)); } throw new ArgumentOutOfRangeException("request", request.Type + " is an unsupported object type"); } } }
public static async Task Execute(RequestPrams requestPrams) { var s = new XmlSerializer(typeof(Prtg)); var result = await Read(requestPrams); s.Serialize(Console.Out, result); }
static void Main(string[] args) { if (args.Length != 4 && args.Length != 6) { Console.WriteLine("Missing Required arguments"); Console.WriteLine("<Server:Port> <user> <password> <Type> <Host> <Name>"); Console.WriteLine("Examples:"); Console.WriteLine("LocalHost:15672 guest guest queues %2f MyQueue"); Console.WriteLine("LocalHost:15672 guest guest exchanges %2f MyExchange"); if (args.Length > 0 && args[0].Equals("protect")) { Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None); ConfigurationSection section = config.GetSection("connectionStrings"); { if (!section.SectionInformation.IsProtected) { if (!section.ElementInformation.IsLocked) { section.SectionInformation.ProtectSection("DataProtectionConfigurationProvider"); section.SectionInformation.ForceSave = true; config.Save(ConfigurationSaveMode.Full); Console.WriteLine(); Console.WriteLine("Encrypted Configuration File"); } } } } Environment.Exit(2); } try { RequestPrams requestPrams; if (args.Length == 4) { var connectionString = ConfigurationManager.ConnectionStrings[args[0]].ToString().Split(':'); requestPrams = new RequestPrams { ServerAndPort = args[0], Type = args[1], Host = args[2], Name = args[3], User = connectionString[0], Password = connectionString[1] }; } else { requestPrams = new RequestPrams { ServerAndPort = args[0], User = args[1], Password = args[2], Type = args[3], Host = args[4], Name = args[5] }; } Execute(requestPrams).Wait(); } catch (Exception e) { // Catch all var s = new XmlSerializer(typeof(Prtg)); s.Serialize(Console.Out, new Prtg { Error = 1, Text = e.GetBaseException().Message }); Environment.Exit(2); } // Return Codes // 0 OK // 1 WARNING // 2 System Error (e.g. a network/socket error) // 3 Protocol Error (e.g. web server returns a 404) // 4 Content Error (e.g. a web page does not contain a required word }