Subversion Repositories camp_sysinfo_client_3

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
11 rodolico 1
#!/usr/bin/env perl
2
 
16 rodolico 3
use warnings;
4
 
11 rodolico 5
my $seedFile = 'sysinfo-client.seed';
6
my $sysinfo2 = '/etc/sysinfo/sysinfo.conf';
18 rodolico 7
my $sysinfo3 = '/etc/camp/sysinfo-client/sysinfo-client.conf';
8
my $configPath = '/etc/camp/sysinfo-client';
12 rodolico 9
my $configFile = 'sysinfo-client.conf';
11 rodolico 10
 
11
my $clientName = '';
12
my $serialNumber = '';
13
my $hostname;
18 rodolico 14
my @moduleDirs = ( '/opt/camp/sysinfo-client/modules', '/etc/camp/sysinfo-client/modules' );
15
my @scriptDirs = ( '/opt/camp/sysinfo-client/scripts', '/etc/camp/sysinfo-client/scripts' );
16 rodolico 16
my $transports = {}; # holds transportation mechanisms
11 rodolico 17
# the following are keys which are specific to the different transport channels
18
 
16 rodolico 19
my %sendTypes = ( 
20
                  'SendEmail' =>    { 'sendScript' => 'sendEmailScript',
21
                                      'keys' => 
22
                                      [
23
                                        'mailTo',
24
                                        'mailSubject',
25
                                        'mailCC',
26
                                        'mailBCC',
27
                                        'mailServer',
28
                                        'mailFrom',
29
                                        'logFile',
30
                                        'otherCLParams',
31
                                        'tls',
32
                                        'smtpUser',
33
                                        'smtpPass',
34
                                        'sendEmailScriptLocation'
35
                                      ],
36
                                    },
37
                  'HTTP Upload' =>  { 'sendScript' => 'upload_http',
38
                                      'keys' => 
39
                                      [
40
                                        'URL',
41
                                        'key for report',
42
                                        'key for date',
43
                                        'key for hostname',
44
                                        'key for client',
45
                                        'key for serial number'
46
                                      ]
47
                                    }
48
                );
49
 
50
 
51
 
11 rodolico 52
sub showConf {
53
   my $conf;
54
   $conf .= "\$clientName = '" . $clientName . "';\n";
16 rodolico 55
   $conf .= "\$serialNumber = '" . ( defined( $serialNumber ) ? $serialNumber : '' ) . "';\n";
11 rodolico 56
   $conf .= "\$hostname = '" . $hostname . "';\n";
57
   $conf .= "\@moduleDirs = ('" . join( "','", @moduleDirs ). "');\n";
58
   $conf .= "\@scriptDirs = ('" . join( "','", @scriptDirs ). "');\n";
16 rodolico 59
   $conf .= &transportsToConfig();
11 rodolico 60
   return $conf;
61
}
62
 
16 rodolico 63
sub transportsToConfig {
64
   my $config;
65
   foreach my $priority ( sort { $a <=> $b } keys %$transports ) {
66
      my $name = $$transports{ $priority }{'-name-'};
67
      $config .= "# Tranport $name at priority $priority\n";
68
      my $thisOne = $$transports{ $priority };
69
      foreach my $key ( sort keys %$thisOne ) {
70
         # if they are wanting variable substitution, surround with double quote
71
         # otherwise, use single quote
72
         if ( $$thisOne{$key} =~ m/(\$hostname)|(\$reportDate)|(\$clientName)|(\$serialNumber)/ ) {
73
            $config .= "\$\$transports{$priority}{'$key'} = \"$$thisOne{$key}\";\n";
74
         } else {
75
            $config .= "\$\$transports{$priority}{'$key'} = '$$thisOne{$key}';\n";
76
         }
77
      } # foreach
78
   } # foreach
79
   return $config;
80
} # transportsToConfig
81
 
82
 
83
 
11 rodolico 84
# prompt the user for a response, then allow them to enter it
85
# the first response is considered the default and is printed
86
# in all caps if more than one exists
87
# first answer is used if they simply press the Enter
88
# key. The response is returned all lower case if more than one
89
# exists.
90
# it is assumed 
91
sub getAnswer {
92
   my ( $prompt, @answers ) = @_;
16 rodolico 93
   $answers[0] = '' unless defined( $answers[0] );
11 rodolico 94
   my $default = $answers[0];
95
   my $onlyOneAnswer = scalar( @answers ) == 1;
96
   print $prompt . '[ ';
97
   $answers[0] = uc $answers[0] unless $onlyOneAnswer;
98
   print join( ' | ', @answers ) . ' ]: ';
99
   $thisAnswer = <>;
100
   chomp $thisAnswer;
101
   $thisAnswer = $default unless $thisAnswer;
102
   return $thisAnswer;
103
}
104
 
