Rev 153 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed
#!/usr/bin/env perl
use warnings;
use strict;
# modified 20190419 RWR
# changed cleanup to remove encapsulating quotes
# 20200220 RWR v1.4.1
# added trim
# Description: Library used by modules. Do NOT enable
our $VERSION = '1.4.1';
# set of libraries to be used by these modules
# cleanUp - passed a delimiter and a string, does the following (delimiter can be '')
# chomps the string (removes trailing newlines)
# removes all text BEFORE the delimiter, the delimiter, and any whitespace
# thus, the string 'xxI Am x a weird string' with a newline will become
# 'a weird string' with no newline
# will also look for single and double quotes surrounding entire string and remove them
# if they exist
sub cleanUp {
my ($delimiter, $text) = @_;
chomp $text;
if ( $delimiter && $text =~ m/[^$delimiter]*$delimiter\s*(.*)/ ) {
$text = $1;
}
if ( $text =~ m/^'(.*)'$/ ) {
$text = $1;
}
if ( $text =~ m/^"(.*)"$/ ) {
$text = $1;
}
return $text;
}
# remove leading and trailing spaces from string
sub trim {
my $value = shift;
$value =~ s/^\s+|\s+$//g if $value;
return $value;
}
# checks if a command is valid on this system. If so, returns the full path to it
# else, returns empty string.
sub validCommandOnSystem {
my $command = shift;
$command = `which $command 2> /dev/null`;
chomp $command;
return -x $command ? $command : '';
}
sub getOperatingSystem {
return &cleanUp('', qx(uname -s));
}
sub getSysctlParameter {
my ( $sysctl, $parameter ) = @_;
my $result = qx( $sysctl $parameter );
chomp $result;
$result =~ s/$parameter:\s*//;
return $result;
}
# simple function to return true if the current date is the first of the week, month or year
# parameter passed in is 'w' (weekly), 'm' (monthly) or 'y' (yearly)
# to have a test run only weekly, place
# exit 1 unless &checkDate( 'w' );
sub checkDate {
return 1 if -e '/tmp/sysinfo.firstrun';
my $frequency = lc shift;
my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
return $mday == 1 if $frequency eq 'm'; # monthly
return $wday == 1 if $frequency eq 'w'; # weekly
return $yday == 1 if $frequency eq 'y'; # yearly
return 0; # we failed
}
1;