| 58 |
rodolico |
1 |
#!/usr/bin/env perl
|
|
|
2 |
use warnings;
|
|
|
3 |
use strict;
|
|
|
4 |
|
| 256 |
rodolico |
5 |
use version ; our $VERSION = 'v1.0.0';
|
| 58 |
rodolico |
6 |
|
| 256 |
rodolico |
7 |
# Template for modules to be used in sysinfo
|
| 58 |
rodolico |
8 |
# Author: R. W. Rodolico
|
| 256 |
rodolico |
9 |
# Date: 2017-11-24
|
|
|
10 |
#
|
|
|
11 |
# # get some information on lvm2
|
|
|
12 |
#
|
|
|
13 |
# Revision History
|
|
|
14 |
#
|
|
|
15 |
#
|
| 58 |
rodolico |
16 |
|
| 256 |
rodolico |
17 |
# find our location and use it for searching for libraries. library.pm must be in the same directory as the calling script
|
|
|
18 |
# or, if run interactively, in the parent of the modules
|
| 58 |
rodolico |
19 |
BEGIN {
|
| 251 |
rodolico |
20 |
use FindBin;
|
|
|
21 |
use File::Spec;
|
| 256 |
rodolico |
22 |
# prepend the bin directory and its parent
|
|
|
23 |
use lib File::Spec->catdir($FindBin::Bin), File::Spec->catdir("$FindBin::Bin/..");
|
|
|
24 |
eval( 'use library;' );
|
|
|
25 |
die sprintf( "Could not find library.pm in %s, INC is %s\n", __FILE__, join( "\n", @INC ) ) if $@;
|
| 58 |
rodolico |
26 |
}
|
|
|
27 |
|
| 256 |
rodolico |
28 |
#####
|
|
|
29 |
##### Change these to match your needs
|
|
|
30 |
#####
|
| 58 |
rodolico |
31 |
|
| 256 |
rodolico |
32 |
# 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
|
|
|
33 |
my $modulesList = {
|
|
|
34 |
'Data::Dumper' => undef,
|
|
|
35 |
};
|
| 251 |
rodolico |
36 |
|
| 256 |
rodolico |
37 |
# hash of commands that are needed for the system. key is the name of the command and, in some cases, the value will become
|
|
|
38 |
# the full path (from which or where)
|
|
|
39 |
my $commandsList = {
|
|
|
40 |
'lvs' => undef,
|
|
|
41 |
'vgs' => undef,
|
|
|
42 |
'pvs' => undef,
|
|
|
43 |
};
|
|
|
44 |
|
|
|
45 |
# list of operating systems this module can be used on.
|
|
|
46 |
my $osList = {
|
|
|
47 |
# 'mswin32' => undef,
|
|
|
48 |
# 'freebsd' => undef,
|
|
|
49 |
'linux' => undef,
|
|
|
50 |
};
|
|
|
51 |
|
|
|
52 |
# the category the return data should go into. See sysinfo for a list
|
| 58 |
rodolico |
53 |
my $CATEGORY = 'diskinfo';
|
|
|
54 |
|
| 256 |
rodolico |
55 |
#####
|
|
|
56 |
##### End of required
|
|
|
57 |
#####
|
| 58 |
rodolico |
58 |
|
| 256 |
rodolico |
59 |
# some variables needed for our system
|
|
|
60 |
my $errorPrepend = 'error: in ' . __FILE__; # this is prepended to any error messages
|
|
|
61 |
my @out; # temporary location for each line of output
|
| 58 |
rodolico |
62 |
|
| 256 |
rodolico |
63 |
# Try to load the modules we need. If we can not, then make a list of missing modules for error message.
|
|
|
64 |
for my $module ( keys %$modulesList ) {
|
|
|
65 |
eval ( "use $module;" );
|
|
|
66 |
push @out, "$errorPrepend Could not load $module" if $@;
|
| 58 |
rodolico |
67 |
}
|
|
|
68 |
|
| 256 |
rodolico |
69 |
if ( ! @out && ! checkOS ( $osList ) ) { # check if we are on an acceptible operating system
|
|
|
70 |
push @out, "$errorPrepend Invalid Operating System";
|
| 58 |
rodolico |
71 |
}
|
| 256 |
rodolico |
72 |
if ( !@out && ! validCommandOnSystem ( $commandsList ) ) {
|
|
|
73 |
push @out, "$errorPrepend Can not find some commands needed";
|
| 58 |
rodolico |
74 |
}
|
| 256 |
rodolico |
75 |
if ( !@out ) { # we made it, we have everything, so do the processing
|
|
|
76 |
#####
|
|
|
77 |
##### Your code starts here. Remember to push all output onto @out
|
|
|
78 |
#####
|
|
|
79 |
|
|
|
80 |
my @temp;
|
|
|
81 |
my @output;
|
|
|
82 |
my $line;
|
| 58 |
rodolico |
83 |
|
| 256 |
rodolico |
84 |
my %pv;
|
|
|
85 |
my %vg;
|
|
|
86 |
my %lv;
|
|
|
87 |
my $name;
|
| 58 |
rodolico |
88 |
|
| 256 |
rodolico |
89 |
@output = qx( pvs --units k --noheadings );
|
|
|
90 |
# this outputs several lines like the following
|
|
|
91 |
# /dev/md0 vg-md0 lvm2 a-- 5860147200.00k 6995968.00k
|
|
|
92 |
chomp @output;
|
|
|
93 |
foreach $line ( @output ) {
|
|
|
94 |
@temp = split( /\s+/, $line );
|
|
|
95 |
$name = $temp[1];
|
|
|
96 |
$pv{$name}{'vg'} = $temp[2];
|
|
|
97 |
$pv{$name}{'size'} = $temp[5];
|
|
|
98 |
$pv{$name}{'size'} =~ s/k//;
|
|
|
99 |
$pv{$name}{'size'} = int( $pv{$name}{'size'} );
|
|
|
100 |
$pv{$name}{'free'} = $temp[5];
|
|
|
101 |
$pv{$name}{'free'} =~ s/k//;
|
|
|
102 |
$pv{$name}{'free'} = int( $pv{$name}{'free'} );
|
|
|
103 |
}
|
| 58 |
rodolico |
104 |
|
| 256 |
rodolico |
105 |
# following outputs something like this
|
|
|
106 |
# vg-md0 1 9 0 wz--n- 5860147200.00k 6995968.00k
|
|
|
107 |
@output = qx( 'vgs' --units k --noheadings );
|
|
|
108 |
chomp @output;
|
|
|
109 |
foreach $line ( @output ) {
|
|
|
110 |
@temp = split( /\s+/, $line );
|
|
|
111 |
$name = $temp[1];
|
|
|
112 |
$vg{$name}{'size'} = $temp[6];
|
|
|
113 |
$vg{$name}{'size'} =~ s/k//;
|
|
|
114 |
$vg{$name}{'size'} = int( $vg{$name}{'size'} );
|
|
|
115 |
$vg{$name}{'free'} = $temp[7];
|
|
|
116 |
$vg{$name}{'free'} =~ s/k//;
|
|
|
117 |
$vg{$name}{'free'} = int( $vg{$name}{'free'} );
|
|
|
118 |
}
|
|
|
119 |
|
|
|
120 |
# outputs something like this
|
|
|
121 |
# centos vg-md0 -wi-ao-- 10485760.00k
|
|
|
122 |
@output = qx( lvs --units k --noheadings );
|
|
|
123 |
chomp @output;
|
|
|
124 |
foreach $line ( @output ) {
|
|
|
125 |
@temp = split( /\s+/, $line );
|
|
|
126 |
$name = $temp[1];
|
|
|
127 |
$lv{$name}{'vg'} = $temp[2];
|
|
|
128 |
$lv{$name}{'size'} = $temp[4];
|
|
|
129 |
$lv{$name}{'size'} =~ s/k//;
|
|
|
130 |
$lv{$name}{'size'} = int( $lv{$name}{'size'} );
|
|
|
131 |
}
|
|
|
132 |
|
|
|
133 |
my @out;
|
|
|
134 |
|
|
|
135 |
foreach $line ( keys %lv ) {
|
|
|
136 |
push @out, "$CATEGORY\t$line\tsize\t" . $lv{$line}{'size'};
|
|
|
137 |
push @out, "$CATEGORY\t$line\tfstype\tlvm2 lv";
|
|
|
138 |
push @out, "$CATEGORY\t$line\tvg\t" . $lv{$line}{'vg'};
|
|
|
139 |
}
|
|
|
140 |
|
|
|
141 |
foreach $line ( keys %vg ) {
|
|
|
142 |
push @out, "$CATEGORY\t$line\tsize\t" . $vg{$line}{'size'};
|
|
|
143 |
push @out, "$CATEGORY\t$line\tused\t" . ( $vg{$line}{'size'} - $vg{$line}{'free'}) ;
|
|
|
144 |
push @out, "$CATEGORY\t$line\tfstype\tlvm2 vg";
|
|
|
145 |
my @components;
|
|
|
146 |
foreach my $pv ( keys %pv ) {
|
|
|
147 |
if ( $pv{$pv}{'vg'} eq $line ) {
|
|
|
148 |
push @components, $pv;
|
|
|
149 |
}
|
| 58 |
rodolico |
150 |
}
|
| 256 |
rodolico |
151 |
push @out, "$CATEGORY\t$line\tcomponents\t" . join( ' ', @components );
|
| 58 |
rodolico |
152 |
}
|
|
|
153 |
|
| 256 |
rodolico |
154 |
foreach $line ( keys %pv ) {
|
|
|
155 |
push @out, "$CATEGORY\t$line\tsize\t" . $pv{$line}{'size'};
|
|
|
156 |
push @out, "$CATEGORY\t$line\tfstype\tlvm2 pv";
|
|
|
157 |
}
|
|
|
158 |
|
|
|
159 |
#####
|
|
|
160 |
##### Your code ends here.
|
|
|
161 |
#####
|
| 58 |
rodolico |
162 |
}
|
|
|
163 |
|
| 256 |
rodolico |
164 |
# If we are testing from the command line (caller is undef), print the results for debugging
|
|
|
165 |
print join( "\n", @out ) . "\n" unless caller;
|
|
|
166 |
# called by do, which has a value of the last assignment made, so make the assignment. The equivilent of a return
|
|
|
167 |
my $return = join( "\n", @out );
|
| 58 |
rodolico |
168 |
|