20steps/commons-uptime-robot-bundle
Installation Run:
composer require 20steps/commons-uptime-robot-bundle
Ensure your config/packages/twentysteps_commons_uptime_robot.yaml includes:
twentysteps_commons_uptime_robot:
api_key: "%env(UPTIMEROBOT_API_KEY)%"
(Use .env for security.)
First Use Case Inject the API service into a controller or command:
use Twentysteps\Commons\UptimeRobotBundle\API\UptimeRobotAPI;
class MyController {
public function __construct(private UptimeRobotAPI $uptimeRobotAPI) {}
}
Verify connectivity with:
$this->uptimeRobotAPI->getMyInfo();
Monitor Creation
Define a monitor via Model\Monitor and create it:
$monitor = new Model\Monitor();
$monitor->setFriendlyName('My Service')
->setUrl('https://example.com')
->setType(Model\Monitor::TYPE_HTTP)
->setInterval(5); // Minutes
$this->uptimeRobotAPI->createMonitor($monitor);
Alert Contacts
Manage contacts (e.g., emails) via Model\AlertContact:
$contact = new Model\AlertContact();
$contact->setContact('admin@example.com')
->setType(Model\AlertContact::TYPE_EMAIL);
$this->uptimeRobotAPI->createAlertContact($contact);
Maintenance Windows Schedule downtime for monitors:
$window = new Model\MaintenanceWindow();
$window->setMonitorId($monitorId)
->setStartTime('2023-12-25T12:00:00')
->setEndTime('2023-12-25T13:00:00');
$this->uptimeRobotAPI->createMaintenanceWindow($window);
KernelEvents):
$event->getResponse()->headers->set('X-UptimeRobot-Monitor', $monitorId);
$monitors = $this->uptimeRobotAPI->getMonitors();
$this->cache->set('uptime_robot_monitors', $monitors, '0', new \DateInterval('P1D'));
try {
$this->uptimeRobotAPI->createMonitor($monitor);
} catch (\Exception $e) {
$this->logger->error('UptimeRobot API Error: ' . $e->getMessage());
}
API Key Security
.env and Symfony’s %env().Rate Limits
429 Too Many Requests gracefully:
if ($e->getCode() === 429) {
sleep(60); // Retry after 1 minute
}
Monitor Types
setType() matches UptimeRobot’s valid types (e.g., TYPE_HTTP, TYPE_HTTPS, TYPE_PING).Time Zones
MaintenanceWindow timestamps to avoid timezone-related issues:
$window->setStartTime((new \DateTime('now', new \DateTimeZone('UTC')))->format(\DateTime::ATOM));
Enable API Logging
Configure Monolog in config/packages/monolog.yaml to log API requests/responses:
handlers:
uptime_robot:
type: stream
path: "%kernel.logs_dir%/uptime_robot.log"
level: debug
Then inject the logger:
$this->uptimeRobotAPI->setLogger($this->logger);
Validate Responses
Use getLastResponse() to inspect raw API responses:
$response = $this->uptimeRobotAPI->getMonitors();
$this->logger->debug('API Response:', [$response->getBody()]);
Custom Models
Extend Model\Monitor or Model\AlertContact to add application-specific fields:
class CustomMonitor extends Model\Monitor {
private $customField;
public function setCustomField($value) { $this->customField = $value; }
public function getCustomField() { return $this->customField; }
}
API Client Overrides
Override the default UptimeRobotAPI service in config/services.yaml:
Twentysteps\Commons\UptimeRobotBundle\API\UptimeRobotAPI:
arguments:
$apiKey: '%env(UPTIMEROBOT_API_KEY)%'
$baseUri: 'https://api.uptimerobot.com/v2'
Webhook Handling
Use Symfony’s HttpClient to process UptimeRobot webhook notifications:
$client = $this->httpClient->request('POST', 'https://api.uptimerobot.com/v2/getMonitor', [
'headers' => ['Authorization' => 'Bearer ' . $this->uptimeRobotAPI->getApiKey()],
'json' => ['api_key' => $this->uptimeRobotAPI->getApiKey(), 'format' => 'json']
]);
How can I help you explore Laravel packages today?