Subversion Repositories camp_sysinfo_client_3

Rev

Rev 161 | Rev 197 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
21 rodolico 1
#!/usr/bin/env perl
2
 
45 rodolico 3
# v1.2.0 20161022 RWR
4
# moved makeConfig here so it is usable by install and configure
77 rodolico 5
#
6
# v1.3.0 20190108 RWR
7
# added UUID and set defaults for config file
85 rodolico 8
#
9
# v1.4.0 20190204 RWR
10
# added autoconvert from v1.2 configuration file to v1.3
11
# using dumper to set configuration file
94 rodolico 12
#
13
# version 2.0 20190330 RWR
14
# changed it so all configs are YAML
125 rodolico 15
#
16
# version 2.1 20191101 RWR
17
# changed --name-- tag to name
129 rodolico 18
#
19
# version 2.2 20191105 RWR
20
# fixed where dmidecode missing caused exception
135 rodolico 21
#
22
# version 2.2.1 20191112 RWR
23
# added timeStamp
194 rodolico 24
#
25
# version 2.3.0 20220609 RWR
26
# moved loadConfigurationFile and timeStamp here
45 rodolico 27
 
21 rodolico 28
package sysinfoconf;
29
 
42 rodolico 30
 
194 rodolico 31
our $VERSION = '2.3.0';
21 rodolico 32
use warnings;
26 rodolico 33
use strict;  
34
 
92 rodolico 35
#use Data::Dumper;
144 rodolico 36
use YAML::Tiny;
35 rodolico 37
use File::Basename;
38
 
21 rodolico 39
use Exporter;
40
 
41
our @ISA = qw( Exporter );
194 rodolico 42
our @EXPORT = qw( $clientName $serialNumber $hostname 
43
                  $transports $sysinfo3 $binDir $moduleDir
44
                  $scriptDir $confDir $binName $confName $configurationFile
45
                  @confFileSearchPath  @moduleDirs @scriptDirs
46
                  %sendTypes
21 rodolico 47
                  &showConf &transportsToConfig &getAnswer &yesno  
144 rodolico 48
                  &writeConfig &processParameters $TESTING
49
                  &setDryRun &enableModules &makeConfig &findConf &timeStamp
194 rodolico 50
                  &loadConfigurationFile &logIt
21 rodolico 51
                );
52
 
36 rodolico 53
our $TESTING = 0;
35 rodolico 54
our $dryRun;
28 rodolico 55
our $clientName = '';
56
our $serialNumber = '';
57
our $hostname = '';
53 rodolico 58
 
194 rodolico 59
# paths to search for configuration file
60
my @confFileSearchPath = ( '.', '/etc/camp/sysinfo-client', '/etc/camp', '/usr/local/etc/camp/sysinfo-client' );
104 rodolico 61
 
194 rodolico 62
my $configurationFile = 'sysinfo-client.yaml'; # name of the configuration file
104 rodolico 63
 
194 rodolico 64
 
53 rodolico 65
my @installDirs = ( '/opt/camp/sysinfo-client', '/usr/local/opt/camp/sysinfo-client' );
66
my @confDirs =    ( '/etc/camp/sysinfo-client', '/usr/local/etc/camp/sysinfo-client' );
67
 
68
 
28 rodolico 69
our @moduleDirs = ( '/opt/camp/sysinfo-client/modules', '/etc/camp/sysinfo-client/modules' );
70
our @scriptDirs = ( '/opt/camp/sysinfo-client/scripts', '/etc/camp/sysinfo-client/scripts' );
111 rodolico 71
our $transports = {}; # holds transportation mechanisms
21 rodolico 72
 
103 rodolico 73
our $sysinfo3 = 'sysinfo-client.yaml';
21 rodolico 74
 
28 rodolico 75
our $binDir = '/opt/camp/sysinfo-client';
44 rodolico 76
our $moduleDir = $binDir . '/modules';
77
our $scriptDir = $binDir . '/scripts';
28 rodolico 78
our $confDir = '/etc/camp/sysinfo-client';
21 rodolico 79
 
