Welcome to Aaron Thompson's Page Sunday, February 12 2012 @ 00:06 CST  
Personal
Contact Info
Family
Resume
 
Development
CGI
probability project
 
Perl Modules
Win32::AD::User (cpan)
 
Shell
backup_file (source)
chkconfig (source)
gen-autohome (source)
group-utils (source)
mail-deny (source)
mail-logs (source | archive)
mailman-qmail (source)
qmail vacation (source)
 
Links
Beast of Burden LLC
Central Iowa LUG
CedarLUG
CedarvalleyPM
ITS-IS Home
ITS Home
UNI Home
 
 
#!/usr/bin/perl -w
  #mail-logs.pl v0.4
  # 
  #this script requires the use of Net:SMTP that can be gotten from CPAN.
  #
  #other commands used in this script:
  # dnsdomainname (developed with version [net-tools 1.54] 1.98 [1998-02-27])
  # hostname      (developed with version [net-tools 1.54] 1.98 [1998-02-27])
  # date          (developed with version [GNU sh-utils] 2.0 by David MacKenzie)
  # ls            (developed with [GNU fileutils] 4.0p by Richard Stallman and 
  #                David MacKenzie)
  #
  #
  # Copyright (C) 2001 Aaron Thompson 
  #  thompson@cns.uni.edu
  #
  # This program is free software; you can redistribute it and/or
  # modify it under the terms of the GNU General Public License
  # as published by the Free Software Foundation; either version 2
  # of the License, or (at your option) any later version.
  #
  # This program is distributed in the hope that it will be useful,
  # but WITHOUT ANY WARRANTY; without even the implied warranty of
  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  # GNU General Public License for more details. 
  #
  # You should have received a copy of the GNU General Public License
  # along with this program; if not, write to the Free Software
  # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
  # or visit http://www.gnu.org/copyleft/gpl.html
  #
  # Written to send a days log files in an email via SMTP. This was written to
  # be used with a daily cron that runs ~12:00.
  # 
  #
  # Changes Between Versions... (CHANGELOG)
  # =======================================
  # v0.1
  # ____________
  # first working version.
  # -> mailed Syslog logfiles.
  #
  # v0.2
  # ____________
  # -> added file names deviders to email
  # -> added support for previous days SMB logs
  # -> added support for previous days Bru logs
  #
  # v0.3
  # ____________
  # -> added support for previous and current days aide files.
  # -> added/fixed zcat support for compressed logs.
  #
  # v0.4
  # ____________
  # -> fixed call to date commands to properly get 'yesterday' (instead of using hack).
  # -> cleaned up &mailAide
  #
  #################################################################################################### 
use strict;
use Net::SMTP;

  #hostname stuph
   my $hostname = ""; chomp($hostname  = `/bin/hostname`);
	                   chomp($hostname .= "." . `/bin/dnsdomainname`);

  #date Stuph
   my $today = ""; chomp($today = `/bin/date +%b`); 
   if (int(`/bin/date +%e`)<10){$today .= "  " . int(`/bin/date +%e`)}
	                        else{$today .= " " . int(`/bin/date +%e`);}
   my $yesterday = ""; chomp($yesterday = `/bin/date -d 'yesterday' +%b`);
   if ((int(`/bin/date -d 'yesterday' +%e`))<10)
	      {$yesterday .= "  " . (int(`/bin/date -d 'yesterday' +%e`));}
     else{$yesterday .= " "  . (int(`/bin/date -d 'yesterday' +%e`));}

  #initialize List of logfiles.
   my @logFiles = ("/var/log/messages",   "/var/log/syslog",
                   "/var/log/kern.log",   "/var/log/auth.log", 
                   "/var/log/daemon.log", "/var/log/user.log",
                   "/var/log/vsftpd.log", "/var/log/mail.err",
                   "/var/log/mail.info",  "/var/log/mail.log",
                   "/var/log/mail.warn",  "/var/log/uucp.log");
   my $logFile;

  #mail defaults
   my $server   = "";                       #default smtp server
   my $mailTo   = "";                       #default email reciever
   my $mailFrom = "mail-logs\@$hostname";   #default email sender
   my $subject  = "";                       #default subject
   my $message  = "";                       #default message

   foreach $logFile (@logFiles){
     &mailLogs($yesterday,$logFile);
   }#hcaerof

#non-Syslog standard parse functions.
 &mailBruLogs;
 &mailSMBLogs;
 #&mailAide;

 exit(0);

