Subversion Repositories camp_sysinfo_client_3

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

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