#!/usr/bin/env perl use warnings; use strict; use version ; our $VERSION = 'v1.0.0'; # Template for modules to be used in sysinfo # Author: R. W. Rodolico # Date: 2017-11-24 # # # get some information on lvm2 # # Revision History # # # find our location and use it for searching for libraries. library.pm must be in the same directory as the calling script # or, if run interactively, in the parent of the modules BEGIN { use FindBin; use File::Spec; # prepend the bin directory and its parent use lib File::Spec->catdir($FindBin::Bin), File::Spec->catdir("$FindBin::Bin/.."); eval( 'use library;' ); die sprintf( "Could not find library.pm in %s, INC is %s\n", __FILE__, join( "\n", @INC ) ) if $@; } ##### ##### Change these to match your needs ##### # Make this a list of all the modules we are going to use. You can replace undef with the version you need, if you like my $modulesList = { 'Data::Dumper' => undef, }; # hash of commands that are needed for the system. key is the name of the command and, in some cases, the value will become # the full path (from which or where) my $commandsList = { 'lvs' => undef, 'vgs' => undef, 'pvs' => undef, }; # list of operating systems this module can be used on. my $osList = { # 'mswin32' => undef, # 'freebsd' => undef, 'linux' => undef, }; # the category the return data should go into. See sysinfo for a list my $CATEGORY = 'diskinfo'; ##### ##### End of required ##### # some variables needed for our system my $errorPrepend = 'error: in ' . __FILE__; # this is prepended to any error messages my @out; # temporary location for each line of output # Try to load the modules we need. If we can not, then make a list of missing modules for error message. for my $module ( keys %$modulesList ) { eval ( "use $module;" ); push @out, "$errorPrepend Could not load $module" if $@; } if ( ! @out && ! checkOS ( $osList ) ) { # check if we are on an acceptible operating system push @out, "$errorPrepend Invalid Operating System"; } if ( !@out && ! validCommandOnSystem ( $commandsList ) ) { push @out, "$errorPrepend Can not find some commands needed"; } if ( !@out ) { # we made it, we have everything, so do the processing ##### ##### Your code starts here. Remember to push all output onto @out ##### my @temp; my @output; my $line; my %pv; my %vg; my %lv; my $name; @output = qx( 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( '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( 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 @out; foreach $line ( keys %lv ) { push @out, "$CATEGORY\t$line\tsize\t" . $lv{$line}{'size'}; push @out, "$CATEGORY\t$line\tfstype\tlvm2 lv"; push @out, "$CATEGORY\t$line\tvg\t" . $lv{$line}{'vg'}; } foreach $line ( keys %vg ) { push @out, "$CATEGORY\t$line\tsize\t" . $vg{$line}{'size'}; push @out, "$CATEGORY\t$line\tused\t" . ( $vg{$line}{'size'} - $vg{$line}{'free'}) ; push @out, "$CATEGORY\t$line\tfstype\tlvm2 vg"; my @components; foreach my $pv ( keys %pv ) { if ( $pv{$pv}{'vg'} eq $line ) { push @components, $pv; } } push @out, "$CATEGORY\t$line\tcomponents\t" . join( ' ', @components ); } foreach $line ( keys %pv ) { push @out, "$CATEGORY\t$line\tsize\t" . $pv{$line}{'size'}; push @out, "$CATEGORY\t$line\tfstype\tlvm2 pv"; } ##### ##### Your code ends here. ##### } # If we are testing from the command line (caller is undef), print the results for debugging print join( "\n", @out ) . "\n" unless caller; # called by do, which has a value of the last assignment made, so make the assignment. The equivilent of a return my $return = join( "\n", @out );