This commit adds scan agent DB connection info caching to help minimize the number of unnecessary DB resync checks that happen.

* Created ScanCore->agent_shutdown() that writes out the time the scan agent last ran, and how many databases were available when it last ran.
* Updated ScanCore->agent_startup() to read the the last run data created above.
* Updated Database->connect() to set 'sys::database::last_db_count' to the scan agent's recorded last DB count.
* Updated all agents to call ScanCore->agent_shutdown() at the end of their run.

Signed-off-by: Digimer <digimer@alteeve.ca>
main
Digimer 3 years ago
parent f5f55d70b3
commit 15d8309095
  1. 1
      Anvil/Tools.pm
  2. 15
      Anvil/Tools/Database.pm
  3. 89
      Anvil/Tools/ScanCore.pm
  4. 7
      scancore-agents/scan-apc-pdu/scan-apc-pdu
  5. 11
      scancore-agents/scan-apc-ups/scan-apc-ups
  6. 6
      scancore-agents/scan-cluster/scan-cluster
  7. 7
      scancore-agents/scan-drbd/scan-drbd
  8. 5
      scancore-agents/scan-filesystems/scan-filesystems
  9. 5
      scancore-agents/scan-hardware/scan-hardware
  10. 7
      scancore-agents/scan-hpacucli/scan-hpacucli
  11. 7
      scancore-agents/scan-ipmitool/scan-ipmitool
  12. 5
      scancore-agents/scan-lvm/scan-lvm
  13. 7
      scancore-agents/scan-network/scan-network
  14. 5
      scancore-agents/scan-server/scan-server
  15. 7
      scancore-agents/scan-storcli/scan-storcli

@ -1092,6 +1092,7 @@ sub _set_paths
status => "/var/www/html/status",
syslinux => "/usr/share/syslinux",
tftpboot => "/var/lib/tftpboot",
temp => "/tmp/anvil",
tools => "/usr/sbin",
units => "/usr/lib/systemd/system",
},

