#!/usr/bin/env perl use warnings; use strict; # Description: get information on lvm2 partitions our $VERSION = '1.0'; # Linux lvm2 # Author: R. W. Rodolico # Date: 2017-11-24 # get some information on lvm2 BEGIN { push @INC, shift; } use library; # category we will use for all values found # see sysinfo for a list of valid categories my $CATEGORY = 'diskinfo'; # run the commands necessary to do whatever you want to do # The library entered above has some helper routines # validCommandOnSystem -- passed a name, returns the fully qualified path or '' if it does not exist # cleanUp - passed a delimiter and a string, does the following (delimiter can be '') # chomps the string (removes trailing newlines) # removes all text BEFORE the delimiter, the delimiter, and any whitespace # thus, the string 'xxI Am x a weird string' with a newline will become # 'a weird string' with no newline # now, return the tab delimited output (to STDOUT). $CATEGORY is the first # item, name as recognized by sysinfo is the second and the value is # the last one. For multiple entries, place on separate lines (ie, newline separated) # check for commands we want to run my %commands = ( 'vgs' => '', 'pvs' => '', 'lvs' => '' ); # check the commands for validity foreach my $command ( keys %commands ) { $commands{$command} = &validCommandOnSystem( $command ); } # bail if we don't have the commands we need exit 1 unless $commands{'lvs'}; my @output; my @temp; my $line; my %pv; my %vg; my %lv; my $name; @output = qx( $commands{pvs} --units k --noheadings ); # this outputs several lines like the following # /dev/md0 vg-md0 lvm2 a-- 5860147200.00k 6995968.00k chomp @output; foreach $line ( @output ) { @temp = split( /\s+/, $line ); $name = $temp[1]; $pv{$name}{'vg'} = $temp[2]; $pv{$name}{'size'} = $temp[5]; $pv{$name}{'size'} =~ s/k//; $pv{$name}{'size'} = int( $pv{$name}{'size'} ); $pv{$name}{'free'} = $temp[5]; $pv{$name}{'free'} =~ s/k//; $pv{$name}{'free'} = int( $pv{$name}{'free'} ); } # following outputs something like this # vg-md0 1 9 0 wz--n- 5860147200.00k 6995968.00k @output = qx( $commands{'vgs'} --units k --noheadings ); chomp @output; foreach $line ( @output ) { @temp = split( /\s+/, $line ); $name = $temp[1]; $vg{$name}{'size'} = $temp[6]; $vg{$name}{'size'} =~ s/k//; $vg{$name}{'size'} = int( $vg{$name}{'size'} ); $vg{$name}{'free'} = $temp[7]; $vg{$name}{'free'} =~ s/k//; $vg{$name}{'free'} = int( $vg{$name}{'free'} ); } # outputs something like this # centos vg-md0 -wi-ao-- 10485760.00k @output = qx( $commands{lvs} --units k --noheadings ); chomp @output; foreach $line ( @output ) { @temp = split( /\s+/, $line ); $name = $temp[1]; $lv{$name}{'vg'} = $temp[2]; $lv{$name}{'size'} = $temp[4]; $lv{$name}{'size'} =~ s/k//; $lv{$name}{'size'} = int( $lv{$name}{'size'} ); } my @results; foreach $line ( keys %lv ) { push @results, "$CATEGORY\t$line\tsize\t" . $lv{$line}{'size'}; push @results, "$CATEGORY\t$line\tfstype\tlvm2 lv"; push @results, "$CATEGORY\t$line\tvg\t" . $lv{$line}{'vg'}; } foreach $line ( keys %vg ) { push @results, "$CATEGORY\t$line\tsize\t" . $vg{$line}{'size'}; push @results, "$CATEGORY\t$line\tused\t" . ( $vg{$line}{'size'} - $vg{$line}{'free'}) ; push @results, "$CATEGORY\t$line\tfstype\tlvm2 vg"; my @components; foreach my $pv ( keys %pv ) { if ( $pv{$pv}{'vg'} eq $line ) { push @components, $pv; } } push @results, "$CATEGORY\t$line\tcomponents\t" . join( ' ', @components ); } foreach $line ( keys %pv ) { push @results, "$CATEGORY\t$line\tsize\t" . $pv{$line}{'size'}; push @results, "$CATEGORY\t$line\tfstype\tlvm2 pv"; } print join( "\n", @results ) . "\n"; exit 0;