Source:MailLog

From FAIWiki
Jump to navigation Jump to search

Source code of the hook script for sending logs by email. Store this code in $FAI/hooks/savelog.DEFAULT file.

#!/usr/bin/perl
# BEGIN LICENCE BLOCK
#
# Copyright (C) 2004 Michal Svamberg <svamberg@civ.zcu.cz>
#
# 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.
#
# END LICENCE BLOCK

use strict;
use Mail::Sender;

# ---------------------------------- START:CONF ---
my @SEND_TO = (
        'your@mail.address',
        'other@email.address'
);
my $SEND_FROM = 'fai@foo.bar';
my $SMTP_SERVER = "smtp.foo.bar";

# ---------------------------------- 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;