blib/lib/App/Netdisco/Web/Plugin/Search/Node.pm untested

filter: Statementstmt 0.0% 0 / 146 Branchbran 0.0% 0 / 52 Conditioncond 0.0% 0 / 45 MC/DCmcdc 0.0% 0 / 27 Subroutinesub 0.0% 0 / 10 Podpod n/a totaltotal 0.0% 0 / 280 CC 1 SCAR 6.9
CC
1
Cov
0%
BEGIN
2.0
BEGIN
2.0
BEGIN
2.0
CRAP 2.0
1 package App::Netdisco::Web::Plugin::Search::Node;
2  
3 use Dancer ':syntax';
4 use Dancer::Plugin::DBIC;
5 use Dancer::Plugin::Auth::Extensible;
6  
7 use NetAddr::IP::Lite ':lower';
8 use Regexp::Common 'net';
9 use NetAddr::MAC ();
10 use POSIX qw/strftime/;
11  
12 use App::Netdisco::Web::Plugin;
13 use App::Netdisco::Util::DNS 'ipv4_from_hostname';
14 use App::Netdisco::Util::Web 'sql_match';
15  
16 register_search_tab({
17 tag => 'node',
18 label => 'Node',
19 api_endpoint => 1,
20 api_parameters => [
21 q => {
22 description => 'MAC Address or IP Address or Hostname (without Domain Suffix) of a Node (supports SQL or "*" wildcards)',
23 required => 1,
24 },
25 partial => {
26 description => 'Partially match the "q" parameter (wildcard characters not required)',
27 type => 'boolean',
28 default => 'false',
29 },
30 deviceports => {
31 description => 'MAC Address search will include Device Port MACs',
32 type => 'boolean',
33 default => 'true',
34 },
35 port_context => {
36 description => 'MAC Address search will include topology context of each sighting\'s switch port (uplink, neighbor, LAG, node count)',
37 type => 'boolean',
38 default => 'false',
39 },
40 show_vendor => {
41 description => 'Include interface Vendor in results',
42 type => 'boolean',
43 default => 'false',
44 },
45 archived => {
46 description => 'Include archived records in results',
47 type => 'boolean',
48 default => 'false',
49 },
50 daterange => {
51 description => 'Date Range in format "YYYY-MM-DD to YYYY-MM-DD"',
52 default => ('1970-01-01 to '. strftime('%Y-%m-%d', gmtime)),
53 },
54 age_invert => {
55 description => 'Results should NOT be within daterange',
56 type => 'boolean',
57 default => 'false',
58 },
59 # mac_format is used only in the template (will be IEEE) in results
60 #mac_format => {
61 #},
62 # stamps param is used only in the template (they will be included)
63 #stamps => {
64 #},
65 ],
66 });
67  
68 # nodes matching the param as an IP or DNS hostname or MAC
69 get '/ajax/content/search/node' => require_login sub {
70 my $node = param('q');
71 send_error('Missing node', 400) unless $node;
72 return unless ($node =~ m/\w/); # need some alphanum at least
73 content_type('text/html');
74  
75 my $agenot = param('age_invert') || '0';
76 my ( $start, $end ) = (param('daterange') // '') =~ m/(\d+-\d+-\d+)/gmx;
77  
78 my $mac = NetAddr::MAC->new(mac => ($node || ''));
79 undef $mac if
80 ($mac and $mac->as_ieee
81 and (($mac->as_ieee eq '00:00:00:00:00:00')
82 or ($mac->as_ieee !~ m/^$RE{net}{MAC}$/i)));
83  
84 my @active = (param('archived') ? () : (-bool => 'active'));
85 my (@times, @wifitimes, @porttimes);
86  
87 if ( $start and $end ) {
88 $start = $start . ' 00:00:00';
89 $end = $end . ' 23:59:59';
90  
91 if ($agenot) {
92 @times = (-or => [
93 time_first => [ undef ],
94 time_last => [ { '<', $start }, { '>', $end } ]
95 ]);
96 @wifitimes = (-or => [
97 time_last => [ undef ],
98 time_last => [ { '<', $start }, { '>', $end } ],
99 ]);
100 @porttimes = (-or => [
101 creation => [ undef ],
102 creation => [ { '<', $start }, { '>', $end } ]
103 ]);
104 }
105 else {
106 @times = (-or => [
107 -and => [
108 time_first => undef,
109 time_last => undef,
110 ],
111 -and => [
112 time_last => { '>=', $start },
113 time_last => { '<=', $end },
114 ],
115 ]);
116 @wifitimes = (-or => [
117 time_last => undef,
118 -and => [
119 time_last => { '>=', $start },
120 time_last => { '<=', $end },
121 ],
122 ]);
123 @porttimes = (-or => [
124 creation => undef,
125 -and => [
126 creation => { '>=', $start },
127 creation => { '<=', $end },
128 ],
129 ]);
130 }
131 }
132  
133 my ($likeval, $likeclause) = sql_match($node, not param('partial'));
134 my $using_wildcards = (($likeval ne $node) ? 1 : 0);
135  
136 my @where_mac =
137 ($using_wildcards ? \['me.mac::text ILIKE ?', $likeval]
138 : ((!defined $mac or $mac->errstr) ? \'0=1' : ('me.mac' => $mac->as_ieee)) );
139  
140 my $sightings = schema(vars->{'tenant'})->resultset('Node')
141 ->search({-and => [@where_mac, @active, @times]}, {
142 order_by => {'-desc' => 'time_last'},
143 '+columns' => [
144 'device.dns',
145 'device.name',
146 { time_first_stamp => \"to_char(time_first, 'YYYY-MM-DD HH24:MI')" },
147 { time_last_stamp => \"to_char(time_last, 'YYYY-MM-DD HH24:MI')" },
148 ],
149 join => 'device',
150 });
151  
152 $sightings = $sightings->with_port_context if param('port_context');
153  
154 my $ips = schema(vars->{'tenant'})->resultset('NodeIp')
155 ->search({-and => [@where_mac, @active, @times]}, {
156 order_by => {'-desc' => 'time_last'},
157 '+columns' => [
158 'manufacturer.company',
159 'manufacturer.abbrev',
160 { time_first_stamp => \"to_char(time_first, 'YYYY-MM-DD HH24:MI')" },
161 { time_last_stamp => \"to_char(time_last, 'YYYY-MM-DD HH24:MI')" },
162 ],
163 join => 'manufacturer'
164 })->with_router->with_l3_domain;
165  
166 my $netbios = schema(vars->{'tenant'})->resultset('NodeNbt')
167 ->search({-and => [@where_mac, @active, @times]}, {
168 order_by => {'-desc' => 'time_last'},
169 '+columns' => [
170 'manufacturer.company',
171 'manufacturer.abbrev',
172 { time_first_stamp => \"to_char(time_first, 'YYYY-MM-DD HH24:MI')" },
173 { time_last_stamp => \"to_char(time_last, 'YYYY-MM-DD HH24:MI')" },
174 ],
175 join => 'manufacturer'
176 });
177  
178 my $wireless = schema(vars->{'tenant'})->resultset('NodeWireless')->search(
179 { -and => [@where_mac, @wifitimes] },
180 { order_by => { '-desc' => 'time_last' },
181 '+columns' => [
182 'manufacturer.company',
183 'manufacturer.abbrev',
184 {
185 time_last_stamp => \"to_char(time_last, 'YYYY-MM-DD HH24:MI')"
186 }],
187 join => 'manufacturer'
188 }
189 );
190  
191 my $rs_dp = schema(vars->{'tenant'})->resultset('DevicePort');
192 if ($sightings->has_rows or $ips->has_rows or $netbios->has_rows) {
193 my $ports = param('deviceports')
194 ? $rs_dp->search({ -and => [@where_mac] }, { order_by => { '-desc' => 'creation' }}) : undef;
195  
196 return template 'ajax/search/node_by_mac.tt', {
197 ips => $ips,
198 sightings => $sightings,
199 ports => $ports,
200 wireless => $wireless,
201 netbios => $netbios,
202 }, { layout => 'noop' };
203 }
204 else {
205 my $ports = param('deviceports')
206 ? $rs_dp->search({ -and => [@where_mac, @porttimes] }, { order_by => { '-desc' => 'creation' }}) : undef;
207  
208 if (defined $ports and $ports->has_rows) {
209 return template 'ajax/search/node_by_mac.tt', {
210 ips => $ips,
211 sightings => $sightings,
212 ports => $ports,
213 wireless => $wireless,
214 netbios => $netbios,
215 }, { layout => 'noop' };
216 }
217 }
218  
219 my $have_rows = 0;
220 my $set = schema(vars->{'tenant'})->resultset('NodeNbt')
221 ->search_by_name({nbname => $likeval, @active, @times});
222 ++$have_rows if $set->has_rows;
223  
224 unless ( $have_rows ) {
225 if ($node =~ m{^(?:$RE{net}{IPv4}|$RE{net}{IPv6})(?:/\d+)?$}i
226 and my $ip = NetAddr::IP::Lite->new($node)) {
227  
228 # search_by_ip() will extract cidr notation if necessary
229 $set = schema(vars->{'tenant'})->resultset('NodeIp')
230 ->search_by_ip({ip => $ip, @active, @times})->with_router->with_l3_domain;
231 ++$have_rows if $set->has_rows;
232 }
233 else {
234 $set = schema(vars->{'tenant'})->resultset('NodeIp')
235 ->search_by_dns({
236 ($using_wildcards ? (dns => $likeval) :
237 (dns => "${likeval}.\%", suffix => setting('domain_suffix'))),
238 @active,
239 @times,
240 })->with_router->with_l3_domain;
241 ++$have_rows if $set->has_rows;
242  
243 # try DNS lookup as fallback
244 if (not $using_wildcards and not $have_rows) {
245 my $resolved_ip = ipv4_from_hostname($node);
246  
247 if ($resolved_ip) {
248 $set = schema(vars->{'tenant'})->resultset('NodeIp')
249 ->search_by_ip({ip => $resolved_ip, @active, @times})->with_router->with_l3_domain;
250 ++$have_rows if $set->has_rows;
251 }
252 }
253  
254 # if the user selects Vendor search opt, then
255 # we'll try the manufacturer company name as a fallback
256  
257 if (param('show_vendor') and not $have_rows) {
258 $set = schema(vars->{'tenant'})->resultset('NodeIp')
259 ->with_times
260 ->search(
261 {'manufacturer.company' => { -ilike => ''.sql_match($node)}, @times},
262 {'prefetch' => 'manufacturer'},
263 )->with_router->with_l3_domain;
264 ++$have_rows if $set->has_rows;
265 }
266 }
267 }
268  
269 return unless $set and ($have_rows or $set->has_rows);
270 $set = $set->search_rs({}, { order_by => 'me.mac' });
271  
272 return template 'ajax/search/node_by_ip.tt', {
273 macs => $set,
274 archive_filter => {@active},
275 }, { layout => 'noop' };
276 };
277  
278 true;