| 58 | 
           rodolico | 
           1 | 
           #!/usr/bin/env perl
  | 
        
        
            | 
            | 
           2 | 
           use warnings;
  | 
        
        
            | 
            | 
           3 | 
           use strict;  
  | 
        
        
            | 
            | 
           4 | 
              | 
        
        
            | 
            | 
           5 | 
           # Description: get information on lvm2 partitions
  | 
        
        
            | 
            | 
           6 | 
              | 
        
        
            | 
            | 
           7 | 
           our $VERSION = '1.0';
  | 
        
        
            | 
            | 
           8 | 
              | 
        
        
            | 
            | 
           9 | 
           # Linux lvm2
  | 
        
        
            | 
            | 
           10 | 
           # Author: R. W. Rodolico
  | 
        
        
            | 
            | 
           11 | 
           # Date:  2017-11-24
  | 
        
        
            | 
            | 
           12 | 
              | 
        
        
            | 
            | 
           13 | 
           # get some information on lvm2
  | 
        
        
            | 
            | 
           14 | 
              | 
        
        
            | 
            | 
           15 | 
           BEGIN {
  | 
        
        
            | 
            | 
           16 | 
              push @INC, shift;
  | 
        
        
            | 
            | 
           17 | 
           }
  | 
        
        
            | 
            | 
           18 | 
              | 
        
        
            | 
            | 
           19 | 
           use library;
  | 
        
        
            | 
            | 
           20 | 
              | 
        
        
            | 
            | 
           21 | 
           # category we will use for all values found
  | 
        
        
            | 
            | 
           22 | 
           # see sysinfo for a list of valid categories
  | 
        
        
            | 
            | 
           23 | 
           my $CATEGORY = 'diskinfo';
  | 
        
        
            | 
            | 
           24 | 
              | 
        
        
            | 
            | 
           25 | 
           # run the commands necessary to do whatever you want to do
  | 
        
        
            | 
            | 
           26 | 
           # The library entered above has some helper routines
  | 
        
        
            | 
            | 
           27 | 
           # validCommandOnSystem -- passed a name, returns the fully qualified path or '' if it does not exist
  | 
        
        
            | 
            | 
           28 | 
           # cleanUp - passed a delimiter and a string, does the following (delimiter can be '')
  | 
        
        
            | 
            | 
           29 | 
           #           chomps the string (removes trailing newlines)
  | 
        
        
            | 
            | 
           30 | 
           #           removes all text BEFORE the delimiter, the delimiter, and any whitespace
  | 
        
        
            | 
            | 
           31 | 
           #           thus, the string 'xxI Am x  a weird string' with a newline will become
  | 
        
        
            | 
            | 
           32 | 
           #           'a weird string' with no newline
  | 
        
        
            | 
            | 
           33 | 
              | 
        
        
            | 
            | 
           34 | 
           # now, return the tab delimited output (to STDOUT). $CATEGORY is the first
  | 
        
        
            | 
            | 
           35 | 
           # item, name as recognized by sysinfo is the second and the value is
  | 
        
        
            | 
            | 
           36 | 
           # the last one. For multiple entries, place on separate lines (ie, newline separated)
  | 
        
        
            | 
            | 
           37 | 
              | 
        
        
            | 
            | 
           38 | 
           # check for commands we want to run
  | 
        
        
            | 
            | 
           39 | 
           my %commands = ( 
  | 
        
        
            | 
            | 
           40 | 
                             'vgs' => '',
  | 
        
        
            | 
            | 
           41 | 
                             'pvs' => '',
  | 
        
        
            | 
            | 
           42 | 
                             'lvs' => ''
  | 
        
        
            | 
            | 
           43 | 
                          );
  | 
        
        
            | 
            | 
           44 | 
              | 
        
        
            | 
            | 
           45 | 
           # check the commands for validity
  | 
        
        
            | 
            | 
           46 | 
           foreach my $command ( keys %commands ) {
  | 
        
        
            | 
            | 
           47 | 
              $commands{$command} = &validCommandOnSystem( $command );
  | 
        
        
            | 
            | 
           48 | 
           }
  | 
        
        
            | 
            | 
           49 | 
              | 
        
        
            | 
            | 
           50 | 
           # bail if we don't have the commands we need
  | 
        
        
            | 
            | 
           51 | 
           exit 1 unless $commands{'lvs'};
  | 
        
        
            | 
            | 
           52 | 
              | 
        
        
            | 
            | 
           53 | 
           my @output;
  | 
        
        
            | 
            | 
           54 | 
           my @temp;
  | 
        
        
            | 
            | 
           55 | 
           my $line;
  | 
        
        
            | 
            | 
           56 | 
              | 
        
        
            | 
            | 
           57 | 
           my %pv;
  | 
        
        
            | 
            | 
           58 | 
           my %vg;
  | 
        
        
            | 
            | 
           59 | 
           my %lv;
  | 
        
        
            | 
            | 
           60 | 
           my $name;
  | 
        
        
            | 
            | 
           61 | 
              | 
        
        
            | 
            | 
           62 | 
           @output = qx( $commands{pvs} --units k --noheadings );
  | 
        
        
            | 
            | 
           63 | 
           # this outputs several lines like the following
  | 
        
        
            | 
            | 
           64 | 
           #  /dev/md0   vg-md0 lvm2 a--  5860147200.00k 6995968.00k
  | 
        
        
            | 
            | 
           65 | 
           chomp @output;
  | 
        
        
            | 
            | 
           66 | 
           foreach $line ( @output ) {
  | 
        
        
            | 
            | 
           67 | 
              @temp = split( /\s+/, $line );
  | 
        
        
            | 
            | 
           68 | 
              $name = $temp[1];
  | 
        
        
            | 
            | 
           69 | 
              $pv{$name}{'vg'} = $temp[2];
  | 
        
        
            | 
            | 
           70 | 
              $pv{$name}{'size'} = $temp[5];
  | 
        
        
            | 
            | 
           71 | 
              $pv{$name}{'size'} =~ s/k//;
  | 
        
        
            | 
            | 
           72 | 
              $pv{$name}{'size'} = int( $pv{$name}{'size'} );
  | 
        
        
            | 
            | 
           73 | 
              $pv{$name}{'free'} = $temp[5];
  | 
        
        
            | 
            | 
           74 | 
              $pv{$name}{'free'} =~ s/k//;
  | 
        
        
            | 
            | 
           75 | 
              $pv{$name}{'free'} = int( $pv{$name}{'free'} );
  | 
        
        
            | 
            | 
           76 | 
           }
  | 
        
        
            | 
            | 
           77 | 
              | 
        
        
            | 
            | 
           78 | 
           # following outputs something like this
  | 
        
        
            | 
            | 
           79 | 
           #  vg-md0   1   9   0 wz--n- 5860147200.00k 6995968.00k
  | 
        
        
            | 
            | 
           80 | 
           @output = qx( $commands{'vgs'} --units k --noheadings );
  | 
        
        
            | 
            | 
           81 | 
           chomp @output;
  | 
        
        
            | 
            | 
           82 | 
           foreach $line ( @output ) {
  | 
        
        
            | 
            | 
           83 | 
              @temp = split( /\s+/, $line );
  | 
        
        
            | 
            | 
           84 | 
              $name = $temp[1];
  | 
        
        
            | 
            | 
           85 | 
              $vg{$name}{'size'} = $temp[6];
  | 
        
        
            | 
            | 
           86 | 
              $vg{$name}{'size'} =~ s/k//;
  | 
        
        
            | 
            | 
           87 | 
              $vg{$name}{'size'} = int( $vg{$name}{'size'} );
  | 
        
        
            | 
            | 
           88 | 
              $vg{$name}{'free'} = $temp[7];
  | 
        
        
            | 
            | 
           89 | 
              $vg{$name}{'free'} =~ s/k//;
  | 
        
        
            | 
            | 
           90 | 
              $vg{$name}{'free'} = int( $vg{$name}{'free'} );
  | 
        
        
            | 
            | 
           91 | 
           }
  | 
        
        
            | 
            | 
           92 | 
              | 
        
        
            | 
            | 
           93 | 
           # outputs something like this
  | 
        
        
            | 
            | 
           94 | 
           #  centos                      vg-md0 -wi-ao--   10485760.00k                                           
  | 
        
        
            | 
            | 
           95 | 
           @output = qx( $commands{lvs} --units k --noheadings );
  | 
        
        
            | 
            | 
           96 | 
           chomp @output;
  | 
        
        
            | 
            | 
           97 | 
           foreach $line ( @output ) {
  | 
        
        
            | 
            | 
           98 | 
              @temp = split( /\s+/, $line );
  | 
        
        
            | 
            | 
           99 | 
              $name = $temp[1];
  | 
        
        
            | 
            | 
           100 | 
              $lv{$name}{'vg'} = $temp[2];
  | 
        
        
            | 
            | 
           101 | 
              $lv{$name}{'size'} = $temp[4];
  | 
        
        
            | 
            | 
           102 | 
              $lv{$name}{'size'} =~ s/k//;
  | 
        
        
            | 
            | 
           103 | 
              $lv{$name}{'size'} = int( $lv{$name}{'size'} );
  | 
        
        
            | 
            | 
           104 | 
           }
  | 
        
        
            | 
            | 
           105 | 
              | 
        
        
            | 
            | 
           106 | 
           my @results;
  | 
        
        
            | 
            | 
           107 | 
              | 
        
        
            | 
            | 
           108 | 
           foreach $line ( keys %lv ) {
  | 
        
        
            | 
            | 
           109 | 
              push @results, "$CATEGORY\t$line\tsize\t" . $lv{$line}{'size'};
  | 
        
        
            | 
            | 
           110 | 
              push @results, "$CATEGORY\t$line\tfstype\tlvm2 lv";
  | 
        
        
            | 
            | 
           111 | 
              push @results, "$CATEGORY\t$line\tvg\t" . $lv{$line}{'vg'};
  | 
        
        
            | 
            | 
           112 | 
           }
  | 
        
        
            | 
            | 
           113 | 
              | 
        
        
            | 
            | 
           114 | 
           foreach $line ( keys %vg ) {
  | 
        
        
            | 
            | 
           115 | 
              push @results, "$CATEGORY\t$line\tsize\t" . $vg{$line}{'size'};
  | 
        
        
            | 
            | 
           116 | 
              push @results, "$CATEGORY\t$line\tused\t" . ( $vg{$line}{'size'} - $vg{$line}{'free'}) ;
  | 
        
        
            | 
            | 
           117 | 
              push @results, "$CATEGORY\t$line\tfstype\tlvm2 vg";
  | 
        
        
            | 
            | 
           118 | 
              my @components;
  | 
        
        
            | 
            | 
           119 | 
              foreach my $pv ( keys %pv ) {
  | 
        
        
            | 
            | 
           120 | 
                 if ( $pv{$pv}{'vg'} eq $line ) {
  | 
        
        
            | 
            | 
           121 | 
                    push @components, $pv;
  | 
        
        
            | 
            | 
           122 | 
                 }
  | 
        
        
            | 
            | 
           123 | 
              }
  | 
        
        
            | 
            | 
           124 | 
              push @results, "$CATEGORY\t$line\tcomponents\t" . join( ' ', @components );
  | 
        
        
            | 
            | 
           125 | 
           }
  | 
        
        
            | 
            | 
           126 | 
              | 
        
        
            | 
            | 
           127 | 
           foreach $line ( keys %pv ) {
  | 
        
        
            | 
            | 
           128 | 
              push @results, "$CATEGORY\t$line\tsize\t" . $pv{$line}{'size'};
  | 
        
        
            | 
            | 
           129 | 
              push @results, "$CATEGORY\t$line\tfstype\tlvm2 pv";
  | 
        
        
            | 
            | 
           130 | 
           }
  | 
        
        
            | 
            | 
           131 | 
              | 
        
        
            | 
            | 
           132 | 
              | 
        
        
            | 
            | 
           133 | 
           print join( "\n", @results ) . "\n";
  | 
        
        
            | 
            | 
           134 | 
              | 
        
        
            | 
            | 
           135 | 
           exit 0;
  |