add pmg-daily timer

This commit is contained in:
Dietmar Maurer 2017-03-25 11:21:07 +01:00
parent 5a6956b768
commit d0d5b9440d
6 changed files with 125 additions and 3 deletions

View File

@ -20,7 +20,7 @@ REPOID=`./repoid.pl .git`
SERVICES = pmgdaemon pmgproxy
CLITOOLS = pmgdb pmgconfig pmgperf
CLISCRIPTS = pmg-smtp-filter pmgsh pmgpolicy
CRONSCRIPTS = pmg-hourly
CRONSCRIPTS = pmg-hourly pmg-daily
CLI_CLASSES = $(addprefix PMG/CLI/, $(addsuffix .pm, ${CLITOOLS}))
SERVICE_CLASSES = $(addprefix PMG/Service/, $(addsuffix .pm, ${SERVICES}))

106
bin/pmg-daily Executable file
View File

@ -0,0 +1,106 @@
#!/usr/bin/perl -T
$ENV{'PATH'} = '/sbin:/bin:/usr/sbin:/usr/bin';
delete @ENV{qw(IFS CDPATH ENV BASH_ENV)};
use strict;
use warnings;
use Time::Local;
use PVE::SafeSyslog;
use PVE::INotify;
use PVE::RESTEnvironment;
use PMG::Utils;
use PMG::Config;
use PMG::ClusterConfig;
use PMG::DBTools;
$SIG{'__WARN__'} = sub {
my $err = $@;
my $t = $_[0];
chomp $t;
print STDERR "$t\n";
syslog('warning', "%s", $t);
$@ = $err;
};
PVE::RESTEnvironment->setup_default_cli_env();
initlog('pmg-daily', 'mail');
my $cfg = PMG::Config->new();
sub get_timespan {
my ($sec, $min, $hour, $mday, $mon, $year) =
localtime(time());
my $end = timelocal(0, 0, 0, $mday, $mon, $year);
my $start = $end - 3600*24;
return ($start, $end);
}
my $statlifetime = $cfg->get('admin', 'statlifetime');
if ($statlifetime && $statlifetime > 0) {
my ($start, $end) = get_timespan();
$statlifetime -= 1;
my $secs = $statlifetime * 86400;
$start -= $secs;
# delete statistics older than $start
my $dbh;
my ($srows, $rrows) = (0, 0);
eval {
my $dbh = PMG::DBTools::open_ruledb();
$dbh->begin_work;
my $sth = $dbh->prepare("DELETE FROM CStatistic WHERE time < $start");
$sth->execute;
$srows = $sth->rows;
$sth->finish;
if ($srows > 0) {
$sth = $dbh->prepare(
"DELETE FROM CReceivers WHERE NOT EXISTS " .
"(SELECT * FROM CStatistic WHERE CID = CStatistic_CID AND RID = CStatistic_RID)");
$sth->execute;
$rrows = $sth->rows;
$sth->finish;
}
$dbh->commit;
};
if (my $err = $@) {
$dbh->rollback if $dbh;
syslog('err', PMG::Utils::msgquote($err));
} else {
syslog('info', "cleanup removed $srows entries from " .
"statistic database ($srows, $rrows)") if $srows;
}
$dbh->disconnect() if $dbh;
}
# fixme: check for available updates
# rotate razor log file
rename('/root/.razor/razor-agent.log', '/root/.razor/razor-agent.log.0');
# run bayes database maintainance
system('sa-learn --force-expire >/dev/null 2>&1');
exit (0);

6
debian/pmg-daily.service vendored Normal file
View File

@ -0,0 +1,6 @@
[Unit]
Description=Daily Proxmox Mail Gateway activities
[Service]
Type=oneshot
ExecStart=/usr/lib/pmg/bin/pmg-daily

9
debian/pmg-daily.timer vendored Normal file
View File

@ -0,0 +1,9 @@
[Unit]
Description=Daily Proxmox Mail Gateway activities
[Timer]
OnCalendar=daily
Persistent=true
[Install]
WantedBy=timers.target

View File

@ -3,4 +3,4 @@ Description=Hourly Proxmox Mail Gateway activities
[Service]
Type=oneshot
ExecStart=/usr/lib/pmg/bin/pmg-cron-hourly
ExecStart=/usr/lib/pmg/bin/pmg-hourly

3
debian/rules vendored
View File

@ -14,9 +14,10 @@ override_dh_installinit:
dh_systemd_enable --name=pmgpolicy pmgpolicy.service
dh_systemd_enable --name=pmgnetcommit pmgnetcommit.service
dh_systemd_enable --name=pmg-hourly pmg-hourly.service
dh_systemd_enable --name=pmg-daily pmg-daily.service
override_dh_systemd_start:
dh_systemd_start pmg-hourly.timer
dh_systemd_start pmg-hourly.timer pmg-daily.timer
dh_systemd_start --no-restart-on-upgrade --no-start pmgnetcommit.service
dh_systemd_start pmgdaemon.service pmgproxy.service pmg-smtp-filter.service pmgpolicy.service