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
 
3
my $seedFile = 'sysinfo-client.seed';
4
my $sysinfo2 = '/etc/sysinfo/sysinfo.conf';
5
my $sysinfo3 = '/etc/camp/sysinfo-client.conf';
12 rodolico 6
my $configPath = '/etc/camp';
7
my $configFile = 'sysinfo-client.conf';
11 rodolico 8
 
9
my $clientName = '';
10
my $serialNumber = '';
11
my $hostname;
12
my @moduleDirs = ( '/opt/camp/sysinfo/modules', '/etc/camp/modules' );
13
my @scriptDirs = ( '/opt/camp/sysinfo/scripts', '/etc/camp/scripts' );
14
my $iSendReports = {};
15
# the following are keys which are specific to the different transport channels
16
my @sendEmailScriptKeys = ( 'mailTo', 'mailSubject','mailCC','mailBCC','mailServer','mailFrom','logFile','otherCLParams','tls','smtpUser','smtpPass', 'sendEmailScriptLocation' );
17
my @sendHTTPKeys = ( 'URL', 'urlVarName' );
18
 
19
sub showConf {
20
   my $conf;
21
   $conf .= "\$clientName = '" . $clientName . "';\n";
22
   $conf .= "\$serialNumber = '" . $serialNumber . "';\n";
23
   $conf .= "\$hostname = '" . $hostname . "';\n";
24
   $conf .= "\@moduleDirs = ('" . join( "','", @moduleDirs ). "');\n";
25
   $conf .= "\@scriptDirs = ('" . join( "','", @scriptDirs ). "');\n";
26
   foreach my $key ( keys %$iSendReports ) {
12 rodolico 27
      $conf .= "\$\$iSendReports{'$key'} = '" . $$iSendReports{$key} . "';\n";
11 rodolico 28
   }
29
   return $conf;
30
}
31
 
32
# prompt the user for a response, then allow them to enter it
33
# the first response is considered the default and is printed
34
# in all caps if more than one exists
35
# first answer is used if they simply press the Enter
36
# key. The response is returned all lower case if more than one
37
# exists.
38
# it is assumed 
39
sub getAnswer {
40
   my ( $prompt, @answers ) = @_;
41
   my $default = $answers[0];
42
   my $onlyOneAnswer = scalar( @answers ) == 1;
43
   print $prompt . '[ ';
44
   $answers[0] = uc $answers[0] unless $onlyOneAnswer;
45
   print join( ' | ', @answers ) . ' ]: ';
46
   $thisAnswer = <>;
47
   chomp $thisAnswer;
48
   $thisAnswer = $default unless $thisAnswer;
49
   return $thisAnswer;
50
}
51
 
52
sub yesno {
53
   my $prompt = shift;
54
   my $answer = &getAnswer( $prompt, ('yes','no' ) );
12 rodolico 55
   return lc( substr( $answer, 0, 1 ) ) eq 'y';
11 rodolico 56
}
57
 
12 rodolico 58
sub findSendEmail {
59
   my $currentLocation = shift;
60
   my @possibles = ( '/opt/sendEmail/sendEmail', '/opt/sendEmail/sendEmail.pl' );
61
   return $currentLocation if ( $currentLocation && -x $currentLocation );
62
   # well, we didn't find it, so look around some more
63
   # we install it in /opt, so try there
64
   foreach my $current ( @possibles ) {
65
      return $current if -x $current;
66
   }
67
   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" ) ) {
68
      $path = `perl getSendEmail.pl`;
69
      chomp $path;
70
      return $path;
71
   }
72
   return '';
73
}
74
 
