| 1 | package Daemonise::Plugin::PagerDuty; | ||
| 2 | |||
| 3 | use Mouse::Role; | ||
| 4 | |||
| 5 | # ABSTRACT: Daemonise PagerDuty plugin | ||
| 6 | |||
| 7 | use WebService::PagerDuty; | ||
| 8 | use Carp; | ||
| 9 | |||
| 10 | |||
| 11 | has 'pagerduty_api_key' => ( | ||
| 12 | is => 'rw', | ||
| 13 | isa => 'Str', | ||
| 14 | required => 1, | ||
| 15 | ); | ||
| 16 | |||
| 17 | |||
| 18 | has 'pagerduty_subdomain' => ( | ||
| 19 | is => 'rw', | ||
| 20 | isa => 'Str', | ||
| 21 | required => 1, | ||
| 22 | ); | ||
| 23 | |||
| 24 | |||
| 25 | has 'pagerduty_service_key' => ( | ||
| 26 | is => 'rw', | ||
| 27 | isa => 'Str', | ||
| 28 | required => 1, | ||
| 29 | ); | ||
| 30 | |||
| 31 | |||
| 32 | has 'pagerduty' => ( | ||
| 33 | is => 'rw', | ||
| 34 | isa => 'WebService::PagerDuty', | ||
| 35 | ); | ||
| 36 | |||
| 37 | |||
| 38 | after 'configure' => sub { | ||
| 39 | my ($self, $reconfig) = @_; | ||
| 40 | |||
| 41 | $self->log("configuring PagerDuty plugin") if $self->debug; | ||
| 42 | |||
| 43 | if ( ref($self->config->{api}) eq 'HASH' | ||
| 44 | and ref($self->config->{api}->{pagerduty}) eq 'HASH') | ||
| 45 | { | ||
| 46 | foreach my $conf_key ('api_key', 'subdomain', 'service_key') { | ||
| 47 | my $attr = "pagerduty_" . $conf_key; | ||
| 48 | $self->$attr($self->config->{api}->{pagerduty}->{$conf_key}) | ||
| 49 | if defined $self->config->{api}->{pagerduty}->{$conf_key}; | ||
| 50 | } | ||
| 51 | } | ||
| 52 | |||
| 53 | $self->pagerduty( | ||
| 54 | WebService::PagerDuty->new( | ||
| 55 | api_key => $self->pagerduty_api_key, | ||
| 56 | subdomain => $self->pagerduty_subdomain, | ||
| 57 | )); | ||
| 58 | |||
| 59 | return; | ||
| 60 | }; | ||
| 61 | |||
| 62 | |||
| 63 | sub alert { | ||
| 64 | my ($self, $incident, $description, $details) = @_; | ||
| 65 | |||
| 66 | unless (ref \$incident eq 'SCALAR' | ||
| 67 | and $incident | ||
| 68 | and ref \$description eq 'SCALAR' | ||
| 69 | and $description) | ||
| 70 | { | ||
| 71 | carp 'missing or wrong type of mandatory arguments! ' | ||
| 72 | . 'usage: $d->alert("$incident", "$description", \%details)'; | ||
| 73 | return; | ||
| 74 | } | ||
| 75 | |||
| 76 | # $self->async and return; | ||
| 77 | |||
| 78 | # force $details to be a hash | ||
| 79 | unless (ref $details eq 'HASH') { | ||
| 80 | $details = {}; | ||
| 81 | } | ||
| 82 | |||
| 83 | # check if we have JobQueue stuff loaded and use it | ||
| 84 | eval { exists $self->job->{message}->{meta}->{command} }; | ||
| 85 | unless ($@) { | ||
| 86 | $details->{workflow} = $self->job->{message}->{data}->{command} | ||
| 87 | if exists $self->job->{message}->{data}->{command}; | ||
| 88 | $details->{workflow} = $self->job->{message}->{meta}->{workflow} | ||
| 89 | if exists $self->job->{message}->{meta}->{workflow}; | ||
| 90 | } | ||
| 91 | |||
| 92 | my $service = join('.', $self->name, $incident); | ||
| 93 | |||
| 94 | # graph that we had an incident if we can | ||
| 95 | if ($self->can('graph')) { | ||
| 96 | $self->graph("incidents.$service", 'error', 1, $description); | ||
| 97 | } | ||
| 98 | |||
| 99 | # and notify hipchat if we can | ||
| 100 | if ($self->can('notify')) { | ||
| 101 | $self->notify("$service: $description", delete $details->{room}); | ||
| 102 | } | ||
| 103 | |||
| 104 | $self->pagerduty->event( | ||
| 105 | service_key => $self->pagerduty_service_key, | ||
| 106 | incident_key => $incident, | ||
| 107 | description => $self->hostname . " $service: $description", | ||
| 108 | details => {%$details}, | ||
| 109 | )->trigger | ||
| 110 | unless $self->debug; | ||
| 111 | |||
| 112 | # exit; # async | ||
| 113 | return; | ||
| 114 | } | ||
| 115 | |||
| 116 | 1; | ||
| 117 | |||
| 118 | __END__ |