]> git.proxmox.com Git - pmg-api.git/blob - PMG/Report.pm
Drop sa-awl output from pmg-system-report
[pmg-api.git] / PMG / Report.pm
1 package PMG::Report;
2
3 use strict;
4 use warnings;
5 use PVE::Tools;
6 use Mail::SpamAssassin::DnsResolver;
7
8 $ENV{'PATH'} = '/sbin:/bin:/usr/sbin:/usr/bin';
9
10 my $cmd_timeout = 10; # generous timeout
11
12 # NOTE: always add new sections to the report_order array!
13 my $report_def = {
14 general => {
15 title => 'general system info',
16 cmds => [
17 'hostname',
18 'pmgversion --verbose',
19 'cat /etc/hosts',
20 'cat /etc/resolv.conf',
21 'top -b -n 1 | head -n 15',
22 'pmgsubscription get',
23 sub { check_dns_resolution() },
24 ],
25 },
26 storage => [
27 'cat /etc/fstab',
28 'findmnt --ascii',
29 'df --human',
30 'lsblk --ascii',
31 ],
32 network => [
33 'ip -details -statistics address',
34 'cat /etc/network/interfaces',
35 ],
36 firewall => [
37 'iptables-save',
38 ],
39 cluster => [
40 'pmgcm status',
41 ],
42 pmg => [
43 'pmgconfig dump',
44 sub { dir2text('/etc/pmg/','(?:domains|mynetworks|tls_policy|transport)' ) },
45 sub { dir2text('/etc/pmg/templates/', '[^.].*' ) },
46 'pmgdb dump',
47 ],
48 };
49
50 my @report_order = ('general', 'storage', 'network', 'firewall', 'cluster', 'pmg');
51
52 my $report = '';
53
54 # output the content of all the files of a directory
55 sub dir2text {
56 my ($target_dir, $regexp) = @_;
57
58 PVE::Tools::dir_glob_foreach($target_dir, $regexp, sub {
59 my ($file) = @_;
60 $report .= "\n# cat $target_dir$file\n";
61 $report .= PVE::Tools::file_get_contents($target_dir.$file)."\n";
62 });
63 }
64
65 # command -v is the posix equivalent of 'which'
66 sub cmd_exists { system("command -v '$_[0]' > /dev/null 2>&1") == 0 }
67
68 sub generate {
69
70 my $record_output = sub {
71 $report .= shift . "\n";
72 };
73
74 my $run_cmd_params = {
75 outfunc => $record_output,
76 errfunc => $record_output,
77 timeout => $cmd_timeout,
78 noerr => 1, # avoid checking programs exit code
79 };
80
81 foreach my $section (@report_order) {
82 my $s = $report_def->{$section};
83
84 my $title = "info about $section";
85 my $commands = $s;
86
87 if (ref($s) eq 'HASH') {
88 $commands = $s->{cmds};
89 $title = $s->{title} if defined($s->{title});
90 } elsif (ref($s) ne 'ARRAY') {
91 die "unknown report definition in section '$section'!";
92 }
93
94 $report .= "\n==== $title ====\n";
95 foreach my $command (@$commands) {
96 eval {
97 if (ref $command eq 'CODE') {
98 PVE::Tools::run_with_timeout($cmd_timeout, $command);
99 } else {
100 $report .= "\n# $command\n";
101 PVE::Tools::run_command($command, %$run_cmd_params);
102 }
103 };
104 $report .= "\nERROR: $@\n" if $@;
105 }
106 }
107
108 return $report;
109 }
110
111 # using SpamAssassin's resolver, since the SA configuration can change which
112 # resolver is used and it uses only one resolver.
113 sub check_dns_resolution {
114
115 my $sa = Mail::SpamAssassin->new ({
116 debug => 0,
117 local_tests_only => 0,
118 home_dir_for_helpers => '/root',
119 userstate_dir => '/root/.spamassassin',
120 dont_copy_prefs => 1,
121 stop_at_threshold => 0,
122 });
123 $sa->init();
124
125 my $packet = $sa->{resolver}->send('www.proxmox.com');
126 my $answer = $packet->{answer}->[0];
127 my $answertext = defined($answer) ? $answer->plain() : 'NXDOMAIN';
128
129 $report .= "\n# resolve www.proxmox.com\n";
130 $report .= $answertext . "\n";
131 }
132
133 1;