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