App-Netdisco

 view release on metacpan or  search on metacpan

lib/App/Netdisco/Web/Plugin/Search/Node.pm  view on Meta::CPAN

package App::Netdisco::Web::Plugin::Search::Node;

use Dancer ':syntax';
use Dancer::Plugin::DBIC;
use Dancer::Plugin::Auth::Extensible;

use NetAddr::IP::Lite ':lower';
use Regexp::Common 'net';
use NetAddr::MAC ();
use POSIX qw/strftime/;

use App::Netdisco::Web::Plugin;
use App::Netdisco::Util::DNS 'ipv4_from_hostname';
use App::Netdisco::Util::Web 'sql_match';

register_search_tab({
    tag => 'node',
    label => 'Node',
    api_endpoint => 1,
    api_parameters => [
      q => {
        description => 'MAC Address or IP Address or Hostname (without Domain Suffix) of a Node (supports SQL or "*" wildcards)',
        required => 1,
      },
      partial => {
        description => 'Partially match the "q" parameter (wildcard characters not required)',
        type => 'boolean',
        default => 'false',
      },
      deviceports => {
        description => 'MAC Address search will include Device Port MACs',
        type => 'boolean',
        default => 'true',
      },
      show_vendor => {
        description => 'Include interface Vendor in results',
        type => 'boolean',
        default => 'false',
      },
      archived => {
        description => 'Include archived records in results',
        type => 'boolean',
        default => 'false',
      },
      daterange => {
        description => 'Date Range in format "YYYY-MM-DD to YYYY-MM-DD"',
        default => ('1970-01-01 to '. strftime('%Y-%m-%d', gmtime)),
      },
      age_invert => {
        description => 'Results should NOT be within daterange',
        type => 'boolean',
        default => 'false',
      },
      # mac_format is used only in the template (will be IEEE) in results
      #mac_format => {
      #},
      # stamps param is used only in the template (they will be included)
      #stamps => {
      #},
    ],
});

# nodes matching the param as an IP or DNS hostname or MAC
get '/ajax/content/search/node' => require_login sub {
    my $node = param('q');
    send_error('Missing node', 400) unless $node;
    return unless ($node =~ m/\w/); # need some alphanum at least
    content_type('text/html');

    my $agenot = param('age_invert') || '0';
    my ( $start, $end ) = param('daterange') =~ m/(\d+-\d+-\d+)/gmx;

    my $mac = NetAddr::MAC->new(mac => ($node || ''));
    undef $mac if
      ($mac and $mac->as_ieee
      and (($mac->as_ieee eq '00:00:00:00:00:00')
        or ($mac->as_ieee !~ m/^$RE{net}{MAC}$/i)));

    my @active = (param('archived') ? () : (-bool => 'active'));



( run in 1.108 second using v1.01-cache-2.11-cpan-39bf76dae61 )