| 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
|
| 85 |
rodolico |
8 |
#
|
|
|
9 |
# v1.4.0 20190204 RWR
|
|
|
10 |
# added autoconvert from v1.2 configuration file to v1.3
|
|
|
11 |
# using dumper to set configuration file
|
| 45 |
rodolico |
12 |
|
| 21 |
rodolico |
13 |
package sysinfoconf;
|
|
|
14 |
|
| 42 |
rodolico |
15 |
|
| 85 |
rodolico |
16 |
our $VERSION = '1.4.0';
|
| 21 |
rodolico |
17 |
use warnings;
|
| 26 |
rodolico |
18 |
use strict;
|
|
|
19 |
|
| 35 |
rodolico |
20 |
use Data::Dumper;
|
|
|
21 |
use File::Basename;
|
|
|
22 |
|
| 21 |
rodolico |
23 |
use Exporter;
|
|
|
24 |
|
|
|
25 |
our @ISA = qw( Exporter );
|
|
|
26 |
our @EXPORT = qw( $clientName $serialNumber $hostname @moduleDirs @scriptDirs
|
| 44 |
rodolico |
27 |
$transports $sysinfo3 %sendTypes $binDir $moduleDir
|
|
|
28 |
$scriptDir $confDir $binName $confName
|
| 21 |
rodolico |
29 |
&showConf &transportsToConfig &getAnswer &yesno
|
| 35 |
rodolico |
30 |
&writeConfig &processParameters $TESTING &checkDirectoryExists
|
| 53 |
rodolico |
31 |
&runCommand &setDryRun &enableModules &makeConfig &findConf
|
| 21 |
rodolico |
32 |
);
|
|
|
33 |
|
| 36 |
rodolico |
34 |
our $TESTING = 0;
|
| 35 |
rodolico |
35 |
our $dryRun;
|
| 28 |
rodolico |
36 |
our $clientName = '';
|
|
|
37 |
our $serialNumber = '';
|
|
|
38 |
our $hostname = '';
|
| 53 |
rodolico |
39 |
|
|
|
40 |
my @installDirs = ( '/opt/camp/sysinfo-client', '/usr/local/opt/camp/sysinfo-client' );
|
|
|
41 |
my @confDirs = ( '/etc/camp/sysinfo-client', '/usr/local/etc/camp/sysinfo-client' );
|
|
|
42 |
|
|
|
43 |
|
| 28 |
rodolico |
44 |
our @moduleDirs = ( '/opt/camp/sysinfo-client/modules', '/etc/camp/sysinfo-client/modules' );
|
|
|
45 |
our @scriptDirs = ( '/opt/camp/sysinfo-client/scripts', '/etc/camp/sysinfo-client/scripts' );
|
|
|
46 |
our $transports = {}; # holds transportation mechanisms
|
| 21 |
rodolico |
47 |
# the following are keys which are specific to the different transport channels
|
|
|
48 |
|
| 53 |
rodolico |
49 |
our $sysinfo3 = 'sysinfo-client.conf';
|
| 21 |
rodolico |
50 |
|
| 28 |
rodolico |
51 |
our $binDir = '/opt/camp/sysinfo-client';
|
| 44 |
rodolico |
52 |
our $moduleDir = $binDir . '/modules';
|
|
|
53 |
our $scriptDir = $binDir . '/scripts';
|
| 28 |
rodolico |
54 |
our $confDir = '/etc/camp/sysinfo-client';
|
| 21 |
rodolico |
55 |
|
| 28 |
rodolico |
56 |
our $binName = 'sysinfo-client';
|
|
|
57 |
our $confName = 'sysinfo-client.conf';
|
| 21 |
rodolico |
58 |
|
| 35 |
rodolico |
59 |
sub setDryRun {
|
|
|
60 |
$dryRun = shift;
|
|
|
61 |
}
|
| 21 |
rodolico |
62 |
|
| 28 |
rodolico |
63 |
our %sendTypes = (
|
| 21 |
rodolico |
64 |
'SendEmail' => { 'sendScript' => 'sendEmailScript',
|
|
|
65 |
'keys' =>
|
|
|
66 |
[
|
|
|
67 |
'mailTo',
|
|
|
68 |
'mailSubject',
|
|
|
69 |
'mailCC',
|
|
|
70 |
'mailBCC',
|
|
|
71 |
'mailServer',
|
|
|
72 |
'mailFrom',
|
|
|
73 |
'logFile',
|
|
|
74 |
'otherCLParams',
|
|
|
75 |
'tls',
|
|
|
76 |
'smtpUser',
|
|
|
77 |
'smtpPass',
|
|
|
78 |
'sendEmailScriptLocation'
|
|
|
79 |
],
|
|
|
80 |
},
|
|
|
81 |
'HTTP Upload' => { 'sendScript' => 'upload_http',
|
|
|
82 |
'keys' =>
|
|
|
83 |
[
|
|
|
84 |
'URL',
|
|
|
85 |
'key for report',
|
|
|
86 |
'key for date',
|
|
|
87 |
'key for hostname',
|
|
|
88 |
'key for client',
|
|
|
89 |
'key for serial number'
|
|
|
90 |
]
|
|
|
91 |
}
|
|
|
92 |
);
|
|
|
93 |
|
|
|
94 |
|
| 37 |
rodolico |
95 |
sub enableModules {
|
|
|
96 |
my $moduleDir = shift;
|
|
|
97 |
my %modules;
|
|
|
98 |
if ( opendir( my $dh, "$moduleDir" ) ) {
|
|
|
99 |
%modules = map{ $_ => { 'enabled' => -x "$moduleDir/$_" } }
|
|
|
100 |
grep { ! /^\./ }
|
|
|
101 |
readdir $dh;
|
|
|
102 |
closedir( $dh );
|
|
|
103 |
foreach my $filename ( keys %modules ) {
|
|
|
104 |
if ( open FILE,"<$moduleDir/$_" ) {
|
|
|
105 |
my @descript = grep{ /^# Description: (.*)/ } <FILE>;
|
|
|
106 |
close FILE;
|
|
|
107 |
chomp @descript;
|
|
|
108 |
$modules{$filename}{'description'} = $descript[0];
|
|
|
109 |
} # if
|
|
|
110 |
} # foreach
|
|
|
111 |
} # if
|
|
|
112 |
} # enableModules
|
|
|
113 |
|
| 21 |
rodolico |
114 |
sub showConf {
|
| 81 |
rodolico |
115 |
use Data::Dumper;
|
|
|
116 |
|
| 35 |
rodolico |
117 |
my $configuration = shift;
|
| 81 |
rodolico |
118 |
local $Data::Dumper::Purity = 1;
|
|
|
119 |
$Data::Dumper::Indent = 3;
|
| 84 |
rodolico |
120 |
$Data::Dumper::Varname = 'configuration';
|
| 81 |
rodolico |
121 |
$Data::Dumper::Quotekeys = 1;
|
|
|
122 |
$Data::Dumper::Sortkeys = 1;
|
| 84 |
rodolico |
123 |
$Data::Dumper::Terse = 1;
|
|
|
124 |
$configuration = '%configuration = ' . Data::Dumper->Dump( [$configuration] );
|
|
|
125 |
return $configuration;
|
| 81 |
rodolico |
126 |
|
| 83 |
rodolico |
127 |
#return $conf;
|
|
|
128 |
#my $conf;
|
|
|
129 |
#$conf .= "\$clientName = '" . ( defined( $$configuration{'clientName'} ) ? $$configuration{'clientName'} : '' ) . "';\n";
|
|
|
130 |
#$conf .= "\$serialNumber = '" . ( defined( $$configuration{'serialNumber'} ) ? $$configuration{'serialNumber'} : '' ) . "';\n";
|
|
|
131 |
#$conf .= "\$hostname = '" . ( defined( $$configuration{'hostname'} ) ? $$configuration{'hostname'} : '' ) . "';\n";
|
|
|
132 |
#$conf .= "\$UUID = '" . ( defined( $$configuration{'UUID'} ) ? $$configuration{'UUID'} : '' ) . "';\n";
|
|
|
133 |
#$conf .= "\@moduleDirs = ('" . join( "','", @{ $$configuration{ 'moduleDirs' } } ) . "');\n";
|
|
|
134 |
#$conf .= "\@scriptDirs = ('" . join( "','", @{ $$configuration{ 'scriptDirs' } } ) . "');\n";
|
|
|
135 |
#$conf .= &transportsToConfig( $$configuration{ 'transports' } ) if defined( $$configuration{ 'transports' } );
|
|
|
136 |
#return $conf;
|
| 21 |
rodolico |
137 |
}
|
|
|
138 |
|
|
|
139 |
sub transportsToConfig {
|
| 35 |
rodolico |
140 |
my $transports = shift;
|
| 21 |
rodolico |
141 |
my $config;
|
|
|
142 |
foreach my $priority ( sort { $a <=> $b } keys %$transports ) {
|
|
|
143 |
my $name = $$transports{ $priority }{'-name-'};
|
|
|
144 |
$config .= "# Tranport $name at priority $priority\n";
|
|
|
145 |
my $thisOne = $$transports{ $priority };
|
|
|
146 |
foreach my $key ( sort keys %$thisOne ) {
|
|
|
147 |
$config .= "\$\$transports{$priority}{'$key'} = '$$thisOne{$key}';\n";
|
|
|
148 |
} # foreach
|
|
|
149 |
} # foreach
|
|
|
150 |
return $config;
|
|
|
151 |
} # transportsToConfig
|
|
|
152 |
|
| 45 |
rodolico |
153 |
sub makeConfig {
|
|
|
154 |
my @configFileNames = @_;
|
|
|
155 |
my %config;
|
| 77 |
rodolico |
156 |
my $clientName = '';
|
|
|
157 |
my $serialNumber = '';
|
|
|
158 |
my $hostname = '';
|
| 80 |
rodolico |
159 |
my @moduleDirs;
|
|
|
160 |
my @scriptDirs;
|
| 77 |
rodolico |
161 |
my $UUID = '';
|
| 45 |
rodolico |
162 |
my $transports = {};
|
|
|
163 |
|
|
|
164 |
foreach my $config ( @configFileNames ) {
|
|
|
165 |
open CONF,"<$config" or die "could not open $config: $!\n";
|
|
|
166 |
my $contents = join( '', <CONF> );
|
|
|
167 |
close CONF;
|
|
|
168 |
# now, eval the information we just read.
|
|
|
169 |
# NOTE: we must turn off strict while doing this, and we die if something breaks.
|
|
|
170 |
no strict "vars"; eval( $contents ); use strict "vars"; die "Error during eval: $@\n" if $@;
|
| 81 |
rodolico |
171 |
# if this is a v2 config, we will update it
|
|
|
172 |
$config{'clientName'} = $clientName unless $config{'clientName'};
|
|
|
173 |
$config{'serialNumber'} = $serialNumber unless $config{'serialNumber'};
|
|
|
174 |
$config{'UUID'} = $UUID unless $config{'UUID'};
|
|
|
175 |
$config{'hostname'} = $hostname unless $config{'hostname'};
|
|
|
176 |
$config{'moduleDirs'} = [ @moduleDirs ] unless $config{'moduleDirs'};
|
|
|
177 |
$config{'scriptDirs'} = [ @scriptDirs ] unless $config{'scriptDirs'};
|
|
|
178 |
$config{'transports'} = $transports unless $config{'transports'};
|
| 45 |
rodolico |
179 |
}
|
| 81 |
rodolico |
180 |
|
|
|
181 |
|
|
|
182 |
unless ( $config{'hostname'} ) {
|
| 45 |
rodolico |
183 |
$hostname = `hostname -f`;
|
|
|
184 |
chomp $hostname;
|
| 81 |
rodolico |
185 |
$config{'hostname'} = $hostname;
|
| 45 |
rodolico |
186 |
}
|
| 84 |
rodolico |
187 |
unless ( $config{'serialNumber'} ) {
|
| 53 |
rodolico |
188 |
$serialNumber = `dmidecode -t 1 | grep 'Serial Number' | cut -d':' -f2` if `which dmidecode`;
|
|
|
189 |
chomp $serialNumber;
|
|
|
190 |
$serialNumber =~ s/\s//gi;
|
| 84 |
rodolico |
191 |
$config{'serialNumber'} = $serialNumber;
|
| 53 |
rodolico |
192 |
}
|
| 81 |
rodolico |
193 |
unless ( $config{'UUID'} ) {
|
| 77 |
rodolico |
194 |
$UUID = `dmidecode -t 1 | grep -i uuid | cut -d':' -f2` if `which dmidecode`;
|
|
|
195 |
$UUID =~ s/\s//gi;
|
| 81 |
rodolico |
196 |
$config{'UUID'} = $UUID;
|
| 77 |
rodolico |
197 |
}
|
| 45 |
rodolico |
198 |
|
|
|
199 |
return \%config;
|
|
|
200 |
}
|
|
|
201 |
|
| 21 |
rodolico |
202 |
# prompt the user for a response, then allow them to enter it
|
|
|
203 |
# the first response is considered the default and is printed
|
|
|
204 |
# in all caps if more than one exists
|
|
|
205 |
# first answer is used if they simply press the Enter
|
|
|
206 |
# key. The response is returned all lower case if more than one
|
|
|
207 |
# exists.
|
|
|
208 |
# it is assumed
|
|
|
209 |
sub getAnswer {
|
|
|
210 |
my ( $prompt, @answers ) = @_;
|
|
|
211 |
$answers[0] = '' unless defined( $answers[0] );
|
|
|
212 |
my $default = $answers[0];
|
|
|
213 |
my $onlyOneAnswer = scalar( @answers ) == 1;
|
|
|
214 |
print $prompt . '[ ';
|
|
|
215 |
$answers[0] = uc $answers[0] unless $onlyOneAnswer;
|
|
|
216 |
print join( ' | ', @answers ) . ' ]: ';
|
| 28 |
rodolico |
217 |
my $thisAnswer = <>;
|
| 21 |
rodolico |
218 |
chomp $thisAnswer;
|
|
|
219 |
$thisAnswer = $default unless $thisAnswer;
|
|
|
220 |
return $thisAnswer;
|
|
|
221 |
}
|
|
|
222 |
|
|
|
223 |
sub yesno {
|
| 40 |
rodolico |
224 |
my ( $prompt, $default ) = @_;
|
|
|
225 |
$default = 'yes' unless $default;
|
|
|
226 |
my $answer = &getAnswer( $prompt, $default eq 'yes' ? ('yes','no' ) : ('no', 'yes') );
|
| 21 |
rodolico |
227 |
return lc( substr( $answer, 0, 1 ) ) eq 'y';
|
|
|
228 |
}
|
|
|
229 |
|
| 35 |
rodolico |
230 |
# runs a system command. Also, if in testing mode, simply shows what
|
|
|
231 |
# would have been done.
|
|
|
232 |
sub runCommand {
|
|
|
233 |
while ( my $command = shift ) {
|
|
|
234 |
if ( $dryRun ) {
|
|
|
235 |
print "$command\n";
|
|
|
236 |
} else {
|
|
|
237 |
`$command`;
|
|
|
238 |
}
|
|
|
239 |
}
|
|
|
240 |
return 1;
|
|
|
241 |
} # runCommand
|
|
|
242 |
|
|
|
243 |
# checks if a directory exists and, if not, creates it
|
|
|
244 |
my %directories; # holds list of directories already created so no need to do an I/O
|
|
|
245 |
|
|
|
246 |
sub checkDirectoryExists {
|
|
|
247 |
my ( $filename,$mod,$owner ) = @_;
|
|
|
248 |
$mod = "0700" unless $mod;
|
|
|
249 |
$owner = "root:root" unless $owner;
|
|
|
250 |
print "Checking Directory for $filename with $mod and $owner\n" if $TESTING > 2;
|
|
|
251 |
my ($fn, $dirname) = fileparse( $filename );
|
|
|
252 |
print "\tParsing out $dirname and $filename\n" if $TESTING > 2;
|
|
|
253 |
return '' if exists $directories{$dirname};
|
|
|
254 |
if ( -d $dirname ) {
|
|
|
255 |
$directories{$dirname} = 1;
|
|
|
256 |
return '';
|
|
|
257 |
}
|
|
|
258 |
if ( &runCommand( "mkdir -p $dirname", "chmod $mod $dirname", "chown $owner $dirname" ) ) {
|
|
|
259 |
$directories{$dirname} = 1;
|
|
|
260 |
}
|
|
|
261 |
return '';
|
|
|
262 |
}
|
|
|
263 |
|
| 21 |
rodolico |
264 |
sub writeConfig {
|
| 35 |
rodolico |
265 |
my ( $filename,$content ) = @_;
|
|
|
266 |
my $path;
|
|
|
267 |
($filename, $path ) = fileparse( $filename );
|
|
|
268 |
|
| 21 |
rodolico |
269 |
my $return;
|
|
|
270 |
`mkdir -p $path` unless -d $path;
|
|
|
271 |
$filename = $path . '/' . $filename;
|
|
|
272 |
if ( -e $filename ) {
|
|
|
273 |
`cp $filename $filename.bak` if ( -e $filename );
|
|
|
274 |
$return .= "Old config copied to $filename.bak\n";
|
|
|
275 |
}
|
| 35 |
rodolico |
276 |
if ( $dryRun ) {
|
|
|
277 |
$return .= "Not writing to configuration file\n";
|
|
|
278 |
} else {
|
|
|
279 |
open CONF,">$filename" or die "Could not write to $filename: $!\n";
|
|
|
280 |
print CONF $content;
|
|
|
281 |
close CONF;
|
|
|
282 |
`chmod 600 $filename`;
|
|
|
283 |
$return .= "Configuration successfully written to $filename\n";
|
|
|
284 |
}
|
| 21 |
rodolico |
285 |
return $return;
|
|
|
286 |
}
|
|
|
287 |
|
|
|
288 |
sub processParameters {
|
|
|
289 |
while ( my $parameter = shift ) {
|
|
|
290 |
if ( $parameter eq '-v' ) {
|
| 26 |
rodolico |
291 |
print "$VERSION\n";
|
| 21 |
rodolico |
292 |
exit;
|
|
|
293 |
}
|
|
|
294 |
} # while
|
|
|
295 |
}
|
|
|
296 |
|
| 53 |
rodolico |
297 |
sub findConf {
|
|
|
298 |
my $confName = shift;
|
|
|
299 |
my @confDirs = ( '/etc/camp/sysinfo-client', '/usr/local/etc/camp/sysinfo-client' );
|
|
|
300 |
for ( my $i = 0; $i < @confDirs; $i++ ) {
|
|
|
301 |
if ( -d $confDirs[ $i ] ) {
|
| 55 |
rodolico |
302 |
return ( $confDirs[$i], $confName );
|
| 53 |
rodolico |
303 |
}
|
|
|
304 |
}
|
|
|
305 |
}
|
|
|
306 |
|
| 21 |
rodolico |
307 |
|
|
|
308 |
1;
|