8 use Time
::HiRes qw
(gettimeofday tv_interval
);
13 use PVE
::APIClient
::LWP
;
18 use PMG
::ClusterConfig
;
25 my ($nodename, $noerr) = @_;
27 my $cinfo = PMG
::ClusterConfig-
>new();
29 foreach my $entry (values %{$cinfo->{ids
}}) {
30 if ($entry->{name
} eq $nodename) {
31 my $ip = $entry->{ip
};
32 return $ip if !wantarray;
33 my $family = PVE
::Tools
::get_host_address_family
($ip);
34 return ($ip, $family);
38 # fallback: try to get IP by other means
39 return PVE
::Network
::get_ip_from_hostname
($nodename, $noerr);
45 $cinfo = PMG
::ClusterConfig-
>new() if !$cinfo;
47 return $cinfo->{master
}->{name
} if defined($cinfo->{master
});
52 sub read_local_ssl_cert_fingerprint
{
53 my $cert_path = "/etc/pmg/pmg-api.pem";
57 my $bio = Net
::SSLeay
::BIO_new_file
($cert_path, 'r');
58 $cert = Net
::SSLeay
::PEM_read_bio_X509
($bio);
59 Net
::SSLeay
::BIO_free
($bio);
62 die "unable to read certificate '$cert_path' - $err\n";
65 if (!defined($cert)) {
66 die "unable to read certificate '$cert_path' - got empty value\n";
71 $fp = Net
::SSLeay
::X509_get_fingerprint
($cert, 'sha256');
74 die "unable to get fingerprint for '$cert_path' - $err\n";
77 if (!defined($fp) || $fp eq '') {
78 die "unable to get fingerprint for '$cert_path' - got empty value\n";
84 my $hostrsapubkey_fn = '/etc/ssh/ssh_host_rsa_key.pub';
85 my $rootrsakey_fn = '/root/.ssh/id_rsa';
86 my $rootrsapubkey_fn = '/root/.ssh/id_rsa.pub';
88 sub read_local_cluster_info
{
92 my $hostrsapubkey = PVE
::Tools
::file_read_firstline
($hostrsapubkey_fn);
93 $hostrsapubkey =~ s/^.*ssh-rsa\s+//i;
94 $hostrsapubkey =~ s/\s+root\@\S+\s*$//i;
96 die "unable to parse ${hostrsapubkey_fn}\n"
97 if $hostrsapubkey !~ m/^[A-Za-z0-9\.\/\
+]{200,}$/;
99 my $nodename = PVE
::INotify
::nodename
();
101 $res->{name
} = $nodename;
103 $res->{ip
} = PVE
::Network
::get_ip_from_hostname
($nodename);
105 $res->{hostrsapubkey
} = $hostrsapubkey;
107 if (! -f
$rootrsapubkey_fn) {
108 unlink $rootrsakey_fn;
109 my $cmd = ['ssh-keygen', '-t', 'rsa', '-N', '', '-b', '2048',
110 '-f', $rootrsakey_fn];
111 PMG
::Utils
::run_silent_cmd
($cmd);
114 my $rootrsapubkey = PVE
::Tools
::file_read_firstline
($rootrsapubkey_fn);
115 $rootrsapubkey =~ s/^.*ssh-rsa\s+//i;
116 $rootrsapubkey =~ s/\s+root\@\S+\s*$//i;
118 die "unable to parse ${rootrsapubkey_fn}\n"
119 if $rootrsapubkey !~ m/^[A-Za-z0-9\.\/\
+]{200,}$/;
121 $res->{rootrsapubkey
} = $rootrsapubkey;
123 $res->{fingerprint
} = read_local_ssl_cert_fingerprint
();
128 # X509 Certificate cache helper
130 my $cert_cache_nodes = {};
131 my $cert_cache_timestamp = time();
132 my $cert_cache_fingerprints = {};
134 sub update_cert_cache
{
136 $cert_cache_timestamp = time();
138 $cert_cache_fingerprints = {};
139 $cert_cache_nodes = {};
141 my $cinfo = PMG
::ClusterConfig-
>new();
143 foreach my $entry (values %{$cinfo->{ids
}}) {
144 my $node = $entry->{name
};
145 my $fp = $entry->{fingerprint
};
147 $cert_cache_fingerprints->{$fp} = 1;
148 $cert_cache_nodes->{$node} = $fp;
153 # load and cache cert fingerprint once
154 sub initialize_cert_cache
{
158 if defined($node) && !defined($cert_cache_nodes->{$node});
161 sub check_cert_fingerprint
{
164 # clear cache every 30 minutes at least
165 update_cert_cache
() if time() - $cert_cache_timestamp >= 60*30;
167 # get fingerprint of server certificate
170 $fp = Net
::SSLeay
::X509_get_fingerprint
($cert, 'sha256');
172 return 0 if $@ || !defined($fp) || $fp eq ''; # error
175 for my $expected (keys %$cert_cache_fingerprints) {
176 return 1 if $fp eq $expected;
181 return 1 if $check->();
183 # clear cache and retry at most once every minute
184 if (time() - $cert_cache_timestamp >= 60) {
185 syslog
('info', "Could not verify remote node certificate '$fp' with list of pinned certificates, refreshing cache");
193 my $sshglobalknownhosts = "/etc/ssh/ssh_known_hosts2";
194 my $rootsshauthkeys = "/root/.ssh/authorized_keys";
195 my $ssh_rsa_id = "/root/.ssh/id_rsa.pub";
197 sub update_ssh_keys
{
203 foreach my $node (values %{$cinfo->{ids
}}) {
204 $data .= "$node->{ip} ssh-rsa $node->{hostrsapubkey}\n";
205 $data .= "$node->{name} ssh-rsa $node->{hostrsapubkey}\n";
208 $old = PVE
::Tools
::file_get_contents
($sshglobalknownhosts, 1024*1024)
209 if -f
$sshglobalknownhosts;
211 PVE
::Tools
::file_set_contents
($sshglobalknownhosts, $data)
218 if (-f
$ssh_rsa_id) {
219 my $pub = PVE
::Tools
::file_get_contents
($ssh_rsa_id);
224 foreach my $node (values %{$cinfo->{ids
}}) {
225 $data .= "ssh-rsa $node->{rootrsapubkey} root\@$node->{name}\n";
228 if (-f
$rootsshauthkeys) {
229 my $mykey = PVE
::Tools
::file_get_contents
($rootsshauthkeys, 128*1024);
236 my @lines = split(/\n/, $data);
237 foreach my $line (@lines) {
238 if ($line !~ /^#/ && $line =~ m/(^|\s)ssh-(rsa|dsa)\s+(\S+)\s+\S+$/) {
239 next if $vhash->{$3}++;
241 $newdata .= "$line\n";
244 $old = PVE
::Tools
::file_get_contents
($rootsshauthkeys, 1024*1024)
245 if -f
$rootsshauthkeys;
247 PVE
::Tools
::file_set_contents
($rootsshauthkeys, $newdata, 0600)
251 my $cfgdir = '/etc/pmg';
252 my $syncdir = "$cfgdir/master";
254 my $cond_commit_synced_file = sub {
255 my ($filename, $dstfn) = @_;
257 $dstfn = "$cfgdir/$filename" if !defined($dstfn);
258 my $srcfn = "$syncdir/$filename";
265 my $new = PVE
::Tools
::file_get_contents
($srcfn, 1024*1024);
268 my $old = PVE
::Tools
::file_get_contents
($dstfn, 1024*1024);
269 return 0 if $new eq $old;
272 # set mtime (touch) to avoid time drift problems
273 utime(undef, undef, $srcfn);
275 rename($srcfn, $dstfn) ||
276 die "cond_rename_file '$filename' failed - $!\n";
278 print STDERR
"updated $dstfn\n";
283 my $rsync_command = sub {
284 my ($host_key_alias, @args) = @_;
286 my $ssh_cmd = '--rsh=ssh -l root -o BatchMode=yes';
287 $ssh_cmd .= " -o HostKeyAlias=${host_key_alias}" if $host_key_alias;
289 my $cmd = ['rsync', $ssh_cmd, '-q', @args];
294 sub sync_quarantine_files
{
295 my ($host_ip, $host_name, $flistname, $rcid) = @_;
297 my $spooldir = $PMG::MailQueue
::spooldir
;
299 mkdir "$spooldir/cluster/";
300 my $syncdir = "$spooldir/cluster/$rcid";
303 my $cmd = $rsync_command->(
304 $host_name, '--timeout', '10', "[${host_ip}]:$spooldir", $spooldir,
305 '--files-from', $flistname);
307 PVE
::Tools
::run_command
($cmd);
311 my ($host_ip, $host_name, $rcid) = @_;
313 my $spooldir = $PMG::MailQueue
::spooldir
;
315 mkdir "$spooldir/cluster/";
316 my $syncdir = "$spooldir/cluster/$rcid";
319 my $cmd = $rsync_command->(
320 $host_name, '-aq', '--timeout', '10', "[${host_ip}]:$syncdir/", $syncdir);
322 foreach my $incl (('spam/', 'spam/*', 'spam/*/*', 'virus/', 'virus/*', 'virus/*/*')) {
323 push @$cmd, '--include', $incl;
326 push @$cmd, '--exclude', '*';
328 PVE
::Tools
::run_command
($cmd);
331 sub sync_master_quar
{
332 my ($host_ip, $host_name) = @_;
334 my $spooldir = $PMG::MailQueue
::spooldir
;
336 my $syncdir = "$spooldir/cluster/";
339 my $cmd = $rsync_command->(
340 $host_name, '-aq', '--timeout', '10', "[${host_ip}]:$syncdir", $syncdir);
342 PVE
::Tools
::run_command
($cmd);
345 sub sync_config_from_master
{
346 my ($master_name, $master_ip, $noreload) = @_;
349 File
::Path
::remove_tree
($syncdir, {keep_root
=> 1});
351 my $sa_conf_dir = "/etc/mail/spamassassin";
352 my $sa_custom_cf = "custom.cf";
353 my $sa_rules_cf = "pmg-scores.cf";
355 my $cmd = $rsync_command->(
357 "[${master_ip}]:$cfgdir/* ${sa_conf_dir}/${sa_custom_cf} ${sa_conf_dir}/${sa_rules_cf}",
359 '--exclude', 'master/',
362 '--exclude', 'pmg-api.pem',
363 '--exclude', 'pmg-tls.pem',
366 my $errmsg = "syncing master configuration from '${master_ip}' failed";
367 PVE
::Tools
::run_command
($cmd, errmsg
=> $errmsg);
369 # verify that the remote host is cluster master
370 open (my $fh, '<', "$syncdir/cluster.conf") ||
371 die "unable to open synced cluster.conf - $!\n";
373 my $cinfo = PMG
::ClusterConfig
::read_cluster_conf
('cluster.conf', $fh);
375 if (!$cinfo->{master
} || ($cinfo->{master
}->{ip
} ne $master_ip)) {
376 die "host '$master_ip' is not cluster master\n";
379 my $role = $cinfo->{'local'}->{type
} // '-';
380 die "local node '$cinfo->{local}->{name}' not part of cluster\n"
383 die "local node '$cinfo->{local}->{name}' is new cluster master\n"
384 if $role eq 'master';
386 $cond_commit_synced_file->('cluster.conf');
388 update_ssh_keys
($cinfo); # rewrite ssh keys
390 PMG
::Fetchmail
::update_fetchmail_default
(0); # disable on slave
405 foreach my $filename (@$files) {
406 $cond_commit_synced_file->($filename);
415 foreach my $dir (@$dirs) {
416 my $srcdir = "$syncdir/$dir";
419 my $cmd = ['rsync', '-aq', '--delete-after', "$srcdir/", "$cfgdir/$dir"];
420 PVE
::Tools
::run_command
($cmd);
425 my $force_restart = {};
427 for my $file (($sa_custom_cf, $sa_rules_cf)) {
428 if ($cond_commit_synced_file->($file, "${sa_conf_dir}/${file}")) {
429 $force_restart->{'pmg-smtp-filter'} = 1;
433 $cond_commit_synced_file->('pmg.conf');
435 return $force_restart;
438 sub sync_ruledb_from_master
{
439 my ($ldb, $rdb, $ni, $ticket) = @_;
441 my $ruledb = PMG
::RuleDB-
>new($ldb);
442 my $rulecache = PMG
::RuleCache-
>new($ruledb);
444 my $conn = PVE
::APIClient
::LWP-
>new(
446 cookie_name
=> 'PMGAuthCookie',
448 cached_fingerprints
=> {
449 $ni->{fingerprint
} => 1,
452 my $digest = $conn->get("/config/ruledb/digest", {});
454 return if $digest eq $rulecache->{digest
}; # no changes
456 syslog
('info', "detected rule database changes - starting sync from '$ni->{ip}'");
461 $ldb->do("DELETE FROM Rule");
462 $ldb->do("DELETE FROM RuleGroup");
463 $ldb->do("DELETE FROM ObjectGroup");
464 $ldb->do("DELETE FROM Object");
465 $ldb->do("DELETE FROM Attribut");
470 # read a consistent snapshot
471 $rdb->do("SET TRANSACTION ISOLATION LEVEL SERIALIZABLE");
473 PMG
::DBTools
::copy_table
($ldb, $rdb, "Rule");
474 PMG
::DBTools
::copy_table
($ldb, $rdb, "RuleGroup");
475 PMG
::DBTools
::copy_table
($ldb, $rdb, "ObjectGroup");
476 PMG
::DBTools
::copy_table
($ldb, $rdb, "Object", 'value');
477 PMG
::DBTools
::copy_table
($ldb, $rdb, "Attribut", 'value');
480 $rdb->rollback; # end transaction
486 $ldb->do("SELECT setval('rule_id_seq', max(id)+1) FROM Rule");
487 $ldb->do("SELECT setval('object_id_seq', max(id)+1) FROM Object");
488 $ldb->do("SELECT setval('objectgroup_id_seq', max(id)+1) FROM ObjectGroup");
497 PMG
::DBTools
::reload_ruledb
();
499 syslog
('info', "finished rule database sync from host '$ni->{ip}'");
502 sub sync_quarantine_db
{
503 my ($ldb, $rdb, $ni, $rsynctime_ref) = @_;
505 my $rcid = $ni->{cid
};
507 my $maxmails = 100000;
511 my $ctime = PMG
::DBTools
::get_remote_time
($rdb);
517 PMG
::DBTools
::create_clusterinfo_default
($ldb, $rcid, 'lastid_CMailStore', -1, undef);
519 do { # get new values
523 my $flistname = "/tmp/quarantinefilelist.$$";
528 open(my $flistfh, '>', $flistname) ||
529 die "unable to open file '$flistname' - $!\n";
531 my $lastid = PMG
::DBTools
::read_int_clusterinfo
($ldb, $rcid, 'lastid_CMailStore');
535 my $sth = $rdb->prepare(
536 "SELECT * from CMailstore WHERE cid = ? AND rid > ? " .
537 "ORDER BY cid,rid LIMIT ?");
538 $sth->execute($rcid, $lastid, $maxcount);
543 $maxid = $ref->{rid
};
544 my $filename = $ref->{file
};
545 # skip files generated before cluster was created
546 return if $filename !~ m!^cluster/!;
547 print $flistfh "$filename\n";
550 my $attrs = [qw(cid rid time qtype bytes spamlevel info sender header file)];
551 $count += PMG
::DBTools
::copy_selected_data
($ldb, $sth, 'CMailStore', $attrs, $callback);
555 my $starttime = [ gettimeofday
() ];
556 sync_quarantine_files
($ni->{ip
}, $ni->{name
}, $flistname, $rcid);
557 $$rsynctime_ref += tv_interval
($starttime);
562 $sth = $rdb->prepare(
563 "SELECT * from CMSReceivers WHERE " .
564 "CMailStore_CID = ? AND CMailStore_RID > ? " .
565 "AND CMailStore_RID <= ?");
566 $sth->execute($rcid, $lastid, $maxid);
568 $attrs = [qw(cmailstore_cid cmailstore_rid pmail receiver ticketid status mtime)];
569 PMG
::DBTools
::copy_selected_data
($ldb, $sth, 'CMSReceivers', $attrs);
571 PMG
::DBTools
::write_maxint_clusterinfo
($ldb, $rcid, 'lastid_CMailStore', $maxid);
587 } while (($count >= $maxcount) && ($mscount < $maxmails));
589 PMG
::DBTools
::create_clusterinfo_default
($ldb, $rcid, 'lastmt_CMSReceivers', 0, undef);
591 eval { # synchronize status updates
594 my $lastmt = PMG
::DBTools
::read_int_clusterinfo
($ldb, $rcid, 'lastmt_CMSReceivers');
596 my $sth = $rdb->prepare ("SELECT * from CMSReceivers WHERE mtime >= ? AND status != 'N'");
597 $sth->execute($lastmt);
599 my $update_sth = $ldb->prepare(
600 "UPDATE CMSReceivers SET status = ? WHERE " .
601 "CMailstore_CID = ? AND CMailstore_RID = ? AND TicketID = ?");
602 while (my $ref = $sth->fetchrow_hashref()) {
603 $update_sth->execute($ref->{status
}, $ref->{cmailstore_cid
},
604 $ref->{cmailstore_rid
}, $ref->{ticketid
});
607 PMG
::DBTools
::write_maxint_clusterinfo
($ldb, $rcid, 'lastmt_CMSReceivers', $ctime);
619 sub sync_statistic_db
{
620 my ($ldb, $rdb, $ni) = @_;
622 my $rcid = $ni->{cid
};
624 my $maxmails = 100000;
632 PMG
::DBTools
::create_clusterinfo_default
(
633 $ldb, $rcid, 'lastid_CStatistic', -1, undef);
635 do { # get new values
642 my $lastid = PMG
::DBTools
::read_int_clusterinfo
(
643 $ldb, $rcid, 'lastid_CStatistic');
647 my $sth = $rdb->prepare(
648 "SELECT * from CStatistic " .
649 "WHERE cid = ? AND rid > ? " .
650 "ORDER BY cid, rid LIMIT ?");
651 $sth->execute($rcid, $lastid, $maxcount);
656 $maxid = $ref->{rid
};
659 my $attrs = [qw(cid rid time bytes direction spamlevel ptime virusinfo sender)];
660 $count += PMG
::DBTools
::copy_selected_data
($ldb, $sth, 'CStatistic', $attrs, $callback);
665 $sth = $rdb->prepare(
666 "SELECT * from CReceivers WHERE " .
667 "CStatistic_CID = ? AND CStatistic_RID > ? AND CStatistic_RID <= ?");
668 $sth->execute($rcid, $lastid, $maxid);
670 $attrs = [qw(cstatistic_cid cstatistic_rid blocked receiver)];
671 PMG
::DBTools
::copy_selected_data
($ldb, $sth, 'CReceivers', $attrs);
674 PMG
::DBTools
::write_maxint_clusterinfo
($ldb, $rcid, 'lastid_CStatistic', $maxid);
685 } while (($count >= $maxcount) && ($mscount < $maxmails));
690 my $sync_generic_mtime_db = sub {
691 my ($ldb, $rdb, $ni, $table, $selectfunc, $mergefunc) = @_;
693 my $ctime = PMG
::DBTools
::get_remote_time
($rdb);
695 PMG
::DBTools
::create_clusterinfo_default
($ldb, $ni->{cid
}, "lastmt_$table", 0, undef);
697 my $lastmt = PMG
::DBTools
::read_int_clusterinfo
($ldb, $ni->{cid
}, "lastmt_$table");
699 my $sql_cmd = $selectfunc->($ctime, $lastmt);
701 my $sth = $rdb->prepare($sql_cmd);
708 # use transaction to speedup things
709 my $max = 1000; # UPDATE MAX ENTRIES AT ONCE
711 while (my $ref = $sth->fetchrow_hashref()) {
712 $ldb->begin_work if !$count;
714 if (++$count >= $max) {
721 $ldb->commit if $count;
728 PMG
::DBTools
::write_maxint_clusterinfo
($ldb, $ni->{cid
}, "lastmt_$table", $ctime);
733 sub sync_localstat_db
{
734 my ($dbh, $rdb, $ni) = @_;
736 my $rcid = $ni->{cid
};
738 my $selectfunc = sub {
739 my ($ctime, $lastmt) = @_;
740 return "SELECT * from LocalStat WHERE mtime >= $lastmt AND cid = $rcid";
743 my $merge_sth = $dbh->prepare(
744 'INSERT INTO LocalStat (Time, RBLCount, PregreetCount, CID, MTime) ' .
745 'VALUES (?, ?, ?, ?, ?) ' .
746 'ON CONFLICT (Time, CID) DO UPDATE SET ' .
747 'RBLCount = excluded.RBLCount, PregreetCount = excluded.PregreetCount, MTime = excluded.MTime');
749 my $mergefunc = sub {
752 $merge_sth->execute($ref->{time}, $ref->{rblcount
}, $ref->{pregreetcount
}, $ref->{cid
}, $ref->{mtime
});
755 return $sync_generic_mtime_db->($dbh, $rdb, $ni, 'LocalStat', $selectfunc, $mergefunc);
758 sub sync_greylist_db
{
759 my ($dbh, $rdb, $ni) = @_;
761 my $selectfunc = sub {
762 my ($ctime, $lastmt) = @_;
763 return "SELECT * from CGreylist WHERE extime >= $ctime AND " .
764 "mtime >= $lastmt AND CID != 0";
767 # FIXME: drop Host column with PMG 7.0
768 my $merge_sth = $dbh->prepare(PMG
::DBTools
::cgreylist_merge_sql
());
769 my $mergefunc = sub {
772 my $ipnet = $ref->{ipnet
};
773 $ipnet .= '.0/24' if $ipnet !~ /\
/\d+$/;
775 $ipnet, 0, $ref->{sender
}, $ref->{receiver
},
776 $ref->{instance
}, $ref->{rctime
}, $ref->{extime
}, $ref->{delay
},
777 $ref->{blocked
}, $ref->{passed
}, 0, $ref->{cid
});
780 return $sync_generic_mtime_db->($dbh, $rdb, $ni, 'CGreylist', $selectfunc, $mergefunc);
783 sub sync_userprefs_db
{
784 my ($dbh, $rdb, $ni) = @_;
786 my $selectfunc = sub {
787 my ($ctime, $lastmt) = @_;
789 return "SELECT * from UserPrefs WHERE mtime >= $lastmt";
792 my $merge_sth = $dbh->prepare(
793 "INSERT INTO UserPrefs (PMail, Name, Data, MTime) " .
794 'VALUES (?, ?, ?, ?) ' .
795 'ON CONFLICT (PMail, Name) DO UPDATE SET ' .
796 # Note: MTime = 0 ==> this is just a copy from somewhere else, not modified
797 'MTime = CASE WHEN excluded.MTime >= UserPrefs.MTime THEN 0 ELSE UserPrefs.MTime END, ' .
798 'Data = CASE WHEN excluded.MTime >= UserPrefs.MTime THEN excluded.Data ELSE UserPrefs.Data END');
800 my $mergefunc = sub {
803 $merge_sth->execute($ref->{pmail
}, $ref->{name
}, $ref->{data
}, $ref->{mtime
});
806 return $sync_generic_mtime_db->($dbh, $rdb, $ni, 'UserPrefs', $selectfunc, $mergefunc);
809 sub sync_domainstat_db
{
810 my ($dbh, $rdb, $ni) = @_;
812 my $selectfunc = sub {
813 my ($ctime, $lastmt) = @_;
814 return "SELECT * from DomainStat WHERE mtime >= $lastmt";
817 my $merge_sth = $dbh->prepare(
818 'INSERT INTO Domainstat ' .
819 '(Time,Domain,CountIn,CountOut,BytesIn,BytesOut,VirusIn,VirusOut,SpamIn,SpamOut,' .
820 'BouncesIn,BouncesOut,PTimeSum,Mtime) ' .
821 'VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?) ' .
822 'ON CONFLICT (Time, Domain) DO UPDATE SET ' .
823 'CountIn = excluded.CountIn, CountOut = excluded.CountOut, ' .
824 'BytesIn = excluded.BytesIn, BytesOut = excluded.BytesOut, ' .
825 'VirusIn = excluded.VirusIn, VirusOut = excluded.VirusOut, ' .
826 'SpamIn = excluded.SpamIn, SpamOut = excluded.SpamOut, ' .
827 'BouncesIn = excluded.BouncesIn, BouncesOut = excluded.BouncesOut, ' .
828 'PTimeSum = excluded.PTimeSum, MTime = excluded.MTime');
830 my $mergefunc = sub {
834 $ref->{time}, $ref->{domain
}, $ref->{countin
}, $ref->{countout
},
835 $ref->{bytesin
}, $ref->{bytesout
},
836 $ref->{virusin
}, $ref->{virusout
}, $ref->{spamin
}, $ref->{spamout
},
837 $ref->{bouncesin
}, $ref->{bouncesout
}, $ref->{ptimesum
}, $ref->{mtime
});
840 return $sync_generic_mtime_db->($dbh, $rdb, $ni, 'DomainStat', $selectfunc, $mergefunc);
843 sub sync_dailystat_db
{
844 my ($dbh, $rdb, $ni) = @_;
846 my $selectfunc = sub {
847 my ($ctime, $lastmt) = @_;
848 return "SELECT * from DailyStat WHERE mtime >= $lastmt";
851 my $merge_sth = $dbh->prepare(
852 'INSERT INTO DailyStat ' .
853 '(Time,CountIn,CountOut,BytesIn,BytesOut,VirusIn,VirusOut,SpamIn,SpamOut,' .
854 'BouncesIn,BouncesOut,GreylistCount,SPFCount,RBLCount,PTimeSum,Mtime) ' .
855 'VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?) ' .
856 'ON CONFLICT (Time) DO UPDATE SET ' .
857 'CountIn = excluded.CountIn, CountOut = excluded.CountOut, ' .
858 'BytesIn = excluded.BytesIn, BytesOut = excluded.BytesOut, ' .
859 'VirusIn = excluded.VirusIn, VirusOut = excluded.VirusOut, ' .
860 'SpamIn = excluded.SpamIn, SpamOut = excluded.SpamOut, ' .
861 'BouncesIn = excluded.BouncesIn, BouncesOut = excluded.BouncesOut, ' .
862 'GreylistCount = excluded.GreylistCount, SPFCount = excluded.SpfCount, ' .
863 'RBLCount = excluded.RBLCount, ' .
864 'PTimeSum = excluded.PTimeSum, MTime = excluded.MTime');
866 my $mergefunc = sub {
870 $ref->{time}, $ref->{countin
}, $ref->{countout
},
871 $ref->{bytesin
}, $ref->{bytesout
},
872 $ref->{virusin
}, $ref->{virusout
}, $ref->{spamin
}, $ref->{spamout
},
873 $ref->{bouncesin
}, $ref->{bouncesout
}, $ref->{greylistcount
},
874 $ref->{spfcount
}, $ref->{rblcount
}, $ref->{ptimesum
}, $ref->{mtime
});
877 return $sync_generic_mtime_db->($dbh, $rdb, $ni, 'DailyStat', $selectfunc, $mergefunc);
880 sub sync_virusinfo_db
{
881 my ($dbh, $rdb, $ni) = @_;
883 my $selectfunc = sub {
884 my ($ctime, $lastmt) = @_;
885 return "SELECT * from VirusInfo WHERE mtime >= $lastmt";
888 my $merge_sth = $dbh->prepare(
889 'INSERT INTO VirusInfo (Time,Name,Count,MTime) ' .
890 'VALUES (?,?,?,?) ' .
891 'ON CONFLICT (Time,Name) DO UPDATE SET ' .
892 'Count = excluded.Count , MTime = excluded.MTime');
894 my $mergefunc = sub {
897 $merge_sth->execute($ref->{time}, $ref->{name
}, $ref->{count
}, $ref->{mtime
});
900 return $sync_generic_mtime_db->($dbh, $rdb, $ni, 'VirusInfo', $selectfunc, $mergefunc);
903 sub sync_deleted_nodes_from_master
{
904 my ($ldb, $masterdb, $cinfo, $masterni, $rsynctime_ref) = @_;
908 my $cid_hash = {}; # fast lookup
909 foreach my $ni (values %{$cinfo->{ids
}}) {
910 $cid_hash->{$ni->{cid
}} = $ni;
913 my $spooldir = $PMG::MailQueue
::spooldir
;
915 my $maxcid = $cinfo->{master
}->{maxcid
} // 0;
917 for (my $rcid = 1; $rcid <= $maxcid; $rcid++) {
918 next if $cid_hash->{$rcid};
920 my $done_marker = "$spooldir/cluster/$rcid/.synced-deleted-node";
922 next if -f
$done_marker; # already synced
924 syslog
('info', "syncing deleted node $rcid from master '$masterni->{ip}'");
926 my $starttime = [ gettimeofday
() ];
927 sync_spooldir
($masterni->{ip
}, $masterni->{name
}, $rcid);
928 $$rsynctime_ref += tv_interval
($starttime);
931 ip
=> $masterni->{ip
},
932 name
=> $masterni->{name
},
936 sync_quarantine_db
($ldb, $masterdb, $fake_ni);
938 sync_statistic_db
($ldb, $masterdb, $fake_ni);
940 open(my $fh, ">>", $done_marker);