11 rodolico 75
sub userConfirmation {
76
   my $temp;
77
 
78
   $clientName = &getAnswer( "Client Name ", ($clientName) );
79
   $serialNumber = &getAnswer( "Serial Number ", ($serialNumber) );
80
   $hostname = &getAnswer( "Host Name ", ($hostname) );
81
 
82
   $temp = join( ',',@moduleDirs );
83
   $temp = &getAnswer( "Locations to search for modules (comma separated) ", ($temp) );
84
   @moduleDirs = split( ',', $temp );
85
 
86
   $temp = join( ',',@scriptDirs );
87
   $temp = &getAnswer( "Locations to search for scripts (comma separated) ", ($temp) );
88
   @scriptDirs = split( ',', $temp );
89
 
90
   $temp = &getAnswer( "How do you want to send the reports ", ('sendEmailScript', 'http', 'sendmail' ) );
91
 
92
   if ( $temp eq 'sendEmailScript' ) {
93
      $$iSendReports{'sendScript'} = 'sendEmailScript';
12 rodolico 94
      $$iSendReports{'sendEmailScriptLocation'} = &findSendEmail( $$iSendReports{'sendEmailScriptLocation'} );
95
 
11 rodolico 96
      # get rid of http keys
97
      delete @{$iSendReports} {@sendHTTPKeys};
98
      for $key ( @sendEmailScriptKeys ) {
99
         $$iSendReports{$key} = &getAnswer( "For key $key", ( $$iSendReports{$key} ) );
100
      }
101
   } elsif ( $temp eq 'http' ) {
102
      $$iSendReports{'sendScript'} = 'upload_http.pl';
103
      # get rid of mail keys
104
      delete @{$iSendReports} { @sendEmailScriptKeys };
105
      for $key ( @sendHTTPKeys ) {
106
         $$iSendReports{$key} = &getAnswer( "For key $key", ( $$iSendReports{$key} ) );
107
      }
108
   } elsif ( $temp eq 'sendmail' ) {
109
      $iSendReports = {}; # null out the script
110
   } else {
111
      $$iSendReports{'sendScript'} = $temp;
112
      delete @{$iSendReports} { @sendEmailScriptKeys, @sendHTTPKeys };
113
      print "Unknown script, please manually configure\n";
114
   }
115
}
116
 
117
# simply attempts to detect the operating system so we can do OS
118
# specific actions at the end.   
119
sub getOperatingSystem {
120
   my @OSTypes = ( 'ipfire','debian' );
121
   my $OS = `uname -a`;
122
   foreach $osType ( @OSTypes ) {
123
      return $osType if $OS =~ m/$osType/i;
124
   }
125
   return '';
126
} # getOperatingSystem
127
 
128
# special purpose for IPFire routers
129
sub ipFire {
130
   my @BACKUP_DIRS = ( '/etc/camp' );
131
   `ln -s /opt/camp/sysinfo/sysinfo-client /etc/fcron.daily/sysinfo.cron` if &yesno( 'Add link to fcron.daily' );
132
   # now, check for backup directories not in include.user
133
   open BACKUP, '</var/ipfire/backup/include.user';
134
   while ( $line = <BACKUP> ) {
135
      chomp $line;
136
      for (my $i = 0; $i < @BACKUP_DIRS; $i++ ) {
137
         if ( $BACKUP_DIRS[$i] eq $line ) {
138
            $BACKUP_DIRS[$i] = '';
139
            last;
140
         } # if
141
      }# for
142
   } # while
143
   close BACKUP;
144
 
145
   # if any remain, append them to include.user
146
   open BACKUP, '>>/var/ipfire/backup/include.user';
147
   foreach my $backupDir ( @BACKUP_DIRS ) {
148
      print BACKUP "$backupDir\n" if $backupDir;
149
   }
150
   close BACKUP;
151
 
152
   # set all modules with ipfire or unix in the name to run
153
   opendir $moduleDir, $moduleDirs[0];
154
   my @modules = grep { /^((ipfire)|(unix))/ } readdir $moduleDir;
155
   closedir $moduleDir;
156
   foreach my $module (@modules) {
157
      `chmod 0700 $MODULES_DIR/$module`;
158
   }
159
   print "All IPFire specific modules have been enabled\n";
160
}
161
 
