]> git.proxmox.com Git - pmg-api.git/blob - src/bin/pmg-daily
add generate_ndr to PMG::SMTP
[pmg-api.git] / src / bin / pmg-daily
1 #!/usr/bin/perl -T
2
3 $ENV{'PATH'} = '/sbin:/bin:/usr/sbin:/usr/bin';
4
5 delete @ENV{qw(IFS CDPATH ENV BASH_ENV)};
6
7 use strict;
8 use warnings;
9 use Time::Local;
10
11 use PVE::SafeSyslog;
12 use PVE::INotify;
13 use PVE::RESTEnvironment;
14
15 use PMG::Utils;
16 use PMG::Config;
17 use PMG::ClusterConfig;
18 use PMG::DBTools;
19 use PMG::API2::Subscription;
20 use PMG::API2::APT;
21
22 $SIG{'__WARN__'} = sub {
23 my $err = $@;
24 my $t = $_[0];
25 chomp $t;
26 print STDERR "$t\n";
27 syslog('warning', "%s", $t);
28 $@ = $err;
29 };
30
31 PVE::RESTEnvironment->setup_default_cli_env();
32
33 initlog('pmg-daily', 'mail');
34
35 my $nodename = PVE::INotify::nodename();
36
37 eval { PMG::API2::Subscription->update({ node => $nodename }); };
38 if (my $err = $@) {
39 syslog ('err', "update subscription info failed: $err");
40 }
41
42 my $cfg = PMG::Config->new();
43
44 if (my $statlifetime = $cfg->get ('admin', 'statlifetime')) {
45 my $count = 0;
46 eval {
47 my $dbh = PMG::DBTools::open_ruledb();
48 $count = PMG::DBTools::purge_statistic_database($dbh, $statlifetime);
49 };
50 if (my $err = $@) {
51 syslog('err', $err);
52 } else {
53 syslog('info', "cleanup removed $count entries from statistic database") if $count;
54 }
55 }
56
57 # check for available updates
58 # We assume that users with subscriptions want informations
59 # about new packages.
60 my $info = PVE::INotify::read_file('subscription');
61 my $notify = ($info && $info->{status} eq 'Active') ? 1 : 0;
62 eval { PMG::API2::APT->update_database({ node => $nodename, notify => $notify, quiet => 1 }); };
63 if (my $err = $@) {
64 syslog ('err', "update apt database failed: $err");
65 }
66
67 # rotate razor log file
68 rename('/root/.razor/razor-agent.log', '/root/.razor/razor-agent.log.0');
69
70 # setup proxy env (assume sa-update use http)
71 if (my $http_proxy = $cfg->get('admin', 'http_proxy')) {
72 $ENV{http_proxy} = $http_proxy;
73 }
74
75 # update spamassassin rules
76 if (system('sa-update') == 0) {
77 # if the exit code is 0, new updates were downloaded
78 # then restart the pmg-smtp-filter to load the new rules
79 PMG::Utils::service_cmd('pmg-smtp-filter', 'restart');
80 }
81
82 # run bayes database maintainance
83 system('sa-learn --force-expire >/dev/null 2>&1');
84
85 exit (0);
86