2 #============================================================= -*-perl-*-
4 # BackupPC_sendEmail: send status emails to users and admins
8 # BackupPC_sendEmail: send status emails to users and admins.
9 # BackupPC_sendEmail is run by BackupPC_nightly, so it runs
13 # Craig Barratt <cbarratt@users.sourceforge.net>
16 # Copyright (C) 2001-2003 Craig Barratt
18 # This program is free software; you can redistribute it and/or modify
19 # it under the terms of the GNU General Public License as published by
20 # the Free Software Foundation; either version 2 of the License, or
21 # (at your option) any later version.
23 # This program is distributed in the hope that it will be useful,
24 # but WITHOUT ANY WARRANTY; without even the implied warranty of
25 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26 # GNU General Public License for more details.
28 # You should have received a copy of the GNU General Public License
29 # along with this program; if not, write to the Free Software
30 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
32 #========================================================================
34 # Version 2.1.0, released 20 Jun 2004.
36 # See http://backuppc.sourceforge.net.
38 #========================================================================
42 use lib "__INSTALLDIR__/lib";
44 use BackupPC::FileZIO;
49 use vars qw($Lang $TopDir $BinDir %Conf);
51 die("BackupPC::Lib->new failed\n") if ( !(my $bpc = BackupPC::Lib->new) );
52 $TopDir = $bpc->TopDir();
53 $BinDir = $bpc->BinDir();
59 use vars qw(%UserEmailInfo);
60 do "$TopDir/log/UserEmailInfo.pl";
63 if ( !getopts("t", \%opts) || @ARGV != 0 ) {
64 print("usage: $0 [-t]\n");
68 my $err = $bpc->ServerConnect($Conf{ServerHost}, $Conf{ServerPort});
70 print("Can't connect to server ($err)\n");
73 my $reply = $bpc->ServerMesg("status hosts");
74 $reply = $1 if ( $reply =~ /(.*)/s );
75 my(%Status, %Info, %Jobs, @BgQueue, @UserQueue, @CmdQueue);
78 ###########################################################################
79 # Generate sysadmin warning messages
80 ###########################################################################
84 foreach my $host ( sort(keys(%Status)) ) {
85 next if ( $Status{$host}{reason} ne "backup failed"
86 || $Status{$host}{error} =~ /^lost network connection to host/ );
87 push(@badHosts, "$host ($Status{$host}{error})");
90 my $badHosts = join("\n - ", sort(@badHosts));
92 The following hosts had an error that is probably caused by a
93 misconfiguration. Please fix these hosts:
100 # Report if we skipped backups because the disk was too full
102 if ( $Info{DUDailySkipHostCntPrev} > 0 ) {
103 my $n = $Info{DUDailySkipHostCntPrev};
104 my $m = $Conf{DfMaxUsagePct};
106 Yesterday $n hosts were skipped because the file system containing
107 $TopDir was too full. The threshold in the
108 configuration file is $m%, while yesterday the file system was
109 up to $Info{DUDailyMaxPrev}% full. Please find more space on the file system,
110 or reduce the number of full or incremental backups that we keep.
116 # Check for bogus directories (probably PCs that are no longer
117 # on the backup list)
119 my $d = DirHandle->new("$TopDir/pc") or die("Can't read $TopDir/pc: $!");
121 my @files = $d->read;
123 foreach my $host ( @files ) {
124 next if ( $host eq "." || $host eq ".." || defined($Status{$host}) );
125 push(@oldDirs, "$TopDir/pc/$host");
128 my $oldDirs = join("\n - ", sort(@oldDirs));
130 The following directories are bogus and are not being used by
131 BackupPC. This typically happens when PCs are removed from the
132 backup list. If you don't need any old backups from these PCs you
133 should remove these directories. If there are machines on this
134 list that should be backed up then there is a problem with the
141 if ( $mesg ne "" && $Conf{EMailAdminUserName} ne "" ) {
143 To: $Conf{EMailAdminUserName}
144 Subject: BackupPC administrative attention needed
150 print("#" x 75, "\n");
157 ###########################################################################
158 # Generate per-host warning messages sent to each user
159 ###########################################################################
160 my $Hosts = $bpc->HostInfoRead();
162 foreach my $host ( sort(keys(%Status)) ) {
163 next if ( $Hosts->{$host}{user} eq "" );
165 # read any per-PC config settings (allowing per-PC email settings)
167 $bpc->ConfigRead($host);
168 %Conf = $bpc->Conf();
169 my $user = $Hosts->{$host}{user};
170 next if ( time - $UserEmailInfo{$user}{lastTime}
171 < $Conf{EMailNotifyMinDays} * 24*3600 );
172 next if ($Conf{XferMethod} eq "archive" );
173 my @Backups = $bpc->BackupInfoRead($host);
174 my $numBackups = @Backups;
175 if ( $numBackups == 0 ) {
176 my $subj = defined($Conf{EMailNoBackupEverSubj})
177 ? $Conf{EMailNoBackupEverSubj}
178 : $Lang->{EMailNoBackupEverSubj};
179 my $mesg = defined($Conf{EMailNoBackupEverMesg})
180 ? $Conf{EMailNoBackupEverMesg}
181 : $Lang->{EMailNoBackupEverMesg};
182 sendUserEmail($user, $host, $mesg, $subj, {
183 userName => user2name($user)
184 }) if ( !defined($Jobs{$host}) );
187 my $last = my $lastFull = my $lastIncr = 0;
188 my $lastGoodOutlook = 0;
190 my $numBadOutlook = 0;
191 for ( my $i = 0 ; $i < @Backups ; $i++ ) {
193 $lastNum = $Backups[$i]{num} if ( $lastNum < $Backups[$i]{num} );
194 if ( $Backups[$i]{type} eq "full" ) {
195 $lastFull = $Backups[$i]{startTime}
196 if ( $lastFull < $Backups[$i]{startTime} );
198 $lastIncr = $Backups[$i]{startTime}
199 if ( $lastIncr < $Backups[$i]{startTime} );
201 $last = $Backups[$i]{startTime}
202 if ( $last < $Backups[$i]{startTime} );
204 my $file = "$TopDir/pc/$host/SmbLOG.$Backups[$i]{num}";
207 $file = "$TopDir/pc/$host/XferLOG.$Backups[$i]{num}";
210 $file = "$TopDir/pc/$host/SmbLOG.$Backups[$i]{num}.z";
211 $file = "$TopDir/pc/$host/XferLOG.$Backups[$i]{num}.z"
215 next if ( !defined($fh = BackupPC::FileZIO->open($file, 0, $comp)) );
217 my $s = $fh->readLine();
218 last if ( $s eq "" );
219 if ( $s =~ /^\s*Error reading file.*\.pst : ERRDOS - ERRlock/
220 || $s =~ /^\s*Error reading file.*\.pst\. Got 0 bytes/ ) {
226 $numBadOutlook += $badOutlook;
227 if ( !$badOutlook ) {
228 $lastGoodOutlook = $Backups[$i]{startTime}
229 if ( $lastGoodOutlook < $Backups[$i]{startTime} );
232 if ( time - $last > $Conf{EMailNotifyOldBackupDays} * 24*3600 ) {
233 my $subj = defined($Conf{EMailNoBackupRecentSubj})
234 ? $Conf{EMailNoBackupRecentSubj}
235 : $Lang->{EMailNoBackupRecentSubj};
236 my $mesg = defined($Conf{EMailNoBackupRecentMesg})
237 ? $Conf{EMailNoBackupRecentMesg}
238 : $Lang->{EMailNoBackupRecentMesg};
239 my $firstTime = sprintf("%.1f",
240 (time - $Backups[0]{startTime}) / (24*3600));
241 my $days = sprintf("%.1f", (time - $last) / (24 * 3600));
242 sendUserEmail($user, $host, $mesg, $subj, {
243 firstTime => $firstTime,
245 userName => user2name($user),
246 numBackups => $numBackups,
247 }) if ( !defined($Jobs{$host}) );
250 if ( $numBadOutlook > 0
251 && time - $lastGoodOutlook > $Conf{EMailNotifyOldOutlookDays}
254 if ( $lastGoodOutlook == 0 ) {
255 $howLong = eval("qq{$Lang->{howLong_not_been_backed_up}}");
257 $days = sprintf("%.1f", (time - $lastGoodOutlook) / (24*3600));
258 $howLong = eval("qq{$Lang->{howLong_not_been_backed_up_for_days_days}}");
260 my $subj = defined($Conf{EMailOutlookBackupSubj})
261 ? $Conf{EMailOutlookBackupSubj}
262 : $Lang->{EMailOutlookBackupSubj};
263 my $mesg = defined($Conf{EMailOutlookBackupMesg})
264 ? $Conf{EMailOutlookBackupMesg}
265 : $Lang->{EMailOutlookBackupMesg};
266 my $firstTime = sprintf("%.1f",
267 (time - $Backups[0]{startTime}) / (24*3600));
268 my $lastTime = sprintf("%.1f",
269 (time - $Backups[$#Backups]{startTime}) / (24*3600));
270 sendUserEmail($user, $host, $mesg, $subj, {
272 firstTime => $firstTime,
273 lastTime => $lastTime,
274 numBackups => $numBackups,
275 userName => user2name($user),
277 serverHost => $Conf{ServerHost},
278 }) if ( !defined($Jobs{$host}) );
282 $Data::Dumper::Indent = 1;
283 my $dumpStr = Data::Dumper->Dump(
285 [qw(*UserEmailInfo)]);
286 if ( open(HOST, ">", "$TopDir/log/UserEmailInfo.pl") ) {
288 print(HOST $dumpStr);
296 my($name) = (getpwnam($user))[6];
298 $name = $user if ( $name eq "" );
304 my($user, $host, $mesg, $subj, $vars) = @_;
305 $vars->{user} = $user;
306 $vars->{host} = $host;
307 $vars->{domain} = $Conf{EMailUserDestDomain};
308 $vars->{CgiURL} = $Conf{CgiURL};
309 $subj =~ s/\$(\w+)/defined($vars->{$1}) ? $vars->{$1} : "\$$1"/eg;
310 $vars->{subj} = $subj;
311 $mesg =~ s/\$(\w+)/defined($vars->{$1}) ? $vars->{$1} : "\$$1"/eg;
313 print("#" x 75, "\n");
318 $UserEmailInfo{$user}{lastTime} = time;
319 $UserEmailInfo{$user}{lastSubj} = $subj;
320 $UserEmailInfo{$user}{lastHost} = $host;
326 my($from) = $Conf{EMailFromUserName};
329 $from = "-f $from" if ( $from ne "" );
330 if ( !open(MAIL, "|$Conf{SendmailPath} -t $from") ) {
331 printf("Can't run sendmail ($Conf{SendmailPath}): $!\n");