Subversion Repositories camp_sysinfo_client_3

Rev

Rev 83 | Rev 85 | 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;
84 rodolico 116
   $Data::Dumper::Varname = 'configuration';
81 rodolico 117
   $Data::Dumper::Quotekeys = 1;
118
   $Data::Dumper::Sortkeys = 1;
84 rodolico 119
   $Data::Dumper::Terse = 1;
120
   $configuration = '%configuration = ' . Data::Dumper->Dump( [$configuration] );
121
   return $configuration;
81 rodolico 122
 
83 rodolico 123
   #return $conf;
124
   #my $conf;
125
   #$conf .= "\$clientName = '" .   ( defined( $$configuration{'clientName'}   ) ? $$configuration{'clientName'}   : '' ) . "';\n";
126
   #$conf .= "\$serialNumber = '" . ( defined( $$configuration{'serialNumber'} ) ? $$configuration{'serialNumber'} : '' ) . "';\n";
127
   #$conf .= "\$hostname = '" .     ( defined( $$configuration{'hostname'}     ) ? $$configuration{'hostname'}     : '' ) . "';\n";
128
   #$conf .= "\$UUID = '" .      ( defined( $$configuration{'UUID'}     ) ? $$configuration{'UUID'}     : '' ) . "';\n";
129
   #$conf .= "\@moduleDirs = ('" . join( "','", @{ $$configuration{ 'moduleDirs' } } ) . "');\n";
130
   #$conf .= "\@scriptDirs = ('" . join( "','", @{ $$configuration{ 'scriptDirs' } } ) . "');\n";
131
   #$conf .= &transportsToConfig( $$configuration{ 'transports' } ) if defined( $$configuration{ 'transports' } );
132
   #return $conf;
21 rodolico 133
}
134
 
135
sub transportsToConfig {
35 rodolico 136
   my $transports = shift;
21 rodolico 137
   my $config;
138
   foreach my $priority ( sort { $a <=> $b } keys %$transports ) {
139
      my $name = $$transports{ $priority }{'-name-'};
140
      $config .= "# Tranport $name at priority $priority\n";
141
      my $thisOne = $$transports{ $priority };
142
      foreach my $key ( sort keys %$thisOne ) {
143
         $config .= "\$\$transports{$priority}{'$key'} = '$$thisOne{$key}';\n";
144
      } # foreach
145
   } # foreach
146
   return $config;
147
} # transportsToConfig
148
 
45 rodolico 149
sub makeConfig {
150
   my @configFileNames = @_;
151
   my %config;
77 rodolico 152
   my $clientName = '';
153
   my $serialNumber = '';
154
   my $hostname = '';
80 rodolico 155
   my @moduleDirs;
156
   my @scriptDirs;
77 rodolico 157
   my $UUID = '';
45 rodolico 158
   my $transports = {};
159
 
160
   foreach my $config ( @configFileNames ) {
161
      open CONF,"<$config" or die "could not open $config: $!\n";
162
      my $contents = join( '', <CONF> );
163
      close CONF;
164
      # now, eval the information we just read.
165
      # NOTE: we must turn off strict while doing this, and we die if something breaks.
166
      no strict "vars"; eval( $contents ); use strict "vars"; die "Error during eval: $@\n" if $@;
81 rodolico 167
      # if this is a v2 config, we will update it
168
      $config{'clientName'} = $clientName unless $config{'clientName'};
169
      $config{'serialNumber'} = $serialNumber unless $config{'serialNumber'};
170
      $config{'UUID'} = $UUID unless $config{'UUID'};
171
      $config{'hostname'} = $hostname unless $config{'hostname'};
172
      $config{'moduleDirs'} = [ @moduleDirs ] unless $config{'moduleDirs'};
173
      $config{'scriptDirs'} = [ @scriptDirs ] unless $config{'scriptDirs'};
174
      $config{'transports'} = $transports unless $config{'transports'};
45 rodolico 175
   }
81 rodolico 176
 
177
 
178
   unless ( $config{'hostname'} ) {
45 rodolico 179
      $hostname = `hostname -f`;
180
      chomp $hostname;
81 rodolico 181
      $config{'hostname'} = $hostname;
45 rodolico 182
   }
84 rodolico 183
   unless ( $config{'serialNumber'} ) {
53 rodolico 184
      $serialNumber = `dmidecode -t 1 | grep 'Serial Number' | cut -d':' -f2` if `which dmidecode`;
185
      chomp $serialNumber;
186
      $serialNumber =~ s/\s//gi;
84 rodolico 187
      $config{'serialNumber'} = $serialNumber;
53 rodolico 188
   }
81 rodolico 189
   unless ( $config{'UUID'} ) {
77 rodolico 190
      $UUID = `dmidecode -t 1 | grep -i uuid | cut -d':' -f2` if `which dmidecode`;
191
      $UUID =~ s/\s//gi;
81 rodolico 192
      $config{'UUID'} = $UUID;
77 rodolico 193
   }
45 rodolico 194
 
195
   return \%config;
196
}
197
 