105
sub yesno {
106
   my $prompt = shift;
107
   my $answer = &getAnswer( $prompt, ('yes','no' ) );
12 rodolico 108
   return lc( substr( $answer, 0, 1 ) ) eq 'y';
11 rodolico 109
}
110
 
12 rodolico 111
sub findSendEmail {
112
   my $currentLocation = shift;
113
   my @possibles = ( '/opt/sendEmail/sendEmail', '/opt/sendEmail/sendEmail.pl' );
114
   return $currentLocation if ( $currentLocation && -x $currentLocation );
115
   # well, we didn't find it, so look around some more
116
   # we install it in /opt, so try there
117
   foreach my $current ( @possibles ) {
118
      return $current if -x $current;
119
   }
120
   if ( &yesno( "You are asking for sendEmail, but I don't see it on the system\nWould you like me to automatically download and install" ) ) {
121
      $path = `perl getSendEmail.pl`;
122
      chomp $path;
123
      return $path;
124
   }
125
   return '';
126
}
127
 
11 rodolico 128
sub userConfirmation {
129
   my $temp;
130
 
131
   $clientName = &getAnswer( "Client Name ", ($clientName) );
132
   $serialNumber = &getAnswer( "Serial Number ", ($serialNumber) );
133
   $hostname = &getAnswer( "Host Name ", ($hostname) );
134
 
135
   $temp = join( ',',@moduleDirs );
136
   $temp = &getAnswer( "Locations to search for modules (comma separated) ", ($temp) );
137
   @moduleDirs = split( ',', $temp );
138
 
139
   $temp = join( ',',@scriptDirs );
140
   $temp = &getAnswer( "Locations to search for scripts (comma separated) ", ($temp) );
141
   @scriptDirs = split( ',', $temp );
142
 
143
}
144
 
145
# simply attempts to detect the operating system so we can do OS
146
# specific actions at the end.   
147
sub getOperatingSystem {
148
   my @OSTypes = ( 'ipfire','debian' );
149
   my $OS = `uname -a`;
150
   foreach $osType ( @OSTypes ) {
151
      return $osType if $OS =~ m/$osType/i;
152
   }
153
   return '';
154
} # getOperatingSystem
155
 
156
# special purpose for IPFire routers
157
sub ipFire {
158
   my @BACKUP_DIRS = ( '/etc/camp' );
159
   `ln -s /opt/camp/sysinfo/sysinfo-client /etc/fcron.daily/sysinfo.cron` if &yesno( 'Add link to fcron.daily' );
160
   # now, check for backup directories not in include.user
161
   open BACKUP, '</var/ipfire/backup/include.user';
162
   while ( $line = <BACKUP> ) {
163
      chomp $line;
164
      for (my $i = 0; $i < @BACKUP_DIRS; $i++ ) {
165
         if ( $BACKUP_DIRS[$i] eq $line ) {
166
            $BACKUP_DIRS[$i] = '';
167
            last;
168
         } # if
169
      }# for
170
   } # while
171
   close BACKUP;
172
 
173
   # if any remain, append them to include.user
174
   open BACKUP, '>>/var/ipfire/backup/include.user';
175
   foreach my $backupDir ( @BACKUP_DIRS ) {
176
      print BACKUP "$backupDir\n" if $backupDir;
177
   }
178
   close BACKUP;
179
 
180
   # set all modules with ipfire or unix in the name to run
16 rodolico 181
   my $moduleDir = $moduleDirs[0];
182
   opendir $moduleDir, $moduleDir;
11 rodolico 183
   my @modules = grep { /^((ipfire)|(unix))/ } readdir $moduleDir;
184
   closedir $moduleDir;
16 rodolico 185
   foreach my $module ( @modules) {
186
      `chmod 0700 $moduleDir/$module`;
11 rodolico 187
   }
188
   print "All IPFire specific modules have been enabled\n";
189
}
190
 
