/// <summary> /// Outputs JSON object of request. /// </summary> /// <param name="args"></param> static void Main(string[] args) { JsConfig.DateHandler = DateHandler.ISO8601DateTime; if (args.Length < 2) { Console.WriteLine(" HAProxyUtil <host>:<port> <command> [arg()] "); Console.WriteLine("------------------ COMMANDS -------------------"); Console.WriteLine("errors"); Console.WriteLine("info"); Console.WriteLine("backend"); Console.WriteLine("stats"); Console.WriteLine("drain <backend> <server>"); Console.WriteLine("disable <backend> <server>"); Console.WriteLine("enable <backend> <server>"); Console.WriteLine("setweight <backend> <server> <weight>"); Console.WriteLine("sendcommand <command> <Y>"); Environment.Exit(0); } var hostData = args[0].Split(":"); var client = new HaProxyClient(hostData[0], hostData[1].ConvertTo <int>()); switch (args[1].ToLower()) { case "errors": Console.Write(client.ShowErrors().ToJson()); break; case "info": Console.Write(client.ShowInfo().ToJson()); break; case "backend": Console.Write(client.ShowBackendServers().ToJson()); break; case "stats": Console.Write(client.ShowStat().ToJson()); break; case "drain": Console.Write(client.DrainServer(args[2], args[3]).ToJson()); break; case "enable": Console.Write(client.EnableServer(args[2], args[3]).ToJson()); break; case "disable": Console.Write(client.DisableServer(args[2], args[3]).ToJson()); break; case "setweight": Console.Write(client.SetWeight(args[2], args[3], args[4].ConvertTo <int>()).ToJson()); break; case "sendcommand": Console.Write(client.SendCommand(args[2], args.Length == 4)); //anything in 4th position will count as true for returning raw string (you gotta parse that yourself) break; } }
private IHttpActionResult PerformBackendServerAction(HAProxyBackendServerRequest request, Func <HaProxyClient, BackendServer> action) { if (action == null) { throw new ArgumentNullException(nameof(action)); } if (!IsValid(request, out IHttpActionResult invalidResult)) { return(invalidResult); } if (request == null) { throw new InvalidOperationException(); } HaProxyClient client = request.CreateClient(); BackendServer backendServer = action(client); if (backendServer == null) { return(NotFound()); } return(Ok(backendServer)); }
static void Main(string[] args) { // https://github.com/clintnetwork/HaProxy.Api Console.WriteLine("Hello World!"); using (var haproxy = new HaProxyClient("172.16.12.225", 9999)) using (var instance = haproxy.GetInstance()) { // Get the help message Console.WriteLine(instance.Help()); string errors = instance.ShowErrors(); Console.WriteLine(errors); // Get all the backends var backends = instance.ShowBackend(); foreach (var backend in backends) { Console.WriteLine(backend); } // Disable the "http-in" frontend instance.DisableFrontend("http-in"); // Re-enable it instance.EnableFrontend("http-in"); } }
public void Connecting() { using (var haproxy = new HaProxyClient("127.0.0.1", 8080)) using (var instance = haproxy.GetInstance()) { Assert.NotNull(instance); } }
public void SendRaw() { using (var haproxy = new HaProxyClient("127.0.0.1", 8080)) using (var instance = haproxy.GetInstance()) { var response = instance.SendRaw("show errors"); Assert.NotEmpty(response); } }
public void ShowBackend() { using (var haproxy = new HaProxyClient("127.0.0.1", 8080)) using (var instance = haproxy.GetInstance()) { var backends = instance.ShowBackend(); Assert.NotEmpty(backends); } }
public void ShowErrors() { using (var haproxy = new HaProxyClient("127.0.0.1", 8080)) using (var instance = haproxy.GetInstance()) { var errors = instance.ShowErrors(); Assert.NotEmpty(errors); } }
public IHttpActionResult GetStatus([FromUri] HAProxyRequest request = null) { if (!IsValid(request, out IHttpActionResult invalidResult)) { return(invalidResult); } if (request == null) { throw new InvalidOperationException(); } HaProxyClient client = request.CreateClient(); return(Ok(new { Backends = client.ShowBackends(), Servers = client.ShowBackendServers(), Info = client.ShowInfo(), Errors = client.ShowErrors() })); }
public IEnumerable <IShowStatResponse> Parse(string rawStats) { const char csvFieldDelimiter = ','; return(HaProxyClient.ParseResponse <ShowStatResponse>(rawStats, csvFieldDelimiter)); }