Source:MailLog

From FAIWiki
Revision as of 12:53, 4 December 2005 by Svamberg (talk | contribs) (add references to documentation)
Jump to navigation Jump to search

Source file of hooks for sending logs by email. You store this code to $FAI/hooks/savelog.DEFAULT file.

#!/usr/bin/perl

use strict;
use Mail::Sender;

# ---------------------------------- START:CONF ---
my @SEND_TO = (
        'svamberg@civ.zcu.cz',
        'obal@civ.zcu.cz',
        'bodik@civ.zcu.cz',
        'steve@students.zcu.cz'
);
my $SEND_FROM = 'fai@fais1.civ.zcu.cz';
my $SMTP_SERVER = "smtp.zcu.cz";

# ---------------------------------- END:CONF ---

# ---------------------------------------------------------------- send_mail ---
sub send_mail {

        my $text = "this message sends via hooks/savelog.DEFAULT. See you attachments.";
         
        my $admin;
        my $attachment;
        my $filename;
        foreach $admin (@SEND_TO) {

                # make mail
                my $sender = new Mail::Sender({
#                               debug => "/tmp/smtp.txt",
                                smtp => '147.228.57.20',
                                from => $SEND_FROM
                          });
                $sender->OpenMultipart({
                        to => $admin,
                        subject => "FAI: " .`hostname` ." ($ENV{FAI_ACTION}, $ENV{FAI_RUNDATE})",
                });

                # body of mail
                $sender->Body({ msg => "$text\n" });
                
                # add logs as attachment
                foreach $attachment (<$ENV{LOGDIR}/*>) {
                        $filename = `basename $attachment`;
                        chomp $filename;
                        $sender->Attach({
                                ctype => "text/plain",
                                encoding => 'none',
                                file => "$attachment",
                                disposition => "attachment; filename=\"$filename\"",
                                description => "$filename"
                        });
                }

                # send mail
                $sender->Close or die "Close failed! $Mail::Sender::Error\n";
        }               
        return 0;
}

# ==============================================================================

print "Send log files via SMTP to " .(join ', ', @SEND_TO) ."\n";

# send emails
send_mail();

exit 0;