]> git.proxmox.com Git - pve-manager.git/blob - PVE/Report.pm
pvereport: code cleanup, line length
[pve-manager.git] / PVE / Report.pm
1 package PVE::Report;
2
3 use strict;
4 use warnings;
5
6 use PVE::Tools;
7
8 $ENV{'PATH'} = '/sbin:/bin:/usr/sbin:/usr/bin';
9
10 my $cmd_timeout = 10; # generous timeout
11
12 my $init_report_cmds = sub {
13 # NOTE: always add new sections to the report_order array!
14 my $report_def = {
15 general => {
16 title => 'general system info',
17 cmds => [
18 'hostname',
19 'pveversion --verbose',
20 'cat /etc/hosts',
21 'top -b -n 1 | head -n 15',
22 'pvesubscription get',
23 'lscpu',
24 'pvesh get /cluster/resources --type node --output-format=yaml',
25 ],
26 },
27 storage => [
28 'cat /etc/pve/storage.cfg',
29 'pvesm status',
30 'cat /etc/fstab',
31 'findmnt --ascii',
32 'df --human',
33 ],
34 'virtual guests' => [
35 'qm list',
36 sub { dir2text('/etc/pve/qemu-server/', '\d.*conf') },
37 'pct list',
38 sub { dir2text('/etc/pve/lxc/', '\d.*conf') },
39 ],
40 network => [
41 'ip -details -statistics address',
42 'ip -details -4 route show',
43 'ip -details -6 route show',
44 'cat /etc/network/interfaces',
45 ],
46 firewall => [
47 sub { dir2text('/etc/pve/firewall/', '.*fw') },
48 'cat /etc/pve/local/host.fw',
49 'iptables-save',
50 ],
51 cluster => [
52 'pvecm nodes',
53 'pvecm status',
54 'cat /etc/pve/corosync.conf 2>/dev/null',
55 'ha-manager status',
56 ],
57 bios => [
58 'dmidecode -t bios',
59 ],
60 pci => [
61 'lspci -nnk',
62 ],
63 disks => [
64 'lsblk --ascii',
65 'ls -l /dev/disk/by-*/',
66 'iscsiadm -m node',
67 'iscsiadm -m session',
68 ],
69 volumes => [
70 'pvs',
71 'lvs',
72 'vgs',
73 ],
74 };
75
76 push @{$report_def->{volumes}}, 'zpool status', 'zpool list -v', 'zfs list'
77 if cmd_exists('zfs');
78
79 if (-e '/etc/ceph/ceph.conf') {
80 # TODO: add (now working) rdb ls over all pools? really needed?
81 push @{$report_def->{volumes}}, 'pveceph status', 'ceph osd status',
82 'ceph df', 'ceph osd df tree', 'cat /etc/ceph/ceph.conf',
83 'ceph config dump', 'pveceph pool ls', 'ceph versions';
84 }
85
86 push @{$report_def->{disks}}, 'multipath -ll', 'multipath -v3'
87 if cmd_exists('multipath');
88
89 return $report_def;
90 };
91
92 my $report;
93 # output the content of all the files of a directory
94 sub dir2text {
95 my ($target_dir, $regexp) = @_;
96
97 PVE::Tools::dir_glob_foreach($target_dir, $regexp, sub {
98 my ($file) = @_;
99 $report .= "\n# cat $target_dir$file\n";
100 $report .= PVE::Tools::file_get_contents($target_dir.$file)."\n";
101 });
102 }
103
104 # command -v is the posix equivalent of 'which'
105 sub cmd_exists { system("command -v '$_[0]' > /dev/null 2>&1") == 0 }
106
107 sub generate {
108
109 my $report_def = $init_report_cmds->();
110
111 my @report_order = ('general', 'storage', 'virtual guests', 'network',
112 'firewall', 'cluster', 'bios', 'pci', 'disks', 'volumes');
113
114 $report = '';
115 my $record_output = sub {
116 $report .= shift . "\n";
117 };
118
119 my $run_cmd_params = {
120 outfunc => $record_output,
121 errfunc => $record_output,
122 timeout => $cmd_timeout,
123 noerr => 1, # avoid checking programs exit code
124 };
125
126 foreach my $section (@report_order) {
127 my $s = $report_def->{$section};
128
129 my $title = "info about $section";
130 my $commands = $s;
131
132 if (ref($s) eq 'HASH') {
133 $commands = $s->{cmds};
134 $title = $s->{title} if defined($s->{title});
135 } elsif (ref($s) ne 'ARRAY') {
136 die "unknown report definition in section '$section'!";
137 }
138
139 $report .= "\n==== $title ====\n";
140 foreach my $command (@$commands) {
141 eval {
142 if (ref $command eq 'CODE') {
143 PVE::Tools::run_with_timeout($cmd_timeout, $command);
144 } else {
145 print STDERR "Process ".$command."...";
146 $report .= "\n# $command\n";
147 PVE::Tools::run_command($command, %$run_cmd_params);
148 }
149 print STDERR "OK";
150 };
151 print STDERR "\n";
152 $report .= "\nERROR: $@\n" if $@;
153 }
154 }
155
156 return $report;
157 }
158
159 1;