bigwhoop/haproxy-api

此包已被废弃,不再维护。未建议替代包。

HAProxy API

dev-master 2012-11-28 19:41 UTC

This package is auto-updated.

Last update: 2023-08-26 20:03:13 UTC


README

使用composer。

{
    ...
    "require": {
        ...
        "bigwhoop/haproxy-api" : "dev-master",
        ...
    }
    ...
}

配置

<?php
use \BigWhoop\HAProxyAPI;

$api = new HAProxyAPI\API('http://example.com');
$api = new HAProxyAPI\API('http://example.com:22002');
$api = new HAProxyAPI\API('http://example.com:22002/status');
$api = new HAProxyAPI\API('http://10.0.0.1/monitor');
$api = new HAProxyAPI\API('http://example.com', 'username');
$api = new HAProxyAPI\API('http://example.com', 'username', 'password');
...

命令

启用服务器

try {
    $status = $api->execute('enableServer', array('backend' => 'webapp', 'server' => 'web-01'));
    if ($status) {
        // Action had an effect
    } else {
        // Action had no effect
    }
}  catch (HAProxyAPI\Client\Exception $e) {
   // Server error
} catch (HAProxyAPI\Command\Exception $e) {
   // Data error
}

禁用服务器

与上述相同,只需将 disableServer 作为 $api->execute() 的第一个参数即可。

统计数据

try {
    $stats = $api->execute('stats');
} catch (HAProxyAPI\Client\Exception $e) {
    // Server error
} catch (HAProxyAPI\Command\Exception $e) {
    // Data error
}

返回一个具有以下属性的原生对象数组

 pxname, svname, qcur, qmax, scur, smax, slim, stot, bin, bout, dreq, dresp, ereq, econ, eresp,
 wretr, wredis, status, weight, act, bck, chkfail, chkdown, lastchg, downtime, qlimit, pid, iid,
 sid, throttle, lbtot, tracked, type, rate, ratelim, ratemax, checkstatus, checkcode, checkduration,
 hrspxx, hrspother, hanafail, reqrate, reqratemax, reqtot, cliabrt, srvabrt

我非常确信你会了解它们的含义。它们以HAProxy的这种格式从HAProxy中获取。

分组

您还可以按后端分组获取统计数组。

try {
    $stats = $api->execute('stats', array('grouping' => HAProxyAPI\Command\StatsCommand::GROUPING_BACKEND));
} catch (HAProxyAPI\Client\Exception $e) {
    // Server error
} catch (HAProxyAPI\Command\Exception $e) {
    // Data error
}

数组现在是以下格式

[
    'backend1' => [{ ... }, { ... }],
    'backend2' => [{ ... }, ...],
    ...
]

许可证

请参阅LICENSE文件。