norberttech/pagerduty

adilbaig/pagerduty 的分支 - PHP 的 PagerDuty 事件 API 封装。

4.0.1 2023-01-24 09:02 UTC

This package is auto-updated.

Last update: 2024-09-21 23:23:12 UTC


README

PHP 实现 PagerDuty 事件 API V2

此存储库是从 adilbaig/pagerduty 分支的,看起来已经不再维护。

升级通知

PagerDuty 事件 API V2PagerDuty 事件 API V1 不兼容。因此,API 已经发生变化。如果您从 2.* 版本 升级,请确保您注意到了 TriggerEvent 构造函数。

Latest Stable Version Total Downloads

特性

安装

将此行添加到项目的 composer.json

{
...
    "require": {
        "norberttech/pagerduty": "4.*"
    }
...
}

Packagist 网址是 https://packagist.org.cn/packages/adilbaig/pagerduty

使用方法

触发事件

use \PagerDuty\TriggerEvent;
use \PagerDuty\Exceptions\PagerDutyException;

$routingKey = "1d334a4819fc4b67a795b1c54f9a"; //Replace this with the integration key of your service.

// In this example, we're triggering a "Service is down" message from a web server.
try {
    $event = new TriggerEvent(
        $routingKey, 
        "Service is down",  // A high-level, text summary message of the event. Will be used to construct an alert's description.
        "web-server-01",    // human-readable unique identifier, such as a hostname, for the system having the problem.
        TriggerEvent::ERROR,// How impacted the affected system is? Influences the priority of any created incidents. 
        true                // Generate the dedup_key from the driver. If false, the dedup_key will be generated on PD 
    );
    $responseCode = $event->send();
    if($responseCode == 200)
        echo "Success";
    elseif($responseCode == 429)
        echo "Rate Limited";  //You're being throttled. Slow down.
    else // An error occured. Try again later
        echo "Some error has occured. Try again later";
} catch(PagerDutyException $exception) { //This doesn't happen unless you've broken their guidelines. The API tries to minimize user mistakes
    var_dump($exception->getErrors());
}

使用自定义连接触发事件,例如:使用代理和/或设置调试详细程度等。

use \PagerDuty\TriggerEvent;
use \PagerDuty\Exceptions\PagerDutyException;
use \PagerDuty\Http\PagerDutyHttpConnection;

try {
    $routingKey = '1d334a4819fc4b67a795b1c54f9a';  //Replace this with the integration key of your service.

    $event = new TriggerEvent(
        $routingKey, 
        "Service is down",  // A high-level, text summary message of the event. Will be used to construct an alert's description.
        "web-server-01",    // human-readable unique identifier, such as a hostname, for the system having the problem.
        TriggerEvent::ERROR,// How impacted the affected system is? Influences the priority of any created incidents. 
        true                // Generate the dedup_key from the driver. If false, the dedup_key will be generated on PD 
    );

    // create a custom proxy connection
    $connection = new PagerDutyHttpConnection();

    // .. and set the proxy
    $connection->setProxy('https://user:password@your-proxy-ip-address:port');

    // set custom CURL options. Here we set verbosity for debugging
    $connection->addCurlOption('CURLOPT_VERBOSE', 1);
    
    // send event through proxy
    $connection->send($event);
}
catch(PagerDutyException $exception) { //This doesn't happen unless you've broken their guidelines. The API tries to minimize user mistakes
    var_dump($exception->getErrors());
}
catch (\Exception $e) {
    // A configuration exception
}

自动发送重复错误的单个 PagerDuty 事件

//You will only see one incident on PD
(TriggerEvent($routingKey, "Service is down", "web-server-01", TriggerEvent::ERROR, true))->send();
(TriggerEvent($routingKey, "Service is down", "web-server-01", TriggerEvent::ERROR, true))->send();
(TriggerEvent($routingKey, "Service is down", "web-server-01", TriggerEvent::ERROR, true))->send();

创建详细的 '触发' 事件,添加可选数据。输出事件并检查 PD 的响应

use \PagerDuty\TriggerEvent;

//Taken from the `trigger` example @ https://v2.developer.pagerduty.com/docs/send-an-event-events-api-v2
//Send a detailed event, and store the `dedup_key` generated on the server

$event = new TriggerEvent(
    $routingKey, 
    "Example alert on host1.example.com", 
    "monitoringtool:cloudvendor:central-region-dc-01:852559987:cluster/api-stats-prod-003", 
    TriggerEvent::INFO
);
$event
    ->setPayloadTimestamp("2015-07-17T08:42:58.315+0000")
    ->setPayloadComponent("postgres")
    ->setPayloadGroup("prod-datapipe")
    ->setPayloadClass("deploy")
    ->setPayloadCustomDetails(["ping_time" => "1500ms", "load_avg" => 0.75])
    ->addLink("https://example.com/", "Link text")
    ->addImage("https://www.pagerduty.com/wp-content/uploads/2016/05/pagerduty-logo-green.png", "https://example.com/", "Example text"))
;

// Pass in the '$response' variable by reference if you want to inspect PD's response. This is optional, and you probably don't need this in production.
$response = null;
$responseCode = $event->send($response);
// In this case, we will save the `dedup_key` generated by the PD server
var_dump($response['dedup_key']);

确认事件

(new AcknowledgeEvent($routingKey, "dedup key"))->send();

解决事件

(new ResolveEvent($routingKey, "dedup key"))->send();

单元测试

> ./vendor/bin/phpunit test/
.....                                                               5 / 5 (100%)

Time: 37 ms, Memory: 4.00MB

OK (5 tests, 6 assertions)

问题

问:我如何从 PagerDuty 获取服务密钥?

答:在您的 PagerDuty 控制台中,点击 '配置' > '服务'。点击 '集成' 列表下的链接。它是 '集成密钥'。

更多信息请参阅:https://v2.developer.pagerduty.com/v2/docs/events-api#getting-started

要求

此库需要 curl pecl 扩展

在 Ubuntu 16.04 中,安装方法如下:

sudo apt install php-curl

在 Ubuntu 18.04 中,安装方法如下:

sudo apt install php7.2-curl