| 1 | package Mail::Decency::LogParser::GeoSource; | ||
| 2 | |||
| 3 | use Moose; | ||
| 4 | extends qw/ | ||
| 5 | Mail::Decency::LogParser::Core | ||
| 6 | /; | ||
| 7 | |||
| 8 | use version 0.74; our $VERSION = qv( "v0.1.4" ); | ||
| 9 | |||
| 10 | use Geo::IP; | ||
| 11 | use Data::Dumper; | ||
| 12 | |||
| 13 | =head1 NAME | ||
| 14 | |||
| 15 | Mail::Decency::LogParser::GeoSource | ||
| 16 | |||
| 17 | |||
| 18 | =head1 DESCRIPTION | ||
| 19 | |||
| 20 | Log statistics about geographical sender sources. | ||
| 21 | |||
| 22 | =head1 CLASS ATTRIBUTES | ||
| 23 | |||
| 24 | =head2 interval_formats : ArrayRef[Str] | ||
| 25 | |||
| 26 | Intervals in strftime format for L<DateTime> | ||
| 27 | |||
| 28 | =cut | ||
| 29 | |||
| 30 | has interval_formats => ( is => 'rw', isa => 'ArrayRef[Str]', default => sub { [] } ); | ||
| 31 | |||
| 32 | =head2 geo_ip : Geo::IP | ||
| 33 | |||
| 34 | Instance of Geo::IP | ||
| 35 | |||
| 36 | =cut | ||
| 37 | |||
| 38 | has geo_ip => ( is => 'ro', isa => 'Geo::IP', default => sub { Geo::IP->new( GEOIP_STANDARD ) } ); | ||
| 39 | |||
| 40 | =head2 enable_per_sender : Bool | ||
| 41 | |||
| 42 | Wheter source stats should be enabled per sender domain.. CAN BECOME VERY HUGE!! | ||
| 43 | |||
| 44 | Default: 0 | ||
| 45 | |||
| 46 | =cut | ||
| 47 | |||
| 48 | has enable_per_sender => ( is => 'rw', isa => 'Bool', default => 0 ); | ||
| 49 | |||
| 50 | =head2 enable_per_recipient : Bool | ||
| 51 | |||
| 52 | Wheter source stats should be enabled per recipient domain.. | ||
| 53 | |||
| 54 | Default: 0 | ||
| 55 | |||
| 56 | =cut | ||
| 57 | |||
| 58 | has enable_per_recipient => ( is => 'rw', isa => 'Bool', default => 0 ); | ||
| 59 | |||
| 60 | =head2 schema_definition : HashRef[HashRef] | ||
| 61 | |||
| 62 | Schema for database | ||
| 63 | |||
| 64 | =cut | ||
| 65 | |||
| 66 | has schema_definition => ( is => 'ro', isa => 'HashRef[HashRef]', default => sub { | ||
| 67 | { | ||
| 68 | geo => { | ||
| 69 | source => { | ||
| 70 | from_domain => [ varchar => 255 ], | ||
| 71 | to_domain => [ varchar => 255 ], | ||
| 72 | country => [ varchar => 2 ], | ||
| 73 | type => [ varchar => 25 ], | ||
| 74 | interval => [ varchar => 25 ], | ||
| 75 | counter => 'integer', | ||
| 76 | last_update => 'integer', | ||
| 77 | -unique => [ 'from_domain', 'to_domain', 'country', 'type', 'interval' ] | ||
| 78 | }, | ||
| 79 | } | ||
| 80 | }; | ||
| 81 | } ); | ||
| 82 | |||
| 83 | =head1 METHODS | ||
| 84 | |||
| 85 | =head2 init | ||
| 86 | |||
| 87 | =cut | ||
| 88 | |||
| 89 | sub setup { | ||
| 90 | my ( $self ) = @_; | ||
| 91 | |||
| 92 | $self->config->{ interval_formats } ||= [ '%Y-%m-%d', '%Y-%m', '%Y' ]; | ||
| 93 | $self->interval_formats( $self->config->{ interval_formats } ); | ||
| 94 | |||
| 95 | $self->enable_per_recipient( 1 ) | ||
| 96 | if $self->config->{ enable_per_recipient }; | ||
| 97 | |||
| 98 | $self->enable_per_sender( 1 ) | ||
| 99 | if $self->config->{ enable_per_sender }; | ||
| 100 | |||
| 101 | |||
| 102 | } | ||
| 103 | |||
| 104 | |||
| 105 | =head2 handle | ||
| 106 | |||
| 107 | Checks wheter incoming mail is whilist for final recipient | ||
| 108 | |||
| 109 | =cut | ||
| 110 | |||
| 111 | sub handle_data { | ||
| 112 | my ( $self, $parsed_ref ) = @_; | ||
| 113 | |||
| 114 | # no relevant | ||
| 115 | return | ||
| 116 | if ! $parsed_ref->{ ip } || ! $parsed_ref->{ final } || ! ( $parsed_ref->{ reject } || $parsed_ref->{ bounced } || $parsed_ref->{ sent } ); | ||
| 117 | |||
| 118 | # determine country | ||
| 119 | my $country = $self->geo_ip->country_code_by_addr( $parsed_ref->{ ip } ) || ""; | ||
| 120 | #return unless $country; | ||
| 121 | |||
| 122 | # setup save pairs | ||
| 123 | my @pairs = ( [ qw/ total total / ] ); | ||
| 124 | push @pairs, [ $parsed_ref->{ from_domain }, 'total' ] | ||
| 125 | if $self->enable_per_sender && $parsed_ref->{ from_domain }; | ||
| 126 | push @pairs, [ 'total', $parsed_ref->{ to_domain } ] | ||
| 127 | if $self->enable_per_recipient && $parsed_ref->{ to_domain }; | ||
| 128 | push @pairs, [ $parsed_ref->{ from_domain }, $parsed_ref->{ to_domain } ] | ||
| 129 | if $self->enable_per_sender && $self->enable_per_recipient | ||
| 130 | && $parsed_ref->{ from_domain } && $parsed_ref->{ to_domain }; | ||
| 131 | |||
| 132 | # determin types | ||
| 133 | my @types = qw/ total /; | ||
| 134 | push @types, 'reject' if $parsed_ref->{ reject }; | ||
| 135 | push @types, 'bounce' if $parsed_ref->{ bounced }; | ||
| 136 | push @types, 'sent' if $parsed_ref->{ sent }; | ||
| 137 | |||
| 138 | |||
| 139 | # determine intervals | ||
| 140 | my $dt = DateTime->now( time_zone => 'local' ); | ||
| 141 | my @intervals = ( 'total' ); | ||
| 142 | push @intervals, map { $dt->strftime( $_ ) } @{ $self->interval_formats }; | ||
| 143 | |||
| 144 | foreach my $interval( @intervals ) { | ||
| 145 | |||
| 146 | foreach my $type( @types ) { | ||
| 147 | |||
| 148 | foreach my $ref( @pairs ) { | ||
| 149 | my ( $from, $to ) = @$ref; | ||
| 150 | |||
| 151 | $self->database->usr_lock; | ||
| 152 | my $entry_ref = $self->database->get( geo => source => my $search_ref = { | ||
| 153 | from_domain => $from, | ||
| 154 | to_domain => $to, | ||
| 155 | type => $type, | ||
| 156 | interval => $interval, | ||
| 157 | country => $country, | ||
| 158 | } ) || { counter => 0 }; | ||
| 159 | $entry_ref->{ last_update } = time(); | ||
| 160 | $entry_ref->{ counter } ++; | ||
| 161 | $self->database->set( geo => source => $search_ref, $entry_ref ); | ||
| 162 | $self->database->usr_unlock; | ||
| 163 | } | ||
| 164 | } | ||
| 165 | } | ||
| 166 | } | ||
| 167 | |||
| 168 | |||
| 169 | |||
| 170 | =head2 print_stats | ||
| 171 | |||
| 172 | =cut | ||
| 173 | |||
| 174 | sub print_stats { | ||
| 175 | my ( $self ) = @_; | ||
| 176 | |||
| 177 | foreach my $type( qw/ sent bounced reject / ) { | ||
| 178 | print "# FROM $type\n"; | ||
| 179 | |||
| 180 | my ( $handle, $meth ) = $self->database->search_read( geo => source => { | ||
| 181 | type => $type, | ||
| 182 | from_domain => { "!=" => 'total' }, | ||
| 183 | } ); | ||
| 184 | while ( my $ref = $handle->$meth ) { | ||
| 185 | print "$ref->{ type };$ref->{ interval };$ref->{ from_domain };$ref->{ to_domain };$ref->{ country };$ref->{ counter }\n"; | ||
| 186 | } | ||
| 187 | |||
| 188 | print "\n# TO $type\n"; | ||
| 189 | ( $handle, $meth ) = $self->database->search_read( geo => source => { | ||
| 190 | type => $type, | ||
| 191 | to_domain => { "!=" => 'total' } | ||
| 192 | } ); | ||
| 193 | while ( my $ref = $handle->$meth ) { | ||
| 194 | print "$ref->{ type };$ref->{ interval };$ref->{ from_domain };$ref->{ to_domain };$ref->{ country };$ref->{ counter }\n"; | ||
| 195 | } | ||
| 196 | |||
| 197 | print "\n# TOTAL $type\n"; | ||
| 198 | ( $handle, $meth ) = $self->database->search_read( geo => source => { | ||
| 199 | type => $type, | ||
| 200 | from_domain => 'total', | ||
| 201 | to_domain => 'total' | ||
| 202 | } ); | ||
| 203 | while ( my $ref = $handle->$meth ) { | ||
| 204 | print "$ref->{ type };$ref->{ interval };$ref->{ from_domain };$ref->{ to_domain };$ref->{ country };$ref->{ counter }\n"; | ||
| 205 | } | ||
| 206 | print "\n\n"; | ||
| 207 | } | ||
| 208 | |||
| 209 | } | ||
| 210 | |||
| 211 | |||
| 212 | =head2 maintenance | ||
| 213 | |||
| 214 | Remove old cumulated entries. See maintenance_ttl | ||
| 215 | |||
| 216 | =cut | ||
| 217 | |||
| 218 | sub maintenance { | ||
| 219 | my ( $self ) = @_; | ||
| 220 | my $obsolete_time = time() - $self->maintenance_ttl; | ||
| 221 | |||
| 222 | $self->logger->debug0( "Clear obsolete entries (TTL ". $self->maintenance_ttl. ")" ); | ||
| 223 | |||
| 224 | $self->database->remove( geo => source => { | ||
| 225 | last_update => { | ||
| 226 | '<' => $obsolete_time | ||
| 227 | } | ||
| 228 | } ); | ||
| 229 | |||
| 230 | } | ||
| 231 | |||
| 232 | |||
| 233 | |||
| 234 | |||
| 235 | |||
| 236 | |||
| 237 | |||
| 238 | =head1 AUTHOR | ||
| 239 | |||
| 240 | Ulrich Kautz <uk@fortrabbit.de> | ||
| 241 | |||
| 242 | =head1 COPYRIGHT | ||
| 243 | |||
| 244 | Copyright (c) 2010 the L</AUTHOR> as listed above | ||
| 245 | |||
| 246 | =head1 LICENCSE | ||
| 247 | |||
| 248 | This library is free software and may be distributed under the same terms as perl itself. | ||
| 249 | |||
| 250 | =cut | ||
| 251 | |||
| 252 | 1; |