* Reworked striker-parse-oui and striker-scan-network to always lower-case the MAC address.

* Updated striker-scan-network to only run once per day unless --force or a given --network is used. This avoids repeated scans when the anvil-daemon restarts frequently for whatever reason.
* Fixed (for real this time) Convert->time's handling of the 'long' parameter.

Signed-off-by: Digimer <digimer@alteeve.ca>
main
Digimer 5 years ago
parent 4d0a02ce74
commit a7f93c59ea
  1. 6
      Anvil/Tools/Convert.pm
  2. 2
      share/words.xml
  3. 1
      tools/anvil-daemon
  4. 2
      tools/striker-manage-install-target
  5. 5
      tools/striker-parse-oui
  6. 104
      tools/striker-scan-network

@ -1001,9 +1001,9 @@ sub time
my $anvil = $self->parent; my $anvil = $self->parent;
my $debug = defined $parameter->{debug} ? $parameter->{debug} : 3; my $debug = defined $parameter->{debug} ? $parameter->{debug} : 3;
my $time = $parameter->{'time'} ? $parameter->{'time'} : 0; my $time = defined $parameter->{'time'} ? $parameter->{'time'} : 0;
my $long = $parameter->{long} ? $parameter->{long} : 0; my $long = defined $parameter->{long} ? $parameter->{long} : 0;
my $translate = $parameter->{translate} ? $parameter->{translate} : 0; my $translate = defined $parameter->{translate} ? $parameter->{translate} : 0;
$anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => $debug, list => { $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => $debug, list => {
'time' => $time, 'time' => $time,
long => $long, long => $long,

@ -781,6 +781,7 @@ Failed to promote the DRBD resource: [#!variable!resource!#] primary. Expected a
<key name="log_0447">About to download: [#!variable!url!#] and save it to: [#!variable!file!#].</key> <key name="log_0447">About to download: [#!variable!url!#] and save it to: [#!variable!file!#].</key>
<key name="log_0448">Ready to parse: [#!variable!file!#].</key> <key name="log_0448">Ready to parse: [#!variable!file!#].</key>
<key name="log_0449">Parsed: [#!variable!records!#], adding/updating them to the database now.</key> <key name="log_0449">Parsed: [#!variable!records!#], adding/updating them to the database now.</key>
<key name="log_0450">Skipping the network scan. The next scheduled scan will be done in: [#!variable!next_scan!#] second(s). Override with '--force'.</key>
<!-- Test words. Do NOT change unless you update 't/Words.t' or tests will needlessly fail. --> <!-- Test words. Do NOT change unless you update 't/Words.t' or tests will needlessly fail. -->
<key name="t_0000">Test</key> <key name="t_0000">Test</key>
@ -969,6 +970,7 @@ If you are comfortable that the target has changed for a known reason, you can s
]]></key> ]]></key>
<key name="striker_0138">New host name</key> <key name="striker_0138">New host name</key>
<key name="striker_0139"><![CDATA[<unchanged>]]></key> <key name="striker_0139"><![CDATA[<unchanged>]]></key>
<key name="striker_0140">Indicates when the last time the networks connected to this host were scanned. The scan is done to help find the IP addresses assigned to hosted servers and virtual machine equipment. The scan is a simple, sequential nmap ping scan in an attempt to be as non-invasive as possible. The frequency of these scans can be controlled by setting 'network-scan::scan-period' to a number of seconds (the current value is: [#!data!network-scan::scan-period!# seconds]).</key>
<!-- These are generally units and appended to numbers --> <!-- These are generally units and appended to numbers -->
<key name="suffix_0001">#!variable!number!#/sec</key> <key name="suffix_0001">#!variable!number!#/sec</key>

@ -337,7 +337,6 @@ sub handle_periodic_tasks
job_progress => 0, job_progress => 0,
}); });
$anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => 3, list => { job_uuid => $job_uuid }}); $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => 3, list => { job_uuid => $job_uuid }});
die;
} }
# Update the next check time. # Update the next check time.

