Download the PHP package kamermans/haproxy-api without Composer
On this page you can find all versions of the php package kamermans/haproxy-api. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download kamermans/haproxy-api
More information about kamermans/haproxy-api
Files in kamermans/haproxy-api
Package haproxy-api
Short Description PHP API for HAProxy administration
License GPLv3
Homepage https://github.com/kamermans/HAProxyAPI
Informations about the package haproxy-api
HAProxyAPI
PHP API for HAProxy administration
This PHP API lets you programatically access HAProxy for both admin and read-only commands like enabling/disabling servers, and checking the status of servers. I can communicate via HAProxy over the builtin HTTP stats web page, using the built-in UNIX domain socket, or over TCP if, for example, you are proxying your domain socket to a TCP socket so you can access it remotely.
The HAProxy HTTP interface only supports Stats
, EnableServer
and DisableServer
, so use of the domain or TCP socket is required for all other commands.
HAProxyAPI was written by Steve Kamerman and is distributed under the GNU GPLv3 license.
Getting Started
Before you can use HAProxyAPI, you'll need to include the class loader. The API is PSR-0 compliant and has builtin Composer support. If you don't know what that means, you probably just want to use the included class loader, autoload.php
:
Then, you'll need to create an HAProxy\Executor
which is used to run all the commands.
Connecting to HAProxy via HTTP
Make sure you have the stats
interface enabled in your HAProxy config. You will also need admin
privileges if you want to enable or disable servers. Currently, authentication is required to use this method.
Connecting to HAProxy via Socket
Socket communications requires the stats
configuration option to be enabled, as well as the appropriate permission level to run the commands. Here's and example config for enabling the stats socket:
To use the UNIX domain socket interface for HAProxy, you pass its full filename to the constructor:
Connecting to HAProxy via TCP/IP
HAProxy does not ship with TCP/IP support, but it does support UNIX domain sockets. You can use something like socat
or netcat
/nc
to make your domain socket accessible via TCP/IP. This example exposes the socket to the localhost on port 10010:
If you can access the socket via TCP/IP, you can use HAProxyAPI to connect to it as well (think socat + ssh-port-forwarding = secure-remote-admin
).
For this setup, you use the same socket setup as above, but you pass a hostname and port instead:
Getting Statistics
To get a statistics object, use HAProxy\Stats::get($exec)
:
That will output something like this:
Using this information, you can get statistics about individual servers:
Output:
Enabling/Disabling Servers
You can put servers into maintanence mode (aka disabled mode) and bring them back up using the HAProxy\Command\DisableServer
and HAProxy\Command\EnableServer
HAProxyAPI commands.
Both commands take a backend service name (ex: foo-nodes
) and a server name (ex: node01.foobar.com
).
To execute commands, you pass them to the HAProxy\Executor::execute($command)
method: