20 |
rodolico |
1 |
#!/usr/bin/env perl
|
|
|
2 |
use warnings;
|
26 |
rodolico |
3 |
use strict;
|
2 |
rodolico |
4 |
|
116 |
rodolico |
5 |
# modified 20190419 RWR
|
|
|
6 |
# changed cleanup to remove encapsulating quotes
|
169 |
rodolico |
7 |
#
|
165 |
rodolico |
8 |
# 20200220 RWR v1.4.1
|
|
|
9 |
# added trim
|
169 |
rodolico |
10 |
# 20200224 RWR v1.4.2
|
|
|
11 |
# Added tableToHash
|
165 |
rodolico |
12 |
|
37 |
rodolico |
13 |
# Description: Library used by modules. Do NOT enable
|
20 |
rodolico |
14 |
|
37 |
rodolico |
15 |
|
169 |
rodolico |
16 |
our $VERSION = '1.4.2';
|
116 |
rodolico |
17 |
|
2 |
rodolico |
18 |
# set of libraries to be used by these modules
|
|
|
19 |
|
169 |
rodolico |
20 |
# parameters
|
|
|
21 |
# $report - a reference to a report array (one line per row)
|
|
|
22 |
# $headerRegex - optional regex to locate the first (header) line.
|
|
|
23 |
# $delimiter - optional delimiter used between header columns
|
|
|
24 |
# if $headerRegex is not defined, assumes first line is the headers
|
|
|
25 |
# if delimiter is not defined, assumes space is the delimitere
|
|
|
26 |
#
|
|
|
27 |
# Determines the field widths based on the header line
|
|
|
28 |
# processes each line in turn, returning an array ref, where each
|
|
|
29 |
# row is a hash ref. The hash ref has the key from the header, and
|
|
|
30 |
# the value found in the row.
|
2 |
rodolico |
31 |
|
169 |
rodolico |
32 |
sub tableToHash {
|
|
|
33 |
my ($report, $headerRegex, $delimiter) = @_;
|
|
|
34 |
my @return;
|
|
|
35 |
my %headers;
|
|
|
36 |
# get rid of any line returns at the end.
|
|
|
37 |
chomp @{$report};
|
|
|
38 |
my $lineNum = 0;
|
|
|
39 |
# bypass all the header information, if $headerRegex is defined
|
|
|
40 |
if ( $headerRegex ) {
|
|
|
41 |
for ( $lineNum = 0; $lineNum < @{$report} && ${$report}[$lineNum] !~ m/$headerRegex/; $lineNum++ ) {}
|
|
|
42 |
}
|
|
|
43 |
if ( $lineNum < @{$report} ) { # did we get an actual report? some drives will not give us one
|
|
|
44 |
# first, process the header line and get the start position and the length
|
|
|
45 |
my $char = 0;
|
|
|
46 |
while ( $char < length(${$report}[$lineNum]) ) {
|
|
|
47 |
substr( ${$report}[$lineNum],$char ) =~ m/^([^ ]+\s*)/;
|
|
|
48 |
my $header = $1;
|
|
|
49 |
my $start = $char;
|
|
|
50 |
my $length = length($header);
|
|
|
51 |
if ( $header = &trim( $header ) ) {
|
|
|
52 |
$headers{$header}{'start'} = $start;
|
|
|
53 |
# note that if this is the last header, we do NOT want a lenght attribute
|
|
|
54 |
$headers{$header}{'length'} = $length-1 if $length + $char < length(${$report}[$lineNum]);
|
|
|
55 |
}
|
|
|
56 |
$char += $length;
|
|
|
57 |
}
|
|
|
58 |
# now, get the data from all the following lines
|
|
|
59 |
while ( ++$lineNum < @{$report} ) {
|
|
|
60 |
last unless ${$report}[$lineNum]; # first blank line bails
|
|
|
61 |
my %thisLine;
|
|
|
62 |
foreach my $thisHeader (keys %headers) {
|
|
|
63 |
$thisLine{$thisHeader} = &trim(
|
|
|
64 |
defined( $headers{$thisHeader}{'length'} ) ?
|
|
|
65 |
substr( ${$report}[$lineNum], $headers{$thisHeader}{'start'}, $headers{$thisHeader}{'length'} )
|
|
|
66 |
: # if no length defined, grab everything to the end of the line
|
|
|
67 |
substr( ${$report}[$lineNum], $headers{$thisHeader}{'start'} )
|
|
|
68 |
);
|
|
|
69 |
}
|
|
|
70 |
push @return, \%thisLine;
|
|
|
71 |
}
|
|
|
72 |
}
|
|
|
73 |
return \@return;
|
|
|
74 |
}
|
|
|
75 |
|
|
|
76 |
|
|
|
77 |
|
|
|
78 |
|
20 |
rodolico |
79 |
# cleanUp - passed a delimiter and a string, does the following (delimiter can be '')
|
|
|
80 |
# chomps the string (removes trailing newlines)
|
|
|
81 |
# removes all text BEFORE the delimiter, the delimiter, and any whitespace
|
|
|
82 |
# thus, the string 'xxI Am x a weird string' with a newline will become
|
|
|
83 |
# 'a weird string' with no newline
|
116 |
rodolico |
84 |
# will also look for single and double quotes surrounding entire string and remove them
|
117 |
rodolico |
85 |
# if they exist
|
20 |
rodolico |
86 |
|
2 |
rodolico |
87 |
sub cleanUp {
|
|
|
88 |
my ($delimiter, $text) = @_;
|
|
|
89 |
chomp $text;
|
117 |
rodolico |
90 |
if ( $delimiter && $text =~ m/[^$delimiter]*$delimiter\s*(.*)/ ) {
|
116 |
rodolico |
91 |
$text = $1;
|
|
|
92 |
}
|
|
|
93 |
if ( $text =~ m/^'(.*)'$/ ) {
|
|
|
94 |
$text = $1;
|
|
|
95 |
}
|
|
|
96 |
if ( $text =~ m/^"(.*)"$/ ) {
|
|
|
97 |
$text = $1;
|
|
|
98 |
}
|
|
|
99 |
return $text;
|
2 |
rodolico |
100 |
}
|
|
|
101 |
|
165 |
rodolico |
102 |
# remove leading and trailing spaces from string
|
|
|
103 |
sub trim {
|
|
|
104 |
my $value = shift;
|
|
|
105 |
$value =~ s/^\s+|\s+$//g if $value;
|
|
|
106 |
return $value;
|
|
|
107 |
}
|
|
|
108 |
|
2 |
rodolico |
109 |
# checks if a command is valid on this system. If so, returns the full path to it
|
|
|
110 |
# else, returns empty string.
|
|
|
111 |
sub validCommandOnSystem {
|
|
|
112 |
my $command = shift;
|
|
|
113 |
$command = `which $command 2> /dev/null`;
|
|
|
114 |
chomp $command;
|
|
|
115 |
return -x $command ? $command : '';
|
|
|
116 |
}
|
|
|
117 |
|
48 |
rodolico |
118 |
sub getOperatingSystem {
|
|
|
119 |
return &cleanUp('', qx(uname -s));
|
|
|
120 |
}
|
|
|
121 |
|
55 |
rodolico |
122 |
sub getSysctlParameter {
|
|
|
123 |
my ( $sysctl, $parameter ) = @_;
|
|
|
124 |
my $result = qx( $sysctl $parameter );
|
|
|
125 |
chomp $result;
|
|
|
126 |
$result =~ s/$parameter:\s*//;
|
|
|
127 |
return $result;
|
|
|
128 |
}
|
|
|
129 |
|
152 |
rodolico |
130 |
# simple function to return true if the current date is the first of the week, month or year
|
|
|
131 |
# parameter passed in is 'w' (weekly), 'm' (monthly) or 'y' (yearly)
|
|
|
132 |
# to have a test run only weekly, place
|
|
|
133 |
# exit 1 unless &checkDate( 'w' );
|
|
|
134 |
sub checkDate {
|
153 |
rodolico |
135 |
return 1 if -e '/tmp/sysinfo.firstrun';
|
152 |
rodolico |
136 |
my $frequency = lc shift;
|
|
|
137 |
my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
|
|
|
138 |
return $mday == 1 if $frequency eq 'm'; # monthly
|
|
|
139 |
return $wday == 1 if $frequency eq 'w'; # weekly
|
|
|
140 |
return $yday == 1 if $frequency eq 'y'; # yearly
|
|
|
141 |
return 0; # we failed
|
|
|
142 |
}
|
|
|
143 |
|
|
|
144 |
|
20 |
rodolico |
145 |
1;
|