@ -1732,7 +1732,7 @@ sub connect
# }
}
# Make sure my host UUID is valod
# Make sure my host UUID is valid
$anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => $debug, list => { "sys::host_uuid" => $anvil->data->{sys}{host_uuid} }});
if ($anvil->data->{sys}{host_uuid} !~ /^[a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12}$/)
{
@ -1753,6 +1753,19 @@ sub connect
return($anvil->data->{sys}{database}{connections});
}
if (exists $anvil->data->{'log'}{scan_agent})
{
my $agent = $anvil->data->{'log'}{scan_agent};
$anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => $debug, list => { agent => $agent }});
if (exists $anvil->data->{scan_agent}{$agent}{last_db_count})
{
$anvil->data->{sys}{database}{last_db_count} = $anvil->data->{scan_agent}{$agent}{last_db_count};
$anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => $debug, list => {
"sys::database::last_db_count" => $anvil->data->{sys}{database}{last_db_count},
}});
}
}
# If we have a previous count and the new count is higher, resync.
if (exists $anvil->data->{sys}{database}{last_db_count})
{

@ -15,6 +15,7 @@ our $VERSION = "3.0.0";
my $THIS_FILE = "ScanCore.pm";
### Methods;
# agent_shutdown
# agent_startup
# call_scan_agents
# check_health
@ -88,6 +89,57 @@ sub parent
#############################################################################################################
=head2 agent_shutdown
This method handles recording run data to the agent's data file.
Parameters;
=head3 agent (required)
This is the name of the scan agent. Usually this can be set as C<< $THIS_FILE >>.
=cut
sub agent_shutdown
{
my $self = shift;
my $parameter = shift;
my $anvil = $self->parent;
my $debug = defined $parameter->{debug} ? $parameter->{debug} : 3;
$anvil->Log->entry({source => $THIS_FILE, line => __LINE__, level => $debug, key => "log_0125", variables => { method => "ScanCore->agent_shutdown()" }});
my $agent = defined $parameter->{agent} ? $parameter->{agent} : "";
$anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => $debug, list => {
agent => $agent,
}});
# Setting this will prepend messages coming grom the agent with the agent's name
$anvil->data->{'log'}{scan_agent} = $agent;
# If this agent ran before, it should have recorded how many databases it last connected to. Read
# that, if so.
my $data_file = $anvil->data->{path}{directories}{temp}."/".$agent.".data";
$anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => $debug, list => { data_file => $data_file }});
my $file_body = "last_run:".time."\n";
$file_body .= "last_db_count:".$anvil->data->{sys}{database}{connections}."\n";
my $error = $anvil->Storage->write_file({
debug => $debug,
file => $data_file,
body => $file_body,
overwrite => 1,
backup => 0,
});
$anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => $debug, list => { error => $error }});
# Mark that we ran.
$anvil->Database->insert_or_update_updated({updated_by => $agent});
$anvil->nice_exit({exit_code => 0});
return(0);
}
=head2 agent_startup
This method handles connecting to the databases, loading the agent's schema, resync'ing database tables if needed and reading in the words files.
@ -129,6 +181,41 @@ sub agent_startup
# Setting this will prepend messages coming grom the agent with the agent's name
$anvil->data->{'log'}{scan_agent} = $agent;
# If this agent ran before, it should have recorded how many databases it last connected to. Read
# that, if so.
my $data_file = $anvil->data->{path}{directories}{temp}."/".$agent.".data";
$anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => $debug, list => { data_file => $data_file }});
$anvil->data->{scan_agent}{$agent}{last_run} = "";
$anvil->data->{scan_agent}{$agent}{last_db_count} = 0;
if (-f $data_file)
{
my $file_body = $anvil->Storage->read_file({
debug => $debug,
file => $data_file,
});
$anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => $debug, list => { file_body => $file_body }});
foreach my $line (split/\n/, $file_body)
{
if ($line =~ /^last_run:(\d+)/)
{
$anvil->data->{scan_agent}{$agent}{last_run} = $1;
$anvil->data->{scan_agent}{$agent}{time_since_last_run} = time - $anvil->data->{scan_agent}{$agent}{last_run};
$anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => $debug, list => {
"scan_agent::${agent}::last_run" => $anvil->data->{scan_agent}{$agent}{last_run},
"scan_agent::${agent}::time_since_last_run" => $anvil->data->{scan_agent}{$agent}{time_since_last_run},
}});
}
if ($line =~ /^last_db_count:(\d+)/)
{
$anvil->data->{scan_agent}{$agent}{last_db_count} = $1;
$anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => $debug, list => {
"scan_agent::${agent}::last_db_count" => $anvil->data->{scan_agent}{$agent}{last_db_count},
}});
}
}
}
$anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => $debug, list => { tables => $tables }});
if ((not $tables) or (ref($tables) ne "ARRAY"))
{
@ -160,7 +247,7 @@ sub agent_startup
}
# Connect to DBs.
$anvil->Database->connect({debug => $debug});
$anvil->Database->connect({debug => 2});
$anvil->Log->entry({source => $agent, line => __LINE__, level => $debug, secure => 0, key => "log_0132"});
if (not $anvil->data->{sys}{database}{connections})
{

@ -212,11 +212,8 @@ gather_pdu_data($anvil);
# Look for changes.
find_changes($anvil);
# Update the database
$anvil->Database->insert_or_update_updated({updated_by => $THIS_FILE});
# Clean up and go away.
$anvil->nice_exit({exit_code => 0});
# Shut down.
$anvil->ScanCore->agent_shutdown({agent => $THIS_FILE});
#############################################################################################################

@ -239,15 +239,8 @@ gather_ups_data($anvil);
# Look for changes.
find_changes($anvil);
# Update the database
my $updated_uuid = $anvil->Database->insert_or_update_updated({
debug => 2,
updated_by => $THIS_FILE,
});
$anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => 2, list => { updated_uuid => $updated_uuid }});
# Clean up and go away.
$anvil->nice_exit({exit_code => 0});
# Shut down.
$anvil->ScanCore->agent_shutdown({agent => $THIS_FILE});
#############################################################################################################

@ -106,12 +106,10 @@ check_config($anvil);
# Check the fence delay
check_fence_delay($anvil);
# Check that the location constraint is sensible.
$anvil->Cluster->check_server_constraints();
# Shut down.
$anvil->ScanCore->agent_shutdown({agent => $THIS_FILE});
$anvil->nice_exit({exit_code => 0});
#############################################################################################################
# Functions #
#############################################################################################################

