relocate repo files so ND2 is the only code

This commit is contained in:
Oliver Gorwits
2017-04-14 23:08:55 +01:00
parent 9a016ea6ba
commit d23b32500f
469 changed files with 0 additions and 6920 deletions

View File

@@ -0,0 +1,32 @@
package App::Netdisco::Web::Plugin::AdminTask::JobQueue;
use Dancer ':syntax';
use Dancer::Plugin::Ajax;
use Dancer::Plugin::DBIC;
use Dancer::Plugin::Auth::Extensible;
use App::Netdisco::Web::Plugin;
use App::Netdisco::JobQueue qw/jq_log jq_delete/;
register_admin_task({
tag => 'jobqueue',
label => 'Job Queue',
});
ajax '/ajax/control/admin/jobqueue/del' => require_role admin => sub {
send_error('Missing job', 400) unless param('job');
jq_delete( param('job') );
};
ajax '/ajax/control/admin/jobqueue/delall' => require_role admin => sub {
jq_delete();
};
ajax '/ajax/content/admin/jobqueue' => require_role admin => sub {
content_type('text/html');
template 'ajax/admintask/jobqueue.tt', {
results => [ jq_log ],
}, { layout => undef };
};
true;

View File

@@ -0,0 +1,75 @@
package App::Netdisco::Web::Plugin::AdminTask::NodeMonitor;
use Dancer ':syntax';
use Dancer::Plugin::Ajax;
use Dancer::Plugin::DBIC;
use Dancer::Plugin::Auth::Extensible;
use App::Netdisco::Web::Plugin;
use App::Netdisco::Util::Node 'check_mac';
register_admin_task({
tag => 'nodemonitor',
label => 'Node Monitor',
});
sub _sanity_ok {
return 0 unless param('mac')
and check_mac(undef, param('mac'));
params->{mac} = check_mac(undef, param('mac'));
return 1;
}
ajax '/ajax/control/admin/nodemonitor/add' => require_role admin => sub {
send_error('Bad Request', 400) unless _sanity_ok();
schema('netdisco')->txn_do(sub {
my $monitor = schema('netdisco')->resultset('NodeMonitor')
->create({
mac => param('mac'),
active => (param('active') ? \'true' : \'false'),
why => param('why'),
cc => param('cc'),
});
});
};
ajax '/ajax/control/admin/nodemonitor/del' => require_role admin => sub {
send_error('Bad Request', 400) unless _sanity_ok();
schema('netdisco')->txn_do(sub {
schema('netdisco')->resultset('NodeMonitor')
->find({mac => param('mac')})->delete;
});
};
ajax '/ajax/control/admin/nodemonitor/update' => require_role admin => sub {
send_error('Bad Request', 400) unless _sanity_ok();
schema('netdisco')->txn_do(sub {
my $monitor = schema('netdisco')->resultset('NodeMonitor')
->find({mac => param('mac')});
return unless $monitor;
$monitor->update({
mac => param('mac'),
active => (param('active') ? \'true' : \'false'),
why => param('why'),
cc => param('cc'),
date => \'now()',
});
});
};
ajax '/ajax/content/admin/nodemonitor' => require_role admin => sub {
my $set = schema('netdisco')->resultset('NodeMonitor')
->search(undef, { order_by => [qw/active date mac/] });
content_type('text/html');
template 'ajax/admintask/nodemonitor.tt', {
results => $set,
}, { layout => undef };
};
true;

View File

