]> git.proxmox.com Git - pve-manager.git/blob - PVE/Report.pm
use 'die' instead of 'raise_param_exc'
[pve-manager.git] / PVE / Report.pm
1 package PVE::Report;
2
3 use strict;
4 use warnings;
5 use PVE::pvecfg;
6 use PVE::Tools;
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 'pveversion --verbose',
19 'cat /etc/hosts',
20 'top -b -n 1 | head -n 15',
21 'pvesubscription get',
22 'lscpu',
23 ],
24 },
25 storage => [
26 'cat /etc/pve/storage.cfg',
27 'pvesm status',
28 'cat /etc/fstab',
29 'findmnt --ascii',
30 'df --human',
31 ],
32 'virtual guests' => [
33 'qm list',
34 sub { dir2text('/etc/pve/qemu-server/', '\d.*conf') },
35 'pct list',
36 sub { dir2text('/etc/pve/lxc/', '\d.*conf') },
37 ],
38 network => [
39 'ip -details -statistics address',
40 'cat /etc/network/interfaces',
41 ],
42 firewall => [
43 sub { dir2text('/etc/pve/firewall/', '.*fw') },
44 'iptables-save',
45 ],
46 cluster => [
47 'pvecm nodes',
48 'pvecm status',
49 'cat /etc/pve/corosync.conf 2>/dev/null'
50 ],
51 bios => [
52 'dmidecode -t bios',
53 ],
54 pci => [
55 'lspci -nnk',
56 ],
57 disks => [
58 'lsblk --ascii',
59 ],
60 volumes => [
61 'lvs',
62 'vgs',
63 ],
64 };
65
66 my @report_order = ('general', 'storage', 'virtual guests', 'network',
67 'firewall', 'cluster', 'bios', 'pci', 'disks', 'volumes');
68
69 push @{$report_def->{volumes}}, 'zpool status', 'zfs list' if cmd_exists('zfs');
70
71 push @{$report_def->{disk}}, 'multipath -ll', 'multipath -v3' if cmd_exists('multipath');
72
73 my $report = '';
74
75 # output the content of all the files of a directory
76 sub dir2text {
77 my ($target_dir, $regexp) = @_;
78
79 PVE::Tools::dir_glob_foreach($target_dir, $regexp, sub {
80 my ($file) = @_;
81 $report .= "\n# cat $target_dir$file\n";
82 $report .= PVE::Tools::file_get_contents($target_dir.$file)."\n";
83 });
84 }
85
86 # command -v is the posix equivalent of 'which'
87 sub cmd_exists { system("command -v '$_[0]' > /dev/null 2>&1") == 0 }
88
89 sub generate {
90
91 my $record_output = sub {
92 $report .= shift . "\n";
93 };
94
95 my $run_cmd_params = {
96 outfunc => $record_output,
97 errfunc => $record_output,
98 timeout => $cmd_timeout,
99 noerr => 1, # avoid checking programs exit code
100 };
101
102 foreach my $section (@report_order) {
103 my $s = $report_def->{$section};
104
105 my $title = "info about $section";
106 my $commands = $s;
107
108 if (ref($s) eq 'HASH') {
109 $commands = $s->{cmds};
110 $title = $s->{title} if defined($s->{title});
111 } elsif (ref($s) ne 'ARRAY') {
112 die "unknown report definition in section '$section'!";
113 }
114
115 $report .= "\n==== $title ====\n";
116 foreach my $command (@$commands) {
117 eval {
118 if (ref $command eq 'CODE') {
119 PVE::Tools::run_with_timeout($cmd_timeout, $command);
120 } else {
121 $report .= "\n# $command\n";
122 PVE::Tools::run_command($command, %$run_cmd_params);
123 }
124 };
125 $report .= "\nERROR: $@\n" if $@;
126 }
127 }
128
129 return $report;
130 }
131
132 1;