@ -103,11 +103,8 @@ find_changes($anvil);
check_config($anvil);
# Update the database
$anvil->Database->insert_or_update_updated({updated_by => $THIS_FILE});
# Clean up and go away.
$anvil->nice_exit({exit_code => 0});
# Shut down.
$anvil->ScanCore->agent_shutdown({agent => $THIS_FILE});
#############################################################################################################

@ -92,10 +92,9 @@ read_last_scan($anvil);
# Loog for changes
find_changes($anvil);
# Mark that we ran.
$anvil->Database->insert_or_update_updated({updated_by => $THIS_FILE});
# Shut down.
$anvil->ScanCore->agent_shutdown({agent => $THIS_FILE});
$anvil->nice_exit({exit_code => 0});
#############################################################################################################
# Functions #

@ -96,10 +96,9 @@ find_changes($anvil);
# Finally, process health weights.
process_health($anvil);
# Mark that we ran.
$anvil->Database->insert_or_update_updated({updated_by => $THIS_FILE});
# Shut down.
$anvil->ScanCore->agent_shutdown({agent => $THIS_FILE});
$anvil->nice_exit({exit_code => 0});
#############################################################################################################
# Functions #

@ -212,11 +212,8 @@ process_temperatures($anvil);
# Finally, process health weights.
process_health($anvil);
# Update the database
$anvil->Database->insert_or_update_updated({updated_by => $THIS_FILE});
# Clean up and go away.
$anvil->nice_exit({exit_code => 0});
# Shut down.
$anvil->ScanCore->agent_shutdown({agent => $THIS_FILE});
#############################################################################################################

@ -248,11 +248,8 @@ find_changes($anvil);
# Finally, process health weights.
process_health($anvil);
# Update the database
$anvil->Database->insert_or_update_updated({updated_by => $THIS_FILE});
# Clean up and go away.
$anvil->nice_exit({exit_code => 0});
# Shut down.
$anvil->ScanCore->agent_shutdown({agent => $THIS_FILE});
#############################################################################################################

@ -91,10 +91,9 @@ read_last_scan($anvil);
# Loog for changes
find_changes($anvil);
# Mark that we ran.
$anvil->Database->insert_or_update_updated({updated_by => $THIS_FILE});
# Shut down.
$anvil->ScanCore->agent_shutdown({agent => $THIS_FILE});
$anvil->nice_exit({exit_code => 0});
#############################################################################################################
# Functions #

@ -60,7 +60,7 @@ if (($anvil->data->{scancore}{'scan-hardware'}{disable}) && (not $anvil->data->{
}
# Handle start-up tasks
my $problem = $anvil->ScanCore->agent_startup({agent => $THIS_FILE});
my $problem = $anvil->ScanCore->agent_startup({debug => 2, agent => $THIS_FILE});
if ($problem)
{
$anvil->nice_exit({exit_code => 1});
@ -90,10 +90,9 @@ find_changes($anvil);
# Finally, process health weights.
process_health($anvil);
# Mark that we ran.
$anvil->Database->insert_or_update_updated({updated_by => $THIS_FILE});
# Shut down.
$anvil->ScanCore->agent_shutdown({debug => 2, agent => $THIS_FILE});
$anvil->nice_exit({exit_code => 0});
#############################################################################################################
# Functions #

@ -94,10 +94,9 @@ record_migration_times($anvil);
# Check if we need to update the websocket stuff.
check_vnc($anvil);
# Mark that we ran.
$anvil->Database->insert_or_update_updated({updated_by => $THIS_FILE});
# Shut down.
$anvil->ScanCore->agent_shutdown({agent => $THIS_FILE});
$anvil->nice_exit({exit_code => 0});
#############################################################################################################
# Functions #

@ -260,11 +260,8 @@ process_temperatures($anvil);
# Finally, process health weights.
process_health($anvil);
# Update the database
$anvil->Database->insert_or_update_updated({updated_by => $THIS_FILE});
# Clean up and go away.
$anvil->nice_exit({exit_code => 0});
# Shut down.
$anvil->ScanCore->agent_shutdown({agent => $THIS_FILE});
#############################################################################################################

Loading…
Cancel
Save