]> git.proxmox.com Git - pmg-api.git/commitdiff
add pmg-daily timer
authorDietmar Maurer <dietmar@proxmox.com>
Sat, 25 Mar 2017 10:21:07 +0000 (11:21 +0100)
committerDietmar Maurer <dietmar@proxmox.com>
Sat, 25 Mar 2017 10:21:07 +0000 (11:21 +0100)
Makefile
bin/pmg-daily [new file with mode: 0755]
debian/pmg-daily.service [new file with mode: 0644]
debian/pmg-daily.timer [new file with mode: 0644]
debian/pmg-hourly.service
debian/rules

index 4daac834f5dd32f52a176f2e70e634b0176a4baf..7a6f488e23d6e3abca662c3793f7c5d3cf8b867f 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -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}))
diff --git a/bin/pmg-daily b/bin/pmg-daily
new file mode 100755 (executable)
index 0000000..e111935
--- /dev/null
@@ -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);
+
diff --git a/debian/pmg-daily.service b/debian/pmg-daily.service
new file mode 100644 (file)
index 0000000..d7a749a
--- /dev/null
@@ -0,0 +1,6 @@
+[Unit]
+Description=Daily Proxmox Mail Gateway activities
+
+[Service]
+Type=oneshot
+ExecStart=/usr/lib/pmg/bin/pmg-daily
diff --git a/debian/pmg-daily.timer b/debian/pmg-daily.timer
new file mode 100644 (file)
index 0000000..a5963eb
--- /dev/null
@@ -0,0 +1,9 @@
+[Unit]
+Description=Daily Proxmox Mail Gateway activities
+
+[Timer]
+OnCalendar=daily
+Persistent=true     
+
+[Install]
+WantedBy=timers.target
\ No newline at end of file
index a1e12501e713020f39e609037912ee4102f965a9..db8732043923cfdad80f0e9331592d0c4f443209 100644 (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
index b6243759bc6972abfb20c7b89fc71ed2a9b28d45..0ab0aa28bf929bb6020ef156765e86f15f237cd7 100755 (executable)
@@ -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