Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding pagerduty target #71

Merged
merged 4 commits into from
Oct 20, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion phplib/Target.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
* @package FOO
*/
abstract class Target extends Element {
public static $TYPES = ['Null_Target', 'WebHook_Target', 'Jira_Target', 'Slack_Target'];
public static $TYPES = ['Null_Target', 'WebHook_Target', 'Jira_Target', 'Slack_Target', 'PagerDuty_Target'];
public static $TABLE = 'search_targets';
public static $PKEY = 'target_id';

Expand Down
76 changes: 76 additions & 0 deletions phplib/Target/PagerDuty.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
<?php

namespace FOO;

/**
* PagerDuty Target Class
* Creates an incident for an Alert.
*/
class PagerDuty_Target extends Target {
public static $TYPE = 'pagerduty';

public static $DESC = 'Create an incident for this alert.';

protected static function generateDataSchema() {
return [
'service_key' => [static::T_STR, null, '']
];
}

/**
* Create event.
*
* @param Alert An Alert object
* @param int $date The current date.
*/
public function process(Alert $alert, $date) {
$site = SiteFinder::getCurrent();
$desc = [
'date' => sprintf('%s', gmdate(DATE_RSS, $alert['alert_date'])),

];

// Don't show the link if this event isn't persisted.
if(!$alert->isNew()) {
$desc['link_to_alert'] = sprintf('https://%s/alert/%d', $site['host'], $alert['alert_id']);
}
foreach($alert['content'] as $key=>$value) {
$desc[$key] = $value;
}

$search = SearchFinder::getById($alert['search_id']);

$ret = self::createEvent(
[
'service_key' => $this->obj['data']['service_key'],
'event_type' => 'trigger',
'client' => '411',
'description' => sprintf('[%s] %s', $site['name'], $search['name']),
'details' => json_encode($desc),
]
);
if(!$ret) {
throw new TargetException(sprintf('Failed to send Event:%d to PagerDuty', $alert['alert_id']));
}
}

/**
* Create a PagerDuty Event.
* @param mixed[] $event_data Event data.
* @return string|null The incident key or null.
*/
public static function createEvent($event_data) {
$curl = new \Curl\Curl;
$curl->setHeader('Content-type', 'application/json');
$ret = $curl->post(
'https://events.pagerduty.com/generic/2010-04-15/create_event.json',
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is that... the api version?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think so? They don't call it out specifically in their API docs: https://v2.developer.pagerduty.com/v2/docs/trigger-events

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

json_encode($event_data)
);

if($curl->httpStatusCode != 200) {
throw new TargetException(sprintf('Remote server returned %d: %s: %s', $curl->httpStatusCode, $curl->httpErrorMessage, $ret));
}

return $ret->incident_key;
}
}