| 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
|
| 125 |
rodolico |
15 |
#
|
|
|
16 |
# version 2.1 20191101 RWR
|
|
|
17 |
# changed --name-- tag to name
|
| 129 |
rodolico |
18 |
#
|
|
|
19 |
# version 2.2 20191105 RWR
|
|
|
20 |
# fixed where dmidecode missing caused exception
|
| 135 |
rodolico |
21 |
#
|
|
|
22 |
# version 2.2.1 20191112 RWR
|
|
|
23 |
# added timeStamp
|
| 194 |
rodolico |
24 |
#
|
|
|
25 |
# version 2.3.0 20220609 RWR
|
|
|
26 |
# moved loadConfigurationFile and timeStamp here
|
| 45 |
rodolico |
27 |
|
| 21 |
rodolico |
28 |
package sysinfoconf;
|
|
|
29 |
|
| 42 |
rodolico |
30 |
|
| 194 |
rodolico |
31 |
our $VERSION = '2.3.0';
|
| 21 |
rodolico |
32 |
use warnings;
|
| 26 |
rodolico |
33 |
use strict;
|
|
|
34 |
|
| 92 |
rodolico |
35 |
#use Data::Dumper;
|
| 144 |
rodolico |
36 |
use YAML::Tiny;
|
| 35 |
rodolico |
37 |
use File::Basename;
|
|
|
38 |
|
| 21 |
rodolico |
39 |
use Exporter;
|
|
|
40 |
|
|
|
41 |
our @ISA = qw( Exporter );
|
| 194 |
rodolico |
42 |
our @EXPORT = qw( $clientName $serialNumber $hostname
|
|
|
43 |
$transports $sysinfo3 $binDir $moduleDir
|
|
|
44 |
$scriptDir $confDir $binName $confName $configurationFile
|
|
|
45 |
@confFileSearchPath @moduleDirs @scriptDirs
|
|
|
46 |
%sendTypes
|
| 21 |
rodolico |
47 |
&showConf &transportsToConfig &getAnswer &yesno
|
| 144 |
rodolico |
48 |
&writeConfig &processParameters $TESTING
|
|
|
49 |
&setDryRun &enableModules &makeConfig &findConf &timeStamp
|
| 197 |
rodolico |
50 |
&loadConfigurationFile &logIt &findFile
|
| 21 |
rodolico |
51 |
);
|
|
|
52 |
|
| 36 |
rodolico |
53 |
our $TESTING = 0;
|
| 35 |
rodolico |
54 |
our $dryRun;
|
| 28 |
rodolico |
55 |
our $clientName = '';
|
|
|
56 |
our $serialNumber = '';
|
|
|
57 |
our $hostname = '';
|
| 53 |
rodolico |
58 |
|
| 194 |
rodolico |
59 |
# paths to search for configuration file
|
|
|
60 |
my @confFileSearchPath = ( '.', '/etc/camp/sysinfo-client', '/etc/camp', '/usr/local/etc/camp/sysinfo-client' );
|
| 104 |
rodolico |
61 |
|
| 194 |
rodolico |
62 |
my $configurationFile = 'sysinfo-client.yaml'; # name of the configuration file
|
| 104 |
rodolico |
63 |
|
| 194 |
rodolico |
64 |
|
| 53 |
rodolico |
65 |
my @installDirs = ( '/opt/camp/sysinfo-client', '/usr/local/opt/camp/sysinfo-client' );
|
|
|
66 |
my @confDirs = ( '/etc/camp/sysinfo-client', '/usr/local/etc/camp/sysinfo-client' );
|
|
|
67 |
|
|
|
68 |
|
| 28 |
rodolico |
69 |
our @moduleDirs = ( '/opt/camp/sysinfo-client/modules', '/etc/camp/sysinfo-client/modules' );
|
|
|
70 |
our @scriptDirs = ( '/opt/camp/sysinfo-client/scripts', '/etc/camp/sysinfo-client/scripts' );
|
| 111 |
rodolico |
71 |
our $transports = {}; # holds transportation mechanisms
|
| 21 |
rodolico |
72 |
|
| 103 |
rodolico |
73 |
our $sysinfo3 = 'sysinfo-client.yaml';
|
| 21 |
rodolico |
74 |
|
| 28 |
rodolico |
75 |
our $binDir = '/opt/camp/sysinfo-client';
|
| 44 |
rodolico |
76 |
our $moduleDir = $binDir . '/modules';
|
|
|
77 |
our $scriptDir = $binDir . '/scripts';
|
| 28 |
rodolico |
78 |
our $confDir = '/etc/camp/sysinfo-client';
|
| 21 |
rodolico |
79 |
|
| 28 |
rodolico |
80 |
our $binName = 'sysinfo-client';
|
| 107 |
rodolico |
81 |
our $confName = 'sysinfo-client.yaml';
|
| 21 |
rodolico |
82 |
|
| 35 |
rodolico |
83 |
sub setDryRun {
|
|
|
84 |
$dryRun = shift;
|
|
|
85 |
}
|
| 21 |
rodolico |
86 |
|
| 28 |
rodolico |
87 |
our %sendTypes = (
|
| 21 |
rodolico |
88 |
'SendEmail' => { 'sendScript' => 'sendEmailScript',
|
|
|
89 |
'keys' =>
|
|
|
90 |
[
|
|
|
91 |
'mailTo',
|
|
|
92 |
'mailSubject',
|
|
|
93 |
'mailCC',
|
|
|
94 |
'mailBCC',
|
|
|
95 |
'mailServer',
|
|
|
96 |
'mailFrom',
|
|
|
97 |
'logFile',
|
|
|
98 |
'otherCLParams',
|
|
|
99 |
'tls',
|
|
|
100 |
'smtpUser',
|
|
|
101 |
'smtpPass',
|
|
|
102 |
'sendEmailScriptLocation'
|
|
|
103 |
],
|
|
|
104 |
},
|
|
|
105 |
'HTTP Upload' => { 'sendScript' => 'upload_http',
|
|
|
106 |
'keys' =>
|
|
|
107 |
[
|
|
|
108 |
'URL',
|
|
|
109 |
'key for report',
|
|
|
110 |
'key for date',
|
|
|
111 |
'key for hostname',
|
|
|
112 |
'key for client',
|
|
|
113 |
'key for serial number'
|
|
|
114 |
]
|
| 86 |
rodolico |
115 |
},
|
|
|
116 |
'Save Local' => { 'sendScript' => 'save_local',
|
|
|
117 |
'keys' =>
|
|
|
118 |
[
|
|
|
119 |
'output directory'
|
|
|
120 |
]
|
|
|
121 |
}
|
| 21 |
rodolico |
122 |
);
|
|
|
123 |
|
|
|
124 |
|
| 135 |
rodolico |
125 |
#######################################################
|
|
|
126 |
#
|
|
|
127 |
# timeStamp
|
|
|
128 |
#
|
|
|
129 |
# return current system date as YYYY-MM-DD HH:MM:SS
|
|
|
130 |
#
|
|
|
131 |
#######################################################
|
|
|
132 |
sub timeStamp {
|
|
|
133 |
my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst)=localtime(time);
|
| 143 |
rodolico |
134 |
return sprintf "%4d-%02d-%02d %02d:%02d:%02d",$year+1900,$mon+1,$mday,$hour,$min,$sec;
|
| 135 |
rodolico |
135 |
}
|
|
|
136 |
|
|
|
137 |
|
| 37 |
rodolico |
138 |
sub enableModules {
|
|
|
139 |
my $moduleDir = shift;
|
|
|
140 |
my %modules;
|
|
|
141 |
if ( opendir( my $dh, "$moduleDir" ) ) {
|
|
|
142 |
%modules = map{ $_ => { 'enabled' => -x "$moduleDir/$_" } }
|
|
|
143 |
grep { ! /^\./ }
|
|
|
144 |
readdir $dh;
|
|
|
145 |
closedir( $dh );
|
|
|
146 |
foreach my $filename ( keys %modules ) {
|
|
|
147 |
if ( open FILE,"<$moduleDir/$_" ) {
|
|
|
148 |
my @descript = grep{ /^# Description: (.*)/ } <FILE>;
|
|
|
149 |
close FILE;
|
|
|
150 |
chomp @descript;
|
|
|
151 |
$modules{$filename}{'description'} = $descript[0];
|
|
|
152 |
} # if
|
|
|
153 |
} # foreach
|
|
|
154 |
} # if
|
|
|
155 |
} # enableModules
|
|
|
156 |
|
| 21 |
rodolico |
157 |
sub showConf {
|
| 81 |
rodolico |
158 |
|
| 35 |
rodolico |
159 |
my $configuration = shift;
|
| 92 |
rodolico |
160 |
$configuration = Dump($configuration);
|
| 84 |
rodolico |
161 |
return $configuration;
|
| 21 |
rodolico |
162 |
}
|
|
|
163 |
|
|
|
164 |
sub transportsToConfig {
|
| 35 |
rodolico |
165 |
my $transports = shift;
|
| 21 |
rodolico |
166 |
my $config;
|
|
|
167 |
foreach my $priority ( sort { $a <=> $b } keys %$transports ) {
|
| 124 |
rodolico |
168 |
my $name = $$transports{ $priority }{'name'};
|
| 21 |
rodolico |
169 |
$config .= "# Tranport $name at priority $priority\n";
|
|
|
170 |
my $thisOne = $$transports{ $priority };
|
|
|
171 |
foreach my $key ( sort keys %$thisOne ) {
|
|
|
172 |
$config .= "\$\$transports{$priority}{'$key'} = '$$thisOne{$key}';\n";
|
|
|
173 |
} # foreach
|
|
|
174 |
} # foreach
|
|
|
175 |
return $config;
|
|
|
176 |
} # transportsToConfig
|
|
|
177 |
|
| 94 |
rodolico |
178 |
|
|
|
179 |
# read the configuration file passed in and return a reference to it
|
|
|
180 |
# to the calling routine
|
|
|
181 |
sub readConfig {
|
|
|
182 |
my $filename = shift;
|
|
|
183 |
my $config;
|
|
|
184 |
return $config unless -e $filename;
|
| 211 |
rodolico |
185 |
my $yaml = YAML::Tiny->read( $filename );
|
|
|
186 |
return $yaml->[0];
|
| 94 |
rodolico |
187 |
}
|
|
|
188 |
|
| 45 |
rodolico |
189 |
sub makeConfig {
|
|
|
190 |
my @configFileNames = @_;
|
| 218 |
rodolico |
191 |
my $config;
|
| 45 |
rodolico |
192 |
|
| 218 |
rodolico |
193 |
use Hash::Merge;
|
|
|
194 |
my $merge = Hash::Merge->new('RIGHT_PRECEDENT');
|
|
|
195 |
foreach my $configFile ( @configFileNames ) {
|
|
|
196 |
next unless $configFile && -e $configFile;
|
|
|
197 |
print "Processing config file $configFile\n";
|
|
|
198 |
my $thisConfig = &readConfig( $configFile );
|
| 94 |
rodolico |
199 |
# add the new config to %config, overwriting any existing keys which are duplicated
|
| 218 |
rodolico |
200 |
$config = $merge->merge( $config, $thisConfig );
|
|
|
201 |
#@config{keys %$thisConfig} = values %$thisConfig;
|
| 45 |
rodolico |
202 |
}
|
| 218 |
rodolico |
203 |
use Data::Dumper;
|
|
|
204 |
|
|
|
205 |
die Dumper( $config );
|
| 94 |
rodolico |
206 |
# now, ensure the correct values are loaded in some areas
|
| 218 |
rodolico |
207 |
unless ( $config->{'hostname'} ) {
|
| 45 |
rodolico |
208 |
$hostname = `hostname -f`;
|
|
|
209 |
chomp $hostname;
|
| 218 |
rodolico |
210 |
$config->{'hostname'} = $hostname;
|
| 45 |
rodolico |
211 |
}
|
| 218 |
rodolico |
212 |
unless ( $config->{'serialNumber'} ) {
|
| 53 |
rodolico |
213 |
$serialNumber = `dmidecode -t 1 | grep 'Serial Number' | cut -d':' -f2` if `which dmidecode`;
|
|
|
214 |
chomp $serialNumber;
|
|
|
215 |
$serialNumber =~ s/\s//gi;
|
| 218 |
rodolico |
216 |
$config->{'serialNumber'} = $serialNumber;
|
| 53 |
rodolico |
217 |
}
|
| 218 |
rodolico |
218 |
unless ( $config->{'UUID'} ) {
|
| 129 |
rodolico |
219 |
my $UUID = `which dmidecode` ? `dmidecode -t 1 | grep -i uuid | cut -d':' -f2` : '';
|
| 77 |
rodolico |
220 |
$UUID =~ s/\s//gi;
|
| 218 |
rodolico |
221 |
$config->{'UUID'} = $UUID;
|
| 77 |
rodolico |
222 |
}
|
| 45 |
rodolico |
223 |
|
| 110 |
rodolico |
224 |
# ensure we have the default SaveLocal transport defined
|
| 218 |
rodolico |
225 |
unless ( defined $config->{'transports'}{'99'} ) {
|
|
|
226 |
$config->{'transports'}{'99'} = {
|
| 110 |
rodolico |
227 |
'name'=> 'SaveLocal',
|
|
|
228 |
'output directory' => '/tmp',
|
|
|
229 |
'sendScript' => 'save_local'
|
|
|
230 |
};
|
|
|
231 |
}
|
|
|
232 |
|
| 218 |
rodolico |
233 |
return $config;
|
| 45 |
rodolico |
234 |
}
|
|
|
235 |
|
| 21 |
rodolico |
236 |
# prompt the user for a response, then allow them to enter it
|
|
|
237 |
# the first response is considered the default and is printed
|
|
|
238 |
# in all caps if more than one exists
|
|
|
239 |
# first answer is used if they simply press the Enter
|
|
|
240 |
# key. The response is returned all lower case if more than one
|
|
|
241 |
# exists.
|
|
|
242 |
# it is assumed
|
|
|
243 |
sub getAnswer {
|
|
|
244 |
my ( $prompt, @answers ) = @_;
|
|
|
245 |
$answers[0] = '' unless defined( $answers[0] );
|
|
|
246 |
my $default = $answers[0];
|
|
|
247 |
my $onlyOneAnswer = scalar( @answers ) == 1;
|
|
|
248 |
print $prompt . '[ ';
|
|
|
249 |
$answers[0] = uc $answers[0] unless $onlyOneAnswer;
|
|
|
250 |
print join( ' | ', @answers ) . ' ]: ';
|
| 28 |
rodolico |
251 |
my $thisAnswer = <>;
|
| 21 |
rodolico |
252 |
chomp $thisAnswer;
|
|
|
253 |
$thisAnswer = $default unless $thisAnswer;
|
|
|
254 |
return $thisAnswer;
|
|
|
255 |
}
|
|
|
256 |
|
|
|
257 |
sub yesno {
|
| 40 |
rodolico |
258 |
my ( $prompt, $default ) = @_;
|
|
|
259 |
$default = 'yes' unless $default;
|
|
|
260 |
my $answer = &getAnswer( $prompt, $default eq 'yes' ? ('yes','no' ) : ('no', 'yes') );
|
| 21 |
rodolico |
261 |
return lc( substr( $answer, 0, 1 ) ) eq 'y';
|
|
|
262 |
}
|
|
|
263 |
|
|
|
264 |
sub writeConfig {
|
| 132 |
rodolico |
265 |
use File::Temp qw / tempfile /;
|
| 35 |
rodolico |
266 |
my ( $filename,$content ) = @_;
|
| 129 |
rodolico |
267 |
if ( $filename ) { # they sent us a filename
|
|
|
268 |
my $path;
|
|
|
269 |
($filename, $path ) = fileparse( $filename );
|
|
|
270 |
`mkdir -p $path` unless -d $path;
|
|
|
271 |
$filename = $path . '/' . $filename;
|
| 21 |
rodolico |
272 |
`cp $filename $filename.bak` if ( -e $filename );
|
| 129 |
rodolico |
273 |
unless ( $dryRun ) {
|
|
|
274 |
open CONF,">$filename" or die "Could not write to $filename: $!\n";
|
|
|
275 |
print CONF $content;
|
|
|
276 |
close CONF;
|
|
|
277 |
`chmod 600 $filename`;
|
|
|
278 |
}
|
|
|
279 |
} else { # no path provided, so just create a temp file
|
|
|
280 |
# we will create a temporary file and return the name
|
|
|
281 |
# it is the calling programs responsiblity to remove the file
|
|
|
282 |
my $fh;
|
|
|
283 |
($fh,$filename) = tempfile( UNLINK => 0 );
|
|
|
284 |
print $fh $content;
|
|
|
285 |
close $fh
|
| 21 |
rodolico |
286 |
}
|
| 129 |
rodolico |
287 |
return $filename;
|
| 21 |
rodolico |
288 |
}
|
|
|
289 |
|
|
|
290 |
sub processParameters {
|
|
|
291 |
while ( my $parameter = shift ) {
|
|
|
292 |
if ( $parameter eq '-v' ) {
|
| 26 |
rodolico |
293 |
print "$VERSION\n";
|
| 21 |
rodolico |
294 |
exit;
|
|
|
295 |
}
|
|
|
296 |
} # while
|
|
|
297 |
}
|
|
|
298 |
|
| 53 |
rodolico |
299 |
sub findConf {
|
|
|
300 |
my $confName = shift;
|
|
|
301 |
for ( my $i = 0; $i < @confDirs; $i++ ) {
|
|
|
302 |
if ( -d $confDirs[ $i ] ) {
|
| 55 |
rodolico |
303 |
return ( $confDirs[$i], $confName );
|
| 53 |
rodolico |
304 |
}
|
|
|
305 |
}
|
| 104 |
rodolico |
306 |
return ( '', $confName );
|
| 53 |
rodolico |
307 |
}
|
| 21 |
rodolico |
308 |
|
| 194 |
rodolico |
309 |
#######################################################
|
|
|
310 |
#
|
|
|
311 |
# findFile( $filename, @directories )
|
|
|
312 |
#
|
|
|
313 |
# Locates a file by searching sequentially in one or more
|
|
|
314 |
# directories, returning the first one found
|
|
|
315 |
#
|
|
|
316 |
# Returns '' if not found
|
|
|
317 |
#
|
|
|
318 |
#######################################################
|
| 104 |
rodolico |
319 |
|
| 194 |
rodolico |
320 |
sub findFile {
|
|
|
321 |
my ( $filename, $directories ) = @_;
|
|
|
322 |
&logIt( 3, "Looking for $filename in findFile" );
|
|
|
323 |
for ( my $i = 0; $i < scalar( @{$directories} ); $i++ ) {
|
|
|
324 |
my $confFile = $$directories[$i] . '/' . $filename;
|
|
|
325 |
&logIt( 4, "Looking for $filename in $confFile" );
|
|
|
326 |
return $confFile if ( -f $confFile );
|
|
|
327 |
}
|
|
|
328 |
return '';
|
|
|
329 |
}
|
|
|
330 |
|
| 104 |
rodolico |
331 |
|
| 194 |
rodolico |
332 |
#######################################################
|
|
|
333 |
#
|
|
|
334 |
# loadConfigurationFile($confFile)
|
|
|
335 |
#
|
|
|
336 |
# Loads configuration file defined by $configurationFile, and dies if not available
|
|
|
337 |
# This is a YAML file containing serialized contents of
|
|
|
338 |
# Parameters:
|
|
|
339 |
# $$fileName - name of file to look for (reference)
|
|
|
340 |
# @searchPath - array of paths to find $filename
|
|
|
341 |
#
|
|
|
342 |
#######################################################
|
| 104 |
rodolico |
343 |
|
| 194 |
rodolico |
344 |
sub loadConfigurationFile {
|
|
|
345 |
my ( $fileName, @searchPath ) = @_;
|
|
|
346 |
$$fileName = $configurationFile unless $$fileName;
|
|
|
347 |
@searchPath = @confFileSearchPath unless @searchPath;
|
|
|
348 |
&logIt( 2, "Looking for config file $$fileName in " . join( ', ', @searchPath ) );
|
|
|
349 |
my $confFile;
|
|
|
350 |
if ( $confFile = &findFile( $$fileName, \@searchPath ) ) {
|
|
|
351 |
&logIt( 3, "Opening configuration from $confFile" );
|
|
|
352 |
my $yaml = YAML::Tiny->read( $confFile );
|
|
|
353 |
&logIt( 4, "Configuration file contents\n$yaml" );
|
|
|
354 |
$$fileName = $confFile;
|
|
|
355 |
return $yaml->[0];
|
|
|
356 |
}
|
|
|
357 |
die "Can not find $fileName in any of " . join( "\n\t", @searchPath ) . "\n";
|
|
|
358 |
}
|
|
|
359 |
|
|
|
360 |
|
|
|
361 |
#######################################################
|
|
|
362 |
# function to simply log things
|
|
|
363 |
# first parameter is the priority, if <= $logDef->{'log level'} will print
|
|
|
364 |
# all subsequent parameters assumed to be strings to sent to the log
|
|
|
365 |
# returns 0 on failure
|
|
|
366 |
# 1 on success
|
|
|
367 |
# 2 if priority > log level
|
|
|
368 |
# -1 if $logDef is unset
|
|
|
369 |
# currently, only logs to a file
|
|
|
370 |
#######################################################
|
|
|
371 |
sub logIt {
|
|
|
372 |
my $priority = shift;
|
|
|
373 |
|
|
|
374 |
# turn off variable checking so it doesn't blow up on lack of %configuration file
|
|
|
375 |
no strict 'vars';
|
|
|
376 |
|
|
|
377 |
return -1 unless exists $configuration{'logging'};
|
|
|
378 |
return 2 unless $priority <= $configuration{'logging'}{'log level'};
|
|
|
379 |
if ( $configuration{'logging'}{'log type'} eq 'cache' ) {
|
|
|
380 |
push @{ $configuration{'logging'}{'cache'} }, @_;
|
|
|
381 |
return;
|
|
|
382 |
} elsif ( defined( $configuration{'logging'}{'cache'} ) ) {
|
|
|
383 |
unshift @_, @{ $configuration{'logging'}{'cache'} };
|
|
|
384 |
delete $configuration{'logging'}{'cache'};
|
|
|
385 |
}
|
|
|
386 |
if ( $configuration{'logging'}{'log type'} eq 'file' ) {
|
|
|
387 |
if ( open LOG, '>>' . $configuration{'logging'}{'log path'} ) {
|
|
|
388 |
while ( my $t = shift ) {
|
|
|
389 |
print LOG &timeStamp() . "\t$t\n";
|
|
|
390 |
}
|
|
|
391 |
close LOG;
|
|
|
392 |
}
|
|
|
393 |
} elsif ( $configuration{'logging'}{'log type'} eq 'syslog' ) {
|
|
|
394 |
use Sys::Syslog; # all except setlogsock()
|
|
|
395 |
use Sys::Syslog qw(:standard :macros); # standard functions & macros
|
|
|
396 |
|
|
|
397 |
my $syslogName = 'sysinfo-client';
|
|
|
398 |
my $logopt = 'nofatal';
|
|
|
399 |
my $facility = 'LOG_LOCAL0';
|
|
|
400 |
my $priority = 'LOG_NOTICE';
|
|
|
401 |
|
|
|
402 |
openlog( $syslogName, $logopt, $facility);
|
|
|
403 |
syslog($priority, '%s', @_ );
|
|
|
404 |
closelog();
|
|
|
405 |
} else {
|
|
|
406 |
warn "Log type $configuration{'logging'} incorrectly configured\n";
|
|
|
407 |
return 0;
|
|
|
408 |
}
|
|
|
409 |
return 1;
|
|
|
410 |
}
|
|
|
411 |
|
|
|
412 |
|
|
|
413 |
|
| 21 |
rodolico |
414 |
1;
|