From 0370d6acef5e57ba7bf85da5c599926772531d71 Mon Sep 17 00:00:00 2001 From: Digimer Date: Sat, 24 Mar 2018 09:57:10 -0400 Subject: [PATCH] * Fixed a bug in the network_interfaces SQL procedure that broke how data was recorded in the history schema. * Added 'file' and 'line' arguments to the Database->insert_or_update_X methods to allow for the original caller's file and line number to be recorded in the SQL call logs. * Cleaned up how logging to 'anvil.log' logging is handled. * Updated anvil-update-states to ignore libvirt bridges and to manually set the speed and duplex of virtio network based interfaces. Signed-off-by: Digimer --- Anvil/Tools/Database.pm | 186 +++++++++++++++++++++++++++----------- Anvil/Tools/Log.pm | 15 ++- cgi-bin/home | 9 +- share/words.xml | 8 +- tools/anvil-prep-database | 6 +- tools/anvil-update-states | 28 +++++- tools/anvil.sql | 6 +- 7 files changed, 185 insertions(+), 73 deletions(-) diff --git a/Anvil/Tools/Database.pm b/Anvil/Tools/Database.pm index e9058ecc..2ce07fed 100755 --- a/Anvil/Tools/Database.pm +++ b/Anvil/Tools/Database.pm @@ -1229,7 +1229,7 @@ FROM =head2 get_local_id -This returns the database ID from 'C<< striker.conf >>' based on matching the 'C<< database::::host >>' to the local machine's host name or one of the active IP addresses on the host. +This returns the database ID from 'C<< anvil.conf >>' based on matching the 'C<< database::::host >>' to the local machine's host name or one of the active IP addresses on the host. # Get the local ID my $local_id = $anvil->Database->get_local_id; @@ -1395,6 +1395,18 @@ If there is an error, an empty string is returned. Parameters; +=head3 id (optional) + +If set, only the corresponding database will be written to. + +=head3 file (optional) + +If set, this is the file name logged as the source of any INSERTs or UPDATEs. + +=head3 line (optional) + +If set, this is the file line number logged as the source of any INSERTs or UPDATEs. + =head3 host_name (required) This default value is the local hostname. @@ -1407,10 +1419,6 @@ This default value is the value returned by C<< System->determine_host_type >>. The default value is the host's UUID (as returned by C<< Get->host_uuid >>. -=head3 id (optional) - -If set, only the corresponding database will be written to. - =cut sub insert_or_update_hosts { @@ -1420,15 +1428,19 @@ sub insert_or_update_hosts my $debug = defined $parameter->{debug} ? $parameter->{debug} : 3; $anvil->Log->entry({source => $THIS_FILE, line => __LINE__, level => $debug, key => "log_0125", variables => { method => "Database->insert_or_update_hosts()" }}); - my $host_name = $parameter->{host_name} ? $parameter->{host_name} : $anvil->_hostname; - my $host_type = $parameter->{host_type} ? $parameter->{host_type} : $anvil->System->determine_host_type; - my $host_uuid = $parameter->{host_uuid} ? $parameter->{host_uuid} : $anvil->Get->host_uuid; - my $id = $parameter->{id} ? $parameter->{id} : ""; + my $id = defined $parameter->{id} ? $parameter->{id} : ""; + my $file = defined $parameter->{file} ? $parameter->{file} : ""; + my $line = defined $parameter->{line} ? $parameter->{line} : ""; + my $host_name = defined $parameter->{host_name} ? $parameter->{host_name} : $anvil->_hostname; + my $host_type = defined $parameter->{host_type} ? $parameter->{host_type} : $anvil->System->determine_host_type; + my $host_uuid = defined $parameter->{host_uuid} ? $parameter->{host_uuid} : $anvil->Get->host_uuid; $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => $debug, list => { + id => $id, + file => $file, + line => $line, host_name => $host_name, host_type => $host_type, host_uuid => $host_uuid, - id => $id, }}); if (not $host_name) @@ -1458,7 +1470,7 @@ WHERE ;"; $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => $debug, list => { query => $query }}); - my $results = $anvil->Database->query({query => $query, id => $id, source => $THIS_FILE, line => __LINE__}); + my $results = $anvil->Database->query({query => $query, id => $id, source => $file ? $file : $THIS_FILE, line => $line ? $line : __LINE__}); my $count = @{$results}; $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => $debug, list => { results => $results, @@ -1493,7 +1505,7 @@ INSERT INTO "; $query =~ s/'NULL'/NULL/g; $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => $debug, list => { query => $query }}); - $anvil->Database->write({query => $query, id => $id, source => $THIS_FILE, line => __LINE__}); + $anvil->Database->write({query => $query, id => $id, source => $file ? $file : $THIS_FILE, line => $line ? $line : __LINE__}); } elsif (($old_host_name ne $host_name) or ($old_host_type ne $host_type)) { @@ -1510,7 +1522,7 @@ WHERE ;"; $query =~ s/'NULL'/NULL/g; $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => $debug, list => { query => $query }}); - $anvil->Database->write({query => $query, id => $id, source => $THIS_FILE, line => __LINE__}); + $anvil->Database->write({query => $query, id => $id, source => $file ? $file : $THIS_FILE, line => $line ? $line : __LINE__}); } $anvil->Log->entry({source => $THIS_FILE, line => __LINE__, level => $debug, key => "log_0126", variables => { method => "Database->insert_or_update_hosts()" }}); @@ -1526,6 +1538,18 @@ If there is an error, an empty string is returned. Parameters; +=head3 id (optional) + +If set, only the corresponding database will be written to. + +=head3 file (optional) + +If set, this is the file name logged as the source of any INSERTs or UPDATEs. + +=head3 line (optional) + +If set, this is the file line number logged as the source of any INSERTs or UPDATEs. + =head3 job_command (required) This is the command (usually a shell call) to run. @@ -1587,6 +1611,9 @@ sub insert_or_update_jobs my $debug = defined $parameter->{debug} ? $parameter->{debug} : 2; $anvil->Log->entry({source => $THIS_FILE, line => __LINE__, level => $debug, key => "log_0125", variables => { method => "Database->insert_or_update_jobs()" }}); + my $id = defined $parameter->{id} ? $parameter->{id} : ""; + my $file = defined $parameter->{file} ? $parameter->{file} : ""; + my $line = defined $parameter->{line} ? $parameter->{line} : ""; my $job_uuid = defined $parameter->{job_uuid} ? $parameter->{job_uuid} : ""; my $job_host_uuid = defined $parameter->{job_host_uuid} ? $parameter->{job_host_uuid} : $anvil->data->{sys}{host_uuid}; my $job_command = defined $parameter->{job_command} ? $parameter->{job_command} : ""; @@ -1600,6 +1627,9 @@ sub insert_or_update_jobs my $job_description = defined $parameter->{job_description} ? $parameter->{job_description} : ""; my $update_progress_only = defined $parameter->{update_progress_only} ? $parameter->{update_progress_only} : 0; $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => $debug, list => { + id => $id, + file => $file, + line => $line, job_uuid => $job_uuid, job_host_uuid => $job_host_uuid, job_command => $job_command, @@ -1688,7 +1718,7 @@ AND ;"; $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => $debug, list => { query => $query }}); - my $results = $anvil->Database->query({query => $query, source => $THIS_FILE, line => __LINE__}); + my $results = $anvil->Database->query({query => $query, source => $file ? $file : $THIS_FILE, line => $line ? $line : __LINE__}); my $count = @{$results}; $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => $debug, list => { results => $results, @@ -1762,7 +1792,7 @@ INSERT INTO "; $query =~ s/'NULL'/NULL/g; $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => $debug, list => { query => $query }}); - $anvil->Database->write({query => $query, source => $THIS_FILE, line => __LINE__}); + $anvil->Database->write({query => $query, source => $file ? $file : $THIS_FILE, line => $line ? $line : __LINE__}); } else { @@ -1786,7 +1816,7 @@ WHERE ;"; $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => $debug, list => { query => $query }}); - my $results = $anvil->Database->query({query => $query, source => $THIS_FILE, line => __LINE__}); + my $results = $anvil->Database->query({query => $query, source => $file ? $file : $THIS_FILE, line => $line ? $line : __LINE__}); my $count = @{$results}; $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => $debug, list => { results => $results, @@ -1834,7 +1864,7 @@ WHERE "; $query =~ s/'NULL'/NULL/g; $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => $debug, list => { query => $query }}); - $anvil->Database->write({query => $query, source => $THIS_FILE, line => __LINE__}); + $anvil->Database->write({query => $query, source => $file ? $file : $THIS_FILE, line => $line ? $line : __LINE__}); } } else @@ -1871,7 +1901,7 @@ WHERE "; $query =~ s/'NULL'/NULL/g; $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => $debug, list => { query => $query }}); - $anvil->Database->write({query => $query, source => $THIS_FILE, line => __LINE__}); + $anvil->Database->write({query => $query, source => $file ? $file : $THIS_FILE, line => $line ? $line : __LINE__}); } } } @@ -1894,6 +1924,14 @@ Parameters; If set, only the corresponding database will be written to. +=head3 file (optional) + +If set, this is the file name logged as the source of any INSERTs or UPDATEs. + +=head3 line (optional) + +If set, this is the file line number logged as the source of any INSERTs or UPDATEs. + =head3 network_interface_bond_uuid (optional) If this interface is part of a bond, this UUID will be the C<< bonds >> -> C<< bond_uuid >> that this interface is slaved to. @@ -1952,6 +1990,8 @@ sub insert_or_update_network_interfaces $anvil->Log->entry({source => $THIS_FILE, line => __LINE__, level => $debug, key => "log_0125", variables => { method => "Database->insert_or_update_network_interfaces()" }}); my $id = defined $parameter->{id} ? $parameter->{id} : ""; + my $file = defined $parameter->{file} ? $parameter->{file} : ""; + my $line = defined $parameter->{line} ? $parameter->{line} : ""; my $network_interface_bond_uuid = defined $parameter->{network_interface_bond_uuid} ? $parameter->{network_interface_bond_uuid} : "--"; my $network_interface_bridge_uuid = defined $parameter->{network_interface_bridge_uuid} ? $parameter->{network_interface_bridge_uuid} : "--"; my $network_interface_duplex = defined $parameter->{network_interface_duplex} ? $parameter->{network_interface_duplex} : "--"; @@ -1966,6 +2006,8 @@ sub insert_or_update_network_interfaces my $network_interface_uuid = defined $parameter->{network_interface_uuid} ? $parameter->{interface_uuid} : ""; $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => $debug, list => { id => $id, + file => $file, + line => $line, network_interface_bond_uuid => $network_interface_bond_uuid, network_interface_bridge_uuid => $network_interface_bridge_uuid, network_interface_duplex => $network_interface_duplex, @@ -1992,7 +2034,7 @@ sub insert_or_update_network_interfaces my $query = "SELECT network_interface_uuid FROM network_interfaces WHERE network_interface_mac_address = ".$anvil->data->{sys}{use_db_fh}->quote($network_interface_mac_address).";"; $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => $debug, list => { query => $query }}); - $network_interface_uuid = $anvil->Database->query({query => $query, source => $THIS_FILE, line => __LINE__})->[0]->[0]; + $network_interface_uuid = $anvil->Database->query({query => $query, source => $file ? $file : $THIS_FILE, line => $line ? $line : __LINE__})->[0]->[0]; $network_interface_uuid = "" if not defined $network_interface_uuid; $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => $debug, list => { network_interface_uuid => $network_interface_uuid }}); } @@ -2021,7 +2063,7 @@ WHERE "; $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => $debug, list => { query => $query }}); - my $results = $anvil->Database->query({query => $query, id => $id, source => $THIS_FILE, line => __LINE__}); + my $results = $anvil->Database->query({query => $query, id => $id, source => $file ? $file : $THIS_FILE, line => $line ? $line : __LINE__}); my $count = @{$results}; $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => $debug, list => { results => $results, @@ -2123,7 +2165,7 @@ WHERE ;"; $query =~ s/'NULL'/NULL/g; $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => $debug, list => { query => $query }}); - $anvil->Database->write({query => $query, id => $id, source => $THIS_FILE, line => __LINE__}); + $anvil->Database->write({query => $query, id => $id, source => $file ? $file : $THIS_FILE, line => $line ? $line : __LINE__}); } else { @@ -2208,7 +2250,7 @@ INSERT INTO "; $query =~ s/'NULL'/NULL/g; $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => $debug, list => { query => $query }}); - $anvil->Database->write({query => $query, id => $id, source => $THIS_FILE, line => __LINE__}); + $anvil->Database->write({query => $query, id => $id, source => $file ? $file : $THIS_FILE, line => $line ? $line : __LINE__}); } $anvil->Log->entry({source => $THIS_FILE, line => __LINE__, level => $debug, key => "log_0126", variables => { method => "Database->insert_or_update_network_interfaces()" }}); @@ -2224,6 +2266,18 @@ If there is an error, an empty string is returned. Parameters; +=head3 id (optional) + +If set, only the corresponding database will be written to. + +=head3 file (optional) + +If set, this is the file name logged as the source of any INSERTs or UPDATEs. + +=head3 line (optional) + +If set, this is the file line number logged as the source of any INSERTs or UPDATEs. + =head3 state_uuid (optional) This is the C<< state_uuid >> to update. If it is not specified but the C<< state_name >> is, a check will be made to see if an entry already exists. If so, that row will be UPDATEd. If not, a random UUID will be generated and a new entry will be INSERTed. @@ -2249,11 +2303,17 @@ sub insert_or_update_states my $debug = defined $parameter->{debug} ? $parameter->{debug} : 3; $anvil->Log->entry({source => $THIS_FILE, line => __LINE__, level => $debug, key => "log_0125", variables => { method => "Database->insert_or_update_states()" }}); - my $state_uuid = $parameter->{state_uuid} ? $parameter->{state_uuid} : ""; - my $state_name = $parameter->{state_name} ? $parameter->{state_name} : ""; - my $state_host_uuid = $parameter->{state_host_uuid} ? $parameter->{state_host_uuid} : $anvil->data->{sys}{host_uuid}; - my $state_note = $parameter->{state_note} ? $parameter->{state_note} : "NULL"; + my $id = defined $parameter->{id} ? $parameter->{id} : ""; + my $file = defined $parameter->{file} ? $parameter->{file} : ""; + my $line = defined $parameter->{line} ? $parameter->{line} : ""; + my $state_uuid = defined $parameter->{state_uuid} ? $parameter->{state_uuid} : ""; + my $state_name = defined $parameter->{state_name} ? $parameter->{state_name} : ""; + my $state_host_uuid = defined $parameter->{state_host_uuid} ? $parameter->{state_host_uuid} : $anvil->data->{sys}{host_uuid}; + my $state_note = defined $parameter->{state_note} ? $parameter->{state_note} : "NULL"; $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => $debug, list => { + id => $id, + file => $file, + line => $line, state_uuid => $state_uuid, state_name => $state_name, state_host_uuid => $state_host_uuid, @@ -2288,7 +2348,7 @@ AND ;"; $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => $debug, list => { query => $query }}); - my $results = $anvil->Database->query({query => $query, source => $THIS_FILE, line => __LINE__}); + my $results = $anvil->Database->query({query => $query, source => $file ? $file : $THIS_FILE, line => $line ? $line : __LINE__}); my $count = @{$results}; $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => $debug, list => { results => $results, @@ -2348,7 +2408,7 @@ INSERT INTO "; $query =~ s/'NULL'/NULL/g; $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => $debug, list => { query => $query }}); - $anvil->Database->write({query => $query, source => $THIS_FILE, line => __LINE__}); + $anvil->Database->write({query => $query, source => $file ? $file : $THIS_FILE, line => $line ? $line : __LINE__}); } else { @@ -2365,7 +2425,7 @@ WHERE ;"; $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => $debug, list => { query => $query }}); - my $results = $anvil->Database->query({query => $query, source => $THIS_FILE, line => __LINE__}); + my $results = $anvil->Database->query({query => $query, source => $file ? $file : $THIS_FILE, line => $line ? $line : __LINE__}); my $count = @{$results}; $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => $debug, list => { results => $results, @@ -2401,7 +2461,7 @@ WHERE "; $query =~ s/'NULL'/NULL/g; $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => $debug, list => { query => $query }}); - $anvil->Database->write({query => $query, source => $THIS_FILE, line => __LINE__}); + $anvil->Database->write({query => $query, source => $file ? $file : $THIS_FILE, line => $line ? $line : __LINE__}); } } } @@ -2420,6 +2480,18 @@ If there is an error, C<< !!error!! >> is returned. Parameters; +=head3 id (optional) + +If set, only the corresponding database will be written to. + +=head3 file (optional) + +If set, this is the file name logged as the source of any INSERTs or UPDATEs. + +=head3 line (optional) + +If set, this is the file line number logged as the source of any INSERTs or UPDATEs. + =head3 variable_uuid (optional) If this is passed, the variable will be updated using this UUID, which allows the C<< variable_name >> to be changed. @@ -2467,6 +2539,9 @@ sub insert_or_update_variables my $debug = defined $parameter->{debug} ? $parameter->{debug} : 3; $anvil->Log->entry({source => $THIS_FILE, line => __LINE__, level => $debug, key => "log_0125", variables => { method => "Database->insert_or_update_variables()" }}); + my $id = defined $parameter->{id} ? $parameter->{id} : ""; + my $file = defined $parameter->{file} ? $parameter->{file} : ""; + my $line = defined $parameter->{line} ? $parameter->{line} : ""; my $variable_uuid = defined $parameter->{variable_uuid} ? $parameter->{variable_uuid} : ""; my $variable_name = defined $parameter->{variable_name} ? $parameter->{variable_name} : ""; my $variable_value = defined $parameter->{variable_value} ? $parameter->{variable_value} : "NULL"; @@ -2478,6 +2553,9 @@ sub insert_or_update_variables my $update_value_only = defined $parameter->{update_value_only} ? $parameter->{update_value_only} : 1; my $log_level = defined $parameter->{log_level} ? $parameter->{log_level} : 3; # Undocumented for now. $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => $log_level, list => { + id => $id, + file => $file, + line => $line, variable_uuid => $variable_uuid, variable_name => $variable_name, variable_value => $variable_value, @@ -2511,7 +2589,7 @@ WHERE variable_uuid = ".$anvil->data->{sys}{use_db_fh}->quote($variable_uuid); $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => $log_level, list => { query => $query }}); - $variable_name = $anvil->Database->query({query => $query, source => $THIS_FILE, line => __LINE__})->[0]->[0]; + $variable_name = $anvil->Database->query({query => $query, source => $file ? $file : $THIS_FILE, line => $line ? $line : __LINE__})->[0]->[0]; $variable_name = "" if not defined $variable_name; $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => $log_level, list => { variable_name => $variable_name }}); } @@ -2537,7 +2615,7 @@ AND $query .= ";"; $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => $log_level, list => { query => $query }}); - my $results = $anvil->Database->query({query => $query, source => $THIS_FILE, line => __LINE__}); + my $results = $anvil->Database->query({query => $query, source => $file ? $file : $THIS_FILE, line => $line ? $line : __LINE__}); my $count = @{$results}; $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => $log_level, list => { results => $results, @@ -2584,7 +2662,7 @@ INSERT INTO $query =~ s/'NULL'/NULL/g; $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => $log_level, list => { query => $query }}); - $anvil->Database->write({query => $query, source => $THIS_FILE, line => __LINE__}); + $anvil->Database->write({query => $query, source => $file ? $file : $THIS_FILE, line => $line ? $line : __LINE__}); } else { @@ -2610,7 +2688,7 @@ AND $query .= ";"; $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => $log_level, list => { query => $query }}); - my $results = $anvil->Database->query({query => $query, source => $THIS_FILE, line => __LINE__}); + my $results = $anvil->Database->query({query => $query, source => $file ? $file : $THIS_FILE, line => $line ? $line : __LINE__}); my $count = @{$results}; $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => $log_level, list => { results => $results, @@ -2646,7 +2724,7 @@ AND $query =~ s/'NULL'/NULL/g; $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => $log_level, list => { query => $query }}); - $anvil->Database->write({query => $query, source => $THIS_FILE, line => __LINE__}); + $anvil->Database->write({query => $query, source => $file ? $file : $THIS_FILE, line => $line ? $line : __LINE__}); } } } @@ -2667,7 +2745,7 @@ WHERE ;"; $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => $log_level, list => { query => $query }}); - my $results = $anvil->Database->query({query => $query, source => $THIS_FILE, line => __LINE__}); + my $results = $anvil->Database->query({query => $query, source => $file ? $file : $THIS_FILE, line => $line ? $line : __LINE__}); my $count = @{$results}; $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => $log_level, list => { results => $results, @@ -2712,7 +2790,7 @@ WHERE $query =~ s/'NULL'/NULL/g; $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => $log_level, list => { query => $query }}); - $anvil->Database->write({query => $query, source => $THIS_FILE, line => __LINE__}); + $anvil->Database->write({query => $query, source => $file ? $file : $THIS_FILE, line => $line ? $line : __LINE__}); } } } @@ -3307,6 +3385,7 @@ sub resync_databases $anvil->Log->entry({source => $THIS_FILE, line => __LINE__, level => $debug, key => "log_0125", variables => { method => "Database->resync_databases()" }}); # If a resync isn't needed, just return. + $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => 2, list => { 'sys::database::resync_needed' => $anvil->data->{sys}{database}{resync_needed} }}); if (not $anvil->data->{sys}{database}{resync_needed}) { return(0); @@ -3410,7 +3489,7 @@ sub resync_databases $query .= " WHERE ".$host_column." = ".$anvil->data->{sys}{use_db_fh}->quote($anvil->data->{sys}{host_uuid}); } $query .= " ORDER BY modified_date DESC;"; - $anvil->Log->entry({source => $THIS_FILE, line => __LINE__, level => 2, key => "log_0074", variables => { id => $id, query => $query }}); + $anvil->Log->entry({source => $THIS_FILE, line => __LINE__, level => $debug, key => "log_0074", variables => { id => $id, query => $query }}); my $results = $anvil->Database->query({id => $id, query => $query, source => $THIS_FILE, line => __LINE__}); my $count = @{$results}; @@ -3420,34 +3499,38 @@ sub resync_databases }}); next if not $count; + my $row_number = 0; foreach my $row (@{$results}) { + $row_number++; my $modified_date = ""; my $row_uuid = ""; - for (my $i = 0; $i < @{$read_columns}; $i++) + for (my $column_number = 0; $column_number < @{$read_columns}; $column_number++) { - my $column_name = $read_columns->[$i]; - my $column_value = defined $row->[$i] ? $row->[$i] : "NULL"; + my $column_name = $read_columns->[$column_number]; + my $column_value = defined $row->[$column_number] ? $row->[$column_number] : "NULL"; my $not_null = $anvil->data->{sys}{database}{table}{$table}{column}{$column_name}{not_null}; my $data_type = $anvil->data->{sys}{database}{table}{$table}{column}{$column_name}{data_type}; - $anvil->Log->variables({source => 2, line => __LINE__, level => 2, list => { - "s1:i" => $i, - "s2:column_name" => $column_name, - "s3:column_value" => $column_value, - "s4:not_null" => $not_null, - "s5:data_type" => $data_type, + $anvil->Log->variables({source => 2, line => __LINE__, level => 3, list => { + "s1:id" => $id, + "s2:row_number" => $row_number, + "s3:column_number" => $column_number, + "s4:column_name" => $column_name, + "s5:column_value" => $column_value, + "s6:not_null" => $not_null, + "s7:data_type" => $data_type, }}); if (($not_null) && ($column_value eq "NULL")) { $column_value = ""; - $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => 2, list => { column_value => $column_value }}); + $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => 3, list => { column_value => $column_value }}); } # The modified_date should be the first row. if ($column_name eq "modified_date") { $modified_date = $column_value; - $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => $debug, list => { modified_date => $modified_date }}); + $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => 3, list => { modified_date => $modified_date }}); next; } @@ -3455,7 +3538,7 @@ sub resync_databases if ($column_name eq $uuid_column) { $row_uuid = $column_value; - $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => $debug, list => { row_uuid => $row_uuid }}); + $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => 3, list => { row_uuid => $row_uuid }}); # This is used to determine if a given entry needs to be # updated or inserted into the public schema @@ -3468,6 +3551,7 @@ sub resync_databases next; } + # TODO: Remove these or make them proper errors die $THIS_FILE." ".__LINE__."; This row's modified_date wasn't the first column returned in query: [$query]\n" if not $modified_date; die $THIS_FILE." ".__LINE__."; This row's UUID column: [$uuid_column] wasn't the second column returned in query: [$query]\n" if not $row_uuid; @@ -3665,8 +3749,6 @@ sub resync_databases # psql -E scancore <<-- LOVE <3 - die $THIS_FILE." ".__LINE__."; testing...\n"; - return(0); } diff --git a/Anvil/Tools/Log.pm b/Anvil/Tools/Log.pm index df89cf95..0e853a0e 100755 --- a/Anvil/Tools/Log.pm +++ b/Anvil/Tools/Log.pm @@ -325,11 +325,20 @@ sub entry if (not $anvil->{HANDLE}{log_file}) { my $shell_call = "/var/log/anvil.log"; - open (my $file_handle, ">>", $shell_call) or $anvil->Log->entry({source => $THIS_FILE, line => __LINE__, level => 0, secure => $secure, priority => "err", key => "log_0016", variables => { shell_call => $shell_call, error => $! }}); + # NOTE: Don't call '$anvil->Log->entry()' here, it will cause a loop! + open (my $file_handle, ">>", $shell_call) or die "Failed to open: [$shell_call] for writing. The error was: $!\n"; + $anvil->{HANDLE}{log_file} = $file_handle; } - my $file_handle = $anvil->{HANDLE}{log_file}; - print $file_handle $string; + + if (not $anvil->{HANDLE}{log_file}) + { + # NOTE: This can't be a normal error because we can't write to the logs. + die $THIS_FILE." ".__LINE__."; log file handle doesn't exist, but it should by now.\n"; + } + + # The handle has to be wrapped in a block to make 'print' happy as it doesn't like non-scalars for file handles + print { $anvil->{HANDLE}{log_file} } $string; } return(0); diff --git a/cgi-bin/home b/cgi-bin/home index b90bc45b..f08aba21 100755 --- a/cgi-bin/home +++ b/cgi-bin/home @@ -11,6 +11,9 @@ use Anvil::Tools; use Data::Dumper; use NetAddr::IP; +# Turn off buffering +$| = 1; + my $THIS_FILE = ($0 =~ /^.*\/(.*)$/)[0]; my $running_directory = ($0 =~ /^(.*?)\/$THIS_FILE$/)[0]; if (($running_directory =~ /^\./) && ($ENV{PWD})) @@ -26,11 +29,11 @@ print $anvil->Template->get({file => "shared.html", name => "http_headers"})."\n # Set the log level to 2. Setting 3 slows he program down a LOT. $anvil->Log->level({set => 2}); -# Read in our words file. -$anvil->Words->read({file => $anvil->data->{path}{directories}{'cgi-bin'}."/words.xml"}); +$anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => 2, list => { testing => "123" }}); # Read the config and then connect to the database. -$anvil->Storage->read_config({file => "/etc/striker/striker.conf"}); +$anvil->Storage->read_config({file => "/etc/anvil/anvil.conf", debug => 2}); + # Make sure I can read the host UUID. if (not $anvil->Get->host_uuid) diff --git a/share/words.xml b/share/words.xml index 9950e33f..bf378ff0 100644 --- a/share/words.xml +++ b/share/words.xml @@ -114,7 +114,7 @@ Connecting to Database with configuration ID: [#!variable!id!#] Database user: [#!variable!user!#] already exists with ID: [#!variable!id!#]. users_home() was asked to find the home directory for the user: [#!variable!user!#], but was unable to do so.]]> SSH session opened without a password to: [#!variable!target!#]. - The database: [#!variable!host!# -> #!variable!name!#] with the ID: [#!variable!id!#] did not respond to pings and 'database::#!variable!id!#::ping' is not set to '0' in '#!data!path::configs::striker.conf!#', skipping it. + The database: [#!variable!host!# -> #!variable!name!#] with the ID: [#!variable!id!#] did not respond to pings and 'database::#!variable!id!#::ping' is not set to '0' in '#!data!path::configs::anvil.conf!#', skipping it. [ Warning ] - The database: [#!variable!name!#] on host: [#!variable!host!#] with ID: [#!variable!id!#] can not be used, skipping it. The database connection error was: @@ -123,9 +123,9 @@ The database connection error was: ---------- Is the database server running on: [#!variable!target!#] and does the target's firewall allow connections on TCP port: [#!variable!port!#]? - ] in: [#!data!path::configs::striker.conf!#].]]> + ] in: [#!data!path::configs::anvil.conf!#].]]> * If the user name is correct, please update: @@ -169,7 +169,7 @@ The database connection error was: - Name: ......... [#!variable!name!#] - Timestamp: .... [#!variable!modified_date!#] - [ Error ] - There is no ScanCore database user set for the local machine. Please check: [#!data!path::config::striker.conf!#]'s DB entry: [#!variable!id!#]. + [ Error ] - There is no ScanCore database user set for the local machine. Please check: [#!data!path::config::anvil.conf!#]'s DB entry: [#!variable!id!#]. Database user: [#!variable!user!#] password has been set/updated. register_alert() was called but the 'title_key' parameter was not passed or it is empty and 'header' is enable (default).]]> I am not recording the alert with message_key: [#!variable!message_key!#] to the database because its log level was lower than any recipients. diff --git a/tools/anvil-prep-database b/tools/anvil-prep-database index bac985b6..80c67993 100755 --- a/tools/anvil-prep-database +++ b/tools/anvil-prep-database @@ -7,7 +7,7 @@ # 0 = Normal exit. # 1 = Failed to initialize postgres # 2 = Failed to start postgres -# 3 = ScanCore user not set in the local ID in striker.conf +# 3 = ScanCore user not set in the local ID in anvil.conf # 4 = Failed to create the database user. # 5 = PostgreSQL not installed. @@ -34,8 +34,8 @@ $anvil->Log->secure({set => 1}); $anvil->Get->switches; # Paths -$anvil->data->{path}{config}{'striker.conf'} = "/etc/anvil/anvil.conf"; -$anvil->Storage->read_config({file => $anvil->data->{path}{config}{'striker.conf'}}); +$anvil->data->{path}{config}{'anvil.conf'} = "/etc/anvil/anvil.conf"; +$anvil->Storage->read_config({file => $anvil->data->{path}{config}{'anvil.conf'}}); my $local_id = $anvil->Database->get_local_id; $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => 3, list => { local_id => $local_id }}); diff --git a/tools/anvil-update-states b/tools/anvil-update-states index 37af78b5..a12814bf 100755 --- a/tools/anvil-update-states +++ b/tools/anvil-update-states @@ -59,8 +59,9 @@ sub report_network next if $file eq "."; next if $file eq ".."; next if $file eq "lo"; + next if $file =~ /virbr\d/; my $full_path = "$directory/$file"; - $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => 2, list => { full_path => $full_path }}); + $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => 3, list => { full_path => $full_path }}); if (-d $full_path) { # Pull out the data I want. Note that some of these don't exist with virtio-net interfaces. @@ -70,8 +71,15 @@ sub report_network my $mtu = -e $full_path."/mtu" ? $anvil->Storage->read_file({file => $full_path."/mtu"}) : 0; my $duplex = -e $full_path."/duplex" ? $anvil->Storage->read_file({file => $full_path."/duplex"}) : "unknown"; # full or half? my $operational = -e $full_path."/operstate" ? $anvil->Storage->read_file({file => $full_path."/operstate"}) : "unknown"; # up or down - my $speed = $link_state ? $anvil->Storage->read_file({file => $full_path."/speed"}) : 0; # Mbps (ie: 1000 = Gbps), gives a very high number for unplugged link - $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => 1, list => { + my $speed = $link_state ? $anvil->Storage->read_file({file => $full_path."/speed", debug => 2}) : 0; # Mbps (ie: 1000 = Gbps), gives a very high number for unplugged link + if ($mac_address =~ /^52:54:00/) + { + ### Set some fake values. + # Speed is "as fast as possible", so we'll record 100 Gbps, but that is really kind of arbitrary. + $speed = 100000 if not $speed; + $duplex = "full" if not $duplex; + } + $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => 2, list => { interface => $interface, mac_address => $mac_address, link_state => $link_state, @@ -80,12 +88,19 @@ sub report_network operational => $operational, speed => $speed, }}); + + # If the MAC address starts with '52:54:00', we've got a virtio NIC. + + die $THIS_FILE." ".__LINE__."; No speed for: [".$full_path."/speed]\n" if ((not defined $speed) or ($speed eq "")); + die $THIS_FILE." ".__LINE__."; Speed: [$speed] isn't numeric for: [".$full_path."/speed]\n" if $speed =~ /\D/; + # + # 100000 if ($speed > 100000) { # NOTE: This is probably 0 now... Though someday >100 Gbps will be reasonable # and we'll need to change this. $speed = 0; - $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => 2, list => { speed => $speed }}); + $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => 3, list => { speed => $speed }}); } # Find the media, if possible. @@ -117,6 +132,9 @@ sub report_network }}); $anvil->Database->insert_or_update_network_interfaces({ + debug => 2, + file => $THIS_FILE, + line => __LINE__, network_interface_name => $interface, network_interface_duplex => $duplex, network_interface_link_state => $link_state, @@ -204,7 +222,7 @@ ORDER BY $network_xml .= "\n"; $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => 3, list => { network_xml => $network_xml }}); - ### TODO: Set the 'status/network.json' name into 'striker.conf' + ### TODO: Set the 'status/network.json' name into 'anvil.conf' # Write the JSON file. my $output_json = $anvil->data->{path}{directories}{html}."/status/network.json"; $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => 3, list => { output_xml => $output_json }}); diff --git a/tools/anvil.sql b/tools/anvil.sql index 88c0954a..498de87f 100644 --- a/tools/anvil.sql +++ b/tools/anvil.sql @@ -396,7 +396,7 @@ AS $$ DECLARE history_network_interfaces RECORD; BEGIN - SELECT INTO history_network_interfaces * FROM network_interfaces WHERE network_interface_host_uuid = new.network_interface_host_uuid; + SELECT INTO history_network_interfaces * FROM network_interfaces WHERE network_interface_uuid = new.network_interface_uuid; INSERT INTO history.network_interfaces (network_interface_uuid, network_interface_host_uuid, @@ -479,7 +479,7 @@ AS $$ DECLARE history_bonds RECORD; BEGIN - SELECT INTO history_bonds * FROM bonds WHERE bond_uuid=new.bond_uuid; + SELECT INTO history_bonds * FROM bonds WHERE bond_uuid = new.bond_uuid; INSERT INTO history.bonds (bond_uuid, bond_host_uuid, @@ -548,7 +548,7 @@ AS $$ DECLARE history_bridges RECORD; BEGIN - SELECT INTO history_bridges * FROM bridges WHERE bridge_uuid=new.bridge_uuid; + SELECT INTO history_bridges * FROM bridges WHERE bridge_uuid = new.bridge_uuid; INSERT INTO history.bridges (bridge_uuid, bridge_host_uuid,