| 1 | package TheEye::Plugin::Notify::Prowl; | ||
| 2 | |||
| 3 | use 5.010; | ||
| 4 | use Mouse::Role; | ||
| 5 | use LWP::UserAgent; | ||
| 6 | use URI::Escape; | ||
| 7 | use XML::Simple; | ||
| 8 | use Data::Dumper; | ||
| 9 | |||
| 10 | # ABSTRACT: Plugin for TheEye to raise alerts via Prowl | ||
| 11 | # | ||
| 12 | our $VERSION = '0.5'; # VERSION | ||
| 13 | |||
| 14 | has 'prowl_apikeys' => ( | ||
| 15 | is => 'rw', | ||
| 16 | isa => 'ArrayRef', | ||
| 17 | lazy => 1, | ||
| 18 | required => 1, | ||
| 19 | default => sub { [] }, | ||
| 20 | ); | ||
| 21 | |||
| 22 | has 'prowl_app' => ( | ||
| 23 | is => 'rw', | ||
| 24 | isa => 'Str', | ||
| 25 | lazy => 1, | ||
| 26 | required => 1, | ||
| 27 | default => 'TheEye' | ||
| 28 | ); | ||
| 29 | |||
| 30 | has 'prowl_prio' => ( | ||
| 31 | is => 'rw', | ||
| 32 | isa => 'Int', | ||
| 33 | lazy => 1, | ||
| 34 | required => 1, | ||
| 35 | default => 0 | ||
| 36 | ); | ||
| 37 | |||
| 38 | has 'prowl_event' => ( | ||
| 39 | is => 'rw', | ||
| 40 | isa => 'Str', | ||
| 41 | lazy => 1, | ||
| 42 | required => 1, | ||
| 43 | default => 'alert' | ||
| 44 | ); | ||
| 45 | |||
| 46 | has 'prowl_message' => ( | ||
| 47 | is => 'rw', | ||
| 48 | isa => 'Str', | ||
| 49 | lazy => 1, | ||
| 50 | required => 1, | ||
| 51 | default => '' | ||
| 52 | ); | ||
| 53 | |||
| 54 | has 'prowl_url' => ( | ||
| 55 | is => 'ro', | ||
| 56 | isa => 'Str', | ||
| 57 | lazy => 1, | ||
| 58 | required => 1, | ||
| 59 | default => 'https://prowl.weks.net/publicapi/' | ||
| 60 | ); | ||
| 61 | |||
| 62 | has 'prowl_downtime' => ( | ||
| 63 | is => 'rw', | ||
| 64 | isa => 'Str', | ||
| 65 | lazy => 1, | ||
| 66 | required => 1, | ||
| 67 | default => '/tmp/theeye.downtime' | ||
| 68 | ); | ||
| 69 | |||
| 70 | has 'prowl_err' => ( | ||
| 71 | is => 'rw', | ||
| 72 | isa => 'Bool', | ||
| 73 | required => 0, | ||
| 74 | default => 0, | ||
| 75 | predicate => 'has_prowl_err', | ||
| 76 | ); | ||
| 77 | |||
| 78 | around 'notify' => sub { | ||
| 79 | my $orig = shift; | ||
| 80 | my ( $self, $tests ) = @_; | ||
| 81 | |||
| 82 | my @errors; | ||
| 83 | foreach my $test (@{$tests}) { | ||
| 84 | foreach my $step ( @{ $test->{steps} } ) { | ||
| 85 | if ( $step->{status} eq 'not_ok' ) { | ||
| 86 | my $message = 'we have a problem: ' . $test->{file} . "\n"; | ||
| 87 | $message .= $step->{message} . "\n"; | ||
| 88 | $message .= $step->{comment} if $step->{comment}; | ||
| 89 | push(@errors, $message); | ||
| 90 | } | ||
| 91 | } | ||
| 92 | } | ||
| 93 | if($errors[0]){ | ||
| 94 | $self->prowl_message(join("\n\n--~==##\n\n", @errors)); | ||
| 95 | $self->prowl_send(); | ||
| 96 | } | ||
| 97 | }; | ||
| 98 | |||
| 99 | sub prowl_verify { | ||
| 100 | my ( $self, $key ) = @_; | ||
| 101 | my $path = 'verify?apikey=' . $key; | ||
| 102 | my $result = $self->_call($path); | ||
| 103 | return $result; | ||
| 104 | } | ||
| 105 | |||
| 106 | sub prowl_add_key { | ||
| 107 | my ( $self, $key ) = @_; | ||
| 108 | if ( $self->prowl_verify($key) ) { | ||
| 109 | push( @{ $self->prowl_apikeys }, $key ); | ||
| 110 | } | ||
| 111 | else { | ||
| 112 | warn $self->err; | ||
| 113 | } | ||
| 114 | return; | ||
| 115 | } | ||
| 116 | |||
| 117 | sub prowl_send { | ||
| 118 | my $self = shift; | ||
| 119 | print STDERR Dumper($self) if $self->is_debug; | ||
| 120 | my @req; | ||
| 121 | if ( length $self->prowl_message > 10000 ) { | ||
| 122 | $self->prowl_message( substr( $self->prowl_message, 0, 10000 ) ); | ||
| 123 | } | ||
| 124 | if ( length $self->prowl_event > 1024 ) { | ||
| 125 | $self->prowl_event( substr( $self->prowl_event, 0, 1024 ) ); | ||
| 126 | } | ||
| 127 | push( @req, "apikey=" . join( ',', @{ $self->prowl_apikeys } ) ); | ||
| 128 | push( @req, "description=" . uri_escape( $self->prowl_message ) ); | ||
| 129 | push( @req, "event=" . uri_escape( $self->prowl_event ) ); | ||
| 130 | push( @req, "priority=" . $self->prowl_prio ); | ||
| 131 | push( @req, "application=" . uri_escape( $self->prowl_app ) ); | ||
| 132 | my $path = 'add?' . join( '&', @req ); | ||
| 133 | print STDERR "Request: $path\n" if $self->is_debug; | ||
| 134 | my $result; | ||
| 135 | $result = $self->_call($path) | ||
| 136 | unless -f $self->prowl_downtime; | ||
| 137 | return $result; | ||
| 138 | } | ||
| 139 | |||
| 140 | sub _call { | ||
| 141 | my ( $self, $path ) = @_; | ||
| 142 | my $uri = $self->prowl_url . $path; | ||
| 143 | print STDERR "URI: $uri\n" if $self->is_debug; | ||
| 144 | |||
| 145 | my $req = HTTP::Request->new(); | ||
| 146 | $req->method('GET'); | ||
| 147 | $req->uri($uri); | ||
| 148 | |||
| 149 | #$req->content( to_json($content) ) if ($content); | ||
| 150 | |||
| 151 | my $ua = LWP::UserAgent->new(); | ||
| 152 | my $res = $ua->request($req); | ||
| 153 | print STDERR "Result: " . $res->decoded_content . "\n" if $self->is_debug; | ||
| 154 | if ( $res->is_success ) { | ||
| 155 | return XMLin( $res->decoded_content ); | ||
| 156 | } | ||
| 157 | else { | ||
| 158 | if ( my $_err = XMLin( $res->decoded_content ) ) { | ||
| 159 | $self->prowl_err($_err); | ||
| 160 | } | ||
| 161 | else { | ||
| 162 | $self->prowl_err( $res->status_line ); | ||
| 163 | } | ||
| 164 | } | ||
| 165 | return; | ||
| 166 | } | ||
| 167 | |||
| 168 | 1; | ||
| 169 | |||
| 170 | =pod | ||
| 171 | |||
| 172 | =encoding UTF-8 | ||
| 173 | |||
| 174 | =head1 NAME | ||
| 175 | |||
| 176 | TheEye::Plugin::Notify::Prowl - Plugin for TheEye to raise alerts via Prowl | ||
| 177 | |||
| 178 | =head1 VERSION | ||
| 179 | |||
| 180 | version 0.5 | ||
| 181 | |||
| 182 | =head1 AUTHOR | ||
| 183 | |||
| 184 | Lenz Gschwendtner <lenz@springtimesoft.com> | ||
| 185 | |||
| 186 | =head1 COPYRIGHT AND LICENSE | ||
| 187 | |||
| 188 | This software is copyright (c) 2014 by springtimesoft LTD. | ||
| 189 | |||
| 190 | This is free software; you can redistribute it and/or modify it under | ||
| 191 | the same terms as the Perl 5 programming language system itself. | ||
| 192 | |||
| 193 | =cut | ||
| 194 | |||
| 195 | __DATA__ |