From 31d1977f1e4b4465dbb75be3d9a6c0fbfadf189f Mon Sep 17 00:00:00 2001 From: Oliver Gorwits Date: Mon, 14 Aug 2017 18:11:42 +0100 Subject: [PATCH] Revert "move expire code to be initial plugin pilot (broken)" I think we'll only do the new backend code for jobs with a device. This reverts commit 07998b72d961f70a9843f172adfebc50a2d329da. --- .../Netdisco/Backend/Worker/Poller/Expiry.pm | 99 ++++++++++++++- lib/App/Netdisco/Core/Plugin.pm | 4 +- .../Netdisco/Core/Plugin/Expire/Main/RFC.pm | 117 ------------------ share/config.yml | 3 +- 4 files changed, 101 insertions(+), 122 deletions(-) delete mode 100644 lib/App/Netdisco/Core/Plugin/Expire/Main/RFC.pm diff --git a/lib/App/Netdisco/Backend/Worker/Poller/Expiry.pm b/lib/App/Netdisco/Backend/Worker/Poller/Expiry.pm index 64962315..fa1f10a8 100644 --- a/lib/App/Netdisco/Backend/Worker/Poller/Expiry.pm +++ b/lib/App/Netdisco/Backend/Worker/Poller/Expiry.pm @@ -3,6 +3,7 @@ package App::Netdisco::Backend::Worker::Poller::Expiry; use Dancer qw/:moose :syntax :script/; use Dancer::Plugin::DBIC 'schema'; +use Time::Piece; use App::Netdisco::Backend::Util ':all'; use Role::Tiny; @@ -11,7 +12,103 @@ use namespace::clean; # expire devices and nodes according to config sub expire { my ($self, $job) = @_; - # TODO somehow run the Expire hooks?! + + if (setting('expire_devices') and setting('expire_devices') > 0) { + schema('netdisco')->txn_do(sub { + schema('netdisco')->resultset('Device')->search({ + -or => [ 'vendor' => undef, 'vendor' => { '!=' => 'netdisco' }], + last_discover => \[q/< (now() - ?::interval)/, + (setting('expire_devices') * 86400)], + })->delete(); + }); + } + + if (setting('expire_nodes') and setting('expire_nodes') > 0) { + schema('netdisco')->txn_do(sub { + schema('netdisco')->resultset('Node')->search({ + time_last => \[q/< (now() - ?::interval)/, + (setting('expire_nodes') * 86400)], + })->delete(); + }); + } + + if (setting('expire_nodes_archive') and setting('expire_nodes_archive') > 0) { + schema('netdisco')->txn_do(sub { + schema('netdisco')->resultset('Node')->search({ + -not_bool => 'active', + time_last => \[q/< (now() - ?::interval)/, + (setting('expire_nodes_archive') * 86400)], + })->delete(); + }); + } + + if (setting('expire_jobs') and setting('expire_jobs') > 0) { + schema('netdisco')->txn_do(sub { + schema('netdisco')->resultset('Admin')->search({ + entered => \[q/< (now() - ?::interval)/, + (setting('expire_jobs') * 86400)], + })->delete(); + }); + } + + # now update stats + my $schema = schema('netdisco'); + eval { require SNMP::Info }; + my $snmpinfo_ver = ($@ ? 'n/a' : $SNMP::Info::VERSION); + + # TODO: (when we have the capabilities table?) + # $stats{waps} = sql_scalar('device',['COUNT(*)'], {"model"=>"AIR%"}); + + $schema->txn_do(sub { + $schema->resultset('Statistics')->update_or_create({ + day => localtime->ymd, + + device_count => + $schema->resultset('Device')->count_rs->as_query, + device_ip_count => + $schema->resultset('DeviceIp')->count_rs->as_query, + device_link_count => + $schema->resultset('Virtual::DeviceLinks') + ->count_rs({'me.left_ip' => {'>', \'me.right_ip'}})->as_query, + device_port_count => + $schema->resultset('DevicePort')->count_rs->as_query, + device_port_up_count => + $schema->resultset('DevicePort')->count_rs({up => 'up'})->as_query, + ip_table_count => + $schema->resultset('NodeIp')->count_rs->as_query, + ip_active_count => + $schema->resultset('NodeIp')->search({-bool => 'active'}, + {columns => 'ip', distinct => 1})->count_rs->as_query, + node_table_count => + $schema->resultset('Node')->count_rs->as_query, + node_active_count => + $schema->resultset('Node')->search({-bool => 'active'}, + {columns => 'mac', distinct => 1})->count_rs->as_query, + + netdisco_ver => pretty_version($App::Netdisco::VERSION, 3), + snmpinfo_ver => $snmpinfo_ver, + schema_ver => $schema->schema_version, + perl_ver => pretty_version($], 3), + pg_ver => + pretty_version($schema->storage->dbh->{pg_server_version}, 2), + + }, { key => 'primary' }); + }); + + return job_done("Checked expiry and updated stats"); +} + +# take perl or pg versions and make pretty +sub pretty_version { + my ($version, $seglen) = @_; + return unless $version and $seglen; + return $version if $version !~ m/^[0-9.]+$/; + $version =~ s/\.//g; + $version = (join '.', reverse map {scalar reverse} + unpack("(A${seglen})*", reverse $version)); + $version =~ s/\.000/.0/g; + $version =~ s/\.0+([1-9]+)/.$1/g; + return $version; } # expire nodes for a specific device diff --git a/lib/App/Netdisco/Core/Plugin.pm b/lib/App/Netdisco/Core/Plugin.pm index 86eb1506..04f0314a 100644 --- a/lib/App/Netdisco/Core/Plugin.pm +++ b/lib/App/Netdisco/Core/Plugin.pm @@ -53,11 +53,11 @@ register 'register_core_worker' => sub { # run worker my $happy = false; try { - my @result = $code->($job, $workerconf); + $code->($job, $workerconf); $happy = true; } catch { debug $_ }; - return @result if $happy; # FIXME + return $happy; }; my $hook = $workerconf->{hook} .'_'. $phase; diff --git a/lib/App/Netdisco/Core/Plugin/Expire/Main/RFC.pm b/lib/App/Netdisco/Core/Plugin/Expire/Main/RFC.pm deleted file mode 100644 index e26c63fe..00000000 --- a/lib/App/Netdisco/Core/Plugin/Expire/Main/RFC.pm +++ /dev/null @@ -1,117 +0,0 @@ -package App::Netdisco::Core::Plugin::Expire::Main::RFC; - -use Dancer ':syntax'; -use Dancer::Plugin::DBIC 'schema'; - -use Time::Piece; -use App::Netdisco::Backend::Util ':all'; - -use App::Netdisco::Core::Plugin; - -# expire devices and nodes according to config -register_core_worker({ driver => 'any' } => sub { - my ($job, $workerconf) = @_; - - if (setting('expire_devices') and setting('expire_devices') > 0) { - schema('netdisco')->txn_do(sub { - schema('netdisco')->resultset('Device')->search({ - -or => [ 'vendor' => undef, 'vendor' => { '!=' => 'netdisco' }], - last_discover => \[q/< (now() - ?::interval)/, - (setting('expire_devices') * 86400)], - })->delete(); - die; # XXX - }); - } - - if (setting('expire_nodes') and setting('expire_nodes') > 0) { - schema('netdisco')->txn_do(sub { - schema('netdisco')->resultset('Node')->search({ - time_last => \[q/< (now() - ?::interval)/, - (setting('expire_nodes') * 86400)], - })->delete(); - die; # XXX - }); - } - - if (setting('expire_nodes_archive') and setting('expire_nodes_archive') > 0) { - schema('netdisco')->txn_do(sub { - schema('netdisco')->resultset('Node')->search({ - -not_bool => 'active', - time_last => \[q/< (now() - ?::interval)/, - (setting('expire_nodes_archive') * 86400)], - })->delete(); - die; # XXX - }); - } - - if (setting('expire_jobs') and setting('expire_jobs') > 0) { - schema('netdisco')->txn_do(sub { - schema('netdisco')->resultset('Admin')->search({ - entered => \[q/< (now() - ?::interval)/, - (setting('expire_jobs') * 86400)], - })->delete(); - die; # XXX - }); - } - - # now update stats - my $schema = schema('netdisco'); - eval { require SNMP::Info }; - my $snmpinfo_ver = ($@ ? 'n/a' : $SNMP::Info::VERSION); - - # TODO: (when we have the capabilities table?) - # $stats{waps} = sql_scalar('device',['COUNT(*)'], {"model"=>"AIR%"}); - - $schema->txn_do(sub { - $schema->resultset('Statistics')->update_or_create({ - day => localtime->ymd, - - device_count => - $schema->resultset('Device')->count_rs->as_query, - device_ip_count => - $schema->resultset('DeviceIp')->count_rs->as_query, - device_link_count => - $schema->resultset('Virtual::DeviceLinks') - ->count_rs({'me.left_ip' => {'>', \'me.right_ip'}})->as_query, - device_port_count => - $schema->resultset('DevicePort')->count_rs->as_query, - device_port_up_count => - $schema->resultset('DevicePort')->count_rs({up => 'up'})->as_query, - ip_table_count => - $schema->resultset('NodeIp')->count_rs->as_query, - ip_active_count => - $schema->resultset('NodeIp')->search({-bool => 'active'}, - {columns => 'ip', distinct => 1})->count_rs->as_query, - node_table_count => - $schema->resultset('Node')->count_rs->as_query, - node_active_count => - $schema->resultset('Node')->search({-bool => 'active'}, - {columns => 'mac', distinct => 1})->count_rs->as_query, - - netdisco_ver => pretty_version($App::Netdisco::VERSION, 3), - snmpinfo_ver => $snmpinfo_ver, - schema_ver => $schema->schema_version, - perl_ver => pretty_version($], 3), - pg_ver => - pretty_version($schema->storage->dbh->{pg_server_version}, 2), - - }, { key => 'primary' }); - }); - - return job_done("Checked expiry and updated stats"); -}); - -# take perl or pg versions and make pretty -sub pretty_version { - my ($version, $seglen) = @_; - return unless $version and $seglen; - return $version if $version !~ m/^[0-9.]+$/; - $version =~ s/\.//g; - $version = (join '.', reverse map {scalar reverse} - unpack("(A${seglen})*", reverse $version)); - $version =~ s/\.000/.0/g; - $version =~ s/\.0+([1-9]+)/.$1/g; - return $version; -} - -true; diff --git a/share/config.yml b/share/config.yml index 172dc4ae..4fe6fb86 100644 --- a/share/config.yml +++ b/share/config.yml @@ -277,8 +277,7 @@ core_plugins: - Arpnip::Nodes::CLI - Arpnip::Nodes::RFC - Arpnip::Subnets::RFC - - Nbtstat::Main::RFC - - Expire::Main::RFC + - NetBIOS::Nbtstat::RFC # --------------- # GraphViz Export