Subversion Repositories camp_sysinfo_client_3

Rev

Rev 153 | Go to most recent revision | Details | Compare with Previous | 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
7
 
165 rodolico 8
# 20200220 RWR v1.4.1
9
# added trim
10
 
37 rodolico 11
# Description: Library used by modules. Do NOT enable
20 rodolico 12
 
37 rodolico 13
 
165 rodolico 14
our $VERSION = '1.4.1';
116 rodolico 15
 
2 rodolico 16
# set of libraries to be used by these modules
17
 
18
 
20 rodolico 19
# cleanUp - passed a delimiter and a string, does the following (delimiter can be '')
20
#           chomps the string (removes trailing newlines)
21
#           removes all text BEFORE the delimiter, the delimiter, and any whitespace
22
#           thus, the string 'xxI Am x  a weird string' with a newline will become
23
#           'a weird string' with no newline
116 rodolico 24
# will also look for single and double quotes surrounding entire string and remove them
117 rodolico 25
# if they exist
20 rodolico 26
 
2 rodolico 27
sub cleanUp {
28
   my ($delimiter, $text) = @_;
29
   chomp $text;
117 rodolico 30
   if ( $delimiter && $text =~ m/[^$delimiter]*$delimiter\s*(.*)/ ) {
116 rodolico 31
      $text = $1;
32
   }
33
   if ( $text =~ m/^'(.*)'$/ ) {
34
      $text = $1;
35
   }
36
   if ( $text =~ m/^"(.*)"$/ ) {
37
      $text = $1;
38
   }
39
   return $text;
2 rodolico 40
}
41
 
165 rodolico 42
# remove leading and trailing spaces from string
43
sub trim {
44
 my $value = shift;
45
 $value =~ s/^\s+|\s+$//g if $value;
46
 return $value;
47
}
48
 
2 rodolico 49
# checks if a command is valid on this system. If so, returns the full path to it
50
# else, returns empty string.
51
sub validCommandOnSystem {
52
   my $command = shift;
53
   $command = `which $command 2> /dev/null`;
54
   chomp $command;
55
   return -x $command ? $command : '';
56
}
57
 
48 rodolico 58
sub getOperatingSystem {
59
   return &cleanUp('', qx(uname -s));
60
}   
61
 
55 rodolico 62
sub getSysctlParameter {
63
   my ( $sysctl, $parameter ) = @_;
64
   my $result = qx( $sysctl $parameter );
65
   chomp $result;
66
   $result =~ s/$parameter:\s*//;
67
   return $result;
68
}
69
 
152 rodolico 70
# simple function to return true if the current date is the first of the week, month or year
71
# parameter passed in is 'w' (weekly), 'm' (monthly) or 'y' (yearly)
72
# to have a test run only weekly, place
73
# exit 1 unless &checkDate( 'w' );
74
sub checkDate {
153 rodolico 75
   return 1 if -e '/tmp/sysinfo.firstrun';
152 rodolico 76
   my $frequency = lc shift;
77
   my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
78
   return $mday == 1 if $frequency eq 'm'; # monthly
79
   return $wday == 1 if $frequency eq 'w'; # weekly
80
   return $yday == 1 if $frequency eq 'y'; # yearly
81
   return 0; # we failed
82
}
83
 
84
 
20 rodolico 85
1;