1package App::Netdisco::Worker::Plugin::Arpnip::Subnets;
2
3use Dancer ':syntax';
4use App::Netdisco::Worker::Plugin;
5use aliased 'App::Netdisco::Worker::Status';
6
7use App::Netdisco::Transport::SNMP ();
8use App::Netdisco::Util::Permission 'check_acl_no';
9use Dancer::Plugin::DBIC 'schema';
10use NetAddr::IP::Lite ':lower';
11use Time::HiRes 'gettimeofday';
12
13register_worker({ phase => 'main', driver => 'snmp' }, sub {
14  my ($job, $workerconf) = @_;
15
16  my $device = $job->device;
17  my $snmp = App::Netdisco::Transport::SNMP->reader_for($device)
18    or return Status->defer("arpnip failed: could not SNMP connect to $device");
19
20  # get directly connected networks
21  my @subnets = gather_subnets($device);
22  # TODO: IPv6 subnets
23
24  my $now = 'to_timestamp('. (join '.', gettimeofday) .')';
25  store_subnet($_, $now) for @subnets;
26
27  return Status->info(sprintf ' [%s] arpnip - processed %s Subnet entries',
28    $device->ip, scalar @subnets);
29});
30
31# gathers device subnets
32sub gather_subnets {
33  my $device = shift;
34  my @subnets = ();
35
36  my $snmp = App::Netdisco::Transport::SNMP->reader_for($device)
37    or return (); # already checked!
38
39  my $ip_netmask = $snmp->ip_netmask;
40  foreach my $entry (keys %$ip_netmask) {
41      my $ip = NetAddr::IP::Lite->new($entry);
42      my $addr = $ip->addr;
43
44      next if $addr eq '0.0.0.0';
45      next if check_acl_no($ip, 'group:__LOCAL_ADDRESSES__');
46      next if setting('ignore_private_nets') and $ip->is_rfc1918;
47
48      my $netmask = $ip_netmask->{$addr};
49      next if $netmask eq '255.255.255.255' or $netmask eq '0.0.0.0';
50
51      my $cidr = NetAddr::IP::Lite->new($addr, $netmask)->network->cidr;
52
53      debug sprintf ' [%s] arpnip - found subnet %s', $device->ip, $cidr;
54      push @subnets, $cidr;
55  }
56
57  return @subnets;
58}
59
60# update subnets with new networks
61sub store_subnet {
62  my ($subnet, $now) = @_;
63
64  schema('netdisco')->txn_do(sub {
65    schema('netdisco')->resultset('Subnet')->update_or_create(
66    {
67      net => $subnet,
68      last_discover => \$now,
69    },
70    { for => 'update' });
71  });
72}
73
74true;
75