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