Subversion Repositories camp_sysinfo_client_3

Rev

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