Subversion Repositories camp_sysinfo_client_3

Rev

Rev 85 | Rev 113 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 85 Rev 112
Line 5... Line 5...
5
# sysinfo
5
# sysinfo
6
# Author: R. W. Rodolico
6
# Author: R. W. Rodolico
7
# Primary client portion of sysinfo system. Will collect information about its current
7
# Primary client portion of sysinfo system. Will collect information about its current
8
# host and create a report containing the information. This report can then be processed
8
# host and create a report containing the information. This report can then be processed
9
# by process_sysinfo.pl on the collection computer.
9
# by process_sysinfo.pl on the collection computer.
10
# output file consists of an XML file of the form:
10
# output file consists of a YAML file of the form:
11
#  <sysinfo3.0.0>
11
#  <sysinfo3.0.0>
12
#    <diskinfo name='/dev/xvda3'>
12
#    <diskinfo name='/dev/xvda3'>
13
#      <fstype>ext3</fstype>
13
#      <fstype>ext3</fstype>
14
#      <mount>/home</mount>
14
#      <mount>/home</mount>
15
#      <size>51606140</size>
15
#      <size>51606140</size>
Line 146... Line 146...
146
# added UUID to configuration file
146
# added UUID to configuration file
147
#
147
#
148
# Version 3.2.1 20180424 RWR
148
# Version 3.2.1 20180424 RWR
149
# Finally got a semi-stable version of this running. Fixed a bunch of bugs
149
# Finally got a semi-stable version of this running. Fixed a bunch of bugs
150
# and appears to be working correctly.
150
# and appears to be working correctly.
-
 
151
#
-
 
152
# Version 3.3.0 20190419 RWR
-
 
153
# Converted to use YAML config file
-
 
154
 
-
 
155
# find our location and use it for searching for libraries
-
 
156
BEGIN {
-
 
157
   use FindBin;
-
 
158
   use File::Spec;
-
 
159
   use lib File::Spec->catdir($FindBin::Bin);
-
 
160
}
151
 
161
 
-
 
162
use YAML::Tiny;
152
 
163
 
153
# Following are global variables overridden if configuration file exists
164
# Following are global variables overridden if configuration file exists
154
 
165
 
155
my $TESTING = 0; # level's 0 (none) to 4 defined and increase verbosity while decreasing functionality
166
my $TESTING = 0; # level's 0 (none) to 4 defined and increase verbosity while decreasing functionality
156
our $VERSION = '3.2.1';
167
our $VERSION = '3.3.0';
157
 
168
 
158
my $indentLevel = 2; # number of spaces to indent per level in XML or YAML
169
my $indentLevel = 2; # number of spaces to indent per level in XML or YAML
159
 
170
 
160
$indentLevel = 3 if $TESTING;
171
$indentLevel = 3 if $TESTING;
161
if ($TESTING) {
172
if ($TESTING) {
Line 163... Line 174...
163
}
174
}
164
 
175
 
165
# paths to search for configuration file
176
# paths to search for configuration file
166
my @confFileSearchPath = ( '.', '/etc/camp/sysinfo-client', '/etc/camp', '/usr/local/etc/camp/sysinfo-client' );
177
my @confFileSearchPath = ( '.', '/etc/camp/sysinfo-client', '/etc/camp', '/usr/local/etc/camp/sysinfo-client' );
167
 
178
 
168
my $configurationFile = 'sysinfo-client.conf'; # name of the configuration file
179
my $configurationFile = 'sysinfo-client.yaml'; # name of the configuration file
169
 
180
 
170
my $reportDate = &getReportDate; # set report date
181
my $reportDate = &getReportDate; # set report date
171
 
182
 
