]> git.proxmox.com Git - pmg-api.git/commitdiff
pmgreport: new tool to send daily system reports
authorDietmar Maurer <dietmar@proxmox.com>
Mon, 21 Aug 2017 11:09:55 +0000 (13:09 +0200)
committerDietmar Maurer <dietmar@proxmox.com>
Mon, 21 Aug 2017 11:09:55 +0000 (13:09 +0200)
Makefile
PMG/CLI/pmgqm.pm
PMG/CLI/pmgreport.pm [new file with mode: 0644]
PMG/Utils.pm
bin/pmgreport [new file with mode: 0644]
templates/pmgreport.tt [new file with mode: 0644]

index 2b3de672a0c25140edab839938489ac663e89bfd..687dee511db4d7f164a0ba3cc5e002f54764a110 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -18,7 +18,7 @@ BASHCOMPLDIR=${DESTDIR}/usr/share/bash-completion/completions/
 REPOID=`./repoid.pl .git`
 
 SERVICES = pmgdaemon pmgproxy pmgtunnel pmgmirror
-CLITOOLS = pmgdb pmgconfig pmgperf pmgcm pmgqm
+CLITOOLS = pmgdb pmgconfig pmgperf pmgcm pmgqm pmgreport
 CLISCRIPTS = pmg-smtp-filter pmgsh pmgpolicy
 CRONSCRIPTS = pmg-hourly pmg-daily
 
@@ -35,6 +35,7 @@ SERVICE_MANS = $(addsuffix .8, ${SERVICES}) pmg-smtp-filter.8 pmgpolicy.8
 CONF_MANS= pmg.conf.5 cluster.conf.5
 
 TEMPLATES =                            \
+       pmgreport.tt                    \
        spamreport-verbose.tt           \
        spamreport-short.tt             \
        main.cf.in                      \
index 7f231b211a7ac4522b210c13052f0c950e5c3039..ca19ef25d3c35c8d4304d12bd9817a196469aa33 100755 (executable)
@@ -112,40 +112,6 @@ sub get_item_data {
     return $item;
 }
 