28 rodolico 80
our $binName = 'sysinfo-client';
107 rodolico 81
our $confName = 'sysinfo-client.yaml';
21 rodolico 82
 
35 rodolico 83
sub setDryRun {
84
   $dryRun = shift;
85
}
21 rodolico 86
 
28 rodolico 87
our %sendTypes = ( 
21 rodolico 88
                  'SendEmail' =>    { 'sendScript' => 'sendEmailScript',
89
                                      'keys' => 
90
                                      [
91
                                        'mailTo',
92
                                        'mailSubject',
93
                                        'mailCC',
94
                                        'mailBCC',
95
                                        'mailServer',
96
                                        'mailFrom',
97
                                        'logFile',
98
                                        'otherCLParams',
99
                                        'tls',
100
                                        'smtpUser',
101
                                        'smtpPass',
102
                                        'sendEmailScriptLocation'
103
                                      ],
104
                                    },
105
                  'HTTP Upload' =>  { 'sendScript' => 'upload_http',
106
                                      'keys' => 
107
                                      [
108
                                        'URL',
109
                                        'key for report',
110
                                        'key for date',
111
                                        'key for hostname',
112
                                        'key for client',
113
                                        'key for serial number'
114
                                      ]
86 rodolico 115
                                    },
116
                  'Save Local' =>   { 'sendScript' => 'save_local',
117
                                      'keys' =>
118
                                      [
119
                                         'output directory'
120
                                      ]
121
                                   }
21 rodolico 122
                );
123
 
124
 
135 rodolico 125
#######################################################
126
#
127
# timeStamp
128
#
129
# return current system date as YYYY-MM-DD HH:MM:SS
130
#
131
#######################################################
132
sub timeStamp {
133
   my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst)=localtime(time);
143 rodolico 134
   return sprintf "%4d-%02d-%02d %02d:%02d:%02d",$year+1900,$mon+1,$mday,$hour,$min,$sec;
135 rodolico 135
}
136
 
137
 
