kamermans/haproxy-api

HAProxy 管理的 PHP API

v1.0.3 2017-07-25 13:05 UTC

This package is auto-updated.

Last update: 2024-09-07 05:09:30 UTC


README

HAProxy 管理的 PHP API

Build Status

这个 PHP API 允许您以编程方式访问 HAProxy,执行管理命令和只读命令,如启用/禁用服务器,检查服务器状态。我可以通过内置的 HTTP stats 网页、内置的 UNIX 域套接字TCP 进行通信,例如,如果您将域套接字代理到 TCP 套接字以便远程访问。

HAProxy HTTP 接口仅支持 StatsEnableServerDisableServer,因此需要使用域或 TCP 套接字来执行所有其他命令。

HAProxyAPI 由 Steve Kamerman 编写,并按 GNU GPLv3 许可证分发。

入门指南

在您可以使用 HAProxyAPI 之前,您需要包含类加载器。该 API 符合 PSR-0 规范,并具有内置的 Composer 支持。如果您不知道这意味着什么,您可能只想使用包含的类加载器 autoload.php

<?php
require_once 'autoload.php';

然后,您需要创建一个 HAProxy\Executor,它用于运行所有命令。

通过 HTTP 连接到 HAProxy

确保您在 HAProxy 配置中启用了 stats 接口。如果您想启用或禁用服务器,还需要 admin 权限。目前,使用此方法需要身份验证。

<?php
// Create a Executor for HTTP
$exec = new HAProxy\Executor('http://hostname:port/haproxy_stats_url', HAProxy\Executor::HTTP);
// Set your HAProxy stats page credentials
$exec->setCredentials('username', 'password');

通过 Socket 连接到 HAProxy

套接字通信需要启用 stats 配置选项以及运行命令的适当权限级别。以下是一个启用 stats 套接字的示例配置

global
    stats    socket /tmp/haproxy-stats    user haproxy group haproxy mode 700 level admin

要使用 HAProxy 的 UNIX 域套接字接口,您将完整文件名传递给构造函数

<?php
// Create a Executor for HTTP
$exec = new HAProxy\Executor('/tmp/haproxy-stats', HAProxy\Executor::SOCKET);

通过 TCP/IP 连接到 HAProxy

HAProxy 不带 TCP/IP 支持,但它支持 UNIX 域套接字。您可以使用类似 socatnetcat/nc 的工具使您的域套接字可通过 TCP/IP 访问。以下示例将套接字公开在本地主机的 10010 端口上

socat TCP-LISTEN:10010,bind=127.0.0.1,reuseaddr,fork,su=haproxy,range=127.0.0.0/8 UNIX-CLIENT:/tmp/haproxy-stats

如果您可以通过 TCP/IP 访问套接字,您也可以使用 HAProxyAPI 来连接它(想想 socat + ssh-port-forwarding = secure-remote-admin)。

对于此设置,您使用上述相同的套接字设置,但传递一个主机名和端口号

<?php
// Create a Executor for HTTP
$exec = new HAProxy\Executor('localhost:10010', HAProxy\Executor::SOCKET);

获取统计数据

要获取统计对象,请使用 HAProxy\Stats::get($exec)

<?php
// Connect
$exec = new HAProxy\Executor('localhost:10010', HAProxy\Executor::SOCKET);
// Get stats
$stats = HAProxy\Stats::get($exec);
// Show a tree of the backends, frontends and servers
echo $stats->dumpServiceTree();

这将输出类似以下内容

+- foo-service
|  +- FRONTEND (OPEN)
|
+- foo-nodes
|  +- node01.foobar.com (UP)
|  +- node02.foobar.com (MAINT)
|  +- node03.foobar.com (UP)
|  +- node04.foobar.com (UP)
|  +- BACKEND (UP)
|
+- stats
|  +- FRONTEND (OPEN)
|  +- BACKEND (UP)

使用此信息,您可以获取有关单个服务器的统计信息

<?php
$server = $stats->getServiceStats('foo-nodes','node01.foobar.com');
echo "-------------------------------------\n";
echo "{$server->info->service_name}: {$server->health->status} ({$server->health->check_status} - {$server->health->check_duration}ms )\n";
echo "-------------------------------------\n";
echo $server->dump();
echo "-------------------------------------\n";

输出

-------------------------------------
node01.foobar.com: UP (L7OK - 4ms )
-------------------------------------
Info Stats:
        proxy_name: foo-nodes
        service_name: node01.foobar.com
        weight: 1
        process_id: 1
        proxy_id: 2
        service_id: 1
        tracked:
        type: 2
Health Stats:
        status: UP
        active: 1
        backup: 0
        check_failed: 414
        up_down_transitions: 11
        status_change: 22700
        downtime: 7875
        throttle:
        selected_total: 1309606
        check_status: L7OK
        check_code: 200
        check_duration: 4
        check_fail_details: 0
Queue Stats:
        current: 0
        max: 0
        limit:
Session Stats:
        current: 0
        max: 26
        limit:
Bytes Stats:
        in: 697255098
        out: 598278314
Rate Stats:
        current: 0
        max: 49
        limit:
Denied Stats:
        requests:
        responses: 0
Error Stats:
        requests:
        responses: 0
        connections: 2
Warning Stats:
        retries: 151
        redispatches: 23
HttpResponseCode Stats:
        http_1xx: 0
        http_2xx: 1308874
        http_3xx: 0
        http_4xx: 537
        http_5xx: 0
-------------------------------------

启用/禁用服务器

您可以使用 HAProxyAPI 的 HAProxy\Command\DisableServerHAProxy\Command\EnableServer 命令将服务器放入维护模式(也称为禁用模式),然后将其重新启动。

这两个命令都接受一个 后端服务名称(例如:foo-nodes)和一个 服务器名称(例如:node01.foobar.com)。

要执行命令,请将它们传递给 HAProxy\Executor::execute($command) 方法

<?php
// Create a Executor for HTTP
$exec = new HAProxy\Executor('http://hostname:port/haproxy_stats_url', HAProxy\Executor::HTTP);
// Set your HAProxy stats page credentials
$exec->setCredentials('username', 'password');

// Disable foo-nodes/node01.foobar.com in the load balancer
$exec->execute(new HAProxy\Command\DisableServer('foo-nodes', 'node01.foobar.com'));

// Show stats - you can see node01 is down
echo HAProxy\Stats::get($exec)->dumpServiceTree();
/*
+- foo-service
|  +- FRONTEND (OPEN)
|
+- foo-nodes
|  +- node01.foobar.com (MAINT)
|  +- node02.foobar.com (UP)
|  +- node03.foobar.com (UP)
|  +- node04.foobar.com (UP)
|  +- BACKEND (UP)
|
+- stats
|  +- FRONTEND (OPEN)
|  +- BACKEND (UP)
*/

// Enable foo-nodes/node01.foobar.com
$exec->execute(new HAProxy\Command\EnableServer('foo-nodes', 'node01.foobar.com'));

// Show stats - you can see node01 is coming up
echo HAProxy\Stats::get($exec)->dumpServiceTree();
/*
+- foo-service
|  +- FRONTEND (OPEN)
|
+- foo-nodes
|  +- node01.foobar.com (UP 1/3)
|  +- node02.foobar.com (UP)
|  +- node03.foobar.com (UP)
|  +- node04.foobar.com (UP)
|  +- BACKEND (UP)
|
+- stats
|  +- FRONTEND (OPEN)
|  +- BACKEND (UP)
*/