adilbaig/pagerduty

PHP wrapper to PagerDuty Events API.

2.0.0 2016-10-11 21:25 UTC

README

PHP implementation of the PagerDuty Events API

Important: v2 is a complete rewrite of the library. It is not backwards compatible with v1.

For all new projects i suggest using v2. It is more flexible and easier to use than v1, and overcomes some of its predecessor's design limitations.

Latest Stable Version Total Downloads

Features :

  • Trigger, acknowledge and resolve incidents.
  • Support for PD contexts.
  • Unit Tests

Installation :

Add this line to your project's composer.json

{
...
    "require": {
        "adilbaig/pagerduty": "2.*"
    }
...
}

The packagist URL is https://packagist.org/packages/adilbaig/pagerduty

Usage:

Trigger an event

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

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

// In this example, we're triggering a "Service is down" message.
try {
    $response = (new TriggerEvent($serviceKey, "Service is down"))->send();
    if($response == 200)
        echo "Success";
    elseif($response == 403)
        echo "Rate Limited";  //You're being throttled. Slow down.
} catch(PagerDutyException $exception) { //This doesn't happen unless you've broken their guidelines. The API tries to minimize user mistakes
    var_dump($exception->getErrors());
}

Automatically send only one PagerDuty incident for repeated errors


//After this example, you will see just one incident on PD

(new TriggerEvent($serviceKey, "Service is down", true))->send();
(new TriggerEvent($serviceKey, "Service is down", true))->send();
(new TriggerEvent($serviceKey, "Service is down", true))->send();
(new TriggerEvent($serviceKey, "Service is down", true))->send();

Create a detailed 'trigger' event, add optional data. Dump the event and inspect response from PD

use \PagerDuty\TriggerEvent;
use \PagerDuty\Context\LinkContext;
use \PagerDuty\Context\ImageContext;

//Taken from the `trigger` example @ https://v2.developer.pagerduty.com/v2/docs/trigger-events

$event = new TriggerEvent($serviceKey, "FAILURE for production/HTTP on machine srv01.acme.com");
$event
    ->setClient("Sample Monitoring Service")
    ->setClientURL("https://monitoring.service.com")
    ->setDetails(["ping time": "1500ms", "load avg": 0.75])
    ->addContext(new LinkContext("http://acme.pagerduty.com"))
    ->addContext(new LinkContext("http://acme.pagerduty.com", "View the incident on PagerDuty"))
    ->addContext(new ImageContext("https://chart.googleapis.com/chart?chs=600x400&chd=t:6,2,9,5,2,5,7,4,8,2,1&cht=lc&chds=a&chxt=y&chm=D,0033FF,0,0,5,1"))

$response = null;
$rez = $event->send($response);
var_dump($response);

Acknowledge an event

(new AcknowledgeEvent($serviceKey, "incident key"))->send();

Resolve an event

(new ResolveEvent($serviceKey, "incident key"))->send();

Questions

Q. How do i get the service key from PagerDuty?

A. In your PagerDuty console, click 'Configuration' > 'Services'. Click the link under 'Integrations' column. It's the 'Integration Key'

Read more here : https://v2.developer.pagerduty.com/v2/docs/events-api#getting-started

Requirements

This library needs the curl pecl extension.

In Ubuntu, install it like so :

sudo apt-get install php5-curl