]> git.proxmox.com Git - pmg-api.git/commitdiff
improve pmgreport (add cluster info)
authorDietmar Maurer <dietmar@proxmox.com>
Tue, 22 Aug 2017 09:44:54 +0000 (11:44 +0200)
committerDietmar Maurer <dietmar@proxmox.com>
Tue, 22 Aug 2017 09:44:54 +0000 (11:44 +0200)
new options --receiver and --auto

PMG/CLI/pmgreport.pm
templates/pmgreport.tt

index 9b697880abc8f412eeeb52781d91342abb80b078..905c1882a630e2a732d4c93632c83db07669db04 100644 (file)
@@ -11,7 +11,11 @@ use PVE::CLIHandler;
 use PMG::Utils;
 use PMG::Config;
 use PMG::RESTEnvironment;
+
 use PMG::API2::Nodes;
+use PMG::ClusterConfig;
+use PMG::Cluster;
+use PMG::API2::Cluster;
 
 use base qw(PVE::CLIHandler);
 
@@ -19,6 +23,11 @@ my $nodename = PVE::INotify::nodename();
 
 sub setup_environment {
     PMG::RESTEnvironment->setup_default_cli_env();
+
+    my $rpcenv = PMG::RESTEnvironment->get();
+    # API /config/cluster/nodes need a ticket to connect to other nodes
+    my $ticket = PMG::Ticket::assemble_ticket('root@pam');
+    $rpcenv->set_ticket($ticket);
 }
 
 my $get_system_table_data = sub {
@@ -37,7 +46,7 @@ my $get_system_table_data = sub {
 
     my $loadavg15 = '-';
     if (my $d = $ni->{loadavg}) {
-       $loadavg15 = $d->[2];
+       $loadavg15 = sprintf("%.2f", $d->[2]);
     }
     push @$data, { text => 'Load', value => $loadavg15 };
 
@@ -56,6 +65,45 @@ my $get_system_table_data = sub {
     return $data
 };
 
+my $get_cluster_table_data = sub {
+
+    my $res = PMG::API2::Cluster->status({});
+    return undef if !scalar(@$res);
+
+    my $data = [];
+
+    foreach my $ni (@$res) {
+       my $state = 'A';
+       $state = 'S' if !$ni->{insync};
+
+       my $loadavg1  = '-';
+       if (my $d = $ni->{loadavg}) {
+           $loadavg1 = sprintf("%.2f", $d->[0]);
+       }
+
+       my $memory = '-';
+       if (my $d = $ni->{memory}) {
+           $memory = sprintf("%.2f%%", $d->{used}*100/$d->{total});
+       }
+
+       my $disk = '-';
+       if (my $d = $ni->{rootfs}) {
+           $disk = sprintf("%.2f%%", $d->{used}*100/$d->{total});
+       }
+
+       push @$data, {
+           hostname => $ni->{name},
+           ip => $ni->{ip},
+           type => $ni->{type},
+           state => $state,
+           loadavg1 => $loadavg1,
+           memory => $memory,
+           disk => $disk,
+       };
+    };
+
+    return $data;
+};
 
 __PACKAGE__->register_method ({
     name => 'pmgreport',
@@ -64,7 +112,25 @@ __PACKAGE__->register_method ({
     description => "Generate and send daily system report email.",
     parameters => {
        additionalProperties => 0,
-       properties => {},
+       properties => {
+           debug => {
+               description => "Debug mode. Print raw email to stdout instead of sending them.",
+               type => 'boolean',
+               optional => 1,
+               default => 0,
+           },
+           auto => {
+               description => "Auto mode. Use setting from system configuration (set when invoked fron cron).",
+               type => 'boolean',
+               optional => 1,
+               default => 0,
+           },
+           receiver => {
+               description => "Send report to this email address. Default is the administratior email address.",
+               type => 'string', format => 'email',
+               optional => 1,
+           },
+       },
     },
     returns => { type => 'null'},
     code => sub {
@@ -80,15 +146,28 @@ __PACKAGE__->register_method ({
            date => strftime("%F", localtime($end - 1)),
        };
 
-       $vars->{system} = $get_system_table_data->();
+       my $cinfo = PMG::ClusterConfig->new();
+       my $role = $cinfo->{local}->{type} // '-';
+
+       if ($role eq '-') {
+           $vars->{system} = $get_system_table_data->();
+       } else {
+           $vars->{cluster} = $get_cluster_table_data->();
+           if ($role eq 'master') {
+               # OK
+           } else {
+               return undef if $param->{auto}; # silent exit - do not send report
+           }
+       }
 
        my $tt = PMG::Config::get_template_toolkit();
 
        my $cfg = PMG::Config->new();
-       my $email = $cfg->get ('admin', 'email');
+       my $email = $param->{receiver} // $cfg->get ('admin', 'email');
 
        if (!defined($email)) {
-           die "STOPHERE";
+           return undef if $param->{auto}; # silent exit - do not send report
+           die "no receiver configured\n";
        }
 
        my $mailfrom = "Proxmox Mail Gateway <postmaster>";
index eb15edcbfd3ae73d504656dbab21ec7e714141dc..d84770c726a46bafbd4b7e3f76dbeb6419d1c0cc 100644 (file)
@@ -26,6 +26,29 @@ border-collapse: collapse;
 <body>
 
   [% IF cluster %]
+  <h2>Cluster Status</h2>
+  <table>
+    <tr>
+      <th>Hostname</th>
+      <th>IP Address</th>
+      <th>Role</th>
+      <th>State</th>
+      <th>Load</th>
+      <th>Memory</th>
+      <th>Disk</th>
+    </tr>
+    [% FOREACH item IN cluster %]
+    <tr>
+      <td>[% item.hostname %]</td>
+      <td>[% item.ip %]</td>
+      <td>[% item.type %]</td>
+      <td>[% item.state %]</td>
+      <td>[% item.loadavg1 %]</td>
+      <td>[% item.memory %]</td>
+      <td>[% item.disk %]</td>
+    </tr>
+    [% END %]
+  </table>
   [% END %]
 
   [% IF system %]