Subversion Repositories camp_sysinfo_client_3

Rev

Rev 124 | Rev 129 | 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
45 rodolico 18
 
21 rodolico 19
package sysinfoconf;
20
 
42 rodolico 21
 
125 rodolico 22
our $VERSION = '2.1';
21 rodolico 23
use warnings;
26 rodolico 24
use strict;  
25
 
92 rodolico 26
#use Data::Dumper;
101 rodolico 27
use YAML::Tiny;
35 rodolico 28
use File::Basename;
29
 
21 rodolico 30
use Exporter;
31
 
32
our @ISA = qw( Exporter );
33
our @EXPORT = qw( $clientName $serialNumber $hostname @moduleDirs @scriptDirs 
44 rodolico 34
                  $transports $sysinfo3 %sendTypes $binDir $moduleDir
35
                  $scriptDir $confDir $binName $confName
21 rodolico 36
                  &showConf &transportsToConfig &getAnswer &yesno  
35 rodolico 37
                  &writeConfig &processParameters $TESTING &checkDirectoryExists
115 rodolico 38
                  &runCommand &setDryRun &enableModules &makeConfig &findConf
21 rodolico 39
                );
40
 
36 rodolico 41
our $TESTING = 0;
35 rodolico 42
our $dryRun;
28 rodolico 43
our $clientName = '';
44
our $serialNumber = '';
45
our $hostname = '';
53 rodolico 46
 
104 rodolico 47
 
48
 
53 rodolico 49
my @installDirs = ( '/opt/camp/sysinfo-client', '/usr/local/opt/camp/sysinfo-client' );
50
my @confDirs =    ( '/etc/camp/sysinfo-client', '/usr/local/etc/camp/sysinfo-client' );
51
 
52
 
28 rodolico 53
our @moduleDirs = ( '/opt/camp/sysinfo-client/modules', '/etc/camp/sysinfo-client/modules' );
54
our @scriptDirs = ( '/opt/camp/sysinfo-client/scripts', '/etc/camp/sysinfo-client/scripts' );
111 rodolico 55
our $transports = {}; # holds transportation mechanisms
21 rodolico 56
 
103 rodolico 57
our $sysinfo3 = 'sysinfo-client.yaml';
21 rodolico 58
 
28 rodolico 59
our $binDir = '/opt/camp/sysinfo-client';
44 rodolico 60
our $moduleDir = $binDir . '/modules';
61
our $scriptDir = $binDir . '/scripts';
28 rodolico 62
our $confDir = '/etc/camp/sysinfo-client';
21 rodolico 63
 
28 rodolico 64
our $binName = 'sysinfo-client';
107 rodolico 65
our $confName = 'sysinfo-client.yaml';
21 rodolico 66
 
35 rodolico 67
sub setDryRun {
68
   $dryRun = shift;
69
}
21 rodolico 70
 
28 rodolico 71
our %sendTypes = ( 
21 rodolico 72
                  'SendEmail' =>    { 'sendScript' => 'sendEmailScript',
73
                                      'keys' => 
74
                                      [
75
                                        'mailTo',
76
                                        'mailSubject',
77
                                        'mailCC',
78
                                        'mailBCC',
79
                                        'mailServer',
80
                                        'mailFrom',
81
                                        'logFile',
82
                                        'otherCLParams',
83
                                        'tls',
84
                                        'smtpUser',
85
                                        'smtpPass',
86
                                        'sendEmailScriptLocation'
87
                                      ],
88
                                    },
89
                  'HTTP Upload' =>  { 'sendScript' => 'upload_http',
90
                                      'keys' => 
91
                                      [
92
                                        'URL',
93
                                        'key for report',
94
                                        'key for date',
95
                                        'key for hostname',
96
                                        'key for client',
97
                                        'key for serial number'
98
                                      ]
86 rodolico 99
                                    },
100
                  'Save Local' =>   { 'sendScript' => 'save_local',
101
                                      'keys' =>
102
                                      [
103
                                         'output directory'
104
                                      ]
105
                                   }
21 rodolico 106
                );
107
 
108
 
