adilbaig / pagerduty
PHP 包装器,用于 PagerDuty 事件 API。
3.2.0
2020-08-04 16:37 UTC
Requires
- php: >=5.4.0
- ext-curl: *
Requires (Dev)
- phpunit/phpunit: >=5.6
This package is auto-updated.
Last update: 2024-09-05 01:28:51 UTC
README
PHP 实现 PagerDuty 事件 API V2
升级通知
PagerDuty 事件 API V2 与 PagerDuty 事件 API V1 不兼容。因此,该 API 已更改。如果您是从 2.* 版本 升级,请确保您注意到了 TriggerEvent
构造函数
特性
- 兼容 PagerDuty 事件 API V2。
- 触发、确认和解决事件。
- 将 链接 和 图片 添加到事件报告中。
- 单元测试
安装
将此行添加到您的项目 composer.json
文件中
{
...
"require": {
"adilbaig/pagerduty": "3.*"
}
...
}
packagist URL 为 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