162
# some debian specific things
163
sub debian {
164
   `ln -s /opt/camp/sysinfo/sysinfo-client /etc/cron.daily/sysinfo.cron` 
165
      if &yesno( 'Add link to cron.daily' );
166
   if ( `dpkg --get-selections | grep  sysinfo-client | grep install` 
167
      && &yesno ('It looks like sysinfo version 2 is installed via apt, should I remove it' ) ) {
168
      if ( &yesno( 'Back up old configuration? ') ) {
12 rodolico 169
         `tar -czvf /root/sysinfo.2.config.tgz /etc/sysinfo`;
11 rodolico 170
         print "Old configuration copied to /root/sysinfo.2.config.tgz\n";
171
      }
172
      `apt-get -y --purge remove sysinfo-client`;
173
      print "There may be some unused packages now. You can remove them with\napt-get autoremove\n\n";
174
   }
175
   `ln -s  /opt/camp/sysinfo/sysinfo-client /usr/local/bin/sysinfo-client` 
176
      if &yesno( 'Would you like a link to /usr/local/bin/sysinfo-client' );
177
   # set all modules with debian or unix in the name to run
178
   opendir  $moduleDirs, $moduleDirs[0];
179
   my @modules = grep { /^((dpkg)|(unix))/ } readdir $moduleDir;
180
   closedir $moduleDir;
181
   foreach my $module (@modules) {
182
      `chmod 0700 $MODULES_DIR/$module`;
183
   }
184
   print "All debian specific modules have been enabled\n";
185
}
186
 
12 rodolico 187
sub writeConfig {
188
   my ($path,$filename,$content) = @_;
189
   my $return;
190
   `mkdir -p $path` unless -d $path;
191
   $filename = $path . '/' . $filename;
192
   if ( -e $filename ) {
193
      `cp $filename $filename.bak` if ( -e $filename );
194
      $return .= "Old config copied to $filename.bak\n";
195
   }
196
   open CONF,">$filename" or die "Could not write to $filename: $!\n";
197
   print CONF $content;
198
   close CONF;
199
   $return .= "Configuration successfully written to $filename\n";
200
   return $return;
201
}
202
 
11 rodolico 203
# See if existing configuration. If so, offer to load it
204
if ( -f $sysinfo3 && &yesno( 'I found an existing configuration, modify it ' ) ) {
205
   print "Loading defaults from existing config $sysinfo3\n";
206
   my $client_name;
207
   open SEED, "<$sysinfo3" or die "Could not open $sysinfo3: $!\n";
208
   my $temp = join( '', <SEED> );
209
   close SEED;
210
   eval( $temp );
211
} else { # nope, so see if an old sysinfo2 or a seed file exists
212
   if ( -f $sysinfo2 && &yesno( 'Old sysinfo 2 config exists, load it for defaults' ) ) {
213
      print "Loading defaults from sysinfo2 config $sysinfo2\n";
214
      my $client_name;
215
      open SEED, "<$sysinfo2" or die "Could not open $sysinfo2: $!\n";
216
      my $temp = join( '', <SEED> );
217
      close SEED;
218
      eval( $temp );
219
      $clientName = $client_name if ( $client_name );
220
   }
221
 
222
   if ( -f $seedFile  && &yesno( 'An installation seed file was found, load it' ) ) {
223
      print "Loading seed file $seedFile\n";
224
      open SEED, "<$seedFile" or die "Could not open $seedFile: $!\n";
225
      my $temp = join( '', <SEED> );
226
      close SEED;
227
      eval( $temp );
228
   }
229
}
230
 
231
unless ( $hostname ) {
232
   $hostname = `hostname -f`;
233
   chomp $hostname;
234
}
235
 
236
&userConfirmation();
237
 
238
print "The following is the configuration I'll write out\n" . '-'x40 . "\n";
239
print &showConf(  );
240
print '-'x40 . "\n";
12 rodolico 241
print &writeConfig( $configPath, $configFile, &showConf() ) if &yesno( "Write this configuration" );
11 rodolico 242
 
12 rodolico 243
 
11 rodolico 244
my $os = &getOperatingSystem();
245
if ( $os && &yesno( "I recognize this system, may I automatically set up a few things" ) ) {
12 rodolico 246
   &ipFire() if $os eq 'ipfire';
247
   &debian() if $os eq 'debian';
11 rodolico 248
}
249
 
250
print "sysinfo-client has been installed configured. You can return to\nthis screen at any time by running /opt/camp/sysinfo/config.pl\n";
251
 
252
1;