37 rodolico 109
sub enableModules {
110
   my $moduleDir = shift;
111
   my %modules;
112
   if ( opendir( my $dh, "$moduleDir" ) ) {
113
      %modules = map{ $_ => { 'enabled' => -x "$moduleDir/$_" } } 
114
                 grep { ! /^\./ } 
115
                 readdir $dh;
116
      closedir( $dh );
117
      foreach my $filename ( keys %modules ) {
118
         if ( open FILE,"<$moduleDir/$_" ) {
119
            my @descript = grep{ /^# Description: (.*)/ } <FILE>;
120
            close FILE;
121
            chomp @descript;
122
            $modules{$filename}{'description'} = $descript[0];
123
         } # if
124
      } # foreach
125
   } # if
126
} # enableModules
127
 
21 rodolico 128
sub showConf {
81 rodolico 129
 
35 rodolico 130
   my $configuration = shift;
92 rodolico 131
   $configuration = Dump($configuration);
84 rodolico 132
   return $configuration;
81 rodolico 133
 
83 rodolico 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;
21 rodolico 144
}
145
 
146
sub transportsToConfig {
35 rodolico 147
   my $transports = shift;
21 rodolico 148
   my $config;
149
   foreach my $priority ( sort { $a <=> $b } keys %$transports ) {
124 rodolico 150
      my $name = $$transports{ $priority }{'name'};
21 rodolico 151
      $config .= "# Tranport $name at priority $priority\n";
152
      my $thisOne = $$transports{ $priority };
153
      foreach my $key ( sort keys %$thisOne ) {
154
         $config .= "\$\$transports{$priority}{'$key'} = '$$thisOne{$key}';\n";
155
      } # foreach
156
   } # foreach
157
   return $config;
158
} # transportsToConfig
159
 
94 rodolico 160
 
161
# read the configuration file passed in and return a reference to it
162
# to the calling routine
163
sub readConfig {
164
   my $filename = shift;
165
   my $config;
166
   return $config unless -e $filename;
167
   if ( open CONF,"<$filename" ) {
168
      my $contents = join( '', <CONF> );
169
      close CONF;
170
      # now, look to see what kind of file this is.
171
      if ( $contents =~ m/^---(\s*#.*)?$/m ) {
172
         print "Reading $filename as YAML\n";
173
         #print "Contents are:\n\n=====================$contents\n=====================\n";
174
         # this is a yaml file
175
         #$contents = YAML::Tiny->read( $config );
176
         $config = Load( $contents );
177
      } elsif ( $contents =~ m/\$clientName/ ) {
178
         # this is old style
179
         print "Reading $filename as old school file\n";
180
         my $clientName = '';
181
         my $serialNumber = '';
182
         my $hostname = '';
183
         my @moduleDirs;
184
         my @scriptDirs;
185
         my $UUID = '';
186
         my $transports;
187
         # now, eval the information we just read.
188
         # NOTE: we must turn off strict while doing this, and we die if something breaks.
189
         no strict "vars";
190
         eval( $contents );
191
         use strict "vars";
192
         die "Error during eval: $@\n" if $@;
193
         $config->{'clientName'} = $clientName if $clientName;
194
         $config->{'serialNumber'} = $serialNumber if $serialNumber;
195
         $config->{'UUID'} = $UUID unless $config->{'UUID'};
196
         $config->{'hostname'} = $hostname if $hostname;
197
         $config->{'moduleDirs'} = [ @moduleDirs ] if @moduleDirs;
198
         $config->{'scriptDirs'} = [ @scriptDirs ] if @scriptDirs;
199
         $config->{'transports'} = $transports if $transports;
200
      }
201
   } else {
202
         warn "Could not read config file $filename, skipped: $!\n";
203
   }
204
   return $config;
205
}
206
 
45 rodolico 207
sub makeConfig {
208
   my @configFileNames = @_;
209
   my %config;
210
 
211
   foreach my $config ( @configFileNames ) {
112 rodolico 212
      my $thisConfig = &readConfig( $config );
94 rodolico 213
      # add the new config to %config, overwriting any existing keys which are duplicated
214
      @config{keys %$thisConfig} = values %$thisConfig;
45 rodolico 215
   }
94 rodolico 216
   # now, ensure the correct values are loaded in some areas
81 rodolico 217
   unless ( $config{'hostname'} ) {
45 rodolico 218
      $hostname = `hostname -f`;
219
      chomp $hostname;
81 rodolico 220
      $config{'hostname'} = $hostname;
45 rodolico 221
   }
84 rodolico 222
   unless ( $config{'serialNumber'} ) {
53 rodolico 223
      $serialNumber = `dmidecode -t 1 | grep 'Serial Number' | cut -d':' -f2` if `which dmidecode`;
224
      chomp $serialNumber;
225
      $serialNumber =~ s/\s//gi;
84 rodolico 226
      $config{'serialNumber'} = $serialNumber;
53 rodolico 227
   }
81 rodolico 228
   unless ( $config{'UUID'} ) {
94 rodolico 229
      my $UUID = `dmidecode -t 1 | grep -i uuid | cut -d':' -f2` if `which dmidecode`;
77 rodolico 230
      $UUID =~ s/\s//gi;
81 rodolico 231
      $config{'UUID'} = $UUID;
77 rodolico 232
   }
45 rodolico 233
 
110 rodolico 234
   # ensure we have the default SaveLocal transport defined
235
   unless ( defined $config{'transports'}{'99'} ) {
236
      $config{'transports'}{'99'} = {
237
                         'name'=> 'SaveLocal',
238
                         'output directory' => '/tmp',
239
                         'sendScript' => 'save_local'
240
                        };
241
   }
242
 
45 rodolico 243
   return \%config;
244
}
245
 
21 rodolico 246
# prompt the user for a response, then allow them to enter it
247
# the first response is considered the default and is printed
248
# in all caps if more than one exists
249
# first answer is used if they simply press the Enter
250
# key. The response is returned all lower case if more than one
251
# exists.
252
# it is assumed 
253
sub getAnswer {
254
   my ( $prompt, @answers ) = @_;
255
   $answers[0] = '' unless defined( $answers[0] );
256
   my $default = $answers[0];
257
   my $onlyOneAnswer = scalar( @answers ) == 1;
258
   print $prompt . '[ ';
259
   $answers[0] = uc $answers[0] unless $onlyOneAnswer;
260
   print join( ' | ', @answers ) . ' ]: ';
28 rodolico 261
   my $thisAnswer = <>;
21 rodolico 262
   chomp $thisAnswer;
263
   $thisAnswer = $default unless $thisAnswer;
264
   return $thisAnswer;
265
}
266
 
267
sub yesno {
40 rodolico 268
   my ( $prompt, $default ) = @_;
269
   $default = 'yes' unless $default;
270
   my $answer = &getAnswer( $prompt, $default eq 'yes' ? ('yes','no' ) : ('no', 'yes') );
21 rodolico 271
   return lc( substr( $answer, 0, 1 ) ) eq 'y';
272
}
273
 
35 rodolico 274
# runs a system command. Also, if in testing mode, simply shows what
275
# would have been done.
276
sub runCommand {
277
   while ( my $command = shift ) {
278
      if ( $dryRun ) {
279
         print "$command\n";
280
      } else {
281
         `$command`;
282
      }
283
   }
284
   return 1;
285
} # runCommand
286
 
287
# checks if a directory exists and, if not, creates it
288
my %directories; # holds list of directories already created so no need to do an I/O
289
 
290
sub checkDirectoryExists {
291
   my ( $filename,$mod,$owner ) = @_;
292
   $mod = "0700" unless $mod;
293
   $owner = "root:root" unless $owner;
294
   print "Checking Directory for $filename with $mod and $owner\n" if $TESTING > 2;
295
   my ($fn, $dirname) = fileparse( $filename );
296
   print "\tParsing out $dirname and $filename\n" if $TESTING > 2;
297
   return '' if exists $directories{$dirname};
298
   if ( -d $dirname ) {
299
      $directories{$dirname} = 1;
300
      return '';
301
   }
302
   if ( &runCommand( "mkdir -p $dirname", "chmod $mod $dirname", "chown $owner $dirname" ) ) {
303
      $directories{$dirname} = 1;
304
   }
305
   return '';   
306
}
307
 
21 rodolico 308
sub writeConfig {
35 rodolico 309
   my ( $filename,$content ) = @_;
310
   my $path;
311
   ($filename, $path ) = fileparse( $filename );
312
 
21 rodolico 313
   my $return;
314
   `mkdir -p $path` unless -d $path;
315
   $filename = $path . '/' . $filename;
316
   if ( -e $filename ) {
317
      `cp $filename $filename.bak` if ( -e $filename );
318
      $return .= "Old config copied to $filename.bak\n";
319
   }
35 rodolico 320
   if ( $dryRun ) {
321
      $return .= "Not writing to configuration file\n";
322
   } else {
323
      open CONF,">$filename" or die "Could not write to $filename: $!\n";
324
      print CONF $content;
325
      close CONF;
326
      `chmod 600 $filename`;
327
      $return .= "Configuration successfully written to $filename\n";
328
   }
21 rodolico 329
   return $return;
330
}
331
 
332
sub processParameters {
333
   while ( my $parameter = shift ) {
334
      if ( $parameter eq '-v' ) {
26 rodolico 335
         print "$VERSION\n";
21 rodolico 336
         exit;
337
      }
338
   } # while
339
}
340
 
53 rodolico 341
sub findConf {
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++ ) {
345
      if ( -d $confDirs[ $i ] ) {
55 rodolico 346
         return ( $confDirs[$i],  $confName );
53 rodolico 347
      }
348
   }
104 rodolico 349
   return ( '', $confName );
53 rodolico 350
}
21 rodolico 351
 
104 rodolico 352
 
353
 
354
 
21 rodolico 355
1;