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