* Updated Words->read to default to 'path::words::words.xml' when the 'file' parameter is not passed. Also updated it to check to see if the words file was read before and, if so, clear the data from the previous read before re-reading it.

* Updated anvil-daemon to re-read the main words file on each loop.
* Updated scancore to read and purge each scan agent's words file between invocations.

Signed-off-by: Digimer <digimer@alteeve.ca>
main
Digimer 6 years ago
parent 946fce018a
commit 8fad67fc5a
  1. 2
      Anvil/Tools.pm
  2. 16
      Anvil/Tools/Words.pm
  3. 2
      share/words.xml
  4. 1
      tools/anvil-daemon
  5. 67
      tools/scancore
  6. 1
      tools/scancore.README

@ -222,7 +222,7 @@ sub new
$anvil->Storage->search_directories({debug => $debug, initialize => 1});
# I need to read the initial words early.
$anvil->Words->read({debug => $debug, file => $anvil->data->{path}{words}{'words.xml'}});
$anvil->Words->read({debug => $debug});
# If the local './tools.conf' file exists, read it in.
if (-r $anvil->data->{path}{configs}{'anvil.conf'})

@ -357,7 +357,7 @@ This reads in a words file containing translated strings used to generated outpu
Example to read 'C<< anvil.xml >>';
my $words_file = $anvil->data->{path}{words}{'an-words.xml'};
my $words_file = $anvil->data->{path}{words}{'words.xml'};
my $anvil->Words->read({file => $words_file}) or die "Failed to read: [$words_file]. Does the file exist?\n";
Successful read will return '0'. Non-0 is an error;
@ -371,9 +371,11 @@ NOTE: Read works are stored in 'C<< $anvil->data->{words}{<file_name>}{language}
Parameters;
=head3 file (required)
=head3 file (optional, default 'path::words::words.xml')
This is the file to read.
This is the XML "words" file to read.
NOTE: When reading the default words file, all existing words are cleared from memory to avoid stale strings hanging around.
=cut
sub read
@ -385,7 +387,7 @@ sub read
# Setup default values
my $return_code = 0;
my $file = defined $parameter->{file} ? $parameter->{file} : 0;
my $file = defined $parameter->{file} ? $parameter->{file} : $anvil->data->{path}{words}{'words.xml'};
$anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => $debug, list => { file => $file }});
if (not $file)
@ -408,6 +410,12 @@ sub read
}
else
{
# If we've read this file before, delete what we had loaded so that no stale keys remain.
if (exists $anvil->data->{words}{$file})
{
delete $anvil->data->{words}{$file};
}
# Read the file with XML::Simple
my $xml = XML::Simple->new();
eval { $anvil->data->{words}{$file} = $xml->XMLin($file, KeyAttr => { language => 'name', key => 'name' }, ForceArray => [ 'language', 'key' ]) };

@ -530,7 +530,7 @@ The body of the file: [#!variable!file!#] does not match the new body. The file
<key name="log_0245">[ Cleared ] - We now have databases accessible, proceeding.</key>
<key name="log_0246">[ Warning ] - The local system is not yet configured. Scancore will check once a minute and start running once configured.</key>
<key name="log_0247">[ Cleared ] - The local system is now configured, proceeding.</key>
<key name="log_0248">ScanCore has entered the main loop.</key>
<key name="log_0248">ScanCore is entering the main loop now.</key>
<key name="log_0249">----=] ScanCore loop finished. Sleeping for: [#!variable!run_interval!#] seconds. ]=--------------------------------------</key> <!-- This is meant to be easily seen in the logs, hence the dashes. -->
<key name="log_0250">
The md5sum of: [#!variable!file!#] has changed since the daemon started.

@ -122,6 +122,7 @@ while(1)
$anvil->_set_paths();
$anvil->_set_defaults();
$anvil->Storage->read_config();
$anvil->Words->read();
$anvil->Database->connect({check_if_configured => $check_if_database_is_configured});
$anvil->Log->entry({source => $THIS_FILE, line => __LINE__, level => 3, secure => 0, key => "log_0132"});

@ -50,16 +50,13 @@ $anvil->Database->disconnect();
$anvil->Log->entry({source => $THIS_FILE, line => __LINE__, 'print' => 1, level => 2, key => "log_0203"});
# The main loop
$anvil->Log->entry({source => $THIS_FILE, line => __LINE__, 'print' => 1, level => 2, key => "log_0248"});
while(1)
{
# Reload defaults, re-read the config and then connect to the database(s)
$anvil->Log->entry({source => $THIS_FILE, line => __LINE__, 'print' => 1, level => 2, key => "log_0248"});
$anvil->_set_paths();
$anvil->_set_defaults();
$anvil->Storage->read_config();
$anvil->Database->connect();
$anvil->Log->entry({source => $THIS_FILE, line => __LINE__, 'print' => 1, level => 1, key => "log_0132"});
# Do the various pre-run tasks.
prepare_for_run($anvil);
# Do we have at least one database?
if ($anvil->data->{sys}{database}{connections})
{
# Run the normal tasks
@ -87,8 +84,10 @@ while(1)
$anvil->nice_exit({code => 0});
}
# Disconnect from the database(s) and sleep now.
$anvil->Database->disconnect();
# Clean up
cleanup_after_run($anvil);
# Sleep until it's time to run again.
my $run_interval = 30;
if ((exists $anvil->data->{scancore}{timing}{run_interval}) && ($anvil->data->{scancore}{timing}{run_interval} =~ /^\d+$/))
{
@ -113,15 +112,48 @@ sub call_agents
{
my ($anvil) = @_;
# Get the list of scan agents on this system.
# Get the current list of scan agents on this system.
$anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => 2, list => {
"path::directories::scan_agents" => $anvil->data->{path}{directories}{scan_agents},
}});
scan_directory($anvil, $anvil->data->{path}{directories}{scan_agents});
# Now loop through the agents I found and call them.
foreach my $agent_name (sort {$a cmp $b} keys %{$anvil->data->{scancore}{agent}})
{
my $agent_path = $anvil->data->{scancore}{agent}{$agent_name};
my $agent_words = $agent_path.".xml";
if ((-e $agent_words) && (-r $agent_words))
{
# Read the words file so that we can generate alerts later.
$anvil->Words->read({file => $agent_words});
}
}
return(0);
}
# This cleans things up after a scan run has completed.
sub cleanup_after_run
{
my ($anvil) = @_;
# Delete what we know about existing scan agents so that the next scan freshens the data.
foreach my $agent_name (sort {$a cmp $b} keys %{$anvil->data->{scancore}{agent}})
{
# Remove the agent's words file data.
my $agent_words = $anvil->data->{scancore}{agent}{$agent_name}.".xml";
delete $anvil->data->{words}{$agent_words};
}
# Disconnect from the database(s) and sleep now.
$anvil->Database->disconnect();
# Now delete all the remaining agent data.
delete $anvil->data->{scancore}{agent};
}
# This checks to see if any files on disk have changed and, if so, exits.
sub exit_if_sums_changed
{
@ -137,6 +169,21 @@ sub exit_if_sums_changed
return(0);
}
# Handle pre-run tasks.
sub prepare_for_run
{
my ($anvil) = @_;
# Reload defaults, re-read the config and then connect to the database(s)
$anvil->_set_paths();
$anvil->_set_defaults();
$anvil->Storage->read_config();
$anvil->Words->read();
$anvil->Database->connect();
$anvil->Log->entry({source => $THIS_FILE, line => __LINE__, 'print' => 1, level => 1, key => "log_0132"});
return(0);
}
# This looks in the passed-in directory for scan agents or sub-directories (which will in turn be scanned).
sub scan_directory
{

@ -63,5 +63,6 @@ and we will be happy to assist however we can.
Any strings used for logging or sending "alerts" to notification recipients are found in this file. Please
see 'words.xml' for more information on how this file is structured.
NOTE: This file MUST be the same as the agent itself, with the file extension '.xml'.
NOTE: To avoid namespace collisions, it is STRONGLY recommended that all string keys start with your agent
name! Ie: 'scan_network_X'.

Loading…
Cancel
Save