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 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494
|
#!/usr/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-2020 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 3 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, see <http://www.gnu.org/licenses/>.
#
#========================================================================
#
# Version 4.4.0, released 20 Jun 2020.
#
# See http://backuppc.sourceforge.net.
#
#========================================================================
use strict;
no utf8;
use lib "__INSTALLDIR__/lib";
use BackupPC::Lib;
use BackupPC::XS;
use BackupPC::DirOps;
use Encode;
use Data::Dumper;
use Getopt::Std;
use vars qw($Lang $TopDir $BinDir $LogDir %Conf %HostConf $Hosts);
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();
$Hosts = $bpc->HostInfoRead();
$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);
}
#
# 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 "" ) {
my $headers = $Conf{EMailHeaders};
$headers .= "\n" if ( $headers !~ /\n$/ );
my $subject =
$Conf{EMailAdminSubject} ne ""
? $Conf{EMailAdminSubject}
: "BackupPC: can't connect to server";
my $mesg = <<EOF;
To: $Conf{EMailAdminUserName}
Subject: $subject
$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 $subject =
$Conf{EMailAdminSubject} ne ""
? $Conf{EMailAdminSubject}
: "BackupPC test email";
my $mesg = <<EOF;
To: $opts{u}
Subject: $subject
$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 @AdminBadHostsEmail = ();
my @AdminBadHosts = ();
foreach my $host ( sort(keys(%Status)) ) {
#
# read any per-PC config settings (allowing per-PC email settings)
#
$bpc->ConfigRead($host);
%HostConf = $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/
&& !$HostConf{BackupsDisable} ) {
push(@AdminBadHostsEmail, $HostConf{EMailAdminUserName});
push(@AdminBadHosts, "$host ($Status{$host}{error})");
}
next
if ( time - $UserEmailInfo{$user}{$host}{lastTime} < $HostConf{EMailNotifyMinDays} * 24 * 3600
|| $HostConf{XferMethod} eq "archive"
|| $HostConf{BackupsDisable}
|| $Hosts->{$host}{user} eq ""
|| $user eq "" );
my @Backups = $bpc->BackupInfoRead($host);
my $last = 0;
my $lastGoodOutlook = 0;
my $lastNum = -1;
my $numBadOutlook = 0;
my $numBackups = 0;
for ( my $i = 0 ; $i < @Backups ; $i++ ) {
my $fh;
#
# ignore partials and active backups -> only fulls and incrs
# should be used in figuring out when the last good backup was
#
next if ( $Backups[$i]{type} eq "partial" || $Backups[$i]{type} eq "active" );
$lastNum = $Backups[$i]{num} if ( $lastNum < $Backups[$i]{num} );
$last = $Backups[$i]{startTime} if ( $last < $Backups[$i]{startTime} );
$numBackups++;
next if ( $Backups[$i]{xferMethod} ne "smb" );
my $badOutlook = 0;
my $comp = 0;
my $file = "$TopDir/pc/$host/XferLOG.$Backups[$i]{num}";
if ( !-f $file ) {
$comp = 1;
$file = "$TopDir/pc/$host/XferLOG.$Backups[$i]{num}.z";
}
next if ( !-f $file || !defined($fh = BackupPC::XS::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 ( $numBackups == 0 ) {
my $subj =
defined($HostConf{EMailNoBackupEverSubj})
? $HostConf{EMailNoBackupEverSubj}
: $Lang->{EMailNoBackupEverSubj};
my $mesg =
defined($HostConf{EMailNoBackupEverMesg})
? $HostConf{EMailNoBackupEverMesg}
: $Lang->{EMailNoBackupEverMesg};
sendUserEmail(
$user, $host, $mesg, $subj,
{
userName => user2name($user)
}
) if ( !defined($Jobs{$host}) );
next;
}
if ( time - $last > $HostConf{EMailNotifyOldBackupDays} * 24 * 3600 ) {
my $subj =
defined($HostConf{EMailNoBackupRecentSubj})
? $HostConf{EMailNoBackupRecentSubj}
: $Lang->{EMailNoBackupRecentSubj};
my $mesg =
defined($HostConf{EMailNoBackupRecentMesg})
? $HostConf{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 > $HostConf{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($HostConf{EMailOutlookBackupSubj})
? $HostConf{EMailOutlookBackupSubj}
: $Lang->{EMailOutlookBackupSubj};
my $mesg =
defined($HostConf{EMailOutlookBackupMesg})
? $HostConf{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 => $HostConf{ServerHost},
}
) if ( !defined($Jobs{$host}) );
}
}
#
# Send per-host errors to per-host admin email
#
if ( @AdminBadHosts ) {
for my $i ( 0 .. $#AdminBadHosts ) {
my $badHost = $AdminBadHosts[$i];
if ( $AdminBadHostsEmail[$i] ne "" ) {
my $badHostMesg .= <<EOF;
The following host had an error that is probably caused by a
misconfiguration. Please fix this host:
- $badHost
EOF
my $headers = $Conf{EMailHeaders};
$headers .= "\n" if ( $headers !~ /\n$/ );
my $subject =
$Conf{EMailAdminSubject} ne ""
? $Conf{EMailAdminSubject}
: "BackupPC administrative attention needed";
$badHostMesg = <<EOF;
To: $AdminBadHostsEmail[$i]
Subject: $subject
$headers
${badHostMesg}Regards,
PC Backup Genie
EOF
SendMail($badHostMesg);
}
}
}
###########################################################################
# Generate sysadmin warning message
###########################################################################
my $adminMesg = "";
#
# Report if we skipped backups because the disk was too full
#
if ( $Info{DUDailySkipHostCntPrev} > 0 ) {
my $n = $Info{DUDailySkipHostCntPrev};
my $m = $Conf{DfMaxUsagePct};
my $mInode = $Conf{DfMaxInodeUsagePct};
$adminMesg .= <<EOF;
Yesterday $n hosts were skipped because the file system containing
$TopDir was too full. The threshold in the configuration
file is currently $m%, while yesterday the file system was up
to $Info{DUDailyMaxPrev}% full. The maximum inode usage yesterday
was $Info{DUInodeDailyMaxPrev}% and the threshold is currently $mInode%.
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 @oldDirs = ();
foreach my $e ( @{BackupPC::DirOps::dirRead($bpc, "$TopDir/pc")} ) {
next if ( $e->{name} eq "." || $e->{name} eq ".." );
my $host = $e->{name};
next if ( 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$/ );
my $subject =
$Conf{EMailAdminSubject} ne ""
? $Conf{EMailAdminSubject}
: "BackupPC administrative attention needed";
$adminMesg = <<EOF;
To: $Conf{EMailAdminUserName}
Subject: $subject
$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) = @_;
return if ( $Conf{BackupsDisable} );
$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} = encode('MIME-Header', $subj);
$mesg =~ s/\$(\w+)/defined($vars->{$1}) ? $vars->{$1} : "\$$1"/eg;
SendMail($mesg);
$UserEmailInfo{$user}{$host}{lastTime} = time;
$UserEmailInfo{$user}{$host}{lastSubj} = $subj;
}
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") ) {
print("Can't run sendmail ($Conf{SendmailPath}): $!\n");
return;
}
binmode(MAIL, ":utf8") if ( $utf8 );
print MAIL $mesg;
close(MAIL);
}
|