X-Git-Url: http://git.rot13.org/?p=BackupPC.git;a=blobdiff_plain;f=bin%2FBackupPC_sendEmail;h=6ac3773226ff7d9df9a205899cc014e358b9d3f5;hp=5c33fae934bd423d49bdffe25fe0327f6214c2c9;hb=ca593f66fd6c35764bd8997c6338b781330f019c;hpb=b81d2da5e16975674f011e4833337ac0fa24e0ea diff --git a/bin/BackupPC_sendEmail b/bin/BackupPC_sendEmail index 5c33fae..6ac3773 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.0beta2, released 11 Nov 2006. +# Version 3.2.0beta1, released 10 Jan 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(); @@ -75,6 +77,57 @@ EOF exit(1); } +# +# Upgrade legacy version of %UserEmailInfo +# +# Prior to 3.2.0, it was a hash with entries: +# +# $UserEmailInfo{$user}{lastTime} +# $UserEmailInfo{$user}{lastSubj} +# $UserEmailInfo{$user}{lastHost} +# +# However, if a user had multiple hosts, then an email about one +# host prevents mail delivery about other hosts. Starting in 3.2.0 +# the hash is: +# +# $UserEmailInfo{$user}{$host}{lastTime} +# $UserEmailInfo{$user}{$host}{lastSubj} +# +my $oldFormat = 0; +foreach my $user ( keys(%UserEmailInfo) ) { + if ( defined($UserEmailInfo{$user}{lastTime}) + && ref($UserEmailInfo{$user}{lastTime}) ne 'HASH' ) { + $oldFormat = 1; + last; + } +} +if ( $oldFormat ) { + # + # Convert to the new format + # + my %UserEmailInfoOld = %UserEmailInfo; + %UserEmailInfo = (); + foreach my $user ( keys(%UserEmailInfoOld) ) { + next if ( $user eq "" ); + my $host = $UserEmailInfoOld{$user}{lastHost}; + next if ( !defined($host) ); + $UserEmailInfo{$user}{$host}{lastTime} = $UserEmailInfoOld{$user}{lastTime}; + $UserEmailInfo{$user}{$host}{lastSubj} = $UserEmailInfoOld{$user}{lastSubj}; + } +} + +# +# Prune hosts that no longer exist +# +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}); +} + my $err = $bpc->ServerConnect($Conf{ServerHost}, $Conf{ServerPort}); if ( $err ) { if ( $opts{c} && $Conf{EMailAdminUserName} ne "" ) { @@ -96,7 +149,7 @@ EOF exit(1); } exit(0) if ( $opts{c} ); -my $reply = $bpc->ServerMesg("status hosts"); +my $reply = $bpc->ServerMesg("status hosts info"); $reply = $1 if ( $reply =~ /(.*)/s ); my(%Status, %Info, %Jobs, @BgQueue, @UserQueue, @CmdQueue); eval($reply); @@ -120,99 +173,38 @@ EOF exit(0); } -########################################################################### -# Generate sysadmin warning messages -########################################################################### -my $mesg = ""; - -my @badHosts = (); -foreach my $host ( sort(keys(%Status)) ) { - next if ( ($Status{$host}{reason} ne "Reason_backup_failed" - && $Status{$host}{reason} ne "Reason_restore_failed") - || $Status{$host}{error} =~ /^lost network connection to host/ ); - push(@badHosts, "$host ($Status{$host}{error})"); -} -if ( @badHosts ) { - my $badHosts = join("\n - ", sort(@badHosts)); - $mesg .= < 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"); -} -if ( @oldDirs ) { - my $oldDirs = join("\n - ", sort(@oldDirs)); - $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 ) { @@ -326,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( @@ -337,6 +403,7 @@ if ( !$opts{t} ) { close(HOST); } } +exit(0); sub user2name { @@ -350,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}; @@ -357,12 +426,11 @@ 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; SendMail($mesg); - $UserEmailInfo{$user}{lastTime} = time; - $UserEmailInfo{$user}{lastSubj} = $subj; - $UserEmailInfo{$user}{lastHost} = $host; + $UserEmailInfo{$user}{$host}{lastTime} = time; + $UserEmailInfo{$user}{$host}{lastSubj} = $subj; } sub SendMail @@ -375,6 +443,7 @@ sub SendMail if ( $opts{t} ) { binmode(STDOUT, ":utf8") if ( $utf8 ); + print("#" x 75, "\n"); print $mesg; return;