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