From bf79b10664bc4f6496662cf80e7d25e58d54923f Mon Sep 17 00:00:00 2001 From: Oliver Gorwits Date: Sun, 16 Dec 2012 17:17:31 +0000 Subject: [PATCH] update OUI data when DB schema is updated --- Changes | 4 ++++ Netdisco/bin/nd-dbic-versions | 2 +- Netdisco/bin/netdisco-db-deploy | 32 +++++++++++++++++++++++++++++++- README.pod | 1 + TODO | 1 - 5 files changed, 37 insertions(+), 3 deletions(-) diff --git a/Changes b/Changes index e9f5c1ad..f3a1e37e 100644 --- a/Changes +++ b/Changes @@ -1,5 +1,9 @@ 2.00_009 - + [NEW FEATURES] + + * Update OUI data when the DB schema is deployed/upgraded + 2.00_008 - 2012-12-16 [NEW FEATURES] diff --git a/Netdisco/bin/nd-dbic-versions b/Netdisco/bin/nd-dbic-versions index 84e56b1c..c87001cc 100755 --- a/Netdisco/bin/nd-dbic-versions +++ b/Netdisco/bin/nd-dbic-versions @@ -9,7 +9,7 @@ use Dancer::Plugin::DBIC 'schema'; use Netdisco::DB; use Getopt::Long; -=head1 create_netdisco_schema_version +=head1 nd-dbic-versions This script creates SQL DDL files of the Netdisco database schema. diff --git a/Netdisco/bin/netdisco-db-deploy b/Netdisco/bin/netdisco-db-deploy index 858713fb..6f942a5a 100755 --- a/Netdisco/bin/netdisco-db-deploy +++ b/Netdisco/bin/netdisco-db-deploy @@ -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; diff --git a/README.pod b/README.pod index a0b4c205..49c9c0be 100644 --- a/README.pod +++ b/README.pod @@ -47,6 +47,7 @@ install Perl dependencies into a custom library path: Role::Tiny \ List::MoreUtils \ Socket6 \ + HTTP::Tiny \ HTML::Entities \ Template::Toolkit \ YAML diff --git a/TODO b/TODO index 8f6a1fc7..9e644354 100644 --- a/TODO +++ b/TODO @@ -1,5 +1,4 @@ * bootstrap/setup script to install perl modules and db schema -* OUI.txt added into db schema upgrade * frontend plugin design and support * document how the templates and JS fit together * add poller to daemon (scheduling automatically?)