File: BackupPC_sendEmail

package info (click to toggle)
backuppc 3.1.0-4lenny3
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 2,416 kB
  • ctags: 957
  • sloc: perl: 28,028; sh: 452; makefile: 72
file content (411 lines) | stat: -rwxr-xr-x 13,652 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
#!/bin/perl
#============================================================= -*-perl-*-
#
# BackupPC_sendEmail: send status emails to users and admins
#
# DESCRIPTION
#
#   BackupPC_sendEmail: send status emails to users and admins.
#   BackupPC_sendEmail is run by BackupPC_nightly, so it runs
#   once every night.
#
# AUTHOR
#   Craig Barratt  <cbarratt@users.sourceforge.net>
#
# COPYRIGHT
#   Copyright (C) 2001-2007  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
#   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
#
#========================================================================
#
# Version 3.1.0, released 25 Nov 2007.
#
# See http://backuppc.sourceforge.net.
#
#========================================================================

use strict;
no  utf8;
use lib "__INSTALLDIR__/lib";
use BackupPC::Lib;
use BackupPC::FileZIO;

use Data::Dumper;
use Getopt::Std;
use DirHandle ();
use vars qw($Lang $TopDir $BinDir $LogDir %Conf);

die("BackupPC::Lib->new failed\n") if ( !(my $bpc = BackupPC::Lib->new) );
$TopDir = $bpc->TopDir();
$LogDir = $bpc->LogDir();
$BinDir = $bpc->BinDir();
%Conf   = $bpc->Conf();
$Lang   = $bpc->Lang();

$bpc->ChildInit();

use vars qw(%UserEmailInfo);
do "$LogDir/UserEmailInfo.pl";

my %opts;
if ( !getopts("ctu:", \%opts) || @ARGV != 0 ) {
    print <<EOF;
usage: $0 [-t] [-c] [-u userEmail]
options:

  -t  display the emails that would be sent, without sending them

  -c  check if BackupPC is alive and send an email if not

  -u  send a test email to userEmail
EOF
    exit(1);
}

my $err = $bpc->ServerConnect($Conf{ServerHost}, $Conf{ServerPort});
if ( $err ) {
    if ( $opts{c} && $Conf{EMailAdminUserName} ne "" ) {
        my $headers = $Conf{EMailHeaders};
        $headers .= "\n" if ( $headers !~ /\n$/ );
        my $mesg = <<EOF;
To: $Conf{EMailAdminUserName}
Subject: BackupPC: can't connect to server
$headers
Error: cannot connect to BackupPC server.

Regards,
PC Backup Genie
EOF
        SendMail($mesg);
        exit(1);
    }
    print("Can't connect to server ($err)\n");
    exit(1);
}
exit(0) if ( $opts{c} );
my $reply = $bpc->ServerMesg("status hosts info");
$reply = $1 if ( $reply =~ /(.*)/s );
my(%Status, %Info, %Jobs, @BgQueue, @UserQueue, @CmdQueue);
eval($reply);

###########################################################################
# Generate test message if required
###########################################################################
if ( $opts{u} ne "" ) {
    my $headers = $Conf{EMailHeaders};
    $headers .= "\n" if ( $headers !~ /\n$/ );
    my $mesg = <<EOF;
To: $opts{u}
Subject: BackupPC test email
$headers
This is a test message from $0.

Regards,
PC Backup Genie
EOF
    SendMail($mesg);
    exit(0);
}

###########################################################################
# Generate per-host warning messages sent to each user
###########################################################################
my $Hosts = $bpc->HostInfoRead();
my @AdminBadHosts = ();

