]>
git.proxmox.com Git - pmg-api.git/blob - PMG/CLI/pmgreport.pm
1 package PMG
:: CLI
:: pmgreport
;
6 use POSIX
qw(strftime) ;
13 use PMG
:: RESTEnvironment
;
16 use PMG
:: ClusterConfig
;
18 use PMG
:: API2
:: Cluster
;
22 use base
qw(PVE::CLIHandler) ;
24 my $nodename = PVE
:: INotify
:: nodename
();
26 sub setup_environment
{
27 PMG
:: RESTEnvironment-
> setup_default_cli_env ();
29 my $rpcenv = PMG
:: RESTEnvironment-
> get ();
30 # API /config/cluster/nodes need a ticket to connect to other nodes
31 my $ticket = PMG
:: Ticket
:: assemble_ticket
( 'root @pam ' );
32 $rpcenv -> set_ticket ( $ticket );
35 my $get_system_table_data = sub {
37 my $ni = PMG
:: API2
:: NodeInfo-
> status ({ node
=> $nodename });
41 push @$data, { text
=> 'Hostname' , value
=> $nodename };
43 my $uptime = $ni ->{ uptime
} ? PMG
:: Utils
:: format_uptime
( $ni ->{ uptime
}) : '-' ;
45 push @$data, { text
=> 'Uptime' , value
=> $uptime };
47 push @$data, { text
=> 'Version' , value
=> $ni ->{ pmgversion
} };
50 if ( my $d = $ni ->{ loadavg
}) {
51 $loadavg15 = sprintf ( "%.2f" , $d ->[ 2 ]);
53 push @$data, { text
=> 'Load' , value
=> $loadavg15 };
56 if ( my $d = $ni ->{ memory
}) {
57 $mem = sprintf ( "%.2f %% " , $d ->{ used
}* 100 / $d ->{ total
});
59 push @$data, { text
=> 'Memory' , value
=> $mem };
62 if ( my $d = $ni ->{ rootfs
}) {
63 $disk = sprintf ( "%.2f %% " , $d ->{ used
}* 100 / $d ->{ total
});
65 push @$data, { text
=> 'Disk' , value
=> $disk };
70 my $get_cluster_table_data = sub {
72 my $res = PMG
:: API2
:: Cluster-
> status ({});
73 return undef if ! scalar ( @$res );
77 foreach my $ni ( @$res ) {
79 $state = 'S' if ! $ni ->{ insync
};
82 if ( my $d = $ni ->{ loadavg
}) {
83 $loadavg1 = sprintf ( "%.2f" , $d ->[ 0 ]);
87 if ( my $d = $ni ->{ memory
}) {
88 $memory = sprintf ( "%.2f %% " , $d ->{ used
}* 100 / $d ->{ total
});
92 if ( my $d = $ni ->{ rootfs
}) {
93 $disk = sprintf ( "%.2f %% " , $d ->{ used
}* 100 / $d ->{ total
});
97 hostname
=> $ni ->{ name
},
101 loadavg1
=> $loadavg1,
110 __PACKAGE__-
> register_method ({
114 description
=> "Generate and send daily system report email." ,
116 additionalProperties
=> 0 ,
119 description
=> "Debug mode. Print raw email to stdout instead of sending them." ,
125 description
=> "Auto mode. Use setting from system configuration (set when invoked fron cron)." ,
131 description
=> "Send report to this email address. Default is the administratior email address." ,
132 type
=> 'string' , format
=> 'email' ,
136 description
=> "Select time span for included email statistics. \n\n NOTE: System and cluster performance data is always from current time (when script is run)." ,
138 enum
=> [ 'today' , 'yesterday' ],
144 returns
=> { type
=> 'null' },
148 my $timespan = $param ->{ timespan
} // 'today' ;
149 my ( $start, $end ) = PMG
:: Utils
:: lookup_timespan
( $timespan );
151 my $fqdn = PVE
:: Tools
:: get_fqdn
( $nodename );
154 hostname
=> $nodename,
156 date
=> strftime
( " %F " , localtime ( $end - 1 )),
159 my $cinfo = PMG
:: ClusterConfig-
> new ();
160 my $role = $cinfo ->{ local }->{ type
} // '-' ;
163 $vars ->{ system } = $get_system_table_data ->();
165 $vars ->{ cluster
} = $get_cluster_table_data ->();
166 if ( $role eq 'master' ) {
169 return undef if $param ->{ auto
}; # silent exit - do not send report
174 my $stat = PMG
:: Statistic-
> new ( $start, $end );
175 my $rdb = PMG
:: RuleDB-
> new ();
178 PMG
:: Statistic
:: update_stats
( $rdb ->{ dbh
}, $cinfo );
180 my $totals = $stat -> total_mail_stat ( $rdb );
182 # Generate data for incoming mails
184 push @$data, { text
=> 'Incoming Mails' , value
=> $totals ->{ count_in
}, percentage
=> $totals ->{ count_in_per
} };
185 push @$data, { text
=> 'Spam Mails' , value
=> $totals ->{ spamcount_in
}, percentage
=> $totals ->{ spamcount_in_per
} };
186 push @$data, { text
=> 'Virus Mails' , value
=> $totals ->{ viruscount_in
}, percentage
=> $totals ->{ viruscount_in_per
} };
187 push @$data, { text
=> 'SPF rejects' , value
=> $totals ->{ spfcount
}, percentage
=> $totals ->{ spfcount_per
} };
188 push @$data, { text
=> 'Mail Traffic' , value
=> sprintf ( "%.3f MByte" , $totals ->{ traffic_in
}) };
190 $vars ->{ incoming
} = $data,
192 # Generate data for outgoing mails
194 push @$data, { text
=> 'Outgoing Mails' , value
=> $totals ->{ count_out
}, percentage
=> $totals ->{ count_out_per
} };
195 push @$data, { text
=> 'Bounces' , value
=> $totals ->{ bounces_out
}, percentage
=> $totals ->{ bounces_out_per
} };
196 push @$data, { text
=> 'Mail Traffic' , value
=> sprintf ( "%.3f MByte" , $totals ->{ traffic_out
}) };
198 $vars ->{ outgoing
} = $data,
200 my $tt = PMG
:: Config
:: get_template_toolkit
();
202 my $cfg = PMG
:: Config-
> new ();
203 my $email = $param ->{ receiver
} // $cfg -> get ( 'admin' , 'email' );
205 if (! defined ( $email )) {
206 return undef if $param ->{ auto
}; # silent exit - do not send report
207 die "no receiver configured \n " ;
210 my $mailfrom = "Proxmox Mail Gateway <postmaster>" ;
211 PMG
:: Utils
:: finalize_report
( $tt, 'pmgreport.tt' , $vars, $mailfrom, $email, $param ->{ debug
});
216 our $cmddef = [ __PACKAGE__
, 'pmgreport' , [], undef ];