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

filter: Statementstmt 0.0% 0 / 150 Branchbran 0.0% 0 / 54 Conditioncond 0.0% 0 / 45 MC/DCmcdc 0.0% 0 / 27 Subroutinesub 0.0% 0 / 11 Podpod 0.0% 0 / 1 totaltotal 0.0% 0 / 288 CC 25 SCAR 61.4
CC
25
Cov
0%
_search_node
650.0
BEGIN
2.0
BEGIN
2.0
CRAP 464.9
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 # JIT compiles longer than an address or prefix search takes; the
76 # template fetches the rows, so the render runs inside the transaction too
77 if ($node =~ m{^(?:$RE{net}{IPv4}|$RE{net}{IPv6})(?:/\d+)?$}i) {
78 return schema(vars->{'tenant'})->without_jit(\&_search_node);
79 }
80 return _search_node();
81 };
82  
83 sub _search_node {
84 my $node = param('q');
85 my $agenot = param('age_invert') || '0';
86 my ( $start, $end ) = (param('daterange') // '') =~ m/(\d+-\d+-\d+)/gmx;
87  
88 my $mac = NetAddr::MAC->new(mac => ($node || ''));
89 undef $mac if
90 ($mac and $mac->as_ieee
91 and (($mac->as_ieee eq '00:00:00:00:00:00')
92 or ($mac->as_ieee !~ m/^$RE{net}{MAC}$/i)));
93  
94 my @active = (param('archived') ? () : (-bool => 'active'));
95 my (@times, @wifitimes, @porttimes);
96  
97 if ( $start and $end ) {
98 $start = $start . ' 00:00:00';
99 $end = $end . ' 23:59:59';
100  
101 if ($agenot) {
102 @times = (-or => [
103 time_first => [ undef ],
104 time_last => [ { '<', $start }, { '>', $end } ]
105 ]);
106 @wifitimes = (-or => [
107 time_last => [ undef ],
108 time_last => [ { '<', $start }, { '>', $end } ],
109 ]);
110 @porttimes = (-or => [
111 creation => [ undef ],
112 creation => [ { '<', $start }, { '>', $end } ]
113 ]);
114 }
115 else {
116 @times = (-or => [
117 -and => [
118 time_first => undef,
119 time_last => undef,
120 ],
121 -and => [
122 time_last => { '>=', $start },
123 time_last => { '<=', $end },
124 ],
125 ]);
126 @wifitimes = (-or => [
127 time_last => undef,
128 -and => [
129 time_last => { '>=', $start },
130 time_last => { '<=', $end },
131 ],
132 ]);
133 @porttimes = (-or => [
134 creation => undef,
135 -and => [
136 creation => { '>=', $start },
137 creation => { '<=', $end },
138 ],
139 ]);
140 }
141 }
142  
143 my ($likeval, $likeclause) = sql_match($node, not param('partial'));
144 my $using_wildcards = (($likeval ne $node) ? 1 : 0);
145  
146 my @where_mac =
147 ($using_wildcards ? \['me.mac::text ILIKE ?', $likeval]
148 : ((!defined $mac or $mac->errstr) ? \'0=1' : ('me.mac' => $mac->as_ieee)) );
149  
150 my $sightings = schema(vars->{'tenant'})->resultset('Node')
151 ->search({-and => [@where_mac, @active, @times]}, {
152 order_by => {'-desc' => 'time_last'},
153 '+columns' => [
154 'device.dns',
155 'device.name',
156 { time_first_stamp => \"to_char(time_first, 'YYYY-MM-DD HH24:MI')" },
157 { time_last_stamp => \"to_char(time_last, 'YYYY-MM-DD HH24:MI')" },
158 ],
159 join => 'device',
160 });
161  
162 $sightings = $sightings->with_port_context if param('port_context');
163  
164 my $ips = schema(vars->{'tenant'})->resultset('NodeIp')
165 ->search({-and => [@where_mac, @active, @times]}, {
166 order_by => {'-desc' => 'time_last'},
167 '+columns' => [
168 'manufacturer.company',
169 'manufacturer.abbrev',
170 { time_first_stamp => \"to_char(time_first, 'YYYY-MM-DD HH24:MI')" },
171 { time_last_stamp => \"to_char(time_last, 'YYYY-MM-DD HH24:MI')" },
172 ],
173 join => 'manufacturer'
174 })->with_router->with_l3_domain;
175  
176 my $netbios = schema(vars->{'tenant'})->resultset('NodeNbt')
177 ->search({-and => [@where_mac, @active, @times]}, {
178 order_by => {'-desc' => 'time_last'},
179 '+columns' => [
180 'manufacturer.company',
181 'manufacturer.abbrev',
182 { time_first_stamp => \"to_char(time_first, 'YYYY-MM-DD HH24:MI')" },
183 { time_last_stamp => \"to_char(time_last, 'YYYY-MM-DD HH24:MI')" },
184 ],
185 join => 'manufacturer'
186 });
187  
188 my $wireless = schema(vars->{'tenant'})->resultset('NodeWireless')->search(
189 { -and => [@where_mac, @wifitimes] },
190 { order_by => { '-desc' => 'time_last' },
191 '+columns' => [
192 'manufacturer.company',
193 'manufacturer.abbrev',
194 {
195 time_last_stamp => \"to_char(time_last, 'YYYY-MM-DD HH24:MI')"
196 }],
197 join => 'manufacturer'
198 }
199 );
200  
201 my $rs_dp = schema(vars->{'tenant'})->resultset('DevicePort');
202 if ($sightings->has_rows or $ips->has_rows or $netbios->has_rows) {
203 my $ports = param('deviceports')
204 ? $rs_dp->search({ -and => [@where_mac] }, { order_by => { '-desc' => 'creation' }}) : undef;
205  
206 return template 'ajax/search/node_by_mac.tt', {
207 ips => $ips,
208 sightings => $sightings,
209 ports => $ports,
210 wireless => $wireless,
211 netbios => $netbios,
212 }, { layout => 'noop' };
213 }
214 else {
215 my $ports = param('deviceports')
216 ? $rs_dp->search({ -and => [@where_mac, @porttimes] }, { order_by => { '-desc' => 'creation' }}) : undef;
217  
218 if (defined $ports and $ports->has_rows) {
219 return template 'ajax/search/node_by_mac.tt', {
220 ips => $ips,
221 sightings => $sightings,
222 ports => $ports,
223 wireless => $wireless,
224 netbios => $netbios,
225 }, { layout => 'noop' };
226 }
227 }
228  
229 my $have_rows = 0;
230 my $set = schema(vars->{'tenant'})->resultset('NodeNbt')
231 ->search_by_name({nbname => $likeval, @active, @times});
232 ++$have_rows if $set->has_rows;
233  
234 unless ( $have_rows ) {
235 if ($node =~ m{^(?:$RE{net}{IPv4}|$RE{net}{IPv6})(?:/\d+)?$}i
236 and my $ip = NetAddr::IP::Lite->new($node)) {
237  
238 # search_by_ip() will extract cidr notation if necessary
239 $set = schema(vars->{'tenant'})->resultset('NodeIp')
240 ->search_by_ip({ip => $ip, @active, @times})->with_router->with_l3_domain;
241 ++$have_rows if $set->has_rows;
242 }
243 else {
244 $set = schema(vars->{'tenant'})->resultset('NodeIp')
245 ->search_by_dns({
246 ($using_wildcards ? (dns => $likeval) :
247 (dns => "${likeval}.\%", suffix => setting('domain_suffix'))),
248 @active,
249 @times,
250 })->with_router->with_l3_domain;
251 ++$have_rows if $set->has_rows;
252  
253 # try DNS lookup as fallback
254 if (not $using_wildcards and not $have_rows) {
255 my $resolved_ip = ipv4_from_hostname($node);
256  
257 if ($resolved_ip) {
258 $set = schema(vars->{'tenant'})->resultset('NodeIp')
259 ->search_by_ip({ip => $resolved_ip, @active, @times})->with_router->with_l3_domain;
260 ++$have_rows if $set->has_rows;
261 }
262 }
263  
264 # if the user selects Vendor search opt, then
265 # we'll try the manufacturer company name as a fallback
266  
267 if (param('show_vendor') and not $have_rows) {
268 $set = schema(vars->{'tenant'})->resultset('NodeIp')
269 ->with_times
270 ->search(
271 {'manufacturer.company' => { -ilike => ''.sql_match($node)}, @times},
272 {'prefetch' => 'manufacturer'},
273 )->with_router->with_l3_domain;
274 ++$have_rows if $set->has_rows;
275 }
276 }
277 }
278  
279 return unless $set and ($have_rows or $set->has_rows);
280 $set = $set->search_rs({}, { order_by => 'me.mac' });
281  
282 return template 'ajax/search/node_by_ip.tt', {
283 macs => $set,
284 archive_filter => {@active},
285 }, { layout => 'noop' };
286 }
287  
288 true;