[pve-devel] [PATCH pve-manager] Pretty format the output of multiple files in a directory

Dietmar Maurer dietmar at proxmox.com
Wed Oct 28 16:53:20 CET 2015


comments inline

> On October 28, 2015 at 3:50 PM Emmanuel Kasper <e.kasper at proxmox.com> wrote:
> 
> 
> Also, don't "cat" non existing files
> ---
>  bin/pvereport | 50 ++++++++++++++++++++++++++++++++++++++------------
>  1 file changed, 38 insertions(+), 12 deletions(-)
> 
> diff --git a/bin/pvereport b/bin/pvereport
> index 514cc9a..91fdb36 100755
> --- a/bin/pvereport
> +++ b/bin/pvereport
> @@ -3,19 +3,39 @@
>  use strict;
>  use warnings;
>  use PVE::pvecfg;
> +use PVE::Tools;
>  
>  ($> == 0 ) || die "please run as root\n";
>  
> +# output the content of all the files of a directory
> +sub dir2text {
> +    my ($target_dir) = @_;
> +
> +    my @files;
> +
> +    opendir (DIR, $target_dir) or die $!;
> +    @files = sort {$a cmp $b} grep { not /^\.|~$/ } readdir DIR;

Instead, I would pass a regex to match the files we want, i.e. m/^*.conf$/

You can use PVE::Tools::dir_glob_foreach() for this.

> +
> +    foreach my $file (@files){
> +	my $abs_path = $target_dir.$file;
> +	print "$abs_path\n";
> +	print "-" x length($abs_path)."\n";

print "# cat $abs_path\n";

> +	print PVE::Tools::file_get_contents($abs_path)."\n";
> +    }
> +
> +    close DIR;
> +}
> +
>  my @general = ('hostname', 'pveversion --verbose', 'cat /etc/hosts', 'top -b
> -n 1  | head -n 15',
> -  'pvesubscription get','lscpu', 'grep --max-count=1 "model name"
> /proc/cpuinfo' );
> +  'pvesubscription get', 'lscpu');
>  
>  my @storage = ('cat /etc/pve/storage.cfg', 'pvesm status', 'cat /etc/fstab',
> 'mount', 'df --human');
>  
>  my @volumes = ('lvdisplay', 'vgdisplay', 'zpool status', 'zfs list');
>  
> -my @machines = ('qm list', 'grep . /etc/pve/qemu-server/*');
> +my @machines = ('qm list', sub { dir2text('/etc/pve/qemu-server/') });
>  
> -my @net = ('ifconfig', 'cat /etc/network/interfaces', 'grep .
> /etc/pve/firewall/*',
> +my @net = ('ifconfig', 'cat /etc/network/interfaces', sub {
> dir2text('/etc/pve/firewall/') },
>    'iptables-save');
>  
>  my @cluster = ('pvecm nodes', 'pvecm status');
> @@ -23,11 +43,12 @@ my @cluster = ('pvecm nodes', 'pvecm status');
>  my @bios = ('dmidecode -t bios');
>  
>  if (PVE::pvecfg::version() >= 4.0) {
> -    push @machines, 'grep . /etc/pve/lxc/*' ;
> -    push @cluster, 'cat /etc/pve/corosync.conf' ;
> +    push @machines, sub { dir2text('/etc/pve/openvz/') };

why openvz?

> +    push @cluster, 'cat /etc/pve/corosync.conf 2> /dev/null' ;
>  } else {
> -    push @machines, 'grep . /etc/pve/openvz/*' ;
> -    push @cluster,  'clustat', 'cat /etc/cluster.conf' ;
> +    push @general, 'grep --max-count=1 "model name" /proc/cpuinfo';
> +    push @machines, sub { dir2text('/etc/pve/openvz/') };
> +    push @cluster,  'clustat', 'cat /etc/cluster.conf 2> /dev/null';
>  }
>  
>  my $general_report = {
> @@ -65,9 +86,9 @@ my @global_report = ($general_report, $storage_report,
> $volume_report, $net_repo
>  # execute commands and display their output as if they've been done on a
> interactive shell
>  # so the local sysadmin can reproduce what we're doing
>  sub do_execute {
> -    my ($shell_command) = @_;
> -    print "$shell_command \n";
> -    system $shell_command;
> +    my ($command) = @_;
> +    print "$command \n";

I usually put a # in front of the command, like

print "# $command\n";

> +    system $command;
>      print "\n";
>  }
>  
> @@ -76,8 +97,13 @@ foreach my $subreport (@global_report) {
>      my @commands = @{$subreport->{'commands'}};
>  
>      print "==== ".$title." ====\n";
> -    foreach my $shell_command (@commands) {
> -	do_execute($shell_command);
> +    foreach my $command (@commands) {
> +	if (ref $command eq 'CODE') {
> +	    &$command;
> +	} else {
> +	   do_execute($command);
> +	   next;
> +	}
>      }
>  }
>  
> -- 
> 2.1.4
> 
> 
> _______________________________________________
> pve-devel mailing list
> pve-devel at pve.proxmox.com
> http://pve.proxmox.com/cgi-bin/mailman/listinfo/pve-devel
> 



More information about the pve-devel mailing list