| 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 |
|
| 251 |
rodolico |
15 |
# find our location and use it for searching for libraries
|
| 58 |
rodolico |
16 |
BEGIN {
|
| 251 |
rodolico |
17 |
use FindBin;
|
|
|
18 |
use File::Spec;
|
|
|
19 |
use lib File::Spec->catdir($FindBin::Bin);
|
|
|
20 |
eval( 'use library;' ); die "Could not find library.pm in the code directory\n" if $@;
|
|
|
21 |
eval( 'use Data::Dumper;' );
|
| 58 |
rodolico |
22 |
}
|
|
|
23 |
|
| 251 |
rodolico |
24 |
# check for valid OS.
|
|
|
25 |
exit 1 unless &checkOS( { 'linux' => undef } );
|
| 58 |
rodolico |
26 |
|
| 251 |
rodolico |
27 |
# check for required commands, return 2 if they don't exist. Enter an full list of all commands required. If one doesn't exist
|
|
|
28 |
# script returns a 2
|
|
|
29 |
foreach my $command ( 'vgs','pvs','lvs' ) {
|
|
|
30 |
exit 2 unless &validCommandOnSystem( $command );
|
|
|
31 |
}
|
|
|
32 |
|
| 58 |
rodolico |
33 |
# category we will use for all values found
|
|
|
34 |
# see sysinfo for a list of valid categories
|
|
|
35 |
my $CATEGORY = 'diskinfo';
|
|
|
36 |
|
|
|
37 |
my @output;
|
|
|
38 |
my @temp;
|
|
|
39 |
my $line;
|
|
|
40 |
|
|
|
41 |
my %pv;
|
|
|
42 |
my %vg;
|
|
|
43 |
my %lv;
|
|
|
44 |
my $name;
|
|
|
45 |
|
| 251 |
rodolico |
46 |
@output = qx( pvs --units k --noheadings );
|
| 58 |
rodolico |
47 |
# this outputs several lines like the following
|
|
|
48 |
# /dev/md0 vg-md0 lvm2 a-- 5860147200.00k 6995968.00k
|
|
|
49 |
chomp @output;
|
|
|
50 |
foreach $line ( @output ) {
|
|
|
51 |
@temp = split( /\s+/, $line );
|
|
|
52 |
$name = $temp[1];
|
|
|
53 |
$pv{$name}{'vg'} = $temp[2];
|
|
|
54 |
$pv{$name}{'size'} = $temp[5];
|
|
|
55 |
$pv{$name}{'size'} =~ s/k//;
|
|
|
56 |
$pv{$name}{'size'} = int( $pv{$name}{'size'} );
|
|
|
57 |
$pv{$name}{'free'} = $temp[5];
|
|
|
58 |
$pv{$name}{'free'} =~ s/k//;
|
|
|
59 |
$pv{$name}{'free'} = int( $pv{$name}{'free'} );
|
|
|
60 |
}
|
|
|
61 |
|
|
|
62 |
# following outputs something like this
|
|
|
63 |
# vg-md0 1 9 0 wz--n- 5860147200.00k 6995968.00k
|
| 251 |
rodolico |
64 |
@output = qx( 'vgs' --units k --noheadings );
|
| 58 |
rodolico |
65 |
chomp @output;
|
|
|
66 |
foreach $line ( @output ) {
|
|
|
67 |
@temp = split( /\s+/, $line );
|
|
|
68 |
$name = $temp[1];
|
|
|
69 |
$vg{$name}{'size'} = $temp[6];
|
|
|
70 |
$vg{$name}{'size'} =~ s/k//;
|
|
|
71 |
$vg{$name}{'size'} = int( $vg{$name}{'size'} );
|
|
|
72 |
$vg{$name}{'free'} = $temp[7];
|
|
|
73 |
$vg{$name}{'free'} =~ s/k//;
|
|
|
74 |
$vg{$name}{'free'} = int( $vg{$name}{'free'} );
|
|
|
75 |
}
|
|
|
76 |
|
|
|
77 |
# outputs something like this
|
|
|
78 |
# centos vg-md0 -wi-ao-- 10485760.00k
|
| 251 |
rodolico |
79 |
@output = qx( lvs --units k --noheadings );
|
| 58 |
rodolico |
80 |
chomp @output;
|
|
|
81 |
foreach $line ( @output ) {
|
|
|
82 |
@temp = split( /\s+/, $line );
|
|
|
83 |
$name = $temp[1];
|
|
|
84 |
$lv{$name}{'vg'} = $temp[2];
|
|
|
85 |
$lv{$name}{'size'} = $temp[4];
|
|
|
86 |
$lv{$name}{'size'} =~ s/k//;
|
|
|
87 |
$lv{$name}{'size'} = int( $lv{$name}{'size'} );
|
|
|
88 |
}
|
|
|
89 |
|
|
|
90 |
my @results;
|
|
|
91 |
|
|
|
92 |
foreach $line ( keys %lv ) {
|
|
|
93 |
push @results, "$CATEGORY\t$line\tsize\t" . $lv{$line}{'size'};
|
|
|
94 |
push @results, "$CATEGORY\t$line\tfstype\tlvm2 lv";
|
|
|
95 |
push @results, "$CATEGORY\t$line\tvg\t" . $lv{$line}{'vg'};
|
|
|
96 |
}
|
|
|
97 |
|
|
|
98 |
foreach $line ( keys %vg ) {
|
|
|
99 |
push @results, "$CATEGORY\t$line\tsize\t" . $vg{$line}{'size'};
|
|
|
100 |
push @results, "$CATEGORY\t$line\tused\t" . ( $vg{$line}{'size'} - $vg{$line}{'free'}) ;
|
|
|
101 |
push @results, "$CATEGORY\t$line\tfstype\tlvm2 vg";
|
|
|
102 |
my @components;
|
|
|
103 |
foreach my $pv ( keys %pv ) {
|
|
|
104 |
if ( $pv{$pv}{'vg'} eq $line ) {
|
|
|
105 |
push @components, $pv;
|
|
|
106 |
}
|
|
|
107 |
}
|
|
|
108 |
push @results, "$CATEGORY\t$line\tcomponents\t" . join( ' ', @components );
|
|
|
109 |
}
|
|
|
110 |
|
|
|
111 |
foreach $line ( keys %pv ) {
|
|
|
112 |
push @results, "$CATEGORY\t$line\tsize\t" . $pv{$line}{'size'};
|
|
|
113 |
push @results, "$CATEGORY\t$line\tfstype\tlvm2 pv";
|
|
|
114 |
}
|
|
|
115 |
|
|
|
116 |
|
|
|
117 |
print join( "\n", @results ) . "\n";
|
|
|
118 |
|
|
|
119 |
exit 0;
|