@ -199,7 +199,7 @@ $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => 3, list
}}); }});
# If this isn't a Striker dashboard, exit. # If this isn't a Striker dashboard, exit.
if ($anvil->_short_host_name !~ /striker/) if ($anvil->System->get_host_type ne "dashboard")
{ {
print $anvil->Words->string({key => "error_0044"})."\n"; print $anvil->Words->string({key => "error_0044"})."\n";
update_progress($anvil, 100, "error_0044"); update_progress($anvil, 100, "error_0044");

@ -3,7 +3,8 @@
# This periodically reads in http://standards-oui.ieee.org/oui/oui.txt, if possible, and parses it to update/ # This periodically reads in http://standards-oui.ieee.org/oui/oui.txt, if possible, and parses it to update/
# populate the oui database table. # populate the oui database table.
# #
# TODO: * Handle jobs properly # TODO:
#
use strict; use strict;
use warnings; use warnings;
@ -229,6 +230,7 @@ EOF
$oui = $1; $oui = $1;
$vendor = $2; $vendor = $2;
$oui =~ s/-/:/g; $oui =~ s/-/:/g;
$oui = lc($oui);
$anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => 3, list => { $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => 3, list => {
oui => $oui, oui => $oui,
vendor => $vendor, vendor => $vendor,
@ -242,6 +244,7 @@ EOF
my $vendor2 = $2; my $vendor2 = $2;
$oui2 =~ s/-/:/g; $oui2 =~ s/-/:/g;
$oui2 =~ s/^(\w\w)(\w\w)(\w\w)$/$1:$2:$3/g; $oui2 =~ s/^(\w\w)(\w\w)(\w\w)$/$1:$2:$3/g;
$oui2 = lc($oui2);
$oui = $oui2 if not $oui; $oui = $oui2 if not $oui;
$vendor = $vendor2 if not $vendor; $vendor = $vendor2 if not $vendor;
$anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => 3, list => { $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => 3, list => {

@ -8,11 +8,11 @@
# 0 = Normal exit # 0 = Normal exit
# 1 = No databases available. # 1 = No databases available.
# 2 = The '--network X' value is not valid. # 2 = The '--network X' value is not valid.
# 3 = Not yet time to rescan.
# #
# TODO: * Support '--dhcp' where, if set, we look up the DHCP range offered by the Striker dashboard(s) and # TODO: * Support '--dhcp' where, if set, we look up the DHCP range offered by the Striker dashboard(s) and
# scan just the lease range. This should speed up discovery of new/replacement foundation pack # scan just the lease range. This should speed up discovery of new/replacement foundation pack
# equipment. # equipment.
# * Handle jobs properly
use strict; use strict;
use warnings; use warnings;
@ -32,7 +32,7 @@ if (($running_directory =~ /^\./) && ($ENV{PWD}))
my $anvil = Anvil::Tools->new(); my $anvil = Anvil::Tools->new();
$anvil->Log->level({set => 2}); $anvil->Log->level({set => 2});
$anvil->Log->secure({set => 0}); $anvil->Log->secure({set => 0});
$anvil->Log->entry({source => $THIS_FILE, line => __LINE__, level => 3, secure => 0, key => "log_0115", variables => { program => $THIS_FILE }}); $anvil->Log->entry({source => $THIS_FILE, line => __LINE__, level => 2, secure => 0, key => "log_0115", variables => { program => $THIS_FILE }});
# Make sure we're running as 'root' # Make sure we're running as 'root'
# $< == real UID, $> == effective UID # $< == real UID, $> == effective UID
@ -44,7 +44,7 @@ if (($< != 0) && ($> != 0))
} }
$anvil->Database->connect; $anvil->Database->connect;
$anvil->Log->entry({source => $THIS_FILE, line => __LINE__, level => 3, secure => 0, key => "log_0132"}); $anvil->Log->entry({source => $THIS_FILE, line => __LINE__, level => 2, secure => 0, key => "log_0132"});
if (not $anvil->data->{sys}{database}{connections}) if (not $anvil->data->{sys}{database}{connections})
{ {
# No databases, exit. # No databases, exit.
@ -55,16 +55,19 @@ if (not $anvil->data->{sys}{database}{connections})
# Do we have a specified network to scan? # Do we have a specified network to scan?
$anvil->data->{switches}{network} = ""; $anvil->data->{switches}{network} = "";
$anvil->data->{switches}{'job-uuid'} = ""; $anvil->data->{switches}{'job-uuid'} = "";
$anvil->data->{switches}{force} = "";
$anvil->Get->switches; $anvil->Get->switches;
$anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => 2, list => { $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => 2, list => {
"switches::network" => $anvil->data->{switches}{network}, "switches::network" => $anvil->data->{switches}{network},
"switches::job-uuid" => $anvil->data->{switches}{'job-uuid'}, "switches::job-uuid" => $anvil->data->{switches}{'job-uuid'},
"switches::force" => $anvil->data->{switches}{force},
}}); }});
update_progress($anvil, 0, "clear"); update_progress($anvil, 0, "clear");
update_progress($anvil, 1, "log_0239,!!job-uuid!".$anvil->data->{switches}{'job-uuid'}."!!"); update_progress($anvil, 1, "log_0239,!!job-uuid!".$anvil->data->{switches}{'job-uuid'}."!!");
$anvil->data->{progress} = 1; $anvil->data->{progress} = 1;
check_if_time($anvil);
scan($anvil); scan($anvil);
# We're done # We're done
@ -97,6 +100,83 @@ sub update_progress
return(0); return(0);
} }
# This checks to see if it's time to run the scan yet or not. If not, it will exit the program. If so, it
# returns with '0'.
sub check_if_time
{
my ($anvil) = @_;
# NOTE: We only scan once a day, unless 'force' is used.
if ($anvil->data->{switches}{force})
{
return(0);
}
elsif ($anvil->data->{switches}{network})
{
# We're being asked to scan a specific network.
return(0);
}
elsif (not $anvil->data->{switches}{'job-uuid'})
{
# No job_uuid, so a manual call.
return(0);
}
else
{
my ($unixtime, $variable_uuid, $modified_date) = $anvil->Database->read_variable({variable_name => "network-scan::scanned"});
$anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => 2, list => {
unixtime => $unixtime,
variable_uuid => $variable_uuid,
modified_date => $modified_date,
}});
if (($unixtime eq "") or ($unixtime =~ /\D/))
{
$unixtime = 0;
$anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => 2, list => { unixtime => $unixtime }});
}
### TODO: Allow the user to set a "scan time" that will wait until the local time is after a
### certain time before scaning. Also, allow the user to disable auto-scan entirely.
# If the database variable 'network-scan::scanned' is not set, or is more than 24 hours old,
# scan.
$anvil->data->{'network-scan'}{'scan-period'} = 86400 if not defined $anvil->data->{'network-scan'}{'scan-period'};
$anvil->data->{'network-scan'}{'scan-period'} =~ s/,//g;
$anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => 2, list => {
'network-scan::scan-period' => $anvil->data->{'network-scan'}{'scan-period'},
}});
if ($anvil->data->{'network-scan'}{'scan-period'} =~ /\D/)
{
$anvil->data->{'network-scan'}{'scan-period'} = 86400;
$anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => 2, list => {
'network-scan::scan-period' => $anvil->data->{'network-scan'}{'scan-period'},
}});
}
my $time_now = time;
my $next_scan = $unixtime + $anvil->data->{'network-scan'}{'scan-period'};
my $difference = ($next_scan - $time_now);
$anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => 2, list => {
's1:time_now' => $time_now,
's2:next_scan' => $next_scan,
's3:difference' => $difference,
}});
if ((not $variable_uuid) or ($unixtime !~ /^\d+/) or ($difference < 0))
{
# It's been long enough (or it's the first time), scan.
return(0);
}
elsif ($difference > 0)
{
# Log when the next scan will happen and then exit.
my $say_when = $anvil->Convert->time({'time' => $difference, long => 1});
$anvil->Log->entry({source => $THIS_FILE, line => __LINE__, level => 2, 'print' => 1, key => "log_0450", variables => { next_scan => $anvil->Convert->add_commas({number => $say_when}) }});
update_progress($anvil, 100, "log_0450,!!next_scan!".$anvil->Convert->add_commas({number => $say_when})."!!");
$anvil->nice_exit({exit_code => 3});
}
}
return(0);
}
# This scans any networks passed in. # This scans any networks passed in.
sub scan sub scan
{ {
@ -221,6 +301,21 @@ sub scan
} }
} }
# Update the rescan time to now, if no specific network was given.
if (not $anvil->data->{switches}{network})
{
$anvil->Database->insert_or_update_variables({
debug => 3,
variable_name => "network-scan::scanned",
variable_value => time,
variable_default => "",
variable_description => "striker_0140",
variable_section => "system",
variable_source_uuid => $anvil->Get->host_uuid,
variable_source_table => "hosts",
});
}
return(0); return(0);
} }
@ -279,6 +374,7 @@ sub call_nmap
if ($line =~ /MAC Address: (.*?) \(/) if ($line =~ /MAC Address: (.*?) \(/)
{ {
$this_mac = $1; $this_mac = $1;
$this_mac = lc($this_mac);
$anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => 2, list => { $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => 2, list => {
's1:this_mac' => $this_mac, 's1:this_mac' => $this_mac,
's2:this_ip' => $this_ip, 's2:this_ip' => $this_ip,
@ -297,7 +393,7 @@ sub call_nmap
{ {
$anvil->data->{progress} += 1; $anvil->data->{progress} += 1;
$anvil->data->{progress} = 95 if $anvil->data->{progress} > 95; $anvil->data->{progress} = 95 if $anvil->data->{progress} > 95;
update_progress($anvil, $anvil->data->{progress}, "log_0446,!!ip!".$this_ip."!!,!!mac!".$this_mac."!!,!!section!".$section."!!"); update_progress($anvil, $anvil->data->{progress}, "log_0446,!!ip!".$this_ip."!!,!!mac!".$this_mac."!!");
$anvil->Log->entry({source => $THIS_FILE, line => __LINE__, level => 0, 'print' => 1, priority => "err", key => "log_0446", variables => { $anvil->Log->entry({source => $THIS_FILE, line => __LINE__, level => 0, 'print' => 1, priority => "err", key => "log_0446", variables => {
ip => $this_ip, ip => $this_ip,
mac => $this_mac, mac => $this_mac,

Loading…
Cancel
Save