@@ -0,0 +1,82 @@
package App::Netdisco::Web::Plugin::AdminTask::OrphanedDevices;
use strict;
use warnings;
use Dancer ':syntax';
use Dancer::Plugin::DBIC;
use Dancer::Plugin::Auth::Extensible;
use App::Netdisco::Web::Plugin;
register_admin_task(
{ tag => 'orphaned',
label => 'Orphaned Devices / Networks',
provides_csv => 1,
}
);
get '/ajax/content/admin/orphaned' => require_role admin => sub {
my @tree = schema('netdisco')->resultset('Virtual::UnDirEdgesAgg')
->search( undef, { prefetch => 'device' } )->hri->all;
my @orphans
= schema('netdisco')->resultset('Virtual::OrphanedDevices')->search()
->order_by('ip')->hri->all;
return unless ( scalar @tree || scalar @orphans );
my @ordered;
if ( scalar @tree ) {
my %tree = map { $_->{'left_ip'} => $_ } @tree;
my $current_graph = 0;
my %visited = ();
my @to_visit = ();
foreach my $node ( keys %tree ) {
next if exists $visited{$node};
$current_graph++;
@to_visit = ($node);
while (@to_visit) {
my $node_to_visit = shift @to_visit;
$visited{$node_to_visit} = $current_graph;
push @to_visit,
grep { !exists $visited{$_} }
@{ $tree{$node_to_visit}->{'links'} };
}
}
my @graphs = ();
foreach my $key ( keys %visited ) {
push @{ $graphs[ $visited{$key} - 1 ] }, $tree{$key}->{'device'};
}
@ordered = sort { scalar @{$b} <=> scalar @{$a} } @graphs;
}
return if ( scalar @ordered < 2 && !scalar @tree );
if ( request->is_ajax ) {
template 'ajax/admintask/orphaned.tt',
{
orphans => \@orphans,
graphs => \@ordered,
},
{ layout => undef };
}
else {
header( 'Content-Type' => 'text/comma-separated-values' );
template 'ajax/admintask/orphaned_csv.tt',
{
orphans => \@orphans,
graphs => \@ordered,
},
{ layout => undef };
}
};
1;

View File

@@ -0,0 +1,24 @@
package App::Netdisco::Web::Plugin::AdminTask::PollerPerformance;
use Dancer ':syntax';
use Dancer::Plugin::Ajax;
use Dancer::Plugin::DBIC;
use Dancer::Plugin::Auth::Extensible;
use App::Netdisco::Web::Plugin;
register_admin_task({
tag => 'performance',
label => 'Poller Performance',
});
ajax '/ajax/content/admin/performance' => require_role admin => sub {
my $set = schema('netdisco')->resultset('Virtual::PollerPerformance');
content_type('text/html');
template 'ajax/admintask/performance.tt', {
results => $set,
}, { layout => undef };
};
true;

View File

@@ -0,0 +1,100 @@
package App::Netdisco::Web::Plugin::AdminTask::PseudoDevice;
use Dancer ':syntax';
use Dancer::Plugin::Ajax;
use Dancer::Plugin::DBIC;
use Dancer::Plugin::Auth::Extensible;
use App::Netdisco::Web::Plugin;
use NetAddr::IP::Lite ':lower';
register_admin_task({
tag => 'pseudodevice',
label => 'Pseudo Devices',
});
sub _sanity_ok {
return 0 unless param('dns')
and param('dns') =~ m/^[[:print:]]+$/
and param('dns') !~ m/[[:space:]]/;
my $ip = NetAddr::IP::Lite->new(param('ip'));
return 0 unless ($ip and $ip->addr ne '0.0.0.0');
return 0 unless param('ports')
and param('ports') =~ m/^[[:digit:]]+$/;
return 1;
}
ajax '/ajax/control/admin/pseudodevice/add' => require_role admin => sub {
send_error('Bad Request', 400) unless _sanity_ok();
schema('netdisco')->txn_do(sub {
my $device = schema('netdisco')->resultset('Device')
->create({
ip => param('ip'),
dns => param('dns'),
vendor => 'netdisco',
last_discover => \'now()',
});
return unless $device;
$device->ports->populate([
[qw/port type/],
map {["Port$_", 'other']} @{[1 .. param('ports')]},
]);
# device_ip table is used to show whether topo is "broken"
schema('netdisco')->resultset('DeviceIp')
->create({
ip => param('ip'),
alias => param('ip'),
});
});
};
ajax '/ajax/control/admin/pseudodevice/del' => require_role admin => sub {
send_error('Bad Request', 400) unless _sanity_ok();
forward '/ajax/control/admin/delete', { device => param('ip') };
};
ajax '/ajax/control/admin/pseudodevice/update' => require_role admin => sub {
send_error('Bad Request', 400) unless _sanity_ok();
schema('netdisco')->txn_do(sub {
my $device = schema('netdisco')->resultset('Device')
->with_port_count->find({ip => param('ip')});
return unless $device;
my $count = $device->port_count;
if (param('ports') > $count) {
my $start = $count + 1;
$device->ports->populate([
[qw/port type/],
map {["Port$_", 'other']} @{[$start .. param('ports')]},
]);
}
elsif (param('ports') < $count) {
my $start = param('ports') + 1;
$device->ports
->single({port => "Port$_"})->delete
for ($start .. $count);
}
});
};
ajax '/ajax/content/admin/pseudodevice' => require_role admin => sub {
my $set = schema('netdisco')->resultset('Device')
->search(
{vendor => 'netdisco'},
{order_by => { -desc => 'last_discover' }},
)->with_port_count;
content_type('text/html');
template 'ajax/admintask/pseudodevice.tt', {
results => $set,
}, { layout => undef };
};
true;

