]> git.proxmox.com Git - pmg-api.git/blob - src/PMG/Report.pm
4973dc528f9866e9d93e406a2f06c588c4c6d7cd
[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 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 sub { dir2text('/etc/postfix/','(?:clientaccess|senderaccess|rcptaccess)' ) },
51 sub { dump_templates() },
52 'pmgdb dump',
53 ],
54 };
55
56 my @report_order = ('general', 'storage', 'network', 'firewall', 'cluster', 'pmg');
57
58 my $report = '';
59
60 # output the content of all the files of a directory
61 sub dir2text {
62 my ($target_dir, $regexp) = @_;
63
64 PVE::Tools::dir_glob_foreach($target_dir, $regexp, sub {
65 my ($file) = @_;
66 $report .= "\n# cat $target_dir$file\n";
67 $report .= PVE::Tools::file_get_contents($target_dir.$file)."\n";
68 });
69 }
70
71 # command -v is the posix equivalent of 'which'
72 sub cmd_exists { system("command -v '$_[0]' > /dev/null 2>&1") == 0 }
73
74 sub generate {
75
76 my $record_output = sub {
77 $report .= shift . "\n";
78 };
79
80 my $run_cmd_params = {
81 outfunc => $record_output,
82 errfunc => $record_output,
83 timeout => $cmd_timeout,
84 noerr => 1, # avoid checking programs exit code
85 };
86
87 foreach my $section (@report_order) {
88 my $s = $report_def->{$section};
89
90 my $title = "info about $section";
91 my $commands = $s;
92
93 if (ref($s) eq 'HASH') {
94 $commands = $s->{cmds};
95 $title = $s->{title} if defined($s->{title});
96 } elsif (ref($s) ne 'ARRAY') {
97 die "unknown report definition in section '$section'!";
98 }
99
100 $report .= "\n==== $title ====\n";
101 foreach my $command (@$commands) {
102 eval {
103 if (ref $command eq 'CODE') {
104 PVE::Tools::run_with_timeout($cmd_timeout, $command);
105 } else {
106 $report .= "\n# $command\n";
107 PVE::Tools::run_command($command, %$run_cmd_params);
108 }
109 };
110 $report .= "\nERROR: $@\n" if $@;
111 }
112 }
113
114 return $report;
115 }
116
117 # using SpamAssassin's resolver, since the SA configuration can change which
118 # resolver is used and it uses only one resolver.
119 sub check_dns_resolution {
120
121 my $sa = Mail::SpamAssassin->new ({
122 debug => 0,
123 local_tests_only => 0,
124 home_dir_for_helpers => '/root',
125 userstate_dir => '/root/.spamassassin',
126 dont_copy_prefs => 1,
127 stop_at_threshold => 0,
128 });
129 $sa->init();
130 $sa->{resolver}->load_resolver();
131
132 my $packet = $sa->{resolver}->send('www.proxmox.com');
133 my $answer = $packet->{answer}->[0];
134 my $answertext = defined($answer) ? $answer->plain() : 'NXDOMAIN';
135
136 $report .= "\n# resolve www.proxmox.com\n";
137 $report .= $answertext . "\n";
138 }
139
140 sub dump_templates {
141 my ($template_dir, $base_dir) = ('/etc/pmg/templates/', '/var/lib/pmg/templates');
142 PVE::Tools::dir_glob_foreach($template_dir, '[^.].*', sub {
143 my ($file) = @_;
144 my $override_but_unmodified = 0;
145 if ($file =~ /.*\.(?:tt|in).*/ && -e "$base_dir/$file") {
146 my $shipped = PVE::Tools::file_get_contents("$base_dir/$file", 1024*1024);
147 my $override = PVE::Tools::file_get_contents("$template_dir/$file", 1024*1024);
148 $override_but_unmodified = $shipped eq $override;
149 }
150 if ($file =~ /\.ucf-(?:dist|new|old)/) {
151 $report .= "\n# SKIP $file\n";
152 } elsif ($override_but_unmodified) {
153 $report .= "\n# NOTE, found a unmodified template-override: $file\n";
154 } else {
155 $report .= "\n# cat $template_dir$file\n";
156 $report .= PVE::Tools::file_get_contents($template_dir.$file)."\n";
157 }
158 });
159 }
160
161 1;