172
my %configuration = (
183
my %configuration = (
173
   'moduleDirs' => [], # search paths for modules
184
   'moduleDirs' => [], # search paths for modules
Line 208... Line 219...
208
#######################################################
219
#######################################################
209
#
220
#
210
# loadConfigurationFile($confFile)
221
# loadConfigurationFile($confFile)
211
#
222
#
212
# Loads configuration file defined by $configurationFile, and dies if not available
223
# Loads configuration file defined by $configurationFile, and dies if not available
213
# Reads entire contents into memory where it is eval'd in main program
224
# This is a YAML file containing serialized contents of 
214
# Parameters: configuration file fully path/file name
225
# Parameters: configuration file fully path/file name
215
# NOTE: conf file must be a valid Perl file
226
# NOTE: conf file must be a valid Perl file
216
#
227
#
217
#######################################################
228
#######################################################
218
 
229
 
219
sub loadConfigurationFile {
230
sub loadConfigurationFile {
220
   my ( $fileName, @searchPath ) = @_;
231
   my ( $fileName, @searchPath ) = @_;
221
   my $confFile;
232
   my $confFile;
222
   if ( $confFile = &findFile( $fileName, \@searchPath ) ) {
233
   if ( $confFile = &findFile( $fileName, \@searchPath ) ) {
223
      print "Opening configuration from $confFile\n" if $TESTING > 2;
234
      print "Opening configuration from $confFile\n" if $TESTING > 2;
224
      open CONFFILE, "<$confFile" or die "Can not open configuration file $confFile: $!\n";
235
      my $yaml = YAML::Tiny->read( $confFile );
225
      my $confFileContents = join( '', <CONFFILE> ); # just slurp it into memory
-
 
226
      close CONFFILE;
-
 
227
      return ($confFileContents);
236
      return $yaml->[0];
228
   }
237
   }
229
   die "Can not find $fileName in any of " . join( "\n\t", @searchPath ) . "\n";
238
   die "Can not find $fileName in any of " . join( "\n\t", @searchPath ) . "\n";
230
}
239
}
231
 
240
 
232
#######################################################
241
#######################################################
Line 328... Line 337...
328
#
337
#
329
# escapeForYAML
338
# escapeForYAML
330
#
339
#
331
# Escapes values put into YAML report
340
# Escapes values put into YAML report
332
#
341
#
-
 
342
# DEPRECATED AS OF VERSION 3.3.0
-
 
343
# uses YAML::Tiny
-
 
344
#
333
#######################################################
345
#######################################################
334
sub escapeForYAML {
346
#sub escapeForYAML {
335
   my $value = shift;
347
#   my $value = shift;
336
   $value =~ s/'/\\'/gi; # escape single quotes
348
#   $value =~ s/'/\\'/gi; # escape single quotes
337
   $value =~ s/"/\\"/gi; # escape double quotes
349
#   $value =~ s/"/\\"/gi; # escape double quotes
338
   # pound sign indicates start of a comment and thus loses part
350
#   # pound sign indicates start of a comment and thus loses part
339
   # of strings. Surrounding it by double quotes in next statement
351
#   # of strings. Surrounding it by double quotes in next statement
340
   # allows 
352
#   # allows 
341
   $value = '"' . $value . '"' if ( $value =~ m/[#:]/ );
353
#   $value = '"' . $value . '"' if ( $value =~ m/[#:]/ );
342
   return $value;
354
#   return $value;
343
}
355
#}
344
 
356
 
345
#######################################################
357
#######################################################
346
#
358
#
347
# hashToYAML( $hashRef, $indent )
359
# hashToYAML( $hashRef, $indent )
348
#
360
#
Line 362... Line 374...
362
# However, I followed all the RFC for the values that were given, so
374
# However, I followed all the RFC for the values that were given, so
363
# assume any YAML reader can parse this
375
# assume any YAML reader can parse this
364
# NOTE: YAML appears to give a resulting file 1/3 smaller than the above
376
# NOTE: YAML appears to give a resulting file 1/3 smaller than the above
365
#       XML, and compresses down in like manner
377
#       XML, and compresses down in like manner
366
#
378
#
-
 
379
# DEPRECATED AS OF VERSION 3.3.0
-
 
380
# uses YAML::Tiny
-
 
381
#
367
#######################################################
382
#######################################################
368
sub hashToYAML {
383
#sub hashToYAML {
369
   my ($hashRef, $indent) = @_;
384
#   my ($hashRef, $indent) = @_;
370
   $indent = 0 unless $indent; # default to 0 if not defined
385
#   $indent = 0 unless $indent; # default to 0 if not defined
371
   
386
#   
372
   my $output; # where the output is stored
387
#   my $output; # where the output is stored
373
   foreach my $key ( keys %$hashRef ) { # for each key in the current reference
388
#   foreach my $key ( keys %$hashRef ) { # for each key in the current reference
374
      print "Looking at $key\n" if $TESTING > 3;
389
#      print "Looking at $key\n" if $TESTING > 3;
375
      # see http://www.perlmonks.org/?node_id=175651 for isa function
390
#      # see http://www.perlmonks.org/?node_id=175651 for isa function
376
      if ( UNIVERSAL::isa( $$hashRef{$key}, 'HASH' ) ) { # is the value another hash?
391
#      if ( UNIVERSAL::isa( $$hashRef{$key}, 'HASH' ) ) { # is the value another hash?
377
            # NOTE: unlike xml, indentation is NOT optional in YAML, so the following line verifies $indentlevel is non-zero
392
#            # NOTE: unlike xml, indentation is NOT optional in YAML, so the following line verifies $indentlevel is non-zero
378
            #       and, if it is, uses a default 3 character indentation
393
#            #       and, if it is, uses a default 3 character indentation
379
            $output .= (' ' x $indent ) . &escapeForYAML($key) . ":\n" . # key, plus colon, plus newline
394
#            $output .= (' ' x $indent ) . &escapeForYAML($key) . ":\n" . # key, plus colon, plus newline
380
                    &hashToYAML( $$hashRef{$key}, $indent+($indentLevel ? $indentLevel : 3) ) . # add results of recursive call
395
#                    &hashToYAML( $$hashRef{$key}, $indent+($indentLevel ? $indentLevel : 3) ) . # add results of recursive call
381
                    "\n";
396
#                    "\n";
382
      } elsif ( UNIVERSAL::isa( $$hashRef{$key}, 'ARRAY' ) ) { # is it an array? ignore it
397
#      } elsif ( UNIVERSAL::isa( $$hashRef{$key}, 'ARRAY' ) ) { # is it an array? ignore it
383
      } else { # it is a scalar, so just do <key>value</key>
398
#      } else { # it is a scalar, so just do <key>value</key>
384
         $output .= (' ' x $indent ) . &escapeForYAML($key) . ': ' . &escapeForYAML($$hashRef{$key}) . "\n";
399
#         $output .= (' ' x $indent ) . &escapeForYAML($key) . ': ' . &escapeForYAML($$hashRef{$key}) . "\n";
385
      }
400
#      }
386
   }
401
#   }
387
   return $output;
402
#   return $output;
388
}
403
#}
389
 
404
 
390
 
405
 
391
#######################################################
406
#######################################################
392
#
407
#
393
# tabDelimitedToHash ($hashRef, $tabdelim)
408
# tabDelimitedToHash ($hashRef, $tabdelim)
Line 507... Line 522...
507
&processParameters( @ARGV );
522
&processParameters( @ARGV );
508
 
523
 
509
# load the configuration file
524
# load the configuration file
510
 
525
 
511
#die "Searching for $configurationFile in = \n" . join( "\n", @confFileSearchPath ) . "\n";
526
#die "Searching for $configurationFile in = \n" . join( "\n", @confFileSearchPath ) . "\n";
-
 
527
%configuration = %{ &loadConfigurationFile( $configurationFile, @confFileSearchPath) };
512
eval ( &loadConfigurationFile( $configurationFile, @confFileSearchPath) ) or die "Could not load config: $@\n";
528
#eval ( &loadConfigurationFile( $configurationFile, @confFileSearchPath) ) or die "Could not load config: $@\n";
513
die Dumper( \%configuration ) . "\n" if $TESTING > 4;
529
die Dumper( \%configuration ) . "\n" if $TESTING > 4;
514
 
530
 
515
# user did not define a serial number, so make something up
531
# user did not define a serial number, so make something up
516
$configuration{'serialNumber'} = '' unless $configuration{'serialNumber'};
532
$configuration{'serialNumber'} = '' unless $configuration{'serialNumber'};
517
# oops, no client name (required) so tell them and exit
533
# oops, no client name (required) so tell them and exit
Line 535... Line 551...
535
foreach my $moduleDir ( @{$configuration{'moduleDirs'}} ) {
551
foreach my $moduleDir ( @{$configuration{'moduleDirs'}} ) {
536
   &ProcessModules( $System, "$moduleDir/" );
552
   &ProcessModules( $System, "$moduleDir/" );
537
}
553
}
538
 
554
 
539
# now, everything ins in $System, so convert it to the proper output format
555
# now, everything ins in $System, so convert it to the proper output format
540
my $out =  "#sysinfo: $VERSION YAML\n---\n" . &hashToYAML( $System ) . "...\n";
556
#my $out = "#sysinfo: $VERSION YAML\n---\n" . &hashToYAML( $System ) . "...\n";
-
 
557
 
-
 
558
my $out =  "#sysinfo: $VERSION YAML\n" . Dump( $System );
541
 
559
 
542
#print Data::Dumper->Dump([$System],['System']) if $TESTING>2;
560
#print Data::Dumper->Dump([$System],['System']) if $TESTING>2;
543
 
561
 
544
# load some global values for use in the script, if required
562
# load some global values for use in the script, if required
545
my $globals = { 
563
my $globals = {