191
# some debian specific things
192
sub debian {
18 rodolico 193
   if ( &yesno( 'Add link to cron.daily' ) ) {
194
      `rm /etc/cron.daily/sysinfo` if -e '/etc/cron.daily/sysinfo';
195
      `ln -s /opt/camp/sysinfo-client/sysinfo-client /etc/cron.daily/sysinfo`;
196
   }
11 rodolico 197
   if ( `dpkg --get-selections | grep  sysinfo-client | grep install` 
198
      && &yesno ('It looks like sysinfo version 2 is installed via apt, should I remove it' ) ) {
199
      if ( &yesno( 'Back up old configuration? ') ) {
12 rodolico 200
         `tar -czvf /root/sysinfo.2.config.tgz /etc/sysinfo`;
11 rodolico 201
         print "Old configuration copied to /root/sysinfo.2.config.tgz\n";
202
      }
203
      `apt-get -y --purge remove sysinfo-client`;
204
      print "There may be some unused packages now. You can remove them with\napt-get autoremove\n\n";
205
   }
18 rodolico 206
   if ( &yesno( 'Would you like a link to /usr/local/bin/sysinfo-client' ) ) {
207
      `rm /usr/local/bin/sysinfo-client` if -e '/usr/local/bin/sysinfo-client';
208
      `ln -s  /opt/camp/sysinfo/sysinfo-client /usr/local/bin/sysinfo-client`;
209
   }
210
 
11 rodolico 211
   # set all modules with debian or unix in the name to run
14 rodolico 212
   foreach my $directory ( @moduleDirs ) {
213
      opendir ( $dh, $directory ) or die "Could not open directory [$directory]\n";
214
      my @modules = grep { /^((dpkg)|(unix))/ } readdir $dh;
215
      closedir $dh;
216
      foreach my $module (@modules) {
217
         `chmod 0700 $directory/$module`;
218
         `chown root:root $directory/$module`;
219
      }
11 rodolico 220
   }
221
   print "All debian specific modules have been enabled\n";
222
}
223
 
16 rodolico 224
sub setUpTransport {
225
   my ($priority, $transport, $fields, $type ) = @_;
226
   $priority = getAnswer( 'Priority', $priority );
227
   $$transport{'sendScript'} = $$fields{'sendScript'} unless $$transport{'sendScript'};
228
   $$transport{'-name-'} = $type unless $$transport{'-name-'};
229
   my $allKeys = $$fields{'keys'};
230
   foreach my $key ( @$allKeys ) {
231
      if ( $key eq 'sendEmailScriptLocation' && ! -e $$transport{$key} ) {
232
         $temp = &findSendEmail( $$transport{$key} );
233
         $$transport{$key} = $temp if $temp;
234
      }
235
      $$transport{$key} = &getAnswer( "$key ", $$transport{$key} ? $$transport{$key} : '' );
236
   }
237
   return ( $priority, $transport );
238
}
239
 
240
sub showAllTransports {
241
   foreach my $priority ( sort { $a <=> $b } keys %$transports ) {
242
      print '='x40 . "\n";
243
      &showTransports( $priority, $$transports{ $priority } );
244
      print '='x40 . "\n";
245
   }
246
}   
247
 
248
sub showTransports {
249
   my ( $priority,$thisOne )  = @_;
250
   print $$thisOne{'-name-'} . " has priority $priority\n";
251
   foreach my $key ( sort keys %$thisOne ) {
252
      next if $key =~ m/^-.*-$/;
253
      print "$key = $$thisOne{$key}\n";
254
   }
255
}
256
 
257
sub doTransports {
258
   my ( $transports ) = @_;
259
 
260
   foreach my $priority ( sort { $a <=> $b } keys %$transports ) {
261
      if ( &yesno( $$transports{$priority}{'-name-'} . " has a priority of $priority, edit it?") ) {
262
         #print Dumper( $sendTypes{$$transports{$priority}{'-name-'}} );
263
         #die;
264
         my ( $newpriority,$temp ) = &setUpTransport( $priority, $$transports{$priority}, $sendTypes{$$transports{$priority}{'-name-'}} );
265
         if ( $newpriority != $priority ) {
266
            delete $$transports{$priority};
267
            $priority = $newpriority;
268
         }
269
         $$transports{$priority} = $temp;
270
      } # if
271
   }
272
 
273
   foreach my $priority ( sort { $a <=> $b } keys %$transports ) {
274
      print '='x40 . "\n";
275
      &showTransports( $priority, $$transports{ $priority } );
276
      print '='x40 . "\n";
277
   }
278
 
279
   while ( &yesno( "Would you like to add any other transport mechanisms?" ) ) {
280
      $newType = &getAnswer( 'Type of Transport? ', keys %sendTypes );
281
      my ( $priority,$temp ) = &setUpTransport( 99, {}, $sendTypes{$newType}, $newType );
282
      $$transports{$priority} = $temp;
283
   }
284
 
285
   foreach my $priority ( sort { $a <=> $b } keys %$transports ) {
286
      print '='x40 . "\n";
287
      &showTransports( $priority, $$transports{ $priority } );
288
      print '='x40 . "\n";
289
   }
290
}
291
 
292
 
