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