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);
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 {
my $loadavg15 = '-';
if (my $d = $ni->{loadavg}) {
- $loadavg15 = $d->[2];
+ $loadavg15 = sprintf("%.2f", $d->[2]);
}
push @$data, { text => 'Load', value => $loadavg15 };
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',
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 {
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>";