12 rodolico 293
sub writeConfig {
294
   my ($path,$filename,$content) = @_;
295
   my $return;
296
   `mkdir -p $path` unless -d $path;
297
   $filename = $path . '/' . $filename;
298
   if ( -e $filename ) {
299
      `cp $filename $filename.bak` if ( -e $filename );
300
      $return .= "Old config copied to $filename.bak\n";
301
   }
302
   open CONF,">$filename" or die "Could not write to $filename: $!\n";
303
   print CONF $content;
304
   close CONF;
18 rodolico 305
   `chmod 600 $filename`;
12 rodolico 306
   $return .= "Configuration successfully written to $filename\n";
307
   return $return;
308
}
309
 
16 rodolico 310
sub convertSysinfo2 {
11 rodolico 311
   my $client_name;
16 rodolico 312
   my $iMailResults;
313
   my $mailTo;
314
   my $mailSubject;
315
   my $mailCC;
316
   my $mailBCC;
317
   my $mailServer;
318
   my $mailServerPort;
319
   my $mailFrom;
320
   my $SENDMAIL;
321
   my $clientName;
322
   my $serialNumber;
323
   my $hostname;
324
   my $transports = {}; # holds transportation mechanisms
325
   open SEED, "<$sysinfo2" or die "Could not open $sysinfo2: $!\n";
11 rodolico 326
   my $temp = join( '', <SEED> );
327
   close SEED;
328
   eval( $temp );
16 rodolico 329
   if ( $iMailResults ) {
330
      $temp = {};
331
      $$temp{'-name-'} = 'SendEmail';
332
      $$temp{'mailTo'} = $mailTo if $mailTo;
333
      $$temp{'$mailFrom'} = $mailFrom if $mailFrom;
334
      $$temp{'mailCC'} = $mailCC if $mailCC;
335
      $$temp{'mailServer'} = $mailServer if $mailServer;
336
      $$temp{'mailServer'} .= ":$mailServerPort" if $mailServerPort;
337
      $$transports{'1'} = $temp;
338
   }   
339
   $clientName = $client_name if ( $client_name );
340
   return ( $clientName,$hostname,$serialNumber,$transports );
341
}
342
 
343
sub loadConfig {
344
   # See if existing configuration. If so, offer to load it
345
   if ( -f $sysinfo3 && &yesno( 'I found an existing configuration, modify it ' ) ) {
346
      print "Loading defaults from existing config $sysinfo3\n";
11 rodolico 347
      my $client_name;
16 rodolico 348
      open SEED, "<$sysinfo3" or die "Could not open $sysinfo3: $!\n";
11 rodolico 349
      my $temp = join( '', <SEED> );
350
      close SEED;
351
      eval( $temp );
16 rodolico 352
   } else { # nope, so see if an old sysinfo2 or a seed file exists
353
      if ( -f $sysinfo2 && &yesno( 'Old sysinfo 2 config exists, load it for defaults' ) ) {
354
         print "Loading defaults from sysinfo2 config $sysinfo2\n";
355
         # NOTE: the return values are all placed into globals!!!!
356
         ( $clientName,$hostname,$serialNumber,$transports ) = &convertSysinfo2();
357
      }
358
      # seed files are expected to be in the correct format already
359
      if ( -f $seedFile  && &yesno( 'An installation seed file was found, load it' ) ) {
360
         print "Loading seed file $seedFile\n";
361
         open SEED, "<$seedFile" or die "Could not open $seedFile: $!\n";
362
         my $temp = join( '', <SEED> );
363
         close SEED;
364
         eval( $temp );
365
      }
11 rodolico 366
   }
367
}
368
 
16 rodolico 369
&loadConfig();
370
 
11 rodolico 371
unless ( $hostname ) {
372
   $hostname = `hostname -f`;
373
   chomp $hostname;
374
}
375
 
376
&userConfirmation();
16 rodolico 377
&doTransports( $transports );
11 rodolico 378
 
379
print "The following is the configuration I'll write out\n" . '-'x40 . "\n";
380
print &showConf(  );
381
print '-'x40 . "\n";
18 rodolico 382
print &writeConfig( $configPath, $configFile, &showConf() ) if &yesno( "Write this configuration" );
11 rodolico 383
 
12 rodolico 384
 
11 rodolico 385
my $os = &getOperatingSystem();
386
if ( $os && &yesno( "I recognize this system, may I automatically set up a few things" ) ) {
12 rodolico 387
   &ipFire() if $os eq 'ipfire';
388
   &debian() if $os eq 'debian';
11 rodolico 389
}
390
 
391
print "sysinfo-client has been installed configured. You can return to\nthis screen at any time by running /opt/camp/sysinfo/config.pl\n";
392
 
393
1;