Subversion Repositories camp_sysinfo_client_3

Rev

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

Rev 125 Rev 129
Line 13... Line 13...
13
# version 2.0 20190330 RWR
13
# version 2.0 20190330 RWR
14
# changed it so all configs are YAML
14
# changed it so all configs are YAML
15
#
15
#
16
# version 2.1 20191101 RWR
16
# version 2.1 20191101 RWR
17
# changed --name-- tag to name
17
# changed --name-- tag to name
-
 
18
#
-
 
19
# version 2.2 20191105 RWR
-
 
20
# fixed where dmidecode missing caused exception
18
 
21
 
19
package sysinfoconf;
22
package sysinfoconf;
20
 
23
 
21
 
24
 
22
our $VERSION = '2.1';
25
our $VERSION = '2.2';
23
use warnings;
26
use warnings;
24
use strict;  
27
use strict;  
25
 
28
 
26
#use Data::Dumper;
29
#use Data::Dumper;
-
 
30
eval( 'use YAML::Tiny' );
27
use YAML::Tiny;
31
#use YAML::Tiny;
28
use File::Basename;
32
use File::Basename;
29
 
33
 
30
use Exporter;
34
use Exporter;
31
 
35
 
32
our @ISA = qw( Exporter );
36
our @ISA = qw( Exporter );
Line 128... Line 132...
128
sub showConf {
132
sub showConf {
129
   
133
   
130
   my $configuration = shift;
134
   my $configuration = shift;
131
   $configuration = Dump($configuration);
135
   $configuration = Dump($configuration);
132
   return $configuration;
136
   return $configuration;
133
 
-
 
134
   #return $conf;
-
 
135
   #my $conf;
-
 
136
   #$conf .= "\$clientName = '" .   ( defined( $$configuration{'clientName'}   ) ? $$configuration{'clientName'}   : '' ) . "';\n";
-
 
137
   #$conf .= "\$serialNumber = '" . ( defined( $$configuration{'serialNumber'} ) ? $$configuration{'serialNumber'} : '' ) . "';\n";
-
 
138
   #$conf .= "\$hostname = '" .     ( defined( $$configuration{'hostname'}     ) ? $$configuration{'hostname'}     : '' ) . "';\n";
-
 
139
   #$conf .= "\$UUID = '" .      ( defined( $$configuration{'UUID'}     ) ? $$configuration{'UUID'}     : '' ) . "';\n";
-
 
140
   #$conf .= "\@moduleDirs = ('" . join( "','", @{ $$configuration{ 'moduleDirs' } } ) . "');\n";
-
 
141
   #$conf .= "\@scriptDirs = ('" . join( "','", @{ $$configuration{ 'scriptDirs' } } ) . "');\n";
-
 
142
   #$conf .= &transportsToConfig( $$configuration{ 'transports' } ) if defined( $$configuration{ 'transports' } );
-
 
143
   #return $conf;
-
 
144
}
137
}
145
 
138
 
146
sub transportsToConfig {
139
sub transportsToConfig {
147
   my $transports = shift;
140
   my $transports = shift;
148
   my $config;
141
   my $config;
Line 195... Line 188...
195
         $config->{'UUID'} = $UUID unless $config->{'UUID'};
188
         $config->{'UUID'} = $UUID unless $config->{'UUID'};
196
         $config->{'hostname'} = $hostname if $hostname;
189
         $config->{'hostname'} = $hostname if $hostname;
197
         $config->{'moduleDirs'} = [ @moduleDirs ] if @moduleDirs;
190
         $config->{'moduleDirs'} = [ @moduleDirs ] if @moduleDirs;
198
         $config->{'scriptDirs'} = [ @scriptDirs ] if @scriptDirs;
191
         $config->{'scriptDirs'} = [ @scriptDirs ] if @scriptDirs;
199
         $config->{'transports'} = $transports if $transports;
192
         $config->{'transports'} = $transports if $transports;
-
 
193
         foreach my $trans ( keys %{$config->{'transports'}} ) {
-
 
194
            if ( exists ( $config->{'transports'}->{$trans}->{'-name-'} ) ) {
-
 
195
               $config->{'transports'}->{$trans}->{'name'} = $config->{'transports'}->{$trans}->{'-name-'};
-
 
196
               delete( $config->{'transports'}->{$trans}->{'-name-'} );
-
 
197
            }
-
 
198
         }
200
      }
199
      }
201
   } else {
200
   } else {
202
         warn "Could not read config file $filename, skipped: $!\n";
201
         warn "Could not read config file $filename, skipped: $!\n";
203
   }
202
   }
204
   return $config;
203
   return $config;