foreach my $host ( sort(keys(%Status)) ) {
    #
    # read any per-PC config settings (allowing per-PC email settings)
    #
    $bpc->ConfigRead($host);
    %Conf = $bpc->Conf();
    my $user = $Hosts->{$host}{user};

    #
    # 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}{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 ) {
        my $subj = defined($Conf{EMailNoBackupEverSubj})
			? $Conf{EMailNoBackupEverSubj}
			: $Lang->{EMailNoBackupEverSubj};
        my $mesg = defined($Conf{EMailNoBackupEverMesg})
		        ? $Conf{EMailNoBackupEverMesg}
			: $Lang->{EMailNoBackupEverMesg};
        sendUserEmail($user, $host, $mesg, $subj, {
                            userName => user2name($user)
                        }) if ( !defined($Jobs{$host}) );
        next;
    }
    my $last = my $lastFull = my $lastIncr = 0;
    my $lastGoodOutlook = 0;
    my $lastNum = -1;
    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}
                    if ( $lastFull < $Backups[$i]{startTime} );
        } else {
            $lastIncr = $Backups[$i]{startTime}
                    if ( $lastIncr < $Backups[$i]{startTime} );
        }
        $last = $Backups[$i]{startTime}
                    if ( $last < $Backups[$i]{startTime} );
        my $badOutlook = 0;
        my $file = "$TopDir/pc/$host/SmbLOG.$Backups[$i]{num}";
        my $comp = 0;
        if ( !-f $file ) {
            $file = "$TopDir/pc/$host/XferLOG.$Backups[$i]{num}";
            if ( !-f $file ) {
                $comp = 1;
                $file = "$TopDir/pc/$host/SmbLOG.$Backups[$i]{num}.z";
                $file = "$TopDir/pc/$host/XferLOG.$Backups[$i]{num}.z"
                                                        if ( !-f $file );
            }
        }
        next if ( !defined($fh = BackupPC::FileZIO->open($file, 0, $comp)) );
        while ( 1 ) {
            my $s = $fh->readLine();
            last if ( $s eq "" );
            if ( $s =~ /^\s*Error reading file.*\.pst : (ERRDOS - ERRlock|NT_STATUS_FILE_LOCK_CONFLICT)/
                  || $s =~ /^\s*Error reading file.*\.pst\. Got 0 bytes/ ) {
                $badOutlook = 1;
                last;
            }
        }
        $fh->close();
        $numBadOutlook += $badOutlook;
        if ( !$badOutlook ) {
            $lastGoodOutlook = $Backups[$i]{startTime}
                    if ( $lastGoodOutlook < $Backups[$i]{startTime} );
        }
    }
    if ( time - $last > $Conf{EMailNotifyOldBackupDays} * 24*3600 ) {
        my $subj = defined($Conf{EMailNoBackupRecentSubj})
			? $Conf{EMailNoBackupRecentSubj}
			: $Lang->{EMailNoBackupRecentSubj};
        my $mesg = defined($Conf{EMailNoBackupRecentMesg})
			? $Conf{EMailNoBackupRecentMesg}
			: $Lang->{EMailNoBackupRecentMesg};
        my $firstTime = sprintf("%.1f",
                        (time - $Backups[0]{startTime}) / (24*3600));
        my $days = sprintf("%.1f", (time - $last) / (24 * 3600));
        sendUserEmail($user, $host, $mesg, $subj, {
                            firstTime  => $firstTime,
                            days       => $days,
                            userName   => user2name($user),
                            numBackups => $numBackups,
                        }) if ( !defined($Jobs{$host}) );
        next;
    }
    if ( $numBadOutlook > 0
          && time - $lastGoodOutlook > $Conf{EMailNotifyOldOutlookDays}
                                             * 24 * 3600 ) {
        my($days, $howLong);
        if ( $lastGoodOutlook == 0 ) {
            $howLong = eval("qq{$Lang->{howLong_not_been_backed_up}}");
        } else {
            $days = sprintf("%.1f", (time - $lastGoodOutlook) / (24*3600));
            $howLong = eval("qq{$Lang->{howLong_not_been_backed_up_for_days_days}}");
        }
        my $subj = defined($Conf{EMailOutlookBackupSubj})
			? $Conf{EMailOutlookBackupSubj}
			: $Lang->{EMailOutlookBackupSubj};
        my $mesg = defined($Conf{EMailOutlookBackupMesg})
			? $Conf{EMailOutlookBackupMesg}
			: $Lang->{EMailOutlookBackupMesg};
        my $firstTime = sprintf("%.1f",
                        (time - $Backups[0]{startTime}) / (24*3600));
        my $lastTime = sprintf("%.1f",
                        (time - $Backups[$#Backups]{startTime}) / (24*3600));
        sendUserEmail($user, $host, $mesg, $subj, {
                            days       => $days,
                            firstTime  => $firstTime,
                            lastTime   => $lastTime,
                            numBackups => $numBackups,
                            userName   => user2name($user),
                            howLong    => $howLong,
                            serverHost => $Conf{ServerHost},
                        }) if ( !defined($Jobs{$host}) );
    }
}

###########################################################################
# Generate sysadmin warning message
###########################################################################
my $adminMesg = "";

if ( @AdminBadHosts ) {
    my $badHosts = join("\n  - ", sort(@AdminBadHosts));
    $adminMesg .= <<EOF;
The following hosts had an error that is probably caused by a
misconfiguration.  Please fix these hosts:
  - $badHosts

EOF
}

#
# Report if we skipped backups because the disk was too full
#
if ( $Info{DUDailySkipHostCntPrev} > 0 ) {
    my $n = $Info{DUDailySkipHostCntPrev};
    my $m = $Conf{DfMaxUsagePct};
    $adminMesg .= <<EOF;
Yesterday $n hosts were skipped because the file system containing
$TopDir was too full.  The threshold in the
configuration file is $m%, while yesterday the file system was
up to $Info{DUDailyMaxPrev}% full.  Please find more space on the file system,
or reduce the number of full or incremental backups that we keep.

EOF
}

#
# Check for bogus directories (probably PCs that are no longer
# on the backup list)
#
my $d = DirHandle->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 .= <<EOF;
The following directories are bogus and are not being used by
BackupPC.  This typically happens when PCs are removed from the
backup list.  If you don't need any old backups from these PCs you
should remove these directories.  If there are machines on this
list that should be backed up then there is a problem with the
hosts file:
  - $oldDirs

EOF
}

if ( $adminMesg ne "" && $Conf{EMailAdminUserName} ne "" ) {
    my $headers = $Conf{EMailHeaders};
    $headers .= "\n" if ( $headers !~ /\n$/ );
    $adminMesg = <<EOF;
To: $Conf{EMailAdminUserName}
Subject: BackupPC administrative attention needed
$headers
${adminMesg}Regards,
PC Backup Genie
EOF
    SendMail($adminMesg);
}

###########################################################################
# Save email state and exit
###########################################################################
if ( !$opts{t} ) {
    $Data::Dumper::Indent = 1;
    my $dumpStr = Data::Dumper->Dump(
             [\%UserEmailInfo],
             [qw(*UserEmailInfo)]);
    if ( open(HOST, ">", "$LogDir/UserEmailInfo.pl") ) {
	binmode(HOST);
        print(HOST $dumpStr);
        close(HOST);
    }
}
exit(0);

sub user2name
{
    my($user) = @_;
    my($name) = (getpwnam($user))[6];
    $name =~ s/\s.*//;
    $name = $user if ( $name eq "" );
    return $name;
}

sub sendUserEmail
{
    my($user, $host, $mesg, $subj, $vars) = @_;
    $vars->{user}     = $user;
    $vars->{host}     = $host;
    $vars->{headers}  = $Conf{EMailHeaders};
    $vars->{headers} .= "\n" if ( $vars->{headers} !~ /\n$/ );
    $vars->{domain}   = $Conf{EMailUserDestDomain};
    $vars->{CgiURL}   = $Conf{CgiURL};
    $subj =~ s/\$(\w+)/defined($vars->{$1}) ? $vars->{$1} : "\$$1"/eg;
    $vars->{subj}   = $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;
}

sub SendMail
{
    my($mesg) = @_;
    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;
    }
    if ( $utf8 ) {
	binmode(MAIL, ":utf8");
	if ($mesg =~ /^Subject: (.*)$/m) {
		my $new_subj = encode('MIME-Header', $1);
		$mesg =~ s/^Subject: .*$/Subject: $new_subj/m;
	}
    }
    print MAIL $mesg;
    close(MAIL);
}