View File

@@ -0,0 +1,24 @@
package App::Netdisco::Web::Plugin::AdminTask::SlowDevices;
use Dancer ':syntax';
use Dancer::Plugin::Ajax;
use Dancer::Plugin::DBIC;
use Dancer::Plugin::Auth::Extensible;
use App::Netdisco::Web::Plugin;
register_admin_task({
tag => 'slowdevices',
label => 'Slowest Devices',
});
ajax '/ajax/content/admin/slowdevices' => require_role admin => sub {
my $set = schema('netdisco')->resultset('Virtual::SlowDevices');
content_type('text/html');
template 'ajax/admintask/slowdevices.tt', {
results => $set,
}, { layout => undef };
};
true;

View File

@@ -0,0 +1,142 @@
package App::Netdisco::Web::Plugin::AdminTask::Topology;
use Dancer ':syntax';
use Dancer::Plugin::Ajax;
use Dancer::Plugin::DBIC;
use Dancer::Plugin::Auth::Extensible;
use App::Netdisco::Web::Plugin;
use App::Netdisco::Util::Device 'get_device';
use Try::Tiny;
use NetAddr::IP::Lite ':lower';
register_admin_task({
tag => 'topology',
label => 'Manual Device Topology',
});
sub _sanity_ok {
my $dev1 = NetAddr::IP::Lite->new(param('dev1'));
return 0 unless ($dev1 and $dev1->addr ne '0.0.0.0');
my $dev2 = NetAddr::IP::Lite->new(param('dev2'));
return 0 unless ($dev2 and $dev2->addr ne '0.0.0.0');
return 0 unless param('port1');
return 0 unless param('port2');
return 1;
}
ajax '/ajax/control/admin/topology/add' => require_role admin => sub {
send_error('Bad Request', 400) unless _sanity_ok();
my $device = schema('netdisco')->resultset('Topology')
->create({
dev1 => param('dev1'),
port1 => param('port1'),
dev2 => param('dev2'),
port2 => param('port2'),
});
# re-set remote device details in affected ports
# could fail for bad device or port names
try {
schema('netdisco')->txn_do(sub {
# only work on root_ips
my $left = get_device(param('dev1'));
my $right = get_device(param('dev2'));
# skip bad entries
return unless ($left->in_storage and $right->in_storage);
$left->ports
->search({port => param('port1')}, {for => 'update'})
->single()
->update({
remote_ip => param('dev2'),
remote_port => param('port2'),
remote_type => undef,
remote_id => undef,
is_uplink => \"true",
manual_topo => \"true",
});
$right->ports
->search({port => param('port2')}, {for => 'update'})
->single()
->update({
remote_ip => param('dev1'),
remote_port => param('port1'),
remote_type => undef,
remote_id => undef,
is_uplink => \"true",
manual_topo => \"true",
});
});
};
};
ajax '/ajax/control/admin/topology/del' => require_role admin => sub {
send_error('Bad Request', 400) unless _sanity_ok();
schema('netdisco')->txn_do(sub {
my $device = schema('netdisco')->resultset('Topology')
->search({
dev1 => param('dev1'),
port1 => param('port1'),
dev2 => param('dev2'),
port2 => param('port2'),
})->delete;
});
# re-set remote device details in affected ports
# could fail for bad device or port names
try {
schema('netdisco')->txn_do(sub {
# only work on root_ips
my $left = get_device(param('dev1'));
my $right = get_device(param('dev2'));
# skip bad entries
return unless ($left->in_storage and $right->in_storage);
$left->ports
->search({port => param('port1')}, {for => 'update'})
->single()
->update({
remote_ip => undef,
remote_port => undef,
remote_type => undef,
remote_id => undef,
is_uplink => \"false",
manual_topo => \"false",
});
$right->ports
->search({port => param('port2')}, {for => 'update'})
->single()
->update({
remote_ip => undef,
remote_port => undef,
remote_type => undef,
remote_id => undef,
is_uplink => \"false",
manual_topo => \"false",
});
});
};
};
ajax '/ajax/content/admin/topology' => require_role admin => sub {
my $set = schema('netdisco')->resultset('Topology')
->search({},{order_by => [qw/dev1 dev2 port1/]});
content_type('text/html');
template 'ajax/admintask/topology.tt', {
results => $set,
}, { layout => undef };
};
true;