######################################################################################################
sub mailAide{
  my $shortYesterday; chomp($shortYesterday = `/bin/date +%m`);
  if( int(`/bin/date -d 'yesterday' +%d`) < 10 )
          {$shortYesterday .= "0" . (int(`/bin/date -d 'yesterday' +%d`))}
      else{$shortYesterday .= (int(`/bin/date -d 'yesterday' +%d`))};#fi
  chomp($shortYesterday .= `/bin/date -d 'yesterday' +%y`);
  my $shortToday; chomp($shortToday = `/bin/date +%m%d%y`);
  my $aideFileBase = "aide-sys.chk-";
  my $aideBase = "/var/lib/aide/"; $aideBase .= $aideFileBase;

  $subject = "$hostname aide-sys.chk-$shortYesterday $yesterday";
  $message = "";

  if (-e $aideBase.$shortYesterday){
    $message.="\n\n$aideFileBase$shortYesterday\n===================================\n\n";
    open (INFILE, "cat $aideBase$shortYesterday |");
      while(<INFILE>){$message .= $_;};#elihw
    close(INFILE);
  }#fi

  if (-e $aideBase.$shortToday){
    $message.="\n\n$aideFileBase$shortToday\n===================================\n\n";
     open (INFILE, "cat $aideBase$shortToday |");
       while(<INFILE>){$message .= $_;};#elihw
     close(INFILE);
  }#fi
  
  if($message){sendMail($message);};#fi
  
}#ediAliam

######################################################################################################
sub mailSMBLogs{
  my $longYesterday; chomp($longYesterday =  `/bin/date +%Y\/%m`);
  if( int(`/bin/date -d 'yesterday' +%d`) < 10 )
        {$longYesterday .= "/0" . (int(`/bin/date -d 'yesterday' +%d`))}
    else{$longYesterday .= "/"  . (int(`/bin/date -d 'yesterday' +%d`))};#fi
  my $smbLog = "/var/log/samba/*.log* /var/log/samba/log*";
  my $file; my @files; my $keep = 0;

  @files = `/bin/ls $smbLog*`;

  $subject = "$hostname $smbLog $yesterday";
  $message = "";

  foreach $file (@files){
    $message .= "\n$file=====================================\n";
    if($file =~ /\.gz/){	 
	   open(INFILE, "/bin/zcat $file |");
        while(<INFILE>){if(/$longYesterday/){$message .= $_; $keep = 1;}else{$keep=0};}#elihw
      close(INFILE);
    }
    else{
      open(INFILE, "/bin/cat $file |");
        while(<INFILE>){
          if(/$longYesterday/){$message .= $_; $keep = 1;}
			                 else{if($keep > 0){if(/^\[200/){$keep=0;}else{$message .= $_;}}};
        }#elihw
      close(INFILE);
    }#fi
  }#hcaerof
  if($message){sendMail($message);};#fi
}#sogLBMSliam

######################################################################################################
sub mailBruLogs{
  my $longYesterday; $longYesterday = int (`/bin/date +%Y%m`);
  if( int(`/bin/date -d 'yesterday' +%d`) < 10 )
        {$longYesterday .= "0" . (int(`/bin/date -d 'yesterday' +%d`))}
    else{$longYesterday .= (int(`/bin/date -d 'yesterday' +%d`))}#fi
  my $bruLog = "/var/log/bruexeclog";
  $subject = "$hostname $bruLog $yesterday";
  $message = "";
  $message .= "\n$bruLog\n=====================================\n";
  open(INFILE, "/bin/cat $bruLog |");
    while(<INFILE>){if(/^$longYesterday/){$message .= $_;};}#elihw
  close(INFILE);
  if($message){sendMail($message);};#fi
}#sgoLurBliam

######################################################################################################
sub mailLogs{
  #$_[0] = filter string
  #$_[1] = file name
  my $filter   = "";
  my $fileName = "";
  my $file     = "";
  my @files    = "";
  
  $filter   = $_[0];
  $fileName = $_[1];

  @files = `/bin/ls $fileName*`;

 #setup email subject and message
  $subject = "$hostname $fileName $yesterday";
  $message = "";

  foreach $file (@files){
    $message .= "\n$file=====================================\n";
    if($file =~ /gz/){
      open(INFILE, "/bin/zcat $file |");
        #while(<INFILE>){if(/^$filter/){$message .= $_;};}#elihw
        while(<INFILE>){if(/$filter/){$message .= $_;};}#elihw
      close(INFILE);
    }
    else{
      open(INFILE, "/bin/cat $file |");
        #while(<INFILE>){if(/^$filter/){$message .= $_;};}#elihw
        while(<INFILE>){if(/$filter/){$message .= $_;};}#elihw
      close(INFILE);
    }#fi
  }#hcaerof
  
  #send message
  if($message){sendMail($message);};#fi
}#sogLliam

######################################################################################################
sub sendMail{
  #$smtp info found:
  #http://faqchest.dynhost.com/prgm/perlu-l/perl-00/perl-0010/perl-001005/perl00103111_16616.html
  # creditted to the Perl Cookbook by O'Reilly
   if($_[0]){$message = $_[0]};
	
   my $smtp = Net::SMTP->new($server);
   $smtp->mail($ENV{USER});
   $smtp->to($mailTo);
   $smtp->data();
   $smtp->datasend("To: $mailTo\n");
   $smtp->datasend("From: $mailFrom\n");
   $smtp->datasend("Subject: $subject\n");
   $smtp->datasend("\n");
   $smtp->datasend($message);
   $smtp->dataend();
   $smtp->quit;
}#end sendMail



syntax highlighted by Code2HTML, v. 0.9.1
Return to mail-logs page