| 58 |
rodolico |
1 |
#!/usr/bin/env perl
|
|
|
2 |
use warnings;
|
| 256 |
rodolico |
3 |
use strict;
|
| 58 |
rodolico |
4 |
|
| 256 |
rodolico |
5 |
use version ; our $VERSION = 'v1.0.0';
|
| 251 |
rodolico |
6 |
|
| 256 |
rodolico |
7 |
# Template for modules to be used in sysinfo
|
|
|
8 |
# Author: R. W. Rodolico
|
|
|
9 |
# Date: 2025-04-02
|
|
|
10 |
#
|
| 58 |
rodolico |
11 |
# Description: get information on md (RAID)
|
| 256 |
rodolico |
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 |
'mdadm' => undef,
|
|
|
41 |
'fdisk' => undef,
|
|
|
42 |
};
|
| 58 |
rodolico |
43 |
|
| 256 |
rodolico |
44 |
# list of operating systems this module can be used on.
|
|
|
45 |
my $osList = {
|
|
|
46 |
# 'mswin32' => undef,
|
|
|
47 |
# 'freebsd' => undef,
|
|
|
48 |
'linux' => undef,
|
|
|
49 |
};
|
| 58 |
rodolico |
50 |
|
| 256 |
rodolico |
51 |
# the category the return data should go into. See sysinfo for a list
|
|
|
52 |
my $CATEGORY = 'diskinfo';
|
| 58 |
rodolico |
53 |
|
| 256 |
rodolico |
54 |
#####
|
|
|
55 |
##### End of required
|
|
|
56 |
#####
|
| 58 |
rodolico |
57 |
|
| 256 |
rodolico |
58 |
# some variables needed for our system
|
|
|
59 |
my $errorPrepend = 'error: in ' . __FILE__; # this is prepended to any error messages
|
|
|
60 |
my @out; # temporary location for each line of output
|
| 58 |
rodolico |
61 |
|
| 256 |
rodolico |
62 |
# Try to load the modules we need. If we can not, then make a list of missing modules for error message.
|
|
|
63 |
for my $module ( keys %$modulesList ) {
|
|
|
64 |
eval ( "use $module;" );
|
|
|
65 |
push @out, "$errorPrepend Could not load $module" if $@;
|
|
|
66 |
}
|
| 58 |
rodolico |
67 |
|
| 256 |
rodolico |
68 |
if ( ! @out && ! checkOS ( $osList ) ) { # check if we are on an acceptible operating system
|
|
|
69 |
push @out, "$errorPrepend Invalid Operating System";
|
|
|
70 |
}
|
|
|
71 |
if ( !@out && ! validCommandOnSystem ( $commandsList ) ) {
|
|
|
72 |
push @out, "$errorPrepend Can not find some commands needed";
|
|
|
73 |
}
|
|
|
74 |
if ( !@out ) { # we made it, we have everything, so do the processing
|
|
|
75 |
#####
|
|
|
76 |
##### Your code starts here. Remember to push all output onto @out
|
|
|
77 |
#####
|
|
|
78 |
|
|
|
79 |
if ( -e '/proc/mdstat' ) {
|
|
|
80 |
open MD,'</proc/mdstat' or exit 1;
|
|
|
81 |
my @temp = <MD>;
|
|
|
82 |
close MD;
|
| 58 |
rodolico |
83 |
|
| 256 |
rodolico |
84 |
while ( my $line = shift @temp ) {
|
|
|
85 |
chomp $line;
|
|
|
86 |
if ( $line =~ m/^(md\d+)\s+:\s+(.*)$/ ) {
|
|
|
87 |
my $device = $1;
|
|
|
88 |
my @components = split( /\s+/, $2 );
|
|
|
89 |
shift @components;
|
|
|
90 |
push @out, "$CATEGORY\t$device\tlevel\t" . shift(@components);
|
|
|
91 |
push @out, "$CATEGORY\t$device\tcomponents\t" . join( " ", @components );
|
|
|
92 |
my $temp = qx( mdadm --detail /dev/$device | grep 'Array Size' | cut -d':' -f2 | cut -d' ' -f2 );
|
|
|
93 |
chomp $temp;
|
|
|
94 |
push @out, "$CATEGORY\t$device\tsize\t$temp";
|
|
|
95 |
}
|
|
|
96 |
}
|
| 58 |
rodolico |
97 |
}
|
| 256 |
rodolico |
98 |
|
|
|
99 |
#####
|
|
|
100 |
##### Your code ends here.
|
|
|
101 |
#####
|
| 58 |
rodolico |
102 |
}
|
|
|
103 |
|
| 256 |
rodolico |
104 |
# If we are testing from the command line (caller is undef), print the results for debugging
|
|
|
105 |
print join( "\n", @out ) . "\n" unless caller;
|
|
|
106 |
# called by do, which has a value of the last assignment made, so make the assignment. The equivilent of a return
|
|
|
107 |
my $return = join( "\n", @out );
|
| 58 |
rodolico |
108 |
|