21 |
rodolico |
1 |
#!/usr/bin/env perl
|
|
|
2 |
|
|
|
3 |
package sysinfoconf;
|
|
|
4 |
|
26 |
rodolico |
5 |
our $VERSION = '1.0';
|
21 |
rodolico |
6 |
use warnings;
|
26 |
rodolico |
7 |
use strict;
|
|
|
8 |
|
21 |
rodolico |
9 |
use Exporter;
|
|
|
10 |
|
|
|
11 |
our @ISA = qw( Exporter );
|
|
|
12 |
our @EXPORT = qw( $clientName $serialNumber $hostname @moduleDirs @scriptDirs
|
|
|
13 |
$transports $sysinfo3 %sendTypes $binDir $modulesDir
|
|
|
14 |
$scriptsDir $confDir $binName $confName
|
|
|
15 |
&showConf &transportsToConfig &getAnswer &yesno
|
|
|
16 |
&writeConfig &processParameters
|
|
|
17 |
);
|
|
|
18 |
|
|
|
19 |
|
|
|
20 |
$clientName = '';
|
|
|
21 |
$serialNumber = '';
|
|
|
22 |
$hostname = '';
|
|
|
23 |
@moduleDirs = ( '/opt/camp/sysinfo-client/modules', '/etc/camp/sysinfo-client/modules' );
|
|
|
24 |
@scriptDirs = ( '/opt/camp/sysinfo-client/scripts', '/etc/camp/sysinfo-client/scripts' );
|
|
|
25 |
$transports = {}; # holds transportation mechanisms
|
|
|
26 |
# the following are keys which are specific to the different transport channels
|
|
|
27 |
|
|
|
28 |
$sysinfo3 = '/etc/camp/sysinfo-client/sysinfo-client.conf';
|
|
|
29 |
|
|
|
30 |
$binDir = '/opt/camp/sysinfo-client';
|
|
|
31 |
$modulesDir = $binDir . '/modules';
|
|
|
32 |
$scriptsDir = $binDir . '/scripts';
|
|
|
33 |
$confDir = '/etc/camp/sysinfo-client';
|
|
|
34 |
|
|
|
35 |
$binName = 'sysinfo-client';
|
|
|
36 |
$confName = 'sysinfo-client.conf';
|
|
|
37 |
|
|
|
38 |
|
|
|
39 |
%sendTypes = (
|
|
|
40 |
'SendEmail' => { 'sendScript' => 'sendEmailScript',
|
|
|
41 |
'keys' =>
|
|
|
42 |
[
|
|
|
43 |
'mailTo',
|
|
|
44 |
'mailSubject',
|
|
|
45 |
'mailCC',
|
|
|
46 |
'mailBCC',
|
|
|
47 |
'mailServer',
|
|
|
48 |
'mailFrom',
|
|
|
49 |
'logFile',
|
|
|
50 |
'otherCLParams',
|
|
|
51 |
'tls',
|
|
|
52 |
'smtpUser',
|
|
|
53 |
'smtpPass',
|
|
|
54 |
'sendEmailScriptLocation'
|
|
|
55 |
],
|
|
|
56 |
},
|
|
|
57 |
'HTTP Upload' => { 'sendScript' => 'upload_http',
|
|
|
58 |
'keys' =>
|
|
|
59 |
[
|
|
|
60 |
'URL',
|
|
|
61 |
'key for report',
|
|
|
62 |
'key for date',
|
|
|
63 |
'key for hostname',
|
|
|
64 |
'key for client',
|
|
|
65 |
'key for serial number'
|
|
|
66 |
]
|
|
|
67 |
}
|
|
|
68 |
);
|
|
|
69 |
|
|
|
70 |
|
|
|
71 |
sub showConf {
|
|
|
72 |
my $conf;
|
|
|
73 |
$conf .= "\$clientName = '" . $clientName . "';\n";
|
|
|
74 |
$conf .= "\$serialNumber = '" . ( defined( $serialNumber ) ? $serialNumber : '' ) . "';\n";
|
|
|
75 |
$conf .= "\$hostname = '" . $hostname . "';\n";
|
|
|
76 |
$conf .= "\@moduleDirs = ('" . join( "','", @moduleDirs ). "');\n";
|
|
|
77 |
$conf .= "\@scriptDirs = ('" . join( "','", @scriptDirs ). "');\n";
|
|
|
78 |
$conf .= &transportsToConfig();
|
|
|
79 |
return $conf;
|
|
|
80 |
}
|
|
|
81 |
|
|
|
82 |
sub transportsToConfig {
|
|
|
83 |
my $config;
|
|
|
84 |
foreach my $priority ( sort { $a <=> $b } keys %$transports ) {
|
|
|
85 |
my $name = $$transports{ $priority }{'-name-'};
|
|
|
86 |
$config .= "# Tranport $name at priority $priority\n";
|
|
|
87 |
my $thisOne = $$transports{ $priority };
|
|
|
88 |
foreach my $key ( sort keys %$thisOne ) {
|
|
|
89 |
$config .= "\$\$transports{$priority}{'$key'} = '$$thisOne{$key}';\n";
|
|
|
90 |
} # foreach
|
|
|
91 |
} # foreach
|
|
|
92 |
return $config;
|
|
|
93 |
} # transportsToConfig
|
|
|
94 |
|
|
|
95 |
# prompt the user for a response, then allow them to enter it
|
|
|
96 |
# the first response is considered the default and is printed
|
|
|
97 |
# in all caps if more than one exists
|
|
|
98 |
# first answer is used if they simply press the Enter
|
|
|
99 |
# key. The response is returned all lower case if more than one
|
|
|
100 |
# exists.
|
|
|
101 |
# it is assumed
|
|
|
102 |
sub getAnswer {
|
|
|
103 |
my ( $prompt, @answers ) = @_;
|
|
|
104 |
$answers[0] = '' unless defined( $answers[0] );
|
|
|
105 |
my $default = $answers[0];
|
|
|
106 |
my $onlyOneAnswer = scalar( @answers ) == 1;
|
|
|
107 |
print $prompt . '[ ';
|
|
|
108 |
$answers[0] = uc $answers[0] unless $onlyOneAnswer;
|
|
|
109 |
print join( ' | ', @answers ) . ' ]: ';
|
|
|
110 |
$thisAnswer = <>;
|
|
|
111 |
chomp $thisAnswer;
|
|
|
112 |
$thisAnswer = $default unless $thisAnswer;
|
|
|
113 |
return $thisAnswer;
|
|
|
114 |
}
|
|
|
115 |
|
|
|
116 |
sub yesno {
|
|
|
117 |
my $prompt = shift;
|
|
|
118 |
my $answer = &getAnswer( $prompt, ('yes','no' ) );
|
|
|
119 |
return lc( substr( $answer, 0, 1 ) ) eq 'y';
|
|
|
120 |
}
|
|
|
121 |
|
|
|
122 |
sub writeConfig {
|
|
|
123 |
my ($path,$filename,$content) = @_;
|
|
|
124 |
my $return;
|
|
|
125 |
`mkdir -p $path` unless -d $path;
|
|
|
126 |
$filename = $path . '/' . $filename;
|
|
|
127 |
if ( -e $filename ) {
|
|
|
128 |
`cp $filename $filename.bak` if ( -e $filename );
|
|
|
129 |
$return .= "Old config copied to $filename.bak\n";
|
|
|
130 |
}
|
|
|
131 |
open CONF,">$filename" or die "Could not write to $filename: $!\n";
|
|
|
132 |
print CONF $content;
|
|
|
133 |
close CONF;
|
|
|
134 |
`chmod 600 $filename`;
|
|
|
135 |
$return .= "Configuration successfully written to $filename\n";
|
|
|
136 |
return $return;
|
|
|
137 |
}
|
|
|
138 |
|
|
|
139 |
sub processParameters {
|
|
|
140 |
while ( my $parameter = shift ) {
|
|
|
141 |
if ( $parameter eq '-v' ) {
|
26 |
rodolico |
142 |
print "$VERSION\n";
|
21 |
rodolico |
143 |
exit;
|
|
|
144 |
}
|
|
|
145 |
} # while
|
|
|
146 |
}
|
|
|
147 |
|
|
|
148 |
|
|
|
149 |
|
|
|
150 |
1;
|