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