Download | Plain Text | No Line Numbers


  1. #!/usr/bin/perl
  2.  
  3. use strict;
  4.  
  5. #-------------------------------------------------------------------------------
  6.  
  7. our $logdir = "/var/log/apache2/confixx/domains/access";
  8. our %filecache = ();
  9. our %logcache = ();
  10. our $alarm = 10;
  11. our $errorlog = ($ARGV[0] eq 'errorlog') ? 1 : 0;
  12. $SIG{ALRM} = \&on_alarm;
  13.  
  14. #-------------------------------------------------------------------------------
  15.  
  16. sub on_alarm
  17. {
  18. #print "ALARM!!!", $/;
  19. foreach my $logfile (keys(%logcache))
  20. {
  21. #print "writing log for ", $logfile, $/;
  22. if (open(LOG, ">>" . $logfile))
  23. {
  24. foreach my $logline (@{$logcache{$logfile}})
  25. {
  26. print LOG $logline;
  27. }
  28. close(LOG);
  29. }
  30. delete($logcache{$logfile});
  31. }
  32.  
  33. alarm($alarm);
  34. }
  35.  
  36. #-------------------------------------------------------------------------------
  37.  
  38. alarm($alarm);
  39. while(my $line = <STDIN>)
  40. {
  41. my ($domain, $log) = split(/:#?:/, $line, 2);
  42. $domain = lc($domain);
  43. #print "received log for ", $domain, $/;
  44.  
  45. my $logfile;
  46. if (!defined($filecache{$domain}))
  47. {
  48. my $logfilelink = $logdir . "/" . $domain;
  49. next
  50. if (!-l $logfilelink);
  51. $logfile = readlink($logfilelink);
  52. $logfile =~ s/\/access_log/\/error_log/
  53. if ($errorlog);
  54. $filecache{$domain} = $logfile;
  55. }
  56. else
  57. {
  58. $logfile = $filecache{$domain};
  59. }
  60.  
  61. #print "received log for ", $domain, " file=", $logfile, $/;
  62. #if (!$errorlog)
  63. #{
  64. # $log =~ s/^(\d+\.\d+\.\d+)\.\d+ /$1.0 /;
  65. #}
  66. #else
  67. #{
  68. # $log =~ s/\[client (\d+\.\d+\.\d+)\.\d+\]/\[client $1.0\]/;
  69. #}
  70. push(@{$logcache{$logfile}}, $domain . ' ' . $log);
  71. }
  72.  
  73.