View File

@@ -0,0 +1,48 @@
package App::Netdisco::Web::Plugin::AdminTask::UndiscoveredNeighbors;
use strict;
use warnings;
use Dancer ':syntax';
use Dancer::Plugin::DBIC;
use Dancer::Plugin::Auth::Extensible;
use App::Netdisco::Util::Device qw/is_discoverable/;
use App::Netdisco::Web::Plugin;
register_admin_task(
{ tag => 'undiscoveredneighbors',
label => 'Undiscovered Neighbors',
provides_csv => 1,
}
);
get '/ajax/content/admin/undiscoveredneighbors' => require_role admin => sub {
my @results
= schema('netdisco')->resultset('Virtual::UndiscoveredNeighbors')
->order_by('ip')->hri->all;
return unless scalar @results;
# Don't include devices excluded from discovery by config
# but only if the number of devices is small, as it triggers a
# SELECT per device to check.
if (scalar @results < 50) {
@results
= grep { is_discoverable( $_->{'remote_ip'}, $_->{'remote_type'} ) }
@results;
}
return unless scalar @results;
if ( request->is_ajax ) {
template 'ajax/admintask/undiscoveredneighbors.tt',
{ results => \@results, },
{ layout => undef };
}
else {
header( 'Content-Type' => 'text/comma-separated-values' );
template 'ajax/admintask/undiscoveredneighbors_csv.tt',
{ results => \@results, },
{ layout => undef };
}
};
1;

View File

