New Poller daemon worker can discover and refresh devices. New Scheduler daemon worker replaces the cron jobs with config. New netdisco-do script allows running a job one-off from CLI. Squashed commit of the following: commitfa25f36e14Author: Oliver Gorwits <oliver@cpan.org> Date: Wed Apr 10 22:43:47 2013 +0100 fix HTTP port at 5000 commit202ea4a84cAuthor: Oliver Gorwits <oliver@cpan.org> Date: Wed Apr 10 22:33:03 2013 +0100 bug fixes in discover commit925d9e4d6bAuthor: Oliver Gorwits <oliver@cpan.org> Date: Wed Apr 10 21:51:44 2013 +0100 add mini app for one-time jobs commitd3a6c08a9dAuthor: Oliver Gorwits <oliver@cpan.org> Date: Wed Apr 10 21:46:55 2013 +0100 better name for subaction commit4adf473b20Author: Oliver Gorwits <oliver@cpan.org> Date: Wed Apr 10 20:15:18 2013 +0100 add logging of db add/del commit8aacafedaaAuthor: Oliver Gorwits <oliver@cpan.org> Date: Wed Apr 10 19:49:00 2013 +0100 copy all remaining messages from netdisco 1 commit3e1156df1fAuthor: Oliver Gorwits <oliver@cpan.org> Date: Mon Apr 8 22:09:40 2013 +0100 alter some log levels and messages commite7ea92920fAuthor: Oliver Gorwits <oliver@cpan.org> Date: Mon Apr 8 20:32:33 2013 +0100 store wireless ssid and port info to DB commitd1d16938a1Author: Oliver Gorwits <oliver@cpan.org> Date: Fri Apr 5 08:52:59 2013 +0100 update packaging for new files and deps commit965990786fAuthor: Oliver Gorwits <oliver@cpan.org> Date: Thu Apr 4 22:22:50 2013 +0100 implementation of find_neighbors commit03c4d8ef09Author: Oliver Gorwits <oliver@cpan.org> Date: Thu Apr 4 20:42:11 2013 +0100 add discoverall and discover_neighbors poller jobs commitdf68ff0890Author: Oliver Gorwits <oliver@cpan.org> Date: Thu Apr 4 19:52:37 2013 +0100 implementation of store_modules commitc2ac19e647Author: Oliver Gorwits <oliver@cpan.org> Date: Thu Apr 4 19:32:14 2013 +0100 implementation of store_power commitb7fb8c64a0Author: Oliver Gorwits <oliver@cpan.org> Date: Thu Apr 4 18:48:09 2013 +0100 implementation of store_vlans commitb8ddbd1ecaAuthor: Oliver Gorwits <oliver@cpan.org> Date: Thu Apr 4 17:08:06 2013 +0100 implementation of store_wireless (without storing, yet) commit2a14057481Author: Oliver Gorwits <oliver@cpan.org> Date: Thu Apr 4 16:47:21 2013 +0100 implementation of store_interfaces (without wireless) commitd5b2b71d34Author: Oliver Gorwits <oliver@cpan.org> Date: Thu Apr 4 15:12:19 2013 +0100 only start Manager if there are pollers or interactives commitf4a3dac760Author: Oliver Gorwits <oliver@cpan.org> Date: Thu Apr 4 15:03:10 2013 +0100 change sub names so as not to collide with Dancer commita8f0894986Author: Oliver Gorwits <oliver@cpan.org> Date: Thu Apr 4 14:47:28 2013 +0100 implementation of refresh, discover, and store_device commit4c2e3cf82dAuthor: Oliver Gorwits <oliver@cpan.org> Date: Thu Apr 4 11:38:15 2013 +0100 make get_device return a new result object commite6ac131658Author: Oliver Gorwits <oliver@cpan.org> Date: Thu Apr 4 10:38:22 2013 +0100 better POD section name commit6c5b6bbbeeAuthor: Oliver Gorwits <oliver@cpan.org> Date: Thu Apr 4 10:37:46 2013 +0100 implement separate snmp_connect and snmp_connect_rw methods commit62c8e19063Author: Oliver Gorwits <oliver@cpan.org> Date: Mon Apr 1 20:06:29 2013 +0100 fix for unique constraint on job queue for locally queued jobs commitebb65996e6Author: Oliver Gorwits <oliver@cpan.org> Date: Mon Apr 1 20:00:36 2013 +0100 add refresh poller job commit05928e8cf6Author: Oliver Gorwits <oliver@cpan.org> Date: Mon Apr 1 19:31:39 2013 +0100 Refactor Util namespace Squashed commit of the following: commit789c528fcfAuthor: Oliver Gorwits <oliver@cpan.org> Date: Mon Apr 1 19:31:07 2013 +0100 update manifest and fix typo commitb95d0951f2Author: Oliver Gorwits <oliver@cpan.org> Date: Mon Apr 1 19:22:41 2013 +0100 refactor ::Util namespace commita8dde50343Author: Oliver Gorwits <oliver@cpan.org> Date: Sun Mar 31 13:45:27 2013 +0100 no need to search for device - IP should already be exact commitb42daee4c1Merge:6e5276295bb8fcAuthor: Oliver Gorwits <oliver@cpan.org> Date: Wed Mar 27 21:00:09 2013 +0000 Merge branch 'master' into og-poller commit6e527629a2Author: Oliver Gorwits <oliver@cpan.org> Date: Tue Mar 26 23:39:23 2013 +0000 fixes and log messages commitcfcb7a956fAuthor: Oliver Gorwits <oliver@cpan.org> Date: Tue Mar 26 22:57:06 2013 +0000 bug fixes commit48f779a8d0Author: Oliver Gorwits <oliver@cpan.org> Date: Tue Mar 26 22:42:16 2013 +0000 add config for scheduled tasks commit2f6efcb312Author: Oliver Gorwits <oliver@cpan.org> Date: Tue Mar 26 22:15:04 2013 +0000 create poller worker and add poller type stubs commit52b28b0ab8Author: Oliver Gorwits <oliver@cpan.org> Date: Tue Mar 26 22:04:00 2013 +0000 code tidy commit96db66739fAuthor: Oliver Gorwits <oliver@cpan.org> Date: Mon Mar 25 22:35:11 2013 +0000 more insane but more working version of the job queue constraint commitcb25216f40Author: Oliver Gorwits <oliver@cpan.org> Date: Sun Mar 24 20:22:11 2013 +0000 make scheduler start automatic based on housekeeping setting existing commit0acbe8abd3Author: Oliver Gorwits <oliver@cpan.org> Date: Sun Mar 24 19:45:24 2013 +0000 add scheduler based on Algorithm::Cron commit49d136b57aAuthor: Oliver Gorwits <oliver@cpan.org> Date: Sun Mar 24 18:48:10 2013 +0000 add unique constraint on admin/job queue
225 lines
4.8 KiB
Perl
225 lines
4.8 KiB
Perl
package App::Netdisco::Util::Port;
|
|
|
|
use Dancer qw/:syntax :script/;
|
|
use Dancer::Plugin::DBIC 'schema';
|
|
|
|
use App::Netdisco::Util::Device 'get_device';
|
|
|
|
use base 'Exporter';
|
|
our @EXPORT = ();
|
|
our @EXPORT_OK = qw/
|
|
vlan_reconfig_check port_reconfig_check
|
|
get_port get_iid get_powerid
|
|
is_vlan_interface port_has_phone
|
|
/;
|
|
our %EXPORT_TAGS = (all => \@EXPORT_OK);
|
|
|
|
=head1 NAME
|
|
|
|
App::Netdisco::Util::Port
|
|
|
|
=head1 DESCRIPTION
|
|
|
|
A set of helper subroutines to support parts of the Netdisco application.
|
|
|
|
There are no default exports, however the C<:all> tag will export all
|
|
subroutines.
|
|
|
|
=head1 EXPORT_OK
|
|
|
|
=head2 vlan_reconfig_check( $port )
|
|
|
|
=over 4
|
|
|
|
=item *
|
|
|
|
Sanity check that C<$port> is not a vlan subinterface.
|
|
|
|
=item *
|
|
|
|
Permission check that C<vlanctl> is true in Netdisco config.
|
|
|
|
=back
|
|
|
|
Will return nothing if these checks pass OK.
|
|
|
|
=cut
|
|
|
|
sub vlan_reconfig_check {
|
|
my $port = shift;
|
|
my $ip = $port->ip;
|
|
my $name = $port->port;
|
|
|
|
my $is_vlan = is_vlan_interface($port);
|
|
|
|
# vlan (routed) interface check
|
|
return "forbidden: [$name] is a vlan interface on [$ip]"
|
|
if $is_vlan;
|
|
|
|
return "forbidden: not permitted to change native vlan"
|
|
if not setting('vlanctl');
|
|
|
|
return;
|
|
}
|
|
|
|
=head2 port_reconfig_check( $port )
|
|
|
|
=over 4
|
|
|
|
=item *
|
|
|
|
Permission check that C<allow_uplinks> is true in Netdisco config, if C<$port>
|
|
is an uplink.
|
|
|
|
=item *
|
|
|
|
Permission check that C<portctl_nophones> is not true in Netdisco config, if
|
|
C<$port> has a phone connected.
|
|
|
|
=item *
|
|
|
|
Permission check that C<portctl_vlans> is true if C<$port> is a vlan
|
|
subinterface.
|
|
|
|
=back
|
|
|
|
Will return nothing if these checks pass OK.
|
|
|
|
=cut
|
|
|
|
sub port_reconfig_check {
|
|
my $port = shift;
|
|
my $ip = $port->ip;
|
|
my $name = $port->port;
|
|
|
|
my $has_phone = port_has_phone($port);
|
|
my $is_vlan = is_vlan_interface($port);
|
|
|
|
# uplink check
|
|
return "forbidden: port [$name] on [$ip] is an uplink"
|
|
if $port->remote_type and not $has_phone and not setting('allow_uplinks');
|
|
|
|
# phone check
|
|
return "forbidden: port [$name] on [$ip] is a phone"
|
|
if $has_phone and setting('portctl_nophones');
|
|
|
|
# vlan (routed) interface check
|
|
return "forbidden: [$name] is a vlan interface on [$ip]"
|
|
if $is_vlan and not setting('portctl_vlans');
|
|
|
|
return;
|
|
}
|
|
|
|
=head2 get_port( $device, $portname )
|
|
|
|
Given a device IP address and a port name, returns a L<DBIx::Class::Row>
|
|
object for the Port on the Device in the Netdisco database.
|
|
|
|
The device IP can also be passed as a Device C<DBIx::Class> object.
|
|
|
|
Returns C<undef> if the device or port are not known to Netdisco.
|
|
|
|
=cut
|
|
|
|
sub get_port {
|
|
my ($device, $portname) = @_;
|
|
|
|
# accept either ip or dbic object
|
|
$device = get_device($device);
|
|
|
|
my $port = schema('netdisco')->resultset('DevicePort')
|
|
->find({ip => $device->ip, port => $portname});
|
|
|
|
return $port;
|
|
}
|
|
|
|
=head2 get_iid( $info, $port )
|
|
|
|
Given an L<SNMP::Info> instance for a device, and the name of a port, returns
|
|
the current interface table index for that port. This can be used in further
|
|
SNMP requests on attributes of the port.
|
|
|
|
Returns C<undef> if there is no such port name on the device.
|
|
|
|
=cut
|
|
|
|
sub get_iid {
|
|
my ($info, $port) = @_;
|
|
|
|
# accept either port name or dbic object
|
|
$port = $port->port if ref $port;
|
|
|
|
my $interfaces = $info->interfaces;
|
|
my %rev_if = reverse %$interfaces;
|
|
my $iid = $rev_if{$port};
|
|
|
|
return $iid;
|
|
}
|
|
|
|
=head2 get_powerid( $info, $port )
|
|
|
|
Given an L<SNMP::Info> instance for a device, and the name of a port, returns
|
|
the current PoE table index for the port. This can be used in further SNMP
|
|
requests on PoE attributes of the port.
|
|
|
|
Returns C<undef> if there is no such port name on the device.
|
|
|
|
=cut
|
|
|
|
sub get_powerid {
|
|
my ($info, $port) = @_;
|
|
|
|
# accept either port name or dbic object
|
|
$port = $port->port if ref $port;
|
|
|
|
my $iid = get_iid($info, $port)
|
|
or return undef;
|
|
|
|
my $p_interfaces = $info->peth_port_ifindex;
|
|
my %rev_p_if = reverse %$p_interfaces;
|
|
my $powerid = $rev_p_if{$iid};
|
|
|
|
return $powerid;
|
|
}
|
|
|
|
=head2 is_vlan_interface( $port )
|
|
|
|
Returns true if the C<$port> L<DBIx::Class> object represents a vlan
|
|
subinterface.
|
|
|
|
This uses simple checks on the port I<type> and I<name>, and therefore might
|
|
sometimes returns a false-negative result.
|
|
|
|
=cut
|
|
|
|
sub is_vlan_interface {
|
|
my $port = shift;
|
|
|
|
my $is_vlan = (($port->type and
|
|
$port->type =~ /^(53|propVirtual|l2vlan|l3ipvlan|135|136|137)$/i)
|
|
or ($port->port and $port->port =~ /vlan/i)
|
|
or ($port->name and $port->name =~ /vlan/i)) ? 1 : 0;
|
|
|
|
return $is_vlan;
|
|
}
|
|
|
|
=head2 port_has_phone( $port )
|
|
|
|
Returns true if the C<$port> L<DBIx::Class> object has a phone connected.
|
|
|
|
This uses a simple check on the I<type> of the remote connected device, and
|
|
therefore might sometimes return a false-negative result.
|
|
|
|
=cut
|
|
|
|
sub port_has_phone {
|
|
my $port = shift;
|
|
|
|
my $has_phone = ($port->remote_type
|
|
and $port->remote_type =~ /ip.phone/i) ? 1 : 0;
|
|
|
|
return $has_phone;
|
|
}
|
|
|
|
1;
|