X-Git-Url: http://git.rot13.org/?p=BackupPC.git;a=blobdiff_plain;f=bin%2FBackupPC_sendEmail;h=c20c6ccfcfbb65db3ab5842f084a726d5d82c6ce;hp=8357bdcb815fb0f903de199a1b0510aaef4c9bfa;hb=488bb662f6d144d42376b3d14e9b1e438e00e6f8;hpb=5b3e6091d542c2e7445d5dd511cdf6e20aec8b8d diff --git a/bin/BackupPC_sendEmail b/bin/BackupPC_sendEmail index 8357bdc..c20c6cc 100755 --- a/bin/BackupPC_sendEmail +++ b/bin/BackupPC_sendEmail @@ -1,4 +1,4 @@ -#!/bin/perl +#!/usr/bin/perl #============================================================= -*-perl-*- # # BackupPC_sendEmail: send status emails to users and admins @@ -13,7 +13,7 @@ # Craig Barratt # # COPYRIGHT -# Copyright (C) 2001-2003 Craig Barratt +# Copyright (C) 2001-2009 Craig Barratt # # 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 @@ -31,7 +31,7 @@ # #======================================================================== # -# Version 3.0.0alpha, released 23 Jan 2006. +# Version 3.2.0, released 31 Jul 2010. # # See http://backuppc.sourceforge.net. # @@ -42,11 +42,12 @@ no utf8; use lib "/usr/local/BackupPC/lib"; use BackupPC::Lib; use BackupPC::FileZIO; +use Encode; use Data::Dumper; use Getopt::Std; use DirHandle (); -use vars qw($Lang $TopDir $BinDir $LogDir %Conf); +use vars qw($Lang $TopDir $BinDir $LogDir %Conf $Hosts); die("BackupPC::Lib->new failed\n") if ( !(my $bpc = BackupPC::Lib->new) ); $TopDir = $bpc->TopDir(); @@ -54,6 +55,7 @@ $LogDir = $bpc->LogDir(); $BinDir = $bpc->BinDir(); %Conf = $bpc->Conf(); $Lang = $bpc->Lang(); +$Hosts = $bpc->HostInfoRead(); $bpc->ChildInit(); @@ -61,119 +63,148 @@ use vars qw(%UserEmailInfo); do "$LogDir/UserEmailInfo.pl"; my %opts; -if ( !getopts("t", \%opts) || @ARGV != 0 ) { - print("usage: $0 [-t]\n"); - exit(1); -} - -my $err = $bpc->ServerConnect($Conf{ServerHost}, $Conf{ServerPort}); -if ( $err ) { - print("Can't connect to server ($err)\n"); - exit(1); -} -my $reply = $bpc->ServerMesg("status hosts"); -$reply = $1 if ( $reply =~ /(.*)/s ); -my(%Status, %Info, %Jobs, @BgQueue, @UserQueue, @CmdQueue); -eval($reply); +if ( !getopts("ctu:", \%opts) || @ARGV != 0 ) { + print < 0 ) { - my $n = $Info{DUDailySkipHostCntPrev}; - my $m = $Conf{DfMaxUsagePct}; - $mesg .= <new("$TopDir/pc") or die("Can't read $TopDir/pc: $!"); -my @oldDirs = (); -my @files = $d->read; -$d->close; -foreach my $host ( @files ) { - next if ( $host =~ /^\./ || defined($Status{$host}) ); - push(@oldDirs, "$TopDir/pc/$host"); +foreach my $user ( keys(%UserEmailInfo) ) { + foreach my $host ( keys(%{$UserEmailInfo{$user}}) ) { + next if ( defined($Hosts->{$host}) ); + delete($UserEmailInfo{$user}{$host}); + } + next if ( $UserEmailInfo{$user} ); + delete($UserEmailInfo{$user}); } -if ( @oldDirs ) { - my $oldDirs = join("\n - ", sort(@oldDirs)); - $mesg .= <ServerConnect($Conf{ServerHost}, $Conf{ServerPort}); +if ( $err ) { + if ( $opts{c} && $Conf{EMailAdminUserName} ne "" ) { + my $headers = $Conf{EMailHeaders}; + $headers .= "\n" if ( $headers !~ /\n$/ ); + my $mesg = <ServerMesg("status hosts info"); +$reply = $1 if ( $reply =~ /(.*)/s ); +my(%Status, %Info, %Jobs, @BgQueue, @UserQueue, @CmdQueue); +eval($reply); -if ( $mesg ne "" && $Conf{EMailAdminUserName} ne "" ) { +########################################################################### +# Generate test message if required +########################################################################### +if ( $opts{u} ne "" ) { my $headers = $Conf{EMailHeaders}; $headers .= "\n" if ( $headers !~ /\n$/ ); - $mesg = <HostInfoRead(); +my @AdminBadHosts = (); foreach my $host ( sort(keys(%Status)) ) { - next if ( $Hosts->{$host}{user} eq "" ); # # read any per-PC config settings (allowing per-PC email settings) # $bpc->ConfigRead($host); %Conf = $bpc->Conf(); my $user = $Hosts->{$host}{user}; - next if ( time - $UserEmailInfo{$user}{lastTime} - < $Conf{EMailNotifyMinDays} * 24*3600 ); - next if ($Conf{XferMethod} eq "archive" ); + + next if ( $user eq "" ); + + # + # Accumulate host errors for the admin email below + # + if ( ($Status{$host}{reason} eq "Reason_backup_failed" + || $Status{$host}{reason} eq "Reason_restore_failed") + && $Status{$host}{error} !~ /^lost network connection to host/ + && !$Conf{BackupsDisable} + ) { + push(@AdminBadHosts, "$host ($Status{$host}{error})"); + } + + next if ( time - $UserEmailInfo{$user}{$host}{lastTime} + < $Conf{EMailNotifyMinDays} * 24*3600 + || $Conf{XferMethod} eq "archive" + || $Conf{BackupsDisable} + || $Hosts->{$host}{user} eq "" + ); my @Backups = $bpc->BackupInfoRead($host); my $numBackups = @Backups; if ( $numBackups == 0 ) { @@ -194,6 +225,11 @@ foreach my $host ( sort(keys(%Status)) ) { my $numBadOutlook = 0; for ( my $i = 0 ; $i < @Backups ; $i++ ) { my $fh; + # + # ignore partials -> only fulls and incrs should be used + # in figuring out when the last good backup was + # + next if ( $Backups[$i]{type} eq "partial" ); $lastNum = $Backups[$i]{num} if ( $lastNum < $Backups[$i]{num} ); if ( $Backups[$i]{type} eq "full" ) { $lastFull = $Backups[$i]{startTime} @@ -282,6 +318,80 @@ foreach my $host ( sort(keys(%Status)) ) { }) if ( !defined($Jobs{$host}) ); } } + +########################################################################### +# Generate sysadmin warning message +########################################################################### +my $adminMesg = ""; + +if ( @AdminBadHosts ) { + my $badHosts = join("\n - ", sort(@AdminBadHosts)); + $adminMesg .= < 0 ) { + my $n = $Info{DUDailySkipHostCntPrev}; + my $m = $Conf{DfMaxUsagePct}; + $adminMesg .= <new("$TopDir/pc") or die("Can't read $TopDir/pc: $!"); +my @oldDirs = (); +my @files = $d->read; +$d->close; +foreach my $host ( @files ) { + next if ( $host =~ /^\./ || defined($Status{$host}) ); + push(@oldDirs, "$TopDir/pc/$host"); +} +if ( @oldDirs ) { + my $oldDirs = join("\n - ", sort(@oldDirs)); + $adminMesg .= <Dump( @@ -293,6 +403,7 @@ if ( !$opts{t} ) { close(HOST); } } +exit(0); sub user2name { @@ -306,6 +417,8 @@ sub user2name sub sendUserEmail { my($user, $host, $mesg, $subj, $vars) = @_; + return if ( $Conf{BackupsDisable} ); + $vars->{user} = $user; $vars->{host} = $host; $vars->{headers} = $Conf{EMailHeaders}; @@ -313,30 +426,36 @@ sub sendUserEmail $vars->{domain} = $Conf{EMailUserDestDomain}; $vars->{CgiURL} = $Conf{CgiURL}; $subj =~ s/\$(\w+)/defined($vars->{$1}) ? $vars->{$1} : "\$$1"/eg; - $vars->{subj} = $subj; + $vars->{subj} = encode('MIME-Header', $subj); $mesg =~ s/\$(\w+)/defined($vars->{$1}) ? $vars->{$1} : "\$$1"/eg; - if ( $opts{t} ) { - print("#" x 75, "\n"); - print $mesg; - } else { - SendMail($mesg); - } - $UserEmailInfo{$user}{lastTime} = time; - $UserEmailInfo{$user}{lastSubj} = $subj; - $UserEmailInfo{$user}{lastHost} = $host; + SendMail($mesg); + $UserEmailInfo{$user}{$host}{lastTime} = time; + $UserEmailInfo{$user}{$host}{lastSubj} = $subj; } sub SendMail { my($mesg) = @_; - my($from) = $Conf{EMailFromUserName}; + my $from = $Conf{EMailFromUserName}; + my $utf8 = 1 + if ( $Conf{EMailHeaders} =~ /Content-Type:.*charset="utf-?8"/i ); local(*MAIL); + if ( $opts{t} ) { + binmode(STDOUT, ":utf8") if ( $utf8 ); + + print("#" x 75, "\n"); + print $mesg; + return; + } $from = "-f $from" if ( $from ne "" ); + print("Sending test email using $Conf{SendmailPath} -t $from\n") + if ( $opts{u} ne "" ); if ( !open(MAIL, "|$Conf{SendmailPath} -t $from") ) { printf("Can't run sendmail ($Conf{SendmailPath}): $!\n"); return; } + binmode(MAIL, ":utf8") if ( $utf8 ); print MAIL $mesg; close(MAIL); }