@@ -0,0 +1,66 @@
package App::Netdisco::Web::Plugin::AdminTask::UserLog;
use Dancer ':syntax';
use Dancer::Plugin::Ajax;
use Dancer::Plugin::DBIC;
use Dancer::Plugin::Auth::Extensible;
use App::Netdisco::Util::ExpandParams 'expand_hash';
use App::Netdisco::Web::Plugin;
register_admin_task(
{ tag => 'userlog',
label => 'User Activity Log',
}
);
ajax '/ajax/control/admin/userlog/data' => require_role admin => sub {
send_error( 'Missing parameter', 400 )
unless ( param('draw') && param('draw') =~ /\d+/ );
my $rs = schema('netdisco')->resultset('UserLog');
my $exp_params = expand_hash( scalar params );
my $recordsTotal = $rs->count;
my @data = $rs->get_datatables_data($exp_params)->hri->all;
my $recordsFiltered = $rs->get_datatables_filtered_count($exp_params);
content_type 'application/json';
return to_json(
{ draw => int( param('draw') ),
recordsTotal => int($recordsTotal),
recordsFiltered => int($recordsFiltered),
data => \@data,
}
);
};
ajax '/ajax/control/admin/userlog/del' => require_role admin => sub {
send_error( 'Missing entry', 400 ) unless param('entry');
schema('netdisco')->txn_do(
sub {
my $device = schema('netdisco')->resultset('UserLog')
->search( { entry => param('entry') } )->delete;
}
);
};
ajax '/ajax/control/admin/userlog/delall' => require_role admin => sub {
schema('netdisco')->txn_do(
sub {
my $device = schema('netdisco')->resultset('UserLog')->delete;
}
);
};
ajax '/ajax/content/admin/userlog' => require_role admin => sub {
content_type('text/html');
template 'ajax/admintask/userlog.tt', {}, { layout => undef };
};
1;

View File

@@ -0,0 +1,106 @@
package App::Netdisco::Web::Plugin::AdminTask::Users;
use Dancer ':syntax';
use Dancer::Plugin::Ajax;
use Dancer::Plugin::DBIC;
use Dancer::Plugin::Auth::Extensible;
use Dancer::Plugin::Passphrase;
use App::Netdisco::Web::Plugin;
use Digest::MD5 ();
register_admin_task({
tag => 'users',
label => 'User Management',
provides_csv => 1,
});
sub _sanity_ok {
return 0 unless param('username')
and param('username') =~ m/^[[:print:] ]+$/;
return 1;
}
sub _make_password {
my $pass = (shift || passphrase->generate_random);
if (setting('safe_password_store')) {
return passphrase($pass)->generate;
}
else {
return Digest::MD5::md5_hex($pass),
}
}
ajax '/ajax/control/admin/users/add' => require_role admin => sub {
send_error('Bad Request', 400) unless _sanity_ok();
schema('netdisco')->txn_do(sub {
my $user = schema('netdisco')->resultset('User')
->create({
username => param('username'),
password => _make_password(param('password')),
fullname => param('fullname'),
ldap => (param('ldap') ? \'true' : \'false'),
port_control => (param('port_control') ? \'true' : \'false'),
admin => (param('admin') ? \'true' : \'false'),
note => param('note'),
});
});
};
ajax '/ajax/control/admin/users/del' => require_role admin => sub {
send_error('Bad Request', 400) unless _sanity_ok();
schema('netdisco')->txn_do(sub {
schema('netdisco')->resultset('User')
->find({username => param('username')})->delete;
});
};
ajax '/ajax/control/admin/users/update' => require_role admin => sub {
send_error('Bad Request', 400) unless _sanity_ok();
schema('netdisco')->txn_do(sub {
my $user = schema('netdisco')->resultset('User')
->find({username => param('username')});
return unless $user;
$user->update({
((param('password') ne '********')
? (password => _make_password(param('password')))
: ()),
fullname => param('fullname'),
ldap => (param('ldap') ? \'true' : \'false'),
port_control => (param('port_control') ? \'true' : \'false'),
admin => (param('admin') ? \'true' : \'false'),
note => param('note'),
});
});
};
get '/ajax/content/admin/users' => require_role admin => sub {
my @results = schema('netdisco')->resultset('User')
->search(undef, {
'+columns' => {
created => \"to_char(creation, 'YYYY-MM-DD HH24:MI')",
last_seen => \"to_char(last_on, 'YYYY-MM-DD HH24:MI')",
},
order_by => [qw/fullname username/]
})->hri->all;
return unless scalar @results;
if ( request->is_ajax ) {
template 'ajax/admintask/users.tt',
{ results => \@results, },
{ layout => undef };
}
else {
header( 'Content-Type' => 'text/comma-separated-values' );
template 'ajax/admintask/users_csv.tt',
{ results => \@results, },
{ layout => undef };
}
};
true;