Subversion Repositories camp_sysinfo_client_3

Rev

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