From 308c2bfc9b8de2e71f2ba3fa155f157206db9f9a Mon Sep 17 00:00:00 2001 From: Dietmar Maurer Date: Tue, 22 Aug 2017 11:44:54 +0200 Subject: [PATCH] improve pmgreport (add cluster info) new options --receiver and --auto --- PMG/CLI/pmgreport.pm | 89 +++++++++++++++++++++++++++++++++++++++--- templates/pmgreport.tt | 23 +++++++++++ 2 files changed, 107 insertions(+), 5 deletions(-) diff --git a/PMG/CLI/pmgreport.pm b/PMG/CLI/pmgreport.pm index 9b69788..905c188 100644 --- a/PMG/CLI/pmgreport.pm +++ b/PMG/CLI/pmgreport.pm @@ -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 "; diff --git a/templates/pmgreport.tt b/templates/pmgreport.tt index eb15edc..d84770c 100644 --- a/templates/pmgreport.tt +++ b/templates/pmgreport.tt @@ -26,6 +26,29 @@ border-collapse: collapse; [% IF cluster %] +

Cluster Status

+ + + + + + + + + + + [% FOREACH item IN cluster %] + + + + + + + + + + [% END %] +
HostnameIP AddressRoleStateLoadMemoryDisk
[% item.hostname %][% item.ip %][% item.type %][% item.state %][% item.loadavg1 %][% item.memory %][% item.disk %]
[% END %] [% IF system %] -- 2.39.5