Subversion Repositories camp_sysinfo_client_3

Rev

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