| 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
|
| 249 |
rodolico |
27 |
#
|
|
|
28 |
# version 2.3.1 20241224 RWR
|
|
|
29 |
# set server info file to be in either /etc or /usr/local/etc
|
| 45 |
rodolico |
30 |
|
| 21 |
rodolico |
31 |
package sysinfoconf;
|
|
|
32 |
|
| 42 |
rodolico |
33 |
|
| 249 |
rodolico |
34 |
our $VERSION = '2.3.1';
|
| 21 |
rodolico |
35 |
use warnings;
|
| 26 |
rodolico |
36 |
use strict;
|
|
|
37 |
|
| 92 |
rodolico |
38 |
#use Data::Dumper;
|
| 144 |
rodolico |
39 |
use YAML::Tiny;
|
| 35 |
rodolico |
40 |
use File::Basename;
|
| 227 |
rodolico |
41 |
use Data::Dumper;
|
| 35 |
rodolico |
42 |
|
| 21 |
rodolico |
43 |
use Exporter;
|
|
|
44 |
|
|
|
45 |
our @ISA = qw( Exporter );
|
| 194 |
rodolico |
46 |
our @EXPORT = qw( $clientName $serialNumber $hostname
|
|
|
47 |
$transports $sysinfo3 $binDir $moduleDir
|
|
|
48 |
$scriptDir $confDir $binName $confName $configurationFile
|
|
|
49 |
@confFileSearchPath @moduleDirs @scriptDirs
|
| 228 |
rodolico |
50 |
%sendTypes %displayOrder
|
| 21 |
rodolico |
51 |
&showConf &transportsToConfig &getAnswer &yesno
|
| 257 |
rodolico |
52 |
&writeConfig &processParameters
|
| 144 |
rodolico |
53 |
&setDryRun &enableModules &makeConfig &findConf &timeStamp
|
| 197 |
rodolico |
54 |
&loadConfigurationFile &logIt &findFile
|
| 21 |
rodolico |
55 |
);
|
|
|
56 |
|
| 257 |
rodolico |
57 |
#our $TESTING = 0;
|
| 35 |
rodolico |
58 |
our $dryRun;
|
| 28 |
rodolico |
59 |
our $clientName = '';
|
|
|
60 |
our $serialNumber = '';
|
|
|
61 |
our $hostname = '';
|
| 53 |
rodolico |
62 |
|
| 227 |
rodolico |
63 |
my %serialNumberTranslation = (
|
|
|
64 |
'notspecified' => '', # not set in DMI
|
|
|
65 |
'0123456789' => '' # used by some SuperMicro computers
|
|
|
66 |
);
|
|
|
67 |
|
|
|
68 |
my %UUIDTranslation = (
|
|
|
69 |
'03000200-0400-0500-0006-000700080009' => '', # some routers
|
| 249 |
rodolico |
70 |
'not settable' => '' # not set in DMI
|
| 227 |
rodolico |
71 |
);
|
|
|
72 |
|
| 194 |
rodolico |
73 |
# paths to search for configuration file
|
|
|
74 |
my @confFileSearchPath = ( '.', '/etc/camp/sysinfo-client', '/etc/camp', '/usr/local/etc/camp/sysinfo-client' );
|
| 249 |
rodolico |
75 |
my @serverInfoFileName = ( '/etc/server.info', '/usr/local/etc/server.info' );
|
| 104 |
rodolico |
76 |
|
| 228 |
rodolico |
77 |
our %displayOrder = (
|
|
|
78 |
'1' => {
|
|
|
79 |
'fieldname' => 'clientName',
|
|
|
80 |
'display' => 'Client Name',
|
|
|
81 |
'type' => 'scalar',
|
|
|
82 |
},
|
|
|
83 |
'2' => {
|
|
|
84 |
'fieldname' => 'hostname',
|
|
|
85 |
'display' => 'Hostname',
|
|
|
86 |
'type' => 'scalar',
|
|
|
87 |
},
|
|
|
88 |
'3' => {
|
|
|
89 |
'fieldname' => 'UUID',
|
|
|
90 |
'display' => 'UUID',
|
|
|
91 |
'type' => 'scalar',
|
|
|
92 |
},
|
|
|
93 |
'4' => {
|
|
|
94 |
'fieldname' => 'serialNumber',
|
|
|
95 |
'display' => 'Serial Number',
|
|
|
96 |
'type' => 'scalar',
|
|
|
97 |
},
|
|
|
98 |
'5' => {
|
|
|
99 |
'fieldname' => 'location',
|
|
|
100 |
'display' => 'Location',
|
|
|
101 |
'type' => 'scalar',
|
|
|
102 |
},
|
|
|
103 |
'6' => {
|
|
|
104 |
'fieldname' => 'os_type',
|
|
|
105 |
'display' => 'OS Type',
|
|
|
106 |
'type' => 'scalar',
|
|
|
107 |
},
|
|
|
108 |
'7' => {
|
|
|
109 |
'fieldname' => 'tags',
|
|
|
110 |
'display' => 'Tags',
|
|
|
111 |
'type' => 'array',
|
|
|
112 |
},
|
|
|
113 |
'8' => {
|
|
|
114 |
'fieldname' => 'moduleDirs',
|
|
|
115 |
'display' => 'Module Directories',
|
|
|
116 |
'type' => 'array',
|
|
|
117 |
},
|
|
|
118 |
'9' => {
|
|
|
119 |
'fieldname' => 'scriptDirs',
|
|
|
120 |
'display' => 'Script Directories',
|
|
|
121 |
'type' => 'array',
|
|
|
122 |
},
|
|
|
123 |
'a' => {
|
|
|
124 |
'fieldname' => 'transports',
|
|
|
125 |
'display' => 'Transports',
|
|
|
126 |
'type' => 'function',
|
|
|
127 |
'function' => 'editTransports'
|
|
|
128 |
},
|
|
|
129 |
);
|
|
|
130 |
|
|
|
131 |
|
| 194 |
rodolico |
132 |
my $configurationFile = 'sysinfo-client.yaml'; # name of the configuration file
|
| 104 |
rodolico |
133 |
|
| 194 |
rodolico |
134 |
|
| 53 |
rodolico |
135 |
my @installDirs = ( '/opt/camp/sysinfo-client', '/usr/local/opt/camp/sysinfo-client' );
|
|
|
136 |
my @confDirs = ( '/etc/camp/sysinfo-client', '/usr/local/etc/camp/sysinfo-client' );
|
|
|
137 |
|
|
|
138 |
|
| 28 |
rodolico |
139 |
our @moduleDirs = ( '/opt/camp/sysinfo-client/modules', '/etc/camp/sysinfo-client/modules' );
|
|
|
140 |
our @scriptDirs = ( '/opt/camp/sysinfo-client/scripts', '/etc/camp/sysinfo-client/scripts' );
|
| 111 |
rodolico |
141 |
our $transports = {}; # holds transportation mechanisms
|
| 21 |
rodolico |
142 |
|
| 103 |
rodolico |
143 |
our $sysinfo3 = 'sysinfo-client.yaml';
|
| 21 |
rodolico |
144 |
|
| 28 |
rodolico |
145 |
our $binDir = '/opt/camp/sysinfo-client';
|
| 44 |
rodolico |
146 |
our $moduleDir = $binDir . '/modules';
|
|
|
147 |
our $scriptDir = $binDir . '/scripts';
|
| 28 |
rodolico |
148 |
our $confDir = '/etc/camp/sysinfo-client';
|
| 21 |
rodolico |
149 |
|
| 28 |
rodolico |
150 |
our $binName = 'sysinfo-client';
|
| 107 |
rodolico |
151 |
our $confName = 'sysinfo-client.yaml';
|
| 21 |
rodolico |
152 |
|
| 35 |
rodolico |
153 |
sub setDryRun {
|
|
|
154 |
$dryRun = shift;
|
|
|
155 |
}
|
| 21 |
rodolico |
156 |
|
| 28 |
rodolico |
157 |
our %sendTypes = (
|
| 21 |
rodolico |
158 |
'SendEmail' => { 'sendScript' => 'sendEmailScript',
|
|
|
159 |
'keys' =>
|
|
|
160 |
[
|
|
|
161 |
'mailTo',
|
|
|
162 |
'mailSubject',
|
|
|
163 |
'mailCC',
|
|
|
164 |
'mailBCC',
|
|
|
165 |
'mailServer',
|
|
|
166 |
'mailFrom',
|
|
|
167 |
'logFile',
|
|
|
168 |
'otherCLParams',
|
|
|
169 |
'tls',
|
|
|
170 |
'smtpUser',
|
|
|
171 |
'smtpPass',
|
|
|
172 |
'sendEmailScriptLocation'
|
|
|
173 |
],
|
|
|
174 |
},
|
|
|
175 |
'HTTP Upload' => { 'sendScript' => 'upload_http',
|
|
|
176 |
'keys' =>
|
|
|
177 |
[
|
|
|
178 |
'URL',
|
|
|
179 |
'key for report',
|
|
|
180 |
'key for date',
|
|
|
181 |
'key for hostname',
|
|
|
182 |
'key for client',
|
|
|
183 |
'key for serial number'
|
|
|
184 |
]
|
| 86 |
rodolico |
185 |
},
|
|
|
186 |
'Save Local' => { 'sendScript' => 'save_local',
|
|
|
187 |
'keys' =>
|
|
|
188 |
[
|
|
|
189 |
'output directory'
|
|
|
190 |
]
|
|
|
191 |
}
|
| 21 |
rodolico |
192 |
);
|
|
|
193 |
|
|
|
194 |
|
| 135 |
rodolico |
195 |
#######################################################
|
|
|
196 |
#
|
|
|
197 |
# timeStamp
|
|
|
198 |
#
|
|
|
199 |
# return current system date as YYYY-MM-DD HH:MM:SS
|
|
|
200 |
#
|
|
|
201 |
#######################################################
|
|
|
202 |
sub timeStamp {
|
|
|
203 |
my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst)=localtime(time);
|
| 143 |
rodolico |
204 |
return sprintf "%4d-%02d-%02d %02d:%02d:%02d",$year+1900,$mon+1,$mday,$hour,$min,$sec;
|
| 135 |
rodolico |
205 |
}
|
|
|
206 |
|
|
|
207 |
|
| 37 |
rodolico |
208 |
sub enableModules {
|
|
|
209 |
my $moduleDir = shift;
|
|
|
210 |
my %modules;
|
|
|
211 |
if ( opendir( my $dh, "$moduleDir" ) ) {
|
|
|
212 |
%modules = map{ $_ => { 'enabled' => -x "$moduleDir/$_" } }
|
|
|
213 |
grep { ! /^\./ }
|
|
|
214 |
readdir $dh;
|
|
|
215 |
closedir( $dh );
|
|
|
216 |
foreach my $filename ( keys %modules ) {
|
|
|
217 |
if ( open FILE,"<$moduleDir/$_" ) {
|
|
|
218 |
my @descript = grep{ /^# Description: (.*)/ } <FILE>;
|
|
|
219 |
close FILE;
|
|
|
220 |
chomp @descript;
|
|
|
221 |
$modules{$filename}{'description'} = $descript[0];
|
|
|
222 |
} # if
|
|
|
223 |
} # foreach
|
|
|
224 |
} # if
|
|
|
225 |
} # enableModules
|
|
|
226 |
|
| 21 |
rodolico |
227 |
sub showConf {
|
| 81 |
rodolico |
228 |
|
| 35 |
rodolico |
229 |
my $configuration = shift;
|
| 92 |
rodolico |
230 |
$configuration = Dump($configuration);
|
| 84 |
rodolico |
231 |
return $configuration;
|
| 21 |
rodolico |
232 |
}
|
|
|
233 |
|
|
|
234 |
sub transportsToConfig {
|
| 35 |
rodolico |
235 |
my $transports = shift;
|
| 21 |
rodolico |
236 |
my $config;
|
|
|
237 |
foreach my $priority ( sort { $a <=> $b } keys %$transports ) {
|
| 124 |
rodolico |
238 |
my $name = $$transports{ $priority }{'name'};
|
| 21 |
rodolico |
239 |
$config .= "# Tranport $name at priority $priority\n";
|
|
|
240 |
my $thisOne = $$transports{ $priority };
|
|
|
241 |
foreach my $key ( sort keys %$thisOne ) {
|
|
|
242 |
$config .= "\$\$transports{$priority}{'$key'} = '$$thisOne{$key}';\n";
|
|
|
243 |
} # foreach
|
|
|
244 |
} # foreach
|
|
|
245 |
return $config;
|
|
|
246 |
} # transportsToConfig
|
|
|
247 |
|
| 227 |
rodolico |
248 |
sub readServerInfo {
|
|
|
249 |
my %return;
|
| 94 |
rodolico |
250 |
|
| 249 |
rodolico |
251 |
while ( my $serverInfoName = shift ) {
|
|
|
252 |
if ( -f $serverInfoName ) {
|
|
|
253 |
print "Reading server info from $serverInfoName\n";
|
|
|
254 |
print "found $serverInfoName\n";
|
|
|
255 |
open DATA, $serverInfoName or die "Could not read $serverInfoName: $!\n";
|
|
|
256 |
print "opened $serverInfoName\n";
|
|
|
257 |
while ( my $line = <DATA> ) {
|
|
|
258 |
next if $line =~ m/^#/;
|
|
|
259 |
chomp $line;
|
|
|
260 |
next if $line =~ m/^\s*$/;
|
|
|
261 |
my ($key,$value) = split( ':', $line );
|
|
|
262 |
$key =~ s/^\s+|\s+$//g;
|
|
|
263 |
$value =~ s/^\s+|\s+$//g;
|
|
|
264 |
# note, we make the key lower case so we can find it
|
|
|
265 |
$return{ lc $key} = $value;
|
|
|
266 |
}
|
| 223 |
rodolico |
267 |
}
|
|
|
268 |
}
|
| 227 |
rodolico |
269 |
return \%return;
|
| 223 |
rodolico |
270 |
}
|
|
|
271 |
|
| 227 |
rodolico |
272 |
|
| 45 |
rodolico |
273 |
sub makeConfig {
|
| 227 |
rodolico |
274 |
my $configFile = shift;
|
| 223 |
rodolico |
275 |
my $config = {}; # make sure it is a ref to a hash so mergeHash can recognize it
|
| 249 |
rodolico |
276 |
my $serverInfo = readServerInfo( @serverInfoFileName );
|
| 227 |
rodolico |
277 |
# die Dumper( $serverInfo );
|
|
|
278 |
if ( -f $configFile ) {
|
| 218 |
rodolico |
279 |
print "Processing config file $configFile\n";
|
| 227 |
rodolico |
280 |
my $yaml = YAML::Tiny->read( $configFile );
|
|
|
281 |
$config = $yaml->[0];
|
| 45 |
rodolico |
282 |
}
|
| 227 |
rodolico |
283 |
|
|
|
284 |
# Fill in any values we are missing but which are found in $serverInfo (/etc/server.info)
|
|
|
285 |
$config->{'clientName'} = defined( $serverInfo->{'owner'} ) ? $serverInfo->{'owner'} : '' unless $config->{'clientName'};
|
|
|
286 |
$config->{'hostname'} = defined( $serverInfo->{'hostname'} ) ? $serverInfo->{'hostname'} : '' unless $config->{'hostname'};
|
|
|
287 |
$config->{'serialNumber'} = defined( $serverInfo->{'serial'} ) ? $serverInfo->{'serial'} : '' unless $config->{'serialNumber'};
|
|
|
288 |
$config->{'UUID'} = defined( $serverInfo->{'uuid'} ) ? $serverInfo->{'uuid'} : '' unless $config->{'UUID'};
|
|
|
289 |
$config->{'location'} = defined( $serverInfo->{'location'} ) ? $serverInfo->{'location'} : '' unless $config->{'location'};
|
|
|
290 |
$config->{'os_type'} = defined( $serverInfo->{'os_type'} ) ? $serverInfo->{'os_type'} : '' unless $config->{'os_type'};
|
|
|
291 |
$config->{'tags'} = defined( $serverInfo->{'tags'} ) ? [ split ',', $serverInfo->{'tags'} ] : [] unless $config->{'tags'};
|
|
|
292 |
|
|
|
293 |
# if these are still missing, we can try to run a program to get the values
|
| 218 |
rodolico |
294 |
unless ( $config->{'hostname'} ) {
|
| 227 |
rodolico |
295 |
$config->{'hostname'} = `hostname -f`;
|
|
|
296 |
chomp $config->{'hostname'};
|
| 45 |
rodolico |
297 |
}
|
| 218 |
rodolico |
298 |
unless ( $config->{'serialNumber'} ) {
|
| 227 |
rodolico |
299 |
$config->{'serialNumber'} = `dmidecode -s system-serial-number` if `which dmidecode`;
|
|
|
300 |
chomp $config->{'serialNumber'};
|
|
|
301 |
$config->{'serialNumber'} =~ s/\s//gi;
|
|
|
302 |
$config->{'serialNumber'} = $serialNumberTranslation{lc($config->{'serialNumber'})} if defined( $serialNumberTranslation{lc($config->{'serialNumber'})} );
|
| 53 |
rodolico |
303 |
}
|
| 218 |
rodolico |
304 |
unless ( $config->{'UUID'} ) {
|
| 227 |
rodolico |
305 |
$config->{'UUID'} = `which dmidecode` ? `dmidecode -t 1 | grep -i uuid | cut -d':' -f2` : '' if `which dmidecode`;
|
|
|
306 |
$config->{'UUID'} =~ s/\s//gi;
|
|
|
307 |
$config->{'UUID'} = $UUIDTranslation{lc($config->{'UUID'})} if defined( $UUIDTranslation{lc($config->{'UUID'})} );
|
| 77 |
rodolico |
308 |
}
|
| 227 |
rodolico |
309 |
unless ( $config->{'os_type'} ) {
|
|
|
310 |
$config->{'os_type'} = `installer/determineOS` if -x 'installer/determineOS';
|
|
|
311 |
}
|
| 219 |
rodolico |
312 |
|
| 228 |
rodolico |
313 |
unless ( $config->{'moduleDirs'} ) {
|
|
|
314 |
$config->{'moduleDirs'} = [];
|
|
|
315 |
push @{ $config->{'moduleDirs'} }, $binDir . '/modules';
|
|
|
316 |
push @{ $config->{'moduleDirs'} }, $confDir . '/modules';
|
|
|
317 |
}
|
|
|
318 |
|
|
|
319 |
unless ( $config->{'scriptDirs'} ) {
|
|
|
320 |
$config->{'scriptDirs'} = [];
|
|
|
321 |
push @{ $config->{'scriptDirs'} }, $binDir . '/scripts';
|
|
|
322 |
push @{ $config->{'scriptDirs'} }, $confDir . '/scripts';
|
|
|
323 |
}
|
|
|
324 |
|
|
|
325 |
unless ( $config->{'transports'} ) {
|
|
|
326 |
$config->{'transports'}->{'30'} = {
|
|
|
327 |
'name' => 'SaveLocal',
|
|
|
328 |
'output directory' => '/tmp',
|
|
|
329 |
'sendScript' => 'save_local'
|
|
|
330 |
}
|
|
|
331 |
}
|
|
|
332 |
|
| 218 |
rodolico |
333 |
return $config;
|
| 45 |
rodolico |
334 |
}
|
|
|
335 |
|
| 21 |
rodolico |
336 |
# prompt the user for a response, then allow them to enter it
|
|
|
337 |
# the first response is considered the default and is printed
|
|
|
338 |
# in all caps if more than one exists
|
|
|
339 |
# first answer is used if they simply press the Enter
|
|
|
340 |
# key. The response is returned all lower case if more than one
|
|
|
341 |
# exists.
|
|
|
342 |
# it is assumed
|
|
|
343 |
sub getAnswer {
|
|
|
344 |
my ( $prompt, @answers ) = @_;
|
|
|
345 |
$answers[0] = '' unless defined( $answers[0] );
|
|
|
346 |
my $default = $answers[0];
|
|
|
347 |
my $onlyOneAnswer = scalar( @answers ) == 1;
|
|
|
348 |
print $prompt . '[ ';
|
|
|
349 |
$answers[0] = uc $answers[0] unless $onlyOneAnswer;
|
|
|
350 |
print join( ' | ', @answers ) . ' ]: ';
|
| 28 |
rodolico |
351 |
my $thisAnswer = <>;
|
| 21 |
rodolico |
352 |
chomp $thisAnswer;
|
|
|
353 |
$thisAnswer = $default unless $thisAnswer;
|
|
|
354 |
return $thisAnswer;
|
|
|
355 |
}
|
|
|
356 |
|
|
|
357 |
sub yesno {
|
| 40 |
rodolico |
358 |
my ( $prompt, $default ) = @_;
|
|
|
359 |
$default = 'yes' unless $default;
|
|
|
360 |
my $answer = &getAnswer( $prompt, $default eq 'yes' ? ('yes','no' ) : ('no', 'yes') );
|
| 21 |
rodolico |
361 |
return lc( substr( $answer, 0, 1 ) ) eq 'y';
|
|
|
362 |
}
|
|
|
363 |
|
|
|
364 |
sub writeConfig {
|
| 132 |
rodolico |
365 |
use File::Temp qw / tempfile /;
|
| 35 |
rodolico |
366 |
my ( $filename,$content ) = @_;
|
| 129 |
rodolico |
367 |
if ( $filename ) { # they sent us a filename
|
|
|
368 |
my $path;
|
|
|
369 |
($filename, $path ) = fileparse( $filename );
|
|
|
370 |
`mkdir -p $path` unless -d $path;
|
|
|
371 |
$filename = $path . '/' . $filename;
|
| 21 |
rodolico |
372 |
`cp $filename $filename.bak` if ( -e $filename );
|
| 129 |
rodolico |
373 |
unless ( $dryRun ) {
|
|
|
374 |
open CONF,">$filename" or die "Could not write to $filename: $!\n";
|
|
|
375 |
print CONF $content;
|
|
|
376 |
close CONF;
|
|
|
377 |
`chmod 600 $filename`;
|
|
|
378 |
}
|
|
|
379 |
} else { # no path provided, so just create a temp file
|
|
|
380 |
# we will create a temporary file and return the name
|
|
|
381 |
# it is the calling programs responsiblity to remove the file
|
|
|
382 |
my $fh;
|
|
|
383 |
($fh,$filename) = tempfile( UNLINK => 0 );
|
|
|
384 |
print $fh $content;
|
|
|
385 |
close $fh
|
| 21 |
rodolico |
386 |
}
|
| 129 |
rodolico |
387 |
return $filename;
|
| 21 |
rodolico |
388 |
}
|
|
|
389 |
|
|
|
390 |
sub processParameters {
|
|
|
391 |
while ( my $parameter = shift ) {
|
|
|
392 |
if ( $parameter eq '-v' ) {
|
| 26 |
rodolico |
393 |
print "$VERSION\n";
|
| 21 |
rodolico |
394 |
exit;
|
|
|
395 |
}
|
|
|
396 |
} # while
|
|
|
397 |
}
|
|
|
398 |
|
| 53 |
rodolico |
399 |
sub findConf {
|
|
|
400 |
my $confName = shift;
|
|
|
401 |
for ( my $i = 0; $i < @confDirs; $i++ ) {
|
|
|
402 |
if ( -d $confDirs[ $i ] ) {
|
| 55 |
rodolico |
403 |
return ( $confDirs[$i], $confName );
|
| 53 |
rodolico |
404 |
}
|
|
|
405 |
}
|
| 104 |
rodolico |
406 |
return ( '', $confName );
|
| 53 |
rodolico |
407 |
}
|
| 21 |
rodolico |
408 |
|
| 194 |
rodolico |
409 |
#######################################################
|
|
|
410 |
#
|
|
|
411 |
# findFile( $filename, @directories )
|
|
|
412 |
#
|
|
|
413 |
# Locates a file by searching sequentially in one or more
|
|
|
414 |
# directories, returning the first one found
|
|
|
415 |
#
|
|
|
416 |
# Returns '' if not found
|
|
|
417 |
#
|
|
|
418 |
#######################################################
|
| 104 |
rodolico |
419 |
|
| 194 |
rodolico |
420 |
sub findFile {
|
|
|
421 |
my ( $filename, $directories ) = @_;
|
| 256 |
rodolico |
422 |
#&logIt( 3, "Looking for $filename in findFile" );
|
| 194 |
rodolico |
423 |
for ( my $i = 0; $i < scalar( @{$directories} ); $i++ ) {
|
|
|
424 |
my $confFile = $$directories[$i] . '/' . $filename;
|
| 256 |
rodolico |
425 |
#&logIt( 4, "Looking for $filename in $confFile" );
|
| 194 |
rodolico |
426 |
return $confFile if ( -f $confFile );
|
|
|
427 |
}
|
|
|
428 |
return '';
|
|
|
429 |
}
|
|
|
430 |
|
| 104 |
rodolico |
431 |
|
| 194 |
rodolico |
432 |
#######################################################
|
|
|
433 |
#
|
|
|
434 |
# loadConfigurationFile($confFile)
|
|
|
435 |
#
|
|
|
436 |
# Loads configuration file defined by $configurationFile, and dies if not available
|
|
|
437 |
# This is a YAML file containing serialized contents of
|
|
|
438 |
# Parameters:
|
|
|
439 |
# $$fileName - name of file to look for (reference)
|
|
|
440 |
# @searchPath - array of paths to find $filename
|
|
|
441 |
#
|
|
|
442 |
#######################################################
|
| 104 |
rodolico |
443 |
|
| 194 |
rodolico |
444 |
sub loadConfigurationFile {
|
|
|
445 |
my ( $fileName, @searchPath ) = @_;
|
|
|
446 |
$$fileName = $configurationFile unless $$fileName;
|
|
|
447 |
@searchPath = @confFileSearchPath unless @searchPath;
|
| 256 |
rodolico |
448 |
#&logIt( '', 2, "Looking for config file $$fileName in " . join( ', ', @searchPath ) );
|
| 194 |
rodolico |
449 |
my $confFile;
|
|
|
450 |
if ( $confFile = &findFile( $$fileName, \@searchPath ) ) {
|
| 256 |
rodolico |
451 |
#&logIt( '', 3, "Opening configuration from $confFile" );
|
| 194 |
rodolico |
452 |
my $yaml = YAML::Tiny->read( $confFile );
|
| 256 |
rodolico |
453 |
#&logIt( '', 4, "Configuration file contents\n$yaml" );
|
| 194 |
rodolico |
454 |
$$fileName = $confFile;
|
|
|
455 |
return $yaml->[0];
|
|
|
456 |
}
|
|
|
457 |
die "Can not find $fileName in any of " . join( "\n\t", @searchPath ) . "\n";
|
|
|
458 |
}
|
|
|
459 |
|
|
|
460 |
|
|
|
461 |
#######################################################
|
|
|
462 |
# function to simply log things
|
|
|
463 |
# first parameter is the priority, if <= $logDef->{'log level'} will print
|
|
|
464 |
# all subsequent parameters assumed to be strings to sent to the log
|
|
|
465 |
# returns 0 on failure
|
|
|
466 |
# 1 on success
|
|
|
467 |
# 2 if priority > log level
|
|
|
468 |
# -1 if $logDef is unset
|
|
|
469 |
# currently, only logs to a file
|
|
|
470 |
#######################################################
|
|
|
471 |
sub logIt {
|
| 256 |
rodolico |
472 |
my $configuration = shift;
|
| 194 |
rodolico |
473 |
my $priority = shift;
|
|
|
474 |
|
|
|
475 |
# turn off variable checking so it doesn't blow up on lack of %configuration file
|
| 256 |
rodolico |
476 |
#no strict 'vars';
|
|
|
477 |
return -1 unless exists $configuration->{'logging'};
|
|
|
478 |
return 2 unless $priority <= $configuration->{'logging'}->{'log level'};
|
|
|
479 |
if ( $configuration->{'logging'}->{'log type'} eq 'cache' ) {
|
|
|
480 |
push @{ $configuration->{'logging'}->{'cache'} }, @_;
|
| 194 |
rodolico |
481 |
return;
|
| 256 |
rodolico |
482 |
} elsif ( defined( $configuration->{'logging'}->{'cache'} ) ) {
|
|
|
483 |
unshift @_, @{ $configuration->{'logging'}->{'cache'} };
|
|
|
484 |
delete $configuration->{'logging'}->{'cache'};
|
| 194 |
rodolico |
485 |
}
|
| 256 |
rodolico |
486 |
if ( $configuration->{'logging'}->{'log type'} eq 'file' ) {
|
|
|
487 |
if ( open LOG, '>>' . $configuration->{'logging'}->{'log path'} ) {
|
| 194 |
rodolico |
488 |
while ( my $t = shift ) {
|
|
|
489 |
print LOG &timeStamp() . "\t$t\n";
|
|
|
490 |
}
|
|
|
491 |
close LOG;
|
| 256 |
rodolico |
492 |
} else {
|
|
|
493 |
warn "Could not write to " . $configuration->{'logging'}->{'log path'} . "\n";
|
| 194 |
rodolico |
494 |
}
|
| 256 |
rodolico |
495 |
} elsif ( $configuration->{'logging'}->{'log type'} eq 'syslog' ) {
|
| 194 |
rodolico |
496 |
use Sys::Syslog; # all except setlogsock()
|
|
|
497 |
use Sys::Syslog qw(:standard :macros); # standard functions & macros
|
|
|
498 |
|
|
|
499 |
my $syslogName = 'sysinfo-client';
|
|
|
500 |
my $logopt = 'nofatal';
|
|
|
501 |
my $facility = 'LOG_LOCAL0';
|
|
|
502 |
my $priority = 'LOG_NOTICE';
|
|
|
503 |
|
|
|
504 |
openlog( $syslogName, $logopt, $facility);
|
|
|
505 |
syslog($priority, '%s', @_ );
|
|
|
506 |
closelog();
|
|
|
507 |
} else {
|
| 256 |
rodolico |
508 |
warn "Log type $configuration->{'logging'} incorrectly configured\n";
|
| 194 |
rodolico |
509 |
return 0;
|
|
|
510 |
}
|
|
|
511 |
return 1;
|
|
|
512 |
}
|
|
|
513 |
|
|
|
514 |
|
|
|
515 |
|
| 21 |
rodolico |
516 |
1;
|