]>
git.proxmox.com Git - pmg-api.git/blob - PMG/CLI/pmgreport.pm
c41de8ca98337e4b445fb5b127b19003fb4a92b8
1 package PMG
:: CLI
:: pmgreport
;
6 use POSIX
qw(strftime) ;
14 use PMG
:: RESTEnvironment
;
17 use PMG
:: ClusterConfig
;
19 use PMG
:: API2
:: Cluster
;
24 use base
qw(PVE::CLIHandler) ;
26 my $nodename = PVE
:: INotify
:: nodename
();
28 sub setup_environment
{
29 PMG
:: RESTEnvironment-
> setup_default_cli_env ();
31 my $rpcenv = PMG
:: RESTEnvironment-
> get ();
32 # API /config/cluster/nodes need a ticket to connect to other nodes
33 my $ticket = PMG
:: Ticket
:: assemble_ticket
( 'root @pam ' );
34 $rpcenv -> set_ticket ( $ticket );
37 my $get_system_table_data = sub {
39 my $ni = PMG
:: API2
:: NodeInfo-
> status ({ node
=> $nodename });
43 push @$data, { text
=> 'Hostname' , value
=> $nodename };
45 my $uptime = $ni ->{ uptime
} ? PMG
:: Utils
:: format_uptime
( $ni ->{ uptime
}) : '-' ;
47 push @$data, { text
=> 'Uptime' , value
=> $uptime };
49 push @$data, { text
=> 'Version' , value
=> $ni ->{ pmgversion
} };
52 if ( my $d = $ni ->{ loadavg
}) {
53 $loadavg15 = sprintf ( "%.2f" , $d ->[ 2 ]);
55 push @$data, { text
=> 'Load' , value
=> $loadavg15 };
58 if ( my $d = $ni ->{ memory
}) {
59 $mem = sprintf ( "%.2f %% " , $d ->{ used
}* 100 / $d ->{ total
});
61 push @$data, { text
=> 'Memory' , value
=> $mem };
64 if ( my $d = $ni ->{ rootfs
}) {
65 $disk = sprintf ( "%.2f %% " , $d ->{ used
}* 100 / $d ->{ total
});
67 push @$data, { text
=> 'Disk' , value
=> $disk };
72 my $get_cluster_table_data = sub {
74 my $res = PMG
:: API2
:: Cluster-
> status ({});
75 return undef if ! scalar ( @$res );
79 foreach my $ni ( @$res ) {
81 $state = 'S' if ! $ni ->{ insync
};
83 if ( my $err = $ni ->{ conn_error
}) {
85 $state = encode_entities
( "ERROR: $err " );
89 if ( my $d = $ni ->{ loadavg
}) {
90 $loadavg1 = sprintf ( "%.2f" , $d ->[ 0 ]);
94 if ( my $d = $ni ->{ memory
}) {
95 $memory = sprintf ( "%.2f %% " , $d ->{ used
}* 100 / $d ->{ total
});
99 if ( my $d = $ni ->{ rootfs
}) {
100 $disk = sprintf ( "%.2f %% " , $d ->{ used
}* 100 / $d ->{ total
});
104 hostname
=> $ni ->{ name
},
108 loadavg1
=> $loadavg1,
117 my $get_incoming_table_data = sub {
123 text
=> 'Incoming Mails' ,
124 value
=> $totals ->{ count_in
},
125 percentage
=> $totals ->{ count_in_per
},
129 text
=> 'Spam Mails' ,
130 value
=> $totals ->{ spamcount_in
},
131 percentage
=> $totals ->{ spamcount_in_per
},
135 text
=> 'Virus Mails' ,
136 value
=> $totals ->{ viruscount_in
},
137 percentage
=> $totals ->{ viruscount_in_per
},
141 text
=> 'SPF rejects' ,
142 value
=> $totals ->{ spfcount
},
143 percentage
=> $totals ->{ spfcount_per
},
147 text
=> 'Mail Traffic' ,
148 value
=> sprintf ( "%.3f MByte" , $totals ->{ traffic_in
}),
154 my $get_outgoing_table_data = sub {
160 text
=> 'Outgoing Mails' ,
161 value
=> $totals ->{ count_out
},
162 percentage
=> $totals ->{ count_out_per
},
167 value
=> $totals ->{ bounces_out
},
168 percentage
=> $totals ->{ bounces_out_per
},
172 text
=> 'Mail Traffic' ,
173 value
=> sprintf ( "%.3f MByte" , $totals ->{ traffic_out
}),
179 my $get_virus_table_data = sub {
180 my ( $virusinfo ) = @_ ;
184 foreach my $entry ( @$virusinfo ) {
185 next if ! $entry ->{ count
};
186 last if scalar ( @$data ) >= 10 ;
187 push @$data, { name
=> $entry ->{ name
}, count
=> $entry ->{ count
} };
190 return undef if ! scalar ( @$data );
195 my $get_quarantine_table_data = sub {
196 my ( $dbh, $qtype ) = @_ ;
198 my $ref = PMG
:: DBTools
:: get_quarantine_count
( $dbh, $qtype );
200 return undef if !( $ref && $ref ->{ count
});
205 text
=> "Quarantine Size (MBytes)" ,
206 value
=> int ( $ref ->{ mbytes
}),
210 text
=> "Number of Mails" ,
211 value
=> $ref ->{ count
},
215 text
=> "Average Size (Bytes)" ,
216 value
=> int ( $ref ->{ avgbytes
}),
221 text
=> "Average Spam Level" ,
222 value
=> int ( $ref ->{ avgspam
}),
229 __PACKAGE__-
> register_method ({
233 description
=> "Generate and send daily system report email." ,
235 additionalProperties
=> 0 ,
238 description
=> "Debug mode. Print raw email to stdout instead of sending them." ,
244 description
=> "Auto mode. Use setting from system configuration (set when invoked fron cron)." ,
250 description
=> "Send report to this email address. Default is the administratior email address." ,
251 type
=> 'string' , format
=> 'email' ,
255 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)." ,
257 enum
=> [ 'today' , 'yesterday' ],
263 returns
=> { type
=> 'null' },
267 my $timespan = $param ->{ timespan
} // 'today' ;
268 my ( $start, $end ) = PMG
:: Utils
:: lookup_timespan
( $timespan );
270 my $fqdn = PVE
:: Tools
:: get_fqdn
( $nodename );
273 hostname
=> $nodename,
275 date
=> strftime
( " %F " , localtime ( $end - 1 )),
278 my $cinfo = PMG
:: ClusterConfig-
> new ();
279 my $role = $cinfo ->{ local }->{ type
} // '-' ;
282 $vars ->{ system } = $get_system_table_data ->();
284 $vars ->{ cluster
} = $get_cluster_table_data ->();
285 if ( $role eq 'master' ) {
288 return undef if $param ->{ auto
}; # silent exit - do not send report
293 my $stat = PMG
:: Statistic-
> new ( $start, $end );
294 my $rdb = PMG
:: RuleDB-
> new ();
297 PMG
:: Statistic
:: update_stats
( $rdb ->{ dbh
}, $cinfo );
299 my $totals = $stat -> total_mail_stat ( $rdb );
301 $vars ->{ incoming
} = $get_incoming_table_data ->( $totals );
303 $vars ->{ outgoing
} = $get_outgoing_table_data ->( $totals );
305 my $virusinfo = $stat -> total_virus_stat ( $rdb );
306 if ( my $data = $get_virus_table_data ->( $virusinfo )) {
307 $vars ->{ virusstat
} = $data ;
310 if ( my $data = $get_quarantine_table_data ->( $rdb ->{ dbh
}, 'V' )) {
311 $vars ->{ virusquar
} = $data ;
314 if ( my $data = $get_quarantine_table_data ->( $rdb ->{ dbh
}, 'S' )) {
315 $vars ->{ spamquar
} = $data ;
318 my $tt = PMG
:: Config
:: get_template_toolkit
();
320 my $cfg = PMG
:: Config-
> new ();
321 my $email = $param ->{ receiver
} // $cfg -> get ( 'admin' , 'email' );
323 if (! defined ( $email )) {
324 return undef if $param ->{ auto
}; # silent exit - do not send report
325 die "no receiver configured \n " ;
328 my $mailfrom = "Proxmox Mail Gateway <postmaster>" ;
329 PMG
:: Utils
:: finalize_report
( $tt, 'pmgreport.tt' , $vars, $mailfrom, $email, $param ->{ debug
});
334 our $cmddef = [ __PACKAGE__
, 'pmgreport' , [], undef ];