20steps / commons-uptime-robot-bundle
Symfony组件,用于UptimeRobot API v2.0的服务端客户
v1.0.4
2017-07-19 16:55 UTC
Requires
- php: >=7.0.0
- 20steps/commons-ensure-bundle: ^1.0
- jane/openapi-runtime: ^2.0
- php-http/guzzle6-adapter: ^1.1
- symfony/framework-bundle: ~3.0
Requires (Dev)
- jane/open-api: ^2.0
This package is not auto-updated.
Last update: 2024-09-29 00:52:18 UTC
README
关于
20steps UptimeRobot Bundle 包含了对 UptimeRobot API v2.0 的面向服务的客户端。
在需要自动创建监控、警报联系人等场景中使用此包,例如为您的应用程序或服务在 UptimeRobot.com 上自动创建,或作为部署过程的一部分以自动管理维护窗口。
此组件采用 LGPL许可版本3.0 许可。
安装
先决条件
- 安装 Composer,这是现代PHP应用程序使用的依赖管理器。
- 设置基于Symfony 3的应用程序
- 使用PHP >= 7.0作为运行时
- 将包添加到composer.json中,并通过调用以下命令下载匹配版本
composer require 20steps/commons-uptime-robot-bundle
- 在config.yml中配置UptimeRobot.com的账户API密钥
twentysteps_commons_uptime_robot:
api_key: "Your API Key"
使用
以下代码展示了如何使用客户端创建一个监控器。
<?php
use twentysteps\Commons\UptimeRobotBundle\API;
use twentysteps\Commons\UptimeRobotBundle\Model;
class MyService {
/**
* @var UptimeRobotAPI
*/
private $uptimeRobotAPI;
/**
* inject dependency to uptimeRobotAPI via your services.yml
* the uptimeRobotAPI is a service itself with the id "twentysteps_commons.uptime_robot.api"
*/
public function __construct(UptimeRobotAPI $uptimeRobotAPI) {
$this->uptimeRobotAPI = $uptimeRobotAPI;
}
/**
* create a monitor
* @return \Psr\Http\Message\ResponseInterface|Error|Monitor
*/
public function createMonitorForMyResource() {
$parameters = [
'friendly_name' => 'My Monitor,
'url' => 'https://my-host.com/my-path'
];
$response = $this->uptimeRobotAPI->monitor()->create($parameters);
if ($response instanceof MonitorResponse) {
/**
* @var $response MonitorResponse
*/
if ($response->getStat()=='ok') {
return $response->getMonitor();
} else {
return $response->getError();
}
}
return $response;
}
}
该组件在twentysteps:commons:uptime-robot命名空间下方提供了一些有用的命令。
例如,要列出所有监控器和它们的状况,只需调用
bin/console twentysteps:commons:uptime-robot:monitor:list
提示
- 如果您的应用程序在UptimeRobot上使用多个账户,您可以按如下方式动态更改API密钥
<?php
$this->uptimeRobotAPI->setApiKey($myApiKey);
- 请参阅UptimeRobot API文档的“参数”部分以了解参数的说明。
- UptimeRobot API已通过一些额外的实用方法得到增强,例如api->monitor()->findOneByUrl(..),api->monitor->createOrUpdate(..),api->monitor->pauseByUrl(),api->monitor->resumeByUrl()等。
作者
- Helmut Hoffer von Ankershoffen hhva@20steps.de