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 "/usr/local/BackupPC/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 "Reason_backup_failed"
86 && $Status{$host}{reason} ne "Reason_restore_failed")
87 || $Status{$host}{error} =~ /^lost network connection to host/ );
88 push(@badHosts, "$host ($Status{$host}{error})");
91 my $badHosts = join("\n - ", sort(@badHosts));
93 The following hosts had an error that is probably caused by a
94 misconfiguration. Please fix these hosts:
101 # Report if we skipped backups because the disk was too full
103 if ( $Info{DUDailySkipHostCntPrev} > 0 ) {
104 my $n = $Info{DUDailySkipHostCntPrev};
105 my $m = $Conf{DfMaxUsagePct};
107 Yesterday $n hosts were skipped because the file system containing
108 $TopDir was too full. The threshold in the
109 configuration file is $m%, while yesterday the file system was
110 up to $Info{DUDailyMaxPrev}% full. Please find more space on the file system,
111 or reduce the number of full or incremental backups that we keep.
117 # Check for bogus directories (probably PCs that are no longer
118 # on the backup list)
120 my $d = DirHandle->new("$TopDir/pc") or die("Can't read $TopDir/pc: $!");
122 my @files = $d->read;
124 foreach my $host ( @files ) {
125 next if ( $host eq "." || $host eq ".." || defined($Status{$host}) );
126 push(@oldDirs, "$TopDir/pc/$host");
129 my $oldDirs = join("\n - ", sort(@oldDirs));
131 The following directories are bogus and are not being used by
132 BackupPC. This typically happens when PCs are removed from the
133 backup list. If you don't need any old backups from these PCs you
134 should remove these directories. If there are machines on this
135 list that should be backed up then there is a problem with the
142 if ( $mesg ne "" && $Conf{EMailAdminUserName} ne "" ) {
144 To: $Conf{EMailAdminUserName}
145 Subject: BackupPC administrative attention needed
151 print("#" x 75, "\n");
158 ###########################################################################
159 # Generate per-host warning messages sent to each user
160 ###########################################################################
161 my $Hosts = $bpc->HostInfoRead();
163 foreach my $host ( sort(keys(%Status)) ) {
164 next if ( $Hosts->{$host}{user} eq "" );
166 # read any per-PC config settings (allowing per-PC email settings)
168 $bpc->ConfigRead($host);
169 %Conf = $bpc->Conf();
170 my $user = $Hosts->{$host}{user};
171 next if ( time - $UserEmailInfo{$user}{lastTime}
172 < $Conf{EMailNotifyMinDays} * 24*3600 );
173 next if ($Conf{XferMethod} eq "archive" );
174 my @Backups = $bpc->BackupInfoRead($host);
175 my $numBackups = @Backups;
176 if ( $numBackups == 0 ) {
177 my $subj = defined($Conf{EMailNoBackupEverSubj})
178 ? $Conf{EMailNoBackupEverSubj}
179 : $Lang->{EMailNoBackupEverSubj};
180 my $mesg = defined($Conf{EMailNoBackupEverMesg})
181 ? $Conf{EMailNoBackupEverMesg}
182 : $Lang->{EMailNoBackupEverMesg};
183 sendUserEmail($user, $host, $mesg, $subj, {
184 userName => user2name($user)
185 }) if ( !defined($Jobs{$host}) );
188 my $last = my $lastFull = my $lastIncr = 0;
189 my $lastGoodOutlook = 0;
191 my $numBadOutlook = 0;
192 for ( my $i = 0 ; $i < @Backups ; $i++ ) {
194 $lastNum = $Backups[$i]{num} if ( $lastNum < $Backups[$i]{num} );
195 if ( $Backups[$i]{type} eq "full" ) {
196 $lastFull = $Backups[$i]{startTime}
197 if ( $lastFull < $Backups[$i]{startTime} );
199 $lastIncr = $Backups[$i]{startTime}
200 if ( $lastIncr < $Backups[$i]{startTime} );
202 $last = $Backups[$i]{startTime}
203 if ( $last < $Backups[$i]{startTime} );
205 my $file = "$TopDir/pc/$host/SmbLOG.$Backups[$i]{num}";
208 $file = "$TopDir/pc/$host/XferLOG.$Backups[$i]{num}";
211 $file = "$TopDir/pc/$host/SmbLOG.$Backups[$i]{num}.z";
212 $file = "$TopDir/pc/$host/XferLOG.$Backups[$i]{num}.z"
216 next if ( !defined($fh = BackupPC::FileZIO->open($file, 0, $comp)) );
218 my $s = $fh->readLine();
219 last if ( $s eq "" );
220 if ( $s =~ /^\s*Error reading file.*\.pst : ERRDOS - ERRlock/
221 || $s =~ /^\s*Error reading file.*\.pst\. Got 0 bytes/ ) {
227 $numBadOutlook += $badOutlook;
228 if ( !$badOutlook ) {
229 $lastGoodOutlook = $Backups[$i]{startTime}
230 if ( $lastGoodOutlook < $Backups[$i]{startTime} );
233 if ( time - $last > $Conf{EMailNotifyOldBackupDays} * 24*3600 ) {
234 my $subj = defined($Conf{EMailNoBackupRecentSubj})
235 ? $Conf{EMailNoBackupRecentSubj}
236 : $Lang->{EMailNoBackupRecentSubj};
237 my $mesg = defined($Conf{EMailNoBackupRecentMesg})
238 ? $Conf{EMailNoBackupRecentMesg}
239 : $Lang->{EMailNoBackupRecentMesg};
240 my $firstTime = sprintf("%.1f",
241 (time - $Backups[0]{startTime}) / (24*3600));
242 my $days = sprintf("%.1f", (time - $last) / (24 * 3600));
243 sendUserEmail($user, $host, $mesg, $subj, {
244 firstTime => $firstTime,
246 userName => user2name($user),
247 numBackups => $numBackups,
248 }) if ( !defined($Jobs{$host}) );
251 if ( $numBadOutlook > 0
252 && time - $lastGoodOutlook > $Conf{EMailNotifyOldOutlookDays}
255 if ( $lastGoodOutlook == 0 ) {
256 $howLong = eval("qq{$Lang->{howLong_not_been_backed_up}}");
258 $days = sprintf("%.1f", (time - $lastGoodOutlook) / (24*3600));
259 $howLong = eval("qq{$Lang->{howLong_not_been_backed_up_for_days_days}}");
261 my $subj = defined($Conf{EMailOutlookBackupSubj})
262 ? $Conf{EMailOutlookBackupSubj}
263 : $Lang->{EMailOutlookBackupSubj};
264 my $mesg = defined($Conf{EMailOutlookBackupMesg})
265 ? $Conf{EMailOutlookBackupMesg}
266 : $Lang->{EMailOutlookBackupMesg};
267 my $firstTime = sprintf("%.1f",
268 (time - $Backups[0]{startTime}) / (24*3600));
269 my $lastTime = sprintf("%.1f",
270 (time - $Backups[$#Backups]{startTime}) / (24*3600));
271 sendUserEmail($user, $host, $mesg, $subj, {
273 firstTime => $firstTime,
274 lastTime => $lastTime,
275 numBackups => $numBackups,
276 userName => user2name($user),
278 serverHost => $Conf{ServerHost},
279 }) if ( !defined($Jobs{$host}) );
283 $Data::Dumper::Indent = 1;
284 my $dumpStr = Data::Dumper->Dump(
286 [qw(*UserEmailInfo)]);
287 if ( open(HOST, ">", "$TopDir/log/UserEmailInfo.pl") ) {
289 print(HOST $dumpStr);
297 my($name) = (getpwnam($user))[6];
299 $name = $user if ( $name eq "" );
305 my($user, $host, $mesg, $subj, $vars) = @_;
306 $vars->{user} = $user;
307 $vars->{host} = $host;
308 $vars->{domain} = $Conf{EMailUserDestDomain};
309 $vars->{CgiURL} = $Conf{CgiURL};
310 $subj =~ s/\$(\w+)/defined($vars->{$1}) ? $vars->{$1} : "\$$1"/eg;
311 $vars->{subj} = $subj;
312 $mesg =~ s/\$(\w+)/defined($vars->{$1}) ? $vars->{$1} : "\$$1"/eg;
314 print("#" x 75, "\n");
319 $UserEmailInfo{$user}{lastTime} = time;
320 $UserEmailInfo{$user}{lastSubj} = $subj;
321 $UserEmailInfo{$user}{lastHost} = $host;
327 my($from) = $Conf{EMailFromUserName};
330 $from = "-f $from" if ( $from ne "" );
331 if ( !open(MAIL, "|$Conf{SendmailPath} -t $from") ) {
332 printf("Can't run sendmail ($Conf{SendmailPath}): $!\n");