21 rodolico 198
# prompt the user for a response, then allow them to enter it
199
# the first response is considered the default and is printed
200
# in all caps if more than one exists
201
# first answer is used if they simply press the Enter
202
# key. The response is returned all lower case if more than one
203
# exists.
204
# it is assumed 
205
sub getAnswer {
206
   my ( $prompt, @answers ) = @_;
207
   $answers[0] = '' unless defined( $answers[0] );
208
   my $default = $answers[0];
209
   my $onlyOneAnswer = scalar( @answers ) == 1;
210
   print $prompt . '[ ';
211
   $answers[0] = uc $answers[0] unless $onlyOneAnswer;
212
   print join( ' | ', @answers ) . ' ]: ';
28 rodolico 213
   my $thisAnswer = <>;
21 rodolico 214
   chomp $thisAnswer;
215
   $thisAnswer = $default unless $thisAnswer;
216
   return $thisAnswer;
217
}
218
 
219
sub yesno {
40 rodolico 220
   my ( $prompt, $default ) = @_;
221
   $default = 'yes' unless $default;
222
   my $answer = &getAnswer( $prompt, $default eq 'yes' ? ('yes','no' ) : ('no', 'yes') );
21 rodolico 223
   return lc( substr( $answer, 0, 1 ) ) eq 'y';
224
}
225
 
35 rodolico 226
# runs a system command. Also, if in testing mode, simply shows what
227
# would have been done.
228
sub runCommand {
229
   while ( my $command = shift ) {
230
      if ( $dryRun ) {
231
         print "$command\n";
232
      } else {
233
         `$command`;
234
      }
235
   }
236
   return 1;
237
} # runCommand
238
 
239
# checks if a directory exists and, if not, creates it
240
my %directories; # holds list of directories already created so no need to do an I/O
241
 
242
sub checkDirectoryExists {
243
   my ( $filename,$mod,$owner ) = @_;
244
   $mod = "0700" unless $mod;
245
   $owner = "root:root" unless $owner;
246
   print "Checking Directory for $filename with $mod and $owner\n" if $TESTING > 2;
247
   my ($fn, $dirname) = fileparse( $filename );
248
   print "\tParsing out $dirname and $filename\n" if $TESTING > 2;
249
   return '' if exists $directories{$dirname};
250
   if ( -d $dirname ) {
251
      $directories{$dirname} = 1;
252
      return '';
253
   }
254
   if ( &runCommand( "mkdir -p $dirname", "chmod $mod $dirname", "chown $owner $dirname" ) ) {
255
      $directories{$dirname} = 1;
256
   }
257
   return '';   
258
}
259
 
21 rodolico 260
sub writeConfig {
35 rodolico 261
   my ( $filename,$content ) = @_;
262
   my $path;
263
   ($filename, $path ) = fileparse( $filename );
264
 
21 rodolico 265
   my $return;
266
   `mkdir -p $path` unless -d $path;
267
   $filename = $path . '/' . $filename;
268
   if ( -e $filename ) {
269
      `cp $filename $filename.bak` if ( -e $filename );
270
      $return .= "Old config copied to $filename.bak\n";
271
   }
35 rodolico 272
   if ( $dryRun ) {
273
      $return .= "Not writing to configuration file\n";
274
   } else {
275
      open CONF,">$filename" or die "Could not write to $filename: $!\n";
276
      print CONF $content;
277
      close CONF;
278
      `chmod 600 $filename`;
279
      $return .= "Configuration successfully written to $filename\n";
280
   }
21 rodolico 281
   return $return;
282
}
283
 
284
sub processParameters {
285
   while ( my $parameter = shift ) {
286
      if ( $parameter eq '-v' ) {
26 rodolico 287
         print "$VERSION\n";
21 rodolico 288
         exit;
289
      }
290
   } # while
291
}
292
 
53 rodolico 293
sub findConf {
294
   my $confName = shift;
295
   my @confDirs =    ( '/etc/camp/sysinfo-client', '/usr/local/etc/camp/sysinfo-client' );
296
   for ( my $i = 0; $i < @confDirs; $i++ ) {
297
      if ( -d $confDirs[ $i ] ) {
55 rodolico 298
         return ( $confDirs[$i],  $confName );
53 rodolico 299
      }
300
   }
301
}
302
 
21 rodolico 303
 
304
1;