37 rodolico 138
sub enableModules {
139
   my $moduleDir = shift;
140
   my %modules;
141
   if ( opendir( my $dh, "$moduleDir" ) ) {
142
      %modules = map{ $_ => { 'enabled' => -x "$moduleDir/$_" } } 
143
                 grep { ! /^\./ } 
144
                 readdir $dh;
145
      closedir( $dh );
146
      foreach my $filename ( keys %modules ) {
147
         if ( open FILE,"<$moduleDir/$_" ) {
148
            my @descript = grep{ /^# Description: (.*)/ } <FILE>;
149
            close FILE;
150
            chomp @descript;
151
            $modules{$filename}{'description'} = $descript[0];
152
         } # if
153
      } # foreach
154
   } # if
155
} # enableModules
156
 
21 rodolico 157
sub showConf {
81 rodolico 158
 
35 rodolico 159
   my $configuration = shift;
92 rodolico 160
   $configuration = Dump($configuration);
84 rodolico 161
   return $configuration;
21 rodolico 162
}
163
 
164
sub transportsToConfig {
35 rodolico 165
   my $transports = shift;
21 rodolico 166
   my $config;
167
   foreach my $priority ( sort { $a <=> $b } keys %$transports ) {
124 rodolico 168
      my $name = $$transports{ $priority }{'name'};
21 rodolico 169
      $config .= "# Tranport $name at priority $priority\n";
170
      my $thisOne = $$transports{ $priority };
171
      foreach my $key ( sort keys %$thisOne ) {
172
         $config .= "\$\$transports{$priority}{'$key'} = '$$thisOne{$key}';\n";
173
      } # foreach
174
   } # foreach
175
   return $config;
176
} # transportsToConfig
177
 
94 rodolico 178
 
179
# read the configuration file passed in and return a reference to it
180
# to the calling routine
181
sub readConfig {
182
   my $filename = shift;
183
   my $config;
184
   return $config unless -e $filename;
185
   if ( open CONF,"<$filename" ) {
186
      my $contents = join( '', <CONF> );
187
      close CONF;
188
      # now, look to see what kind of file this is.
189
      if ( $contents =~ m/^---(\s*#.*)?$/m ) {
190
         print "Reading $filename as YAML\n";
191
         #print "Contents are:\n\n=====================$contents\n=====================\n";
192
         # this is a yaml file
193
         #$contents = YAML::Tiny->read( $config );
161 rodolico 194
         # try to load the contents into $config, and warn if there is a problem.
195
         eval( $config = Load( $contents ) ); warn $@ if $@;
94 rodolico 196
      } elsif ( $contents =~ m/\$clientName/ ) {
197
         # this is old style
198
         print "Reading $filename as old school file\n";
199
         my $clientName = '';
200
         my $serialNumber = '';
201
         my $hostname = '';
202
         my @moduleDirs;
203
         my @scriptDirs;
204
         my $UUID = '';
205
         my $transports;
206
         # now, eval the information we just read.
207
         # NOTE: we must turn off strict while doing this, and we die if something breaks.
208
         no strict "vars";
209
         eval( $contents );
210
         use strict "vars";
211
         die "Error during eval: $@\n" if $@;
212
         $config->{'clientName'} = $clientName if $clientName;
213
         $config->{'serialNumber'} = $serialNumber if $serialNumber;
214
         $config->{'UUID'} = $UUID unless $config->{'UUID'};
215
         $config->{'hostname'} = $hostname if $hostname;
216
         $config->{'moduleDirs'} = [ @moduleDirs ] if @moduleDirs;
217
         $config->{'scriptDirs'} = [ @scriptDirs ] if @scriptDirs;
218
         $config->{'transports'} = $transports if $transports;
129 rodolico 219
         foreach my $trans ( keys %{$config->{'transports'}} ) {
220
            if ( exists ( $config->{'transports'}->{$trans}->{'-name-'} ) ) {
221
               $config->{'transports'}->{$trans}->{'name'} = $config->{'transports'}->{$trans}->{'-name-'};
222
               delete( $config->{'transports'}->{$trans}->{'-name-'} );
223
            }
224
         }
94 rodolico 225
      }
226
   } else {
227
         warn "Could not read config file $filename, skipped: $!\n";
228
   }
229
   return $config;
230
}
231
 
45 rodolico 232
sub makeConfig {
233
   my @configFileNames = @_;
234
   my %config;
235
 
236
   foreach my $config ( @configFileNames ) {
129 rodolico 237
      my $thisConfig = &readConfig( $config ) if $config && -e $config;
94 rodolico 238
      # add the new config to %config, overwriting any existing keys which are duplicated
239
      @config{keys %$thisConfig} = values %$thisConfig;
45 rodolico 240
   }
94 rodolico 241
   # now, ensure the correct values are loaded in some areas
81 rodolico 242
   unless ( $config{'hostname'} ) {
45 rodolico 243
      $hostname = `hostname -f`;
244
      chomp $hostname;
81 rodolico 245
      $config{'hostname'} = $hostname;
45 rodolico 246
   }
84 rodolico 247
   unless ( $config{'serialNumber'} ) {
53 rodolico 248
      $serialNumber = `dmidecode -t 1 | grep 'Serial Number' | cut -d':' -f2` if `which dmidecode`;
249
      chomp $serialNumber;
250
      $serialNumber =~ s/\s//gi;
84 rodolico 251
      $config{'serialNumber'} = $serialNumber;
53 rodolico 252
   }
81 rodolico 253
   unless ( $config{'UUID'} ) {
129 rodolico 254
      my $UUID = `which dmidecode` ?  `dmidecode -t 1 | grep -i uuid | cut -d':' -f2` : '';
77 rodolico 255
      $UUID =~ s/\s//gi;
81 rodolico 256
      $config{'UUID'} = $UUID;
77 rodolico 257
   }
45 rodolico 258
 
110 rodolico 259
   # ensure we have the default SaveLocal transport defined
260
   unless ( defined $config{'transports'}{'99'} ) {
261
      $config{'transports'}{'99'} = {
262
                         'name'=> 'SaveLocal',
263
                         'output directory' => '/tmp',
264
                         'sendScript' => 'save_local'
265
                        };
266
   }
267
 
45 rodolico 268
   return \%config;
269
}
270
 
21 rodolico 271
# prompt the user for a response, then allow them to enter it
272
# the first response is considered the default and is printed
273
# in all caps if more than one exists
274
# first answer is used if they simply press the Enter
275
# key. The response is returned all lower case if more than one
276
# exists.
277
# it is assumed 
278
sub getAnswer {
279
   my ( $prompt, @answers ) = @_;
280
   $answers[0] = '' unless defined( $answers[0] );
281
   my $default = $answers[0];
282
   my $onlyOneAnswer = scalar( @answers ) == 1;
283
   print $prompt . '[ ';
284
   $answers[0] = uc $answers[0] unless $onlyOneAnswer;
285
   print join( ' | ', @answers ) . ' ]: ';
28 rodolico 286
   my $thisAnswer = <>;
21 rodolico 287
   chomp $thisAnswer;
288
   $thisAnswer = $default unless $thisAnswer;
289
   return $thisAnswer;
290
}
291
 
292
sub yesno {
40 rodolico 293
   my ( $prompt, $default ) = @_;
294
   $default = 'yes' unless $default;
295
   my $answer = &getAnswer( $prompt, $default eq 'yes' ? ('yes','no' ) : ('no', 'yes') );
21 rodolico 296
   return lc( substr( $answer, 0, 1 ) ) eq 'y';
297
}
298
 
299
sub writeConfig {
132 rodolico 300
   use File::Temp qw / tempfile /;
35 rodolico 301
   my ( $filename,$content ) = @_;
129 rodolico 302
   if ( $filename ) { # they sent us a filename
303
      my $path;
304
      ($filename, $path ) = fileparse( $filename );
305
      `mkdir -p $path` unless -d $path;
306
      $filename = $path . '/' . $filename;
21 rodolico 307
      `cp $filename $filename.bak` if ( -e $filename );
129 rodolico 308
      unless ( $dryRun ) {
309
         open CONF,">$filename" or die "Could not write to $filename: $!\n";
310
         print CONF $content;
311
         close CONF;
312
         `chmod 600 $filename`;
313
      }
314
   } else { # no path provided, so just create a temp file
315
      # we will create a temporary file and return the name
316
      # it is the calling programs responsiblity to remove the file
317
      my $fh;
318
      ($fh,$filename) = tempfile( UNLINK => 0 );
319
      print $fh $content;
320
      close $fh
21 rodolico 321
   }
129 rodolico 322
   return $filename;
21 rodolico 323
}
324
 
325
sub processParameters {
326
   while ( my $parameter = shift ) {
327
      if ( $parameter eq '-v' ) {
26 rodolico 328
         print "$VERSION\n";
21 rodolico 329
         exit;
330
      }
331
   } # while
332
}
333
 
53 rodolico 334
sub findConf {
335
   my $confName = shift;
336
   for ( my $i = 0; $i < @confDirs; $i++ ) {
337
      if ( -d $confDirs[ $i ] ) {
55 rodolico 338
         return ( $confDirs[$i],  $confName );
53 rodolico 339
      }
340
   }
104 rodolico 341
   return ( '', $confName );
53 rodolico 342
}
21 rodolico 343
 
194 rodolico 344
#######################################################
345
#
346
# findFile( $filename, @directories )
347
#
348
# Locates a file by searching sequentially in one or more
349
# directories, returning the first one found
350
# 
351
# Returns '' if not found
352
#
353
#######################################################
104 rodolico 354
 
194 rodolico 355
sub findFile {
356
   my ( $filename, $directories ) = @_;
357
   &logIt( 3, "Looking for $filename in findFile" );
358
   for ( my $i = 0; $i < scalar( @{$directories} ); $i++ ) {
359
      my $confFile = $$directories[$i] . '/' . $filename;
360
      &logIt( 4, "Looking for $filename in $confFile" );
361
      return $confFile if ( -f $confFile );
362
   }
363
   return '';
364
}
365
 
104 rodolico 366
 
194 rodolico 367
#######################################################
368
#
369
# loadConfigurationFile($confFile)
370
#
371
# Loads configuration file defined by $configurationFile, and dies if not available
372
# This is a YAML file containing serialized contents of 
373
# Parameters:
374
#    $$fileName - name of file to look for (reference)
375
#    @searchPath - array of paths to find $filename
376
#
377
#######################################################
104 rodolico 378
 
194 rodolico 379
sub loadConfigurationFile {   
380
   my ( $fileName, @searchPath ) = @_;
381
   $$fileName = $configurationFile unless $$fileName;
382
   @searchPath = @confFileSearchPath unless @searchPath;
383
   &logIt( 2, "Looking for config file $$fileName in " . join( ', ', @searchPath ) );
384
   my $confFile;
385
   if ( $confFile = &findFile( $$fileName, \@searchPath ) ) {
386
      &logIt( 3, "Opening configuration from $confFile" );
387
      my $yaml = YAML::Tiny->read( $confFile );
388
      &logIt( 4, "Configuration file contents\n$yaml" );
389
      $$fileName = $confFile;
390
      return $yaml->[0];
391
   }
392
   die "Can not find $fileName in any of " . join( "\n\t", @searchPath ) . "\n";
393
}
394
 
395
 
396
#######################################################
397
# function to simply log things
398
# first parameter is the priority, if <= $logDef->{'log level'} will print
399
# all subsequent parameters assumed to be strings to sent to the log
400
# returns 0 on failure
401
#         1 on success
402
#         2 if priority > log level
403
#        -1 if $logDef is unset
404
# currently, only logs to a file
405
#######################################################
406
sub logIt {
407
   my $priority = shift;
408
 
409
   # turn off variable checking so it doesn't blow up on lack of %configuration file
410
   no strict 'vars';
411
 
412
   return -1 unless exists $configuration{'logging'};
413
   return 2 unless $priority <= $configuration{'logging'}{'log level'};
414
   if ( $configuration{'logging'}{'log type'} eq 'cache' ) {
415
      push @{ $configuration{'logging'}{'cache'} }, @_;
416
      return;
417
   } elsif ( defined( $configuration{'logging'}{'cache'} ) ) {
418
      unshift @_, @{ $configuration{'logging'}{'cache'} };
419
      delete $configuration{'logging'}{'cache'};
420
   }
421
   if ( $configuration{'logging'}{'log type'} eq 'file' ) {
422
      if ( open LOG, '>>' . $configuration{'logging'}{'log path'} ) {
423
         while ( my $t = shift ) {
424
            print LOG &timeStamp() . "\t$t\n";
425
         }
426
         close LOG;
427
      }
428
   } elsif ( $configuration{'logging'}{'log type'} eq 'syslog' ) {
429
      use Sys::Syslog;                        # all except setlogsock()
430
      use Sys::Syslog qw(:standard :macros);  # standard functions & macros
431
 
432
      my $syslogName = 'sysinfo-client';
433
      my $logopt = 'nofatal';
434
      my $facility = 'LOG_LOCAL0';
435
      my $priority = 'LOG_NOTICE';
436
 
437
      openlog( $syslogName, $logopt, $facility);
438
      syslog($priority, '%s', @_ );
439
      closelog();
440
   } else {
441
      warn "Log type $configuration{'logging'} incorrectly configured\n";
442
      return 0;
443
   }
444
   return 1;
445
}
446
 
447
 
448
 
21 rodolico 449
1;