Line 207... Line 206...
207
sub makeConfig {
206
sub makeConfig {
208
   my @configFileNames = @_;
207
   my @configFileNames = @_;
209
   my %config;
208
   my %config;
210
 
209
 
211
   foreach my $config ( @configFileNames ) {
210
   foreach my $config ( @configFileNames ) {
212
      my $thisConfig = &readConfig( $config );
211
      my $thisConfig = &readConfig( $config ) if $config && -e $config;
213
      # add the new config to %config, overwriting any existing keys which are duplicated
212
      # add the new config to %config, overwriting any existing keys which are duplicated
214
      @config{keys %$thisConfig} = values %$thisConfig;
213
      @config{keys %$thisConfig} = values %$thisConfig;
215
   }
214
   }
216
   # now, ensure the correct values are loaded in some areas
215
   # now, ensure the correct values are loaded in some areas
217
   unless ( $config{'hostname'} ) {
216
   unless ( $config{'hostname'} ) {
Line 224... Line 223...
224
      chomp $serialNumber;
223
      chomp $serialNumber;
225
      $serialNumber =~ s/\s//gi;
224
      $serialNumber =~ s/\s//gi;
226
      $config{'serialNumber'} = $serialNumber;
225
      $config{'serialNumber'} = $serialNumber;
227
   }
226
   }
228
   unless ( $config{'UUID'} ) {
227
   unless ( $config{'UUID'} ) {
229
      my $UUID = `dmidecode -t 1 | grep -i uuid | cut -d':' -f2` if `which dmidecode`;
228
      my $UUID = `which dmidecode` ?  `dmidecode -t 1 | grep -i uuid | cut -d':' -f2` : '';
230
      $UUID =~ s/\s//gi;
229
      $UUID =~ s/\s//gi;
231
      $config{'UUID'} = $UUID;
230
      $config{'UUID'} = $UUID;
232
   }
231
   }
233
 
232
 
234
   # ensure we have the default SaveLocal transport defined
233
   # ensure we have the default SaveLocal transport defined
Line 305... Line 304...
305
   return '';   
304
   return '';   
306
}
305
}
307
 
306
 
308
sub writeConfig {
307
sub writeConfig {
309
   my ( $filename,$content ) = @_;
308
   my ( $filename,$content ) = @_;
-
 
309
   if ( $filename ) { # they sent us a filename
310
   my $path;
310
      my $path;
311
   ($filename, $path ) = fileparse( $filename );
311
      ($filename, $path ) = fileparse( $filename );
312
 
-
 
313
   my $return;
-
 
314
   `mkdir -p $path` unless -d $path;
312
      `mkdir -p $path` unless -d $path;
315
   $filename = $path . '/' . $filename;
313
      $filename = $path . '/' . $filename;
316
   if ( -e $filename ) {
-
 
317
      `cp $filename $filename.bak` if ( -e $filename );
314
      `cp $filename $filename.bak` if ( -e $filename );
318
      $return .= "Old config copied to $filename.bak\n";
-
 
319
   }
-
 
320
   if ( $dryRun ) {
315
      unless ( $dryRun ) {
321
      $return .= "Not writing to configuration file\n";
-
 
322
   } else {
-
 
323
      open CONF,">$filename" or die "Could not write to $filename: $!\n";
316
         open CONF,">$filename" or die "Could not write to $filename: $!\n";
324
      print CONF $content;
317
         print CONF $content;
325
      close CONF;
318
         close CONF;
326
      `chmod 600 $filename`;
319
         `chmod 600 $filename`;
-
 
320
      }
-
 
321
   } else { # no path provided, so just create a temp file
-
 
322
      # we will create a temporary file and return the name
327
      $return .= "Configuration successfully written to $filename\n";
323
      # it is the calling programs responsiblity to remove the file
-
 
324
      my $fh;
-
 
325
      ($fh,$filename) = tempfile( UNLINK => 0 );
-
 
326
      print $fh $content;
-
 
327
      close $fh
328
   }
328
   }
329
   return $return;
329
   return $filename;
330
}
330
}
331
 
331
 
332
sub processParameters {
332
sub processParameters {
333
   while ( my $parameter = shift ) {
333
   while ( my $parameter = shift ) {
334
      if ( $parameter eq '-v' ) {
334
      if ( $parameter eq '-v' ) {
Line 338... Line 338...
338
   } # while
338
   } # while
339
}
339
}
340
 
340
 
341
sub findConf {
341
sub findConf {
342
   my $confName = shift;
342
   my $confName = shift;
343
   my @confDirs =    ( '/etc/camp/sysinfo-client', '/usr/local/etc/camp/sysinfo-client' );
-
 
344
   for ( my $i = 0; $i < @confDirs; $i++ ) {
343
   for ( my $i = 0; $i < @confDirs; $i++ ) {
345
      if ( -d $confDirs[ $i ] ) {
344
      if ( -d $confDirs[ $i ] ) {
346
         return ( $confDirs[$i],  $confName );
345
         return ( $confDirs[$i],  $confName );
347
      }
346
      }
348
   }
347
   }