update OUI data when DB schema is updated

This commit is contained in:
Oliver Gorwits
2012-12-16 17:17:31 +00:00
parent 369a235ff8
commit bf79b10664
5 changed files with 37 additions and 3 deletions

View File

@@ -6,9 +6,10 @@ use warnings FATAL => 'all';
use Dancer ':script';
use Dancer::Plugin::DBIC 'schema';
use HTTP::Tiny;
use Try::Tiny;
=head1 upgrade_netdisco_schema_version
=head1 netdisco-db-deploy
This script upgrades or initialises a Netdisco database schema.
@@ -22,6 +23,10 @@ interaction. If there's no Nedisco schema, it is deployed. If there's an
unversioned schema then versioning is added, and updates applied. Otherwise
only necessary updates are applied to an already versioned schema.
Additionally this script will download the latest MAC address vendor prefix
data from the Internet, and update the OUI table in the database. Hence
Internet access is required to run the script.
=head2 Versions
=over 4
@@ -70,4 +75,29 @@ try {
# upgrade from whatever dbix_class_schema_versions says, to $VERSION
$schema->txn_do(sub { $schema->upgrade });
# now populate/update the OUI data
my $url = 'http://standards.ieee.org/develop/regauth/oui/oui.txt';
my $resp = HTTP::Tiny->new->get($url);
my %data = ();
if ($resp->{success}) {
foreach my $line (split /\n/, $resp->{content}) {
if ($line =~ m/^(.{2}-.{2}-.{2})\s+\(hex\)\s+(.*)\s*$/i) {
my ($oui, $company) = ($1, $2);
$oui =~ s/-/:/g;
$data{lc($oui)} = $company;
}
}
if ((scalar keys %data) > 15_000) {
$schema->txn_do(sub{
$schema->resultset('Oui')->delete;
$schema->resultset('Oui')->populate([
map {{oui => $_, company => $data{$_}}} keys %data
]);
});
}
}
exit 0;