Advertisement
Guest User

Untitled

a guest
Jul 3rd, 2015
227
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.27 KB | None | 0 0
  1. #!/usr/local/bin/perl -w
  2.  
  3. #============================================================= -*-perl-*-
  4. # courier-imap_postfix_last-login.pl
  5. #========================================================================
  6. # Version 0.0.1, 2015-06-04
  7. #========================================================================
  8.  
  9. # Parsing is possible only for one year at time due to syslog date/time format limitation.
  10. # The current year is hardcoded in the script.
  11.  
  12. use strict;
  13.  
  14. use Time::Local;
  15. use POSIX qw(strftime);
  16.  
  17. my $logDir = '/var/log';
  18.  
  19. # Jun 4 15:15:40 hostname imapd: LOGIN, user=user@example.com, ip=[::1], port=[43029], protocol=IMAP
  20. # Jun 4 15:15:46 hostname pop3d: LOGIN, user=user@example.com, ip=[::ffff:127.0.0.238], port=[15226]
  21. my $REcourierLOGIN =
  22. qr/^((\w{3})\s+(\d+) (\d{2}):(\d{2}):(\d{2})) \S+ (\S+): LOGIN, user=([^,]+),/;
  23.  
  24. # Jun 3 17:46:46 hostname postfix/smtpd[739497]: 9E9721E40254: client=127-0-0-2.example.com[127.0.0.2], sasl_method=LOGIN, sasl_username=user@example.com
  25. # Jun 4 00:00:02 hostname postfix/smtpd[999616]: 7F3D91E40254: client=unknown[127.0.0.6], sasl_method=LOGIN, sasl_username=user@example.com
  26. my $REpostfixSASL =
  27. qr/^((\w{3})\s+(\d+) (\d{2}):(\d{2}):(\d{2})) \S+ postfix\/(\S+)\[\d+\]: [[:xdigit:]]+: client=[^,]+, sasl_method=[^,]+, sasl_username=(.+)$/;
  28.  
  29. my $currentYear = strftime "%Y", localtime(time);
  30. my $filename = "-name maillog-$currentYear* ! -name maillog-${currentYear}01* -o -name maillog";
  31.  
  32. my %lastAccess;
  33.  
  34. my %month = (
  35. Jan => 1,
  36. Feb => 2,
  37. Mar => 3,
  38. Apr => 4,
  39. May => 5,
  40. Jun => 6,
  41. Jul => 7,
  42. Aug => 8,
  43. Sep => 9,
  44. Oct => 10,
  45. Nov => 11,
  46. Dec => 12,
  47. );
  48.  
  49. open( LIST, "find $logDir $filename | sort |" )
  50. or die("Cannot fetch filelist: $!\n");
  51.  
  52. foreach my $file (<LIST>) {
  53. my $counter = 0;
  54. chomp $file;
  55. print "Processing $file ... ";
  56.  
  57. open( MAILLOG, "zcat -f $file|" )
  58. or die("Cannot open $0: $!\n");
  59.  
  60. while (<MAILLOG>) {
  61. ( index( $_, ': LOGIN, ' ) == -1 )
  62. && ( index( $_, ', sasl_username=' ) == -1 )
  63. && next;
  64. if ( /$REcourierLOGIN/ || /$REpostfixSASL/ ) {
  65.  
  66. my $epoch =
  67. timelocal( $6, $5, $4, $3, $month{$2} - 1, $currentYear );
  68.  
  69. if ( !defined( $lastAccess{$8}{'epoch'} )
  70. || $epoch > $lastAccess{$8}{'epoch'} )
  71. {
  72. $lastAccess{$8}{'time'} = $1;
  73. $lastAccess{$8}{'epoch'} = $epoch;
  74. }
  75.  
  76. $lastAccess{$8}{'count'}++;
  77. $lastAccess{$8}{'service'}{$7}++;
  78. $counter++;
  79. #print 'U';
  80. }
  81. }
  82.  
  83. close(MAILLOG);
  84. print "matches: $counter\n";
  85. }
  86. close(LIST);
  87.  
  88. printf "\n%30s | %16s | %6s\n", "E-mail account", "Last access time",
  89. "Access count";
  90.  
  91. my $i = 77;
  92. while ( $i-- ) { print "-" }
  93. print "\n";
  94.  
  95. map {
  96. printf "%30s | %16s | %6d (",
  97. $_, $lastAccess{$_}{'time'}, $lastAccess{$_}{'count'};
  98. my $time = $_;
  99. map { print " $_=$lastAccess{$time}{'service'}{$_}"; }
  100. sort keys %{ $lastAccess{$_}{'service'} };
  101. print " )\n";
  102.  
  103. #my $epochTime = strftime "%b %e %H:%M:%S", localtime($lastAccess{$_}{'epoch'});
  104. #print $epochTime, "\n";
  105.  
  106. } sort keys %lastAccess;
  107.  
  108. beep();
  109.  
  110. exit;
  111.  
  112. sub beep {
  113. my $i = 0;
  114. $| = 1;
  115. while ( $i++ <= 2 ) { print "\a"; system("sleep .12"); }
  116. print "\a";
  117. $| = 0;
  118. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement