| 1 | package TheEye::Plugin::Notify::HipChat; | ||
| 2 | |||
| 3 | use 5.010; | ||
| 4 | use Mouse::Role; | ||
| 5 | use LWP::UserAgent; | ||
| 6 | |||
| 7 | # ABSTRACT: Plugin for TheEye to raise alerts in Pager Duty | ||
| 8 | # | ||
| 9 | our $VERSION = '0.5'; # VERSION | ||
| 10 | |||
| 11 | has 'hc_token' => ( | ||
| 12 | is => 'rw', | ||
| 13 | isa => 'Str', | ||
| 14 | lazy => 1, | ||
| 15 | required => 1, | ||
| 16 | default => '', | ||
| 17 | ); | ||
| 18 | |||
| 19 | has 'hc_room' => ( | ||
| 20 | is => 'rw', | ||
| 21 | isa => 'Str', | ||
| 22 | lazy => 1, | ||
| 23 | required => 1, | ||
| 24 | default => 'monitoring' | ||
| 25 | ); | ||
| 26 | |||
| 27 | has 'hc_from' => ( | ||
| 28 | is => 'rw', | ||
| 29 | isa => 'Str', | ||
| 30 | lazy => 1, | ||
| 31 | required => 1, | ||
| 32 | default => 'TheEye' | ||
| 33 | ); | ||
| 34 | |||
| 35 | has 'hc_url' => ( | ||
| 36 | is => 'ro', | ||
| 37 | isa => 'Str', | ||
| 38 | lazy => 1, | ||
| 39 | required => 1, | ||
| 40 | default => 'https://api.hipchat.com/v1/rooms/message?format=json&auth_token=', | ||
| 41 | ); | ||
| 42 | |||
| 43 | has 'hc_err' => ( | ||
| 44 | is => 'rw', | ||
| 45 | isa => 'HashRef', | ||
| 46 | required => 0, | ||
| 47 | default => 0, | ||
| 48 | predicate => 'has_hc_err', | ||
| 49 | ); | ||
| 50 | |||
| 51 | around 'notify' => sub { | ||
| 52 | my $orig = shift; | ||
| 53 | my ( $self, $tests ) = @_; | ||
| 54 | |||
| 55 | my @errors; | ||
| 56 | my $ua = LWP::UserAgent->new(agent => 'TheEye'); | ||
| 57 | foreach my $test (@{$tests}) { | ||
| 58 | foreach my $step ( @{ $test->{steps} } ) { | ||
| 59 | if ( $step->{status} eq 'not_ok' ) { | ||
| 60 | |||
| 61 | if($step->{message} =~ m{^(not ok \d+ -) ([^\s]+) (.*)$}){ | ||
| 62 | $step->{lead_in} = $1; | ||
| 63 | $step->{node} = $2; | ||
| 64 | $step->{test_error} = $3; | ||
| 65 | } | ||
| 66 | |||
| 67 | |||
| 68 | my $res = $ua->post( | ||
| 69 | $self->hc_url . $self->hc_token, { | ||
| 70 | room_id => $self->hc_room, | ||
| 71 | from => $self->hc_from, | ||
| 72 | message => $step->{message}, | ||
| 73 | message_format => 'text', | ||
| 74 | notify => 1, | ||
| 75 | color => 'red', | ||
| 76 | }); | ||
| 77 | print $res->decoded_content if $self->is_debug; | ||
| 78 | } | ||
| 79 | } | ||
| 80 | } | ||
| 81 | }; | ||
| 82 | |||
| 83 | 1; | ||
| 84 | |||
| 85 | =pod | ||
| 86 | |||
| 87 | =encoding UTF-8 | ||
| 88 | |||
| 89 | =head1 NAME | ||
| 90 | |||
| 91 | TheEye::Plugin::Notify::HipChat - Plugin for TheEye to raise alerts in Pager Duty | ||
| 92 | |||
| 93 | =head1 VERSION | ||
| 94 | |||
| 95 | version 0.5 | ||
| 96 | |||
| 97 | =head1 AUTHOR | ||
| 98 | |||
| 99 | Lenz Gschwendtner <lenz@springtimesoft.com> | ||
| 100 | |||
| 101 | =head1 COPYRIGHT AND LICENSE | ||
| 102 | |||
| 103 | This software is copyright (c) 2014 by springtimesoft LTD. | ||
| 104 | |||
| 105 | This is free software; you can redistribute it and/or modify it under | ||
| 106 | the same terms as the Perl 5 programming language system itself. | ||
| 107 | |||
| 108 | =cut | ||
| 109 | |||
| 110 | __DATA__ |