We've created an easy-to-use open source PHP package that allows you to communicate with the Oh Dear API. It provides some convenient models and methods to quickly get started.
The API itself is a REST API. If you're unfamiliar with it, we suggest you read through the API documentation first, it'll make things a lot clearer.
If you want to dive straight in, here's what we suggest;
- Read the getting started guide guide for the SDK. This will get the package installed and configure your API keys.
- Start managing your monitors via the SDK (get data, add new site, remove an existing site).
Once you've got that going, you're all set.
Getting started
We offer a PHP SDK package that is ready for use. It's open source and we happily accept contributions to it.
First, grab the latest stable version via composer
.
composer require ohdearapp/ohdear-php-sdk
Next, create an instance of the SDK. This takes your API key as a single, mandatory, parameter. If you don't have an API key yet, read up on the API authentication first first.
$apiKey = "iK4YfZe2i1RAe22tKP4xejGKDZP .... ";
$ohDear = new OhDear\PhpSdk\OhDear($apiKey);
Once the SDK has been initiated, you can continue adding monitors or checks to your account.
User Info
You can get your user info with the convenient me()
method. Behind the scenes, it queries the user info API endpoint.
$user = $ohDear->me();
The me()
method will return an instance of OhDear\PhpSdk\Resources\User
.
$user->name;
$user->email;
You can get the teams you belong to with through the teams
property.
$teams = $user->teams;
The $teams
variable is an array of OhDear\PhpSdk\Resources\Team
.
$team->id
$team->name
A user can belong to multiple teams. The API key you used to authenticate with belongs to a single user.
Monitors
Retrieve all sites
The simplest example is one where we list all your monitors on your Oh Dear accounts.
$monitors = $ohDear->sites();
This will return an array of OhDear\PhpSdk\Resources\Site
instances.
You can get a few properties of a site.
$monitor->id;
$monitor->url;
...
You can also get a single site:
$ohDear->site($monitorId);
Add a monitor through the SDK
A new site can be created with createSite()
.
$ohDear->createSite([
'url' => 'https://yourbrandnewsite.tld',
'team_id' => $yourTeamId
]);
Take a look at the User section to learn how to get your team id.
When an https
site is created, all checks will automatically be enabled. When an http
site is created only the uptime and broken links checks will be enabled.
If you only want to enable specific checks when creating a site you can just pass them like this:
$ohDear->createSite([
'url' => 'https://yourbrandnewsite.tld',
'team_id' => $yourTeamId
'checks' => ['uptime', 'mixed_content']
]);
Valid values to pass to checks are uptime
, broken_links
, certificate_health
, mixed_content
.
Deleting a site
A site can easily be deleted. This assumes the $monitor
is an instance of OhDear\PhpSdk\Resources\Site
, which you can get with the sites()
method.
$monitor->delete();
See the retrieving all sites section above to get all available sites.
Managing maintenance periods
You can use the SDK to put a site in maintenance mode. This means that Oh Dear will continue to monitor your site, but will not send any notification while the monitor is in maintenance mode.
$ohDear->site($monitorId)->startMaintenance();
A maintenance period will stop after an hour by default. To shorten or extend that period, you can pass an amount of seconds.
$ohDear->site($monitorId)->startMaintenance($durationOfMaintenancePeriodInSeconds);
To manually stop maintenance mode, use this code:
$ohDear->site($monitorId)->stopMaintenance();
When integrated into your deployment or maintenance mechanisme, this will allow you to create and stop a maintenance period for just the right amount of time.
Checks
Get all checks per site
The simplest example is one where we list all your checks for a particular site. First, make sure you retrieve the monitors via the SDK.
You can get all checks of a site with the checks
property.
$checks = $monitor->checks;
This will return an array with instances of OhDear\PhpSdk\Resources\Check
.
$check->id;
$check->type;
Each OhDear\PhpSdk\Resources\Check
instance has several methods you can call.
Disabling a check
If you want to disable a check just call disable on it.
$check->disable();
Enabling a check
If you want to enable the check just call enable on it.
$check->enable();
Requesting a new run
If you want Oh Dear to perform a specific check now, call the requestRun()
method.
$check->requestRun();
It'll be added to the queue and checked instantly.
Snoozing a check
We can temporarily stop sending notifications for checks, by snoozing them. You can call the snooze($minutes)
method to snooze.
$check->snooze(10);
Unsnoozing a check
To remove an existing snooze, you can call the unsnooze()
method.
$check->unsnooze();
From then on, all new notifications will be sent again.
Uptime
Getting uptime data
You can get uptime with the uptime()
method on a site. The method expects three parameters:
start
: a date in the form of YmdHis
that determines the start of the range you want to get uptime for
end
: a date in the form of YmdHis
that determines the end of the range you want to get uptime for
- split: a string that determines how fine grained the answer periods should be. Valid values are
hour
, day
and month
.
$ohDear->site($monitorId)->uptime($start, $end, $split);
$ohDear->uptime($monitorId, $start, $end, $split);
Both methods above will return an array of OhDear\PhpSdk\Resources\Uptime
instances. A Uptime
instance.
$uptime->datetime;
$uptime->uptimePercentage;
Downtime
Getting downtime data
Here's how to get the downtime for a given site.
$ohDear->site($monitorId)->downtime();
$ohDear->downtime($monitorId);
Both methods above will return an array of OhDear\PhpSdk\Resources\Downtime
instances. A Downtime
instance.
$downtime->startedAt;
$downtime->endedAt;
Broken Links
Getting broken links
Here's how to get the broken links for a given site.
$ohDear->site($monitorId)->brokenLinks();
$ohDear->brokenLinks($monitorId);
Both methods above will return an array of OhDear\PhpSdk\Resources\BrokenLink
instances.
You can get a few properties of a broken link.
$brokenLink->statusCode;
$brokenLink->crawledUrl;
$brokenLink->foundOnUrl;
Mixed Content
Here's how to get the mixed content for a given site.
$ohDear->site($monitorId)->mixedContent();
$ohDear->mixedContent($monitorId);
Both methods above will return an array of OhDear\PhpSdk\Resources\MixedContentItem
instances.
You can get a few properties of a mixed content item.
$mixedContentItem->elementName;
$mixedContentItem->mixedContentUrl;
$mixedContentItem->foundOnUrl;
Certificate health
If your site has its certificate health check enabled, you can get some information regarding the found certificate.
$certificate = $ohDear->site($monitorId)->certificateHealth();
$certificate = $ohDear->certificateHealth($monitorId);
Both methods above will return an OhDear\PhpSdk\Resources\CertificateHealth
instance.
You can get the properties of a certificate
$certificate->certificateDetails;
$certificate->certificateChecks;
$certificate->certificateChainIssuers;
The specific return values are described in the certificate health API endpoint.
Status pages
The simplest example is one where we list all your status pages that have been configured.
$statusPages = $ohDear->statusPages();
This will return an array of OhDear\PhpSdk\Resources\StatusPage
instances.
The individual attributes will be explained in the next section.
Retrieve the status of a single status page
The simplest example is one where we get the details of a single status page that has been configured. Below, we'll retrieve the status page with the ID of 1.
$statusPage = $ohDear->statusPage(1);
This will return an instance of OhDear\PhpSdk\Resources\StatusPage
.
You can get a few properties of a Status Page.
$statusPage->id;
$statusPage->title;
$statusPage->attributes->sites;
$statusPage->attributes->updates;
$statusPage->summarized_status;
...
For more extensive understanding of the return values, please have a look at our status page API documentation.
The simplest example is one where we retrieve the performance metrics for a site in a particular time window. Here, we'll get the performance periods for a site with ID 1
.
$records = $ohDear->performanceRecords(1, '2020-06-07', '2020-06-08');
The above example will retrieve the performance metrics between 2020-06-07 and 2020-06-08. You can also be more specific with your request, by including the hour/minute/seconds:
$records = $ohDear->performanceRecords(1, '2020-06-07 09:00:00', '2020-06-08 17:00:00');
The SDK will convert any date format to the correct notation our API expects.
This will return an array of OhDear\PhpSdk\Resources\PerformanceRecord
instances.
You can get a few properties of a performance record.
$record->id;
$record->monitorId;
$record->timeRemoteserver;
$record->timeTotal;
Cron job monitoring
Cron job monitors are attached to a site. In order to get a list of defined monitors, you'll need a site ID.
In this example, let's use site ID 1
and retrieve all cron job monitors for it.
$cronChecks = $ohDear->cronChecks(1);
This will return an array of OhDear\PhpSdk\Resources\CronCheck
instances.
You can get a few properties of a cron job monitor:
$check->id;
$check->name;
$check->uuid;
$check->type;
$check->checkId;
$check->frequencyInMinutes;
$check->pingUrl;
$check->graceTimeInMinutes;
$check->cronExpression;
$check->description;
...
Add a cron job monitor through the SDK
You can create a new cron job monitor using one of two methods:
createSimpleCronCheck()
: lets you define a frequency and a grace time
createCronCheck()
: lets you provide a crontab
-style syntax for defining the frequency
For example, to use the simple cron monitor definition:
$check = $ohDear->createSimpleCronCheck(
1,
'your-cron-name',
5,
10,
'your description',
);
Or the crontab-style one:
$check = $ohDear->createSimpleCronCheck(
1,
'your-cron-name',
'* * * * *',
10,
'your description',
'UTC',
);
The return value is an instance of OhDear\PhpSdk\Resources\CronCheck
, with all the properties available as defined above.
Deleting a cron job monitor
A cron job monitor can easily be deleted. If you have the cron object, you can call the delete()
method on it:
$cronCheck->delete();
Alternatively, you can use the SDK to delete a particular ID:
$ohdear->deleteCronCheck(1);
Syncing a large list of cron job monitors
There's a special function available called syncCronChecks()
that allows you to bulk-update a list of cron job monitors for a site.
$ohdear->syncCronChecks(1, [
$cronCheck1,
$cronCheck2,
$cronCheck3,
]);
Please note: this will delete all cron jobs that are not present in the new syncCronChecks()
call!
Status pages
The simplest example is one where we list all the updates for a status page.
$ohDear->statusPage(1)->updates();
$statusPages = $ohDear->statusPageUpdates(1);
This will return an array of OhDear\PhpSdk\Resources\StatusPageUpdate
instances.
The individual attributes will be explained in the next section.
Add a status page update through the SDK
A new status page update can be created with createStatusPageUpdate()
.
$statusPageUpdate = $ohDear->createStatusPageUpdate([
'status_page_id' => 1,
'title' => 'Our site is down',
'text' => 'We are working on it!',
'pinned' => true,
'severity' => 'high',
'time' => '2021-02-09 12:25',
]);
This will return an instance of OhDear\PhpSdk\Resources\StatusPageUpdate
You can get a few properties of a Status Page Update.
$statusPageUpdate->id;
$statusPageUpdate->title;
$statusPageUpdate->text;
$statusPageUpdate->pinned;
$statusPageUpdate->severity;
$statusPageUpdate->time;
$statusPageUpdate->statusPageUrl;
Deleting a status page update
A site can easily be deleted. This assumes the $statusPageUpdate
is an instance of OhDear\PhpSdk\Resources\StatusPageUpdate
, which you can get with the statusPageUpdates()
method.
$statusPageUpdate->delete();
$ohDear->deleteStatusPageUpdate(1);
For more extensive understanding of the return values, please have a look at our status page updates API documentation.
Maintenance windows
The simplest example is one where we list the maintenance windows for a particular website. Here, we'll get the maintenance periods for a site with ID 1
.
$maintenanceWindows = $ohDear->maintenancePeriods(1);
This will return an array of OhDear\PhpSdk\Resources\MaintenancePeriod
instances.
You can get a few properties of a maintenance window.
$window->id;
$window->monitorId;
$window->startsAt;
$window->endsAt;
Add a maintenance period through the SDK
We have a convenient method for adding a maintenance window now to any site. This can be useful in deploy-scripts, to create a maintenance period that lasts for X amount of minutes.
This can be done using the startSiteMaintenance()
method.
$ohDear->startSiteMaintenance(
1,
120
);
This creates a new maintenance period for site ID 1
that lasts for 120 seconds, or 2 minutes. It's a shorthand for this, longer version, of the maintenance period creation:
$ohDear->createSiteMaintenance(
1,
'2020-05-19 13:00',
'2020-05-19 13:02'
)
The createSiteMaintenance()
method allows a $startDate
and $endDate
to be specified. You can pass the date as any string, it will be formatted by the SDK into the correct format.
Stopping any ongoing maintenance
If you used the startSiteMaintenance()
shorthand for creating a new maintenance window, you can also use the stopSiteMaintenance()
method.
$ohDear->stopSiteMaintenance(1);
This will stop any currently ongoing maintenance window.
The ideal use case is in deployment scripts, where you can call the following methods:
$ohDear->startSiteMaintenance(1, 600);
$ohDear->stopSiteMaintenance(1);
This will create a perfectly timed, on-demand maintenance window.
Deleting a particular maintenance window
If you have the maintenance period ID, you can also delete it.
$ohDear->deleteSiteMaintenance(1);
The above will delete maintenance period with ID 1
.