-sub finalize_report {
-    my ($tt, $template, $data, $mailfrom, $receiver, $debug) = @_;
-
-    my $html = '';
-
-    $tt->process($template, $data, \$html) ||
-       die $tt->error() . "\n";
-
-    my $title;
-    if ($html =~ m|^\s*<title>(.*)</title>|m) {
-       $title = $1;
-    } else {
-       die "unable to extract template title\n";
-    }
-
-    my $top = MIME::Entity->build(
-       Type    => "multipart/related",
-       To      => $data->{pmail},
-       From    => $mailfrom,
-       Subject => PMG::Utils::bencode_header(decode_entities($title)));
-
-    $top->attach(
-       Data     => $html,
-       Type     => "text/html",
-       Encoding => $debug ? 'binary' : 'quoted-printable');
-
-    if ($debug) {
-       $top->print();
-       return;
-    }
-    # we use an empty envelope sender (we dont want to receive NDRs)
-    PMG::Utils::reinject_mail ($top, '', [$receiver], undef, $data->{fqdn});
-}
-
 __PACKAGE__->register_method ({
     name => 'send',
     path => 'send',
@@ -297,7 +263,7 @@ __PACKAGE__->register_method ({
                if (!$extern) {
                    $data->{mailcount} = $mailcount;
                    my $sendto = $redirect ? $redirect : $creceiver;
-                   finalize_report($tt, $template, $data, $mailfrom, $sendto, $param->{debug});
+                   PMG::Utils::finalize_report($tt, $template, $data, $mailfrom, $sendto, $param->{debug});
                }
            } else {
                my $hint = $extern ? " (external address)" : "";
diff --git a/PMG/CLI/pmgreport.pm b/PMG/CLI/pmgreport.pm
new file mode 100644 (file)
index 0000000..9b69788
--- /dev/null
@@ -0,0 +1,102 @@
+package PMG::CLI::pmgreport;
+
+use strict;
+use Data::Dumper;
+use Template;
+use POSIX qw(strftime);
+
+use PVE::INotify;
+use PVE::CLIHandler;
+
+use PMG::Utils;
+use PMG::Config;
+use PMG::RESTEnvironment;
+use PMG::API2::Nodes;
+
+use base qw(PVE::CLIHandler);
+
+my $nodename = PVE::INotify::nodename();
+
+sub setup_environment {
+    PMG::RESTEnvironment->setup_default_cli_env();
+}
+
+my $get_system_table_data = sub {
+
+    my $ni = PMG::API2::NodeInfo->status({ node => $nodename });
+
+    my $data = [];
+
+    push @$data, { text => 'Hostname', value => $nodename };
+
+    my $uptime = $ni->{uptime} ? PMG::Utils::format_uptime($ni->{uptime}) : '-';
+
+    push @$data, { text => 'Uptime', value => $uptime };
+
+    push @$data, { text => 'Version', value => $ni->{pmgversion} };
+
+    my $loadavg15 = '-';
+    if (my $d = $ni->{loadavg}) {
+       $loadavg15 = $d->[2];
+    }
+    push @$data, { text => 'Load', value => $loadavg15 };
+
+    my $mem = '-';
+    if (my $d = $ni->{memory}) {
+       $mem = sprintf("%.2f%%", $d->{used}*100/$d->{total});
+    }
+    push @$data, { text => 'Memory', value => $mem };
+
+    my $disk = '-';
+    if (my $d = $ni->{rootfs}) {
+       $disk = sprintf("%.2f%%", $d->{used}*100/$d->{total});
+    }
+    push @$data, { text => 'Disk', value => $disk };
+
+    return $data
+};
+
+
+__PACKAGE__->register_method ({
+    name => 'pmgreport',
+    path => 'pmgreport',
+    method => 'POST',
+    description => "Generate and send daily system report email.",
+    parameters => {
+       additionalProperties => 0,
+       properties => {},
+    },
+    returns => { type => 'null'},
+    code => sub {
+       my ($param) = @_;
+
+       my $fqdn = PVE::Tools::get_fqdn($nodename);
+
+       my $end = time(); # fixme
+
+       my $vars = {
+           hostname => $nodename,
+           fqdn => $fqdn,
+           date => strftime("%F", localtime($end - 1)),
+       };
+
+       $vars->{system} = $get_system_table_data->();
+
+       my $tt = PMG::Config::get_template_toolkit();
+
+       my $cfg = PMG::Config->new();
+       my $email = $cfg->get ('admin', 'email');
+
+       if (!defined($email)) {
+           die "STOPHERE";
+       }
+
+       my $mailfrom = "Proxmox Mail Gateway <postmaster>";
+       PMG::Utils::finalize_report($tt, 'pmgreport.tt', $vars, $mailfrom, $email, $param->{debug});
+
+       return undef;
+    }});
+
+our $cmddef = [ __PACKAGE__, 'pmgreport', [], undef ];
+
+1;
index 5f6974fc9e1b67a92fa6b89e0ce5175fa457dabc..81d2dcacf9049174da47f3abf24327563799c1b7 100644 (file)
@@ -995,4 +995,38 @@ sub format_uptime {
     }
 }
 
+sub finalize_report {
+    my ($tt, $template, $data, $mailfrom, $receiver, $debug) = @_;
+
+    my $html = '';
+
+    $tt->process($template, $data, \$html) ||
+       die $tt->error() . "\n";
+
+    my $title;
+    if ($html =~ m|^\s*<title>(.*)</title>|m) {
+       $title = $1;
+    } else {
+       die "unable to extract template title\n";
+    }
+
+    my $top = MIME::Entity->build(
+       Type    => "multipart/related",
+       To      => $data->{pmail},
+       From    => $mailfrom,
+       Subject => bencode_header(decode_entities($title)));
+
+    $top->attach(
+       Data     => $html,
+       Type     => "text/html",
+       Encoding => $debug ? 'binary' : 'quoted-printable');
+
+    if ($debug) {
+       $top->print();
+       return;
+    }
+    # we use an empty envelope sender (we dont want to receive NDRs)
+    PMG::Utils::reinject_mail ($top, '', [$receiver], undef, $data->{fqdn});
+}
+
 1;
diff --git a/bin/pmgreport b/bin/pmgreport
new file mode 100644 (file)
index 0000000..abf6cc7
--- /dev/null
@@ -0,0 +1,8 @@
+#!/usr/bin/perl -T
+
+use strict;
+use warnings;
+
+use PMG::CLI::pmgreport;
+
+PMG::CLI::pmgreport->run_cli_handler();
diff --git a/templates/pmgreport.tt b/templates/pmgreport.tt
new file mode 100644 (file)
index 0000000..eb15edc
--- /dev/null
@@ -0,0 +1,49 @@
+<!DOCTYPE html>
+<html>
+<head>
+<title>Proxmox Status Report - [% date %] ([% fqdn %])</title>
+<style>
+
+table {
+width: 500px;
+}
+
+table tr:nth-child(even) {
+background-color: #eee;
+}
+
+table tr:nth-child(odd) {
+background-color: #fff;
+}
+
+table, th, td {
+border: 1px solid black;
+border-collapse: collapse;
+}
+  
+</style>
+</head>
+<body>
+
+  [% IF cluster %]
+  [% END %]
+
+  [% IF system %]
+  <h2>System Status</h2>
+  <table>
+    <tr>
+      <th>Property</th>
+      <th>Value</th>
+    </tr>
+    [% FOREACH item IN system %]
+    <tr>
+      <td>[% item.text %]</td>
+      <td>[% item.value %]</td>
+    </tr>
+    [% END %]
+  </table>
+  [% END %]
+
+
+</body>
+</html>