Subversion Repositories camp_sysinfo_client_3

Rev

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