1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350
|
#!/usr/bin/perl
#
# $Id: Logwatch.pm,v 1.5 2004/06/21 13:45:00 kirk Exp $
package Logwatch;
use strict;
use Exporter;
=pod
=head1 NAME
Logwatch -- Utility functions for Logwatch Perl modules.
=head1 SYNOPSIS
use Logwatch ':sort';
##
## Show CountOrder()
##
# Sample Data
my %UnknownUsers = (jb1o => 4, eo00 => 1, ma3d => 4, dr4b => 1);
my $sortClosure = CountOrder(%UnknownUsers);
foreach my $user (sort $sortClosure keys %UnknownUsers) {
my $plural = ($UnknownUsers{$user} > 1) ? "s" : "";
printf " %-8s : %2d time%s\n", $user, $UnknownUsers{$user}, $plural;
}
##
## Show TotalCountOrder()
##
# Sample Data
my %RelayDenied = ( some.server => {you@some.where => 2, foo@bar.com => 4},
other.server => { foo@bar.com => 14 }
);
my $sub = TotalCountOrder(%RelayDenied);
foreach my $relay (sort $sub keys %RelayDenied) {
print " $relay:\n";
my $countOrder = CountOrder(%{$RelayDenied{$relay}});
foreach my $dest (sort $countOrder keys %{$RelayDenied{$relay}}) {
my $plural = ($RelayDenied{$relay}{$dest} > 1) ? "s" : "";
printf " %-36s: %3d Time%s\n", $dest,
$RelayDenied{$relay}{$dest}, $plural;
}
}
use Logwatch ':ip';
##
## Show SortIP()
##
# Sample Data
@ReverseFailures = qw{10.1.1.1 172.16.1.1 10.2.2.2 192.168.1.1 };
@ReverseFailures = sort SortIP @ReverseFailures;
{ local $" = "\n "; print "Reverse DNS Failures:\n @ReverseFailures\n" }
-or-
##
## Show LookupIP()
##
foreach my $ip (sort SortIP @ReverseFailures) {
printf "%15s : %s\n", $ip, LookupIP($ip);
}
=head1 DESCRIPTION
This module provides utility functions intended for authors of Logwatch
scripts. The purpose is to abstract commonly performed actions into a
set of generally available subroutines. The subroutines can optionally
be imported into the local namespace.
=over 4
=cut
our @ISA = qw{Exporter};
our @EXPORT;
our @EXPORT_OK;
our %EXPORT_TAGS = (sort => [qw(CountOrder TotalCountOrder SortIP)],
ip => [qw(LookupIP SortIP)]);
Exporter::export_ok_tags(qw{sort ip});
$EXPORT_TAGS{all} = [@EXPORT, @EXPORT_OK];
=pod
=item I<CountOrder(%hash [, $coderef ])>
This function returns a closure suitable to be passed to Perl's C<sort>
builtin. When two values are passed to the closure, it compares the
numeric values of those keys in C<%hash>, and if they're equal, the
lexically order of the keys. Thus:
my $sortClosure = CountOrder(%UnknownUsers);
foreach my $user (sort $sortClosure keys %UnknownUsers) {
my $plural = ($UnknownUsers{$user} > 1) ? "s" : "";
printf " %-8s : %2d time%s\n", $user, $UnknownUsers{$user}, $plural;
}
Will print the keys and values of C<%UnknownUsers> in frequency order,
with keys of equal values sorted lexically.
The optional second argument is a coderef to be used to sort the keys in
an order other than lexically. (a reference to C<SortIP>, for example.)
=cut
# Use a closure to abstract the sort algorithm
sub CountOrder(\%;&) {
my $href = shift;
my $coderef = shift;
return sub {
# $a & $b are in the caller's namespace, moving this inside
# guarantees that the namespace of the sort is used, in case
# it's different (admittedly, that's highly unlikely), at a
# miniscule performance cost.
my $package = (caller)[0];
no strict 'refs'; # Back off, man. I'm a scientist.
my $A = $ {"${package}::a"};
my $B = $ {"${package}::b"};
use strict 'refs'; # We are a hedge. Please move along.
# Reverse the count, but not the compare
my $count = $href->{$B} <=> $href->{$A};
return $count if $count;
if (ref $coderef) {
$a = $A;
$b = $B;
&$coderef();
} else {
($A cmp $B);
}
}
}
=pod
=item I<TotalCountOrder(%hash [, $coderef ])>
This function returns a closure similar to that returned by
C<CountOrder()>, except that it assumes a hash of hashes, and totals the
keys of each sub hash. Thus:
my $sub = TotalCountOrder(%RelayDenied);
foreach my $relay (sort $sub keys %RelayDenied) {
print " $relay:\n";
my $countOrder = CountOrder(%{$RelayDenied{$relay}});
foreach my $dest (sort $countOrder keys %{$RelayDenied{$relay}}) {
my $plural = ($RelayDenied{$relay}{$dest} > 1) ? "s" : "";
printf " %-36s: %3d Time%s\n", $dest,
$RelayDenied{$relay}{$dest}, $plural;
}
}
Will print the relays in the order of their total denied destinations
(equal keys sort lexically), with each sub hash printed in frequency
order (equal keys sorted lexically)
The optional second argument is a coderef to be used to sort the keys in
an order other than lexically. (a reference to C<SortIP>, for example.)
=cut
sub TotalCountOrder(\%;&) {
my $href = shift;
my $coderef = shift;
my $cache = {};
return sub {
# $a & $b are in the caller's namespace, moving this inside
# guarantees that the namespace of the sort is used, in case
# it's different (admittedly, that's highly unlikely), at a
# miniscule performance cost.
my $package = (caller)[0];
no strict 'refs'; # Back off, man. I'm a scientist.
my $A = $ {"${package}::a"};
my $B = $ {"${package}::b"};
use strict 'refs'; # We are a hedge. Please move along.
my ($AA, $BB);
foreach my $tuple ( [\$A, \$AA], [\$B, \$BB] ) {
my $keyRef = $tuple->[0];
my $totalRef = $tuple->[1];
if (exists($cache->{$$keyRef})) {
$$totalRef = $cache->{$$keyRef};
} else {
grep {$$totalRef += $href->{$$keyRef}->{$_}}
keys %{$href->{$$keyRef}};
$cache->{$$keyRef} = $$totalRef;
}
}
my $count = $BB <=> $AA;
return $count if $count;
if (ref $coderef) {
$a = $A;
$b = $B;
&$coderef();
} else {
($A cmp $B);
}
}
}
=pod
=item I<SortIP>
This function is meant to be passed to the perl C<sort> builtin. It
sorts a list of "dotted quad" IP addresses by the values of the
individual octets.
=cut
sub canonical_ipv6_address {
my @a = split /:/, shift;
my @b = qw(0 0 0 0 0 0 0 0);
my $i = 0;
while (defined $a[0] and $a[0] ne '') {$b[$i++] = shift @a;}
@a = reverse @a;
$i = 7;
while (defined $a[0] and $a[0] ne '') {$b[$i--] = shift @a;}
@b;
}
sub SortIP {
# $a & $b are in the caller's namespace.
my $package = (caller)[0];
no strict 'refs'; # Back off, man. I'm a scientist.
my $A = $ {"${package}::a"};
my $B = $ {"${package}::b"};
$A =~ s/^::(ffff:)?(\d+\.\d+\.\d+\.\d+)$/$2/;
$B =~ s/^::(ffff:)?(\d+\.\d+\.\d+\.\d+)$/$2/;
use strict 'refs'; # We are a hedge. Please move along.
if ($A =~ /:/ and $B =~ /:/) {
my @a = canonical_ipv6_address($A);
my @b = canonical_ipv6_address($B);
while ($a[1] and $a[0] == $b[0]) {shift @a; shift @b;}
$a[0] <=> $b[0];
} elsif ($A =~ /:/) {
-1;
} elsif ($B =~ /:/) {
1;
} else {
my ($a1, $a2, $a3, $a4) = split /\./, $A;
my ($b1, $b2, $b3, $b4) = split /\./, $B;
$a1 <=> $b1 || $a2 <=> $b2 || $a3 <=> $b3 || $a4 <=> $b4;
}
}
=pod
=item I<LookupIP($dottedQuadIPaddress)>
This function performs a hostname lookup on a passed in IP address. It
returns the hostname (with the IP in parentheses) on success and the IP
address on failure. Results are cached, so that many calls with the same
argument don't tax the resolver resources.
For (new) backward compatibility, this function now uses the $DoLookup
variable in the caller's namespace to determine if lookups will be made.
=cut
# Might as well cache it for the duration of the run
my %LookupCache = ();
sub LookupIP {
my $Addr = $_[0];
# OOPS! The 4.3.2 scripts have a $DoLookup variable. Time for some
# backwards compatible hand-waving.
# for 99% of the uses of this function, assuming package 'main' would
# be sufficient, but a good perl hacker designs so that the other 1%
# isn't in for a nasty suprise.
my $pkg = (caller)[0];
# Default to true
my $DoLookup = 1;
{
# An eval() here would be shorter (and probably clearer to more
# people), but QUITE a bit slower. This function should be
# designed to be called a lot, so efficiency is important.
local *symTable = $main::{"$pkg\::"};
# here comes the "black magic," (this "no" is bound to the
# enclosing block)
no strict 'vars';
if (exists $symTable{'DoLookup'} && defined $symTable{'DoLookup'}) {
*symTable = $symTable{'DoLookup'};
$DoLookup = $symTable;
}
}
return $Addr unless($DoLookup);
return $LookupCache{$Addr} if exists ($LookupCache{$Addr});
if ($Addr =~ /:/ and $Addr !~ /^::ffff:(\d+\.\d+\.\d+\.\d+)/) {
return "unresolved IPv6 addr: $Addr";
}
$Addr =~ s/::ffff://;
my $PackedAddr = pack('C4', split /\./,$Addr);
if (my $name = gethostbyaddr ($PackedAddr,2)) {
my $val = "$name ($Addr)";
$LookupCache{$Addr} = $val;
return $val;
} else {
$LookupCache{$Addr} = $Addr;
return ($Addr);
}
}
=pod
=back
=head1 TAGS
In addition to importing each function name explicitly, the following
tags can be used.
=over 4
=item I<:sort>
Imports C<CountOrder>, C<TotalCountOrder and C<SortIP>
=item I<:ip>
Imports C<SortIP> and C<LookupIP>
=item I<:all>
Imports all importable symbols.
=cut
1;
# vi: shiftwidth=3 tabstop=3 et
|