287 lines
		
	
	
		
			6.7 KiB
		
	
	
	
		
			Perl
		
	
	
		
			Executable File
		
	
	
	
	
			
		
		
	
	
			287 lines
		
	
	
		
			6.7 KiB
		
	
	
	
		
			Perl
		
	
	
		
			Executable File
		
	
	
	
	
| #!/usr/bin/env perl
 | |
| 
 | |
| use strict;
 | |
| use warnings;
 | |
| 
 | |
| our $home;
 | |
| 
 | |
| BEGIN {
 | |
|   use FindBin;
 | |
|   FindBin::again();
 | |
| 
 | |
|   $home = ($ENV{NETDISCO_HOME} || $ENV{HOME});
 | |
| 
 | |
|   # try to find a localenv if one isn't already in place.
 | |
|   if (!exists $ENV{PERL_LOCAL_LIB_ROOT}) {
 | |
|       use File::Spec;
 | |
|       my $localenv = File::Spec->catfile($FindBin::RealBin, 'localenv');
 | |
|       exec($localenv, $0, @ARGV) if -f $localenv;
 | |
|       $localenv = File::Spec->catfile($home, 'perl5', 'bin', 'localenv');
 | |
|       exec($localenv, $0, @ARGV) if -f $localenv;
 | |
| 
 | |
|       die "Sorry, can't find libs required for App::Netdisco.\n"
 | |
|         if !exists $ENV{PERLBREW_PERL};
 | |
|   }
 | |
| }
 | |
| 
 | |
| BEGIN {
 | |
|   use Path::Class;
 | |
| 
 | |
|   # stuff useful locations into @INC and $PATH
 | |
|   unshift @INC,
 | |
|     dir($FindBin::RealBin)->parent->subdir('lib')->stringify,
 | |
|     dir($FindBin::RealBin, 'lib')->stringify;
 | |
| }
 | |
| 
 | |
| # for netdisco app config
 | |
| use App::Netdisco;
 | |
| use Dancer qw/:moose :script/;
 | |
| use Dancer::Plugin::DBIC 'schema';
 | |
| 
 | |
| info "App::Netdisco version $App::Netdisco::VERSION loaded.";
 | |
| 
 | |
| use Try::Tiny;
 | |
| use Pod::Usage;
 | |
| use Getopt::Long;
 | |
| Getopt::Long::Configure ("bundling");
 | |
| 
 | |
| my ($device, $port, $extra, $debug) = (undef, undef, undef, 0);
 | |
| my $result = GetOptions(
 | |
|   'device|d=s' => \$device,
 | |
|   'port|p=s'   => \$port,
 | |
|   'extra|e=s'  => \$extra,
 | |
|   'debug|D+'   => \$debug,
 | |
| ) or pod2usage(
 | |
|   -msg => 'error: bad options',
 | |
|   -verbose => 0,
 | |
|   -exitval => 1,
 | |
| );
 | |
| 
 | |
| my $CONFIG = config();
 | |
| $CONFIG->{logger} = 'console';
 | |
| $CONFIG->{log} = ($debug ? 'debug' : 'info');
 | |
| 
 | |
| $ENV{INFO_TRACE} = 1 if $debug > 1;
 | |
| $ENV{DBIC_TRACE} = 1 if $debug > 2; $ENV{DBIC_TRACE_PROFILE} = 'console';
 | |
| $ENV{SNMP_TRACE} = 1 if $debug > 3;
 | |
| $ENV{INFO_TRACE} = 2 if $debug > 4;
 | |
| $ENV{SNMP_TRACE} = 2 if $debug > 5;
 | |
| $ENV{SNMP_TRACE} = 3 if $debug > 6;
 | |
| 
 | |
| # reconfigure logging to force console output
 | |
| Dancer::Logger->init('console', $CONFIG);
 | |
| 
 | |
| # for the in-memory local job queue
 | |
| schema('daemon')->deploy;
 | |
| 
 | |
| # get requested action
 | |
| my $action = shift @ARGV;
 | |
| 
 | |
| unless ($action) {
 | |
|     pod2usage(
 | |
|       -msg => 'error: missing action!',
 | |
|       -verbose => 2,
 | |
|       -exitval => 2,
 | |
|     );
 | |
| }
 | |
| 
 | |
| # create worker (placeholder object for the role methods)
 | |
| {
 | |
|   package MyWorker;
 | |
| 
 | |
|   use Moo;
 | |
|   use Module::Load ();
 | |
|   use Data::Printer ();
 | |
| 
 | |
|   with 'App::Netdisco::Daemon::Worker::Poller::Device';
 | |
|   with 'App::Netdisco::Daemon::Worker::Poller::Arpnip';
 | |
|   with 'App::Netdisco::Daemon::Worker::Poller::Macsuck';
 | |
|   with 'App::Netdisco::Daemon::Worker::Poller::Nbtstat';
 | |
|   with 'App::Netdisco::Daemon::Worker::Poller::Expiry';
 | |
|   with 'App::Netdisco::Daemon::Worker::Interactive::DeviceActions';
 | |
|   with 'App::Netdisco::Daemon::Worker::Interactive::PortActions';
 | |
| 
 | |
|   eval { Module::Load::load 'App::Netdisco::Util::Graph' };
 | |
|   sub graph {
 | |
|     App::Netdisco::Util::Graph::graph();
 | |
|     return ('done', 'Generated graph data.');
 | |
|   }
 | |
| 
 | |
|   use App::Netdisco::Util::NodeMonitor ();
 | |
|   sub monitor {
 | |
|     App::Netdisco::Util::NodeMonitor::monitor();
 | |
|     return ('done', 'Generated monitor data.');
 | |
|   }
 | |
| 
 | |
|   use App::Netdisco::Util::SNMP ();
 | |
|   sub show {
 | |
|     $extra ||= 'interfaces'; my $class = undef;
 | |
|     ($class, $extra) = split(/::([^:]+)$/, $extra);
 | |
|     if ($class and $extra) {
 | |
|         $class = 'SNMP::Info::'.$class;
 | |
|     }
 | |
|     else {
 | |
|         $extra = $class;
 | |
|         undef $class;
 | |
|     }
 | |
|     my $i = App::Netdisco::Util::SNMP::snmp_connect($device, $class);
 | |
|     Data::Printer::p($i->$extra);
 | |
|     return ('done', "Showed $extra response from $device.");
 | |
|   }
 | |
| }
 | |
| my $worker = MyWorker->new();
 | |
| 
 | |
| # belt and braces check before we go ahead
 | |
| if (not $worker->can( $action )) {
 | |
|   pod2usage(
 | |
|     -msg => (sprintf 'error: %s is not a valid action', $action),
 | |
|     -verbose => 2,
 | |
|     -exitval => 3,
 | |
|   );
 | |
| }
 | |
| 
 | |
| # what job are we asked to do?
 | |
| my $job = schema('daemon')->resultset('Admin')->new_result({
 | |
|   job => 0,
 | |
|   action => $action,
 | |
|   device => $device,
 | |
|   port   => $port,
 | |
|   subaction => $extra,
 | |
| });
 | |
| 
 | |
| # do job
 | |
| my ($status, $log);
 | |
| try {
 | |
|     info sprintf '%s: started at %s', $action, scalar localtime;
 | |
|     ($status, $log) = $worker->$action($job);
 | |
| }
 | |
| catch {
 | |
|     $status = 'error';
 | |
|     $log = "error running job: $_";
 | |
| };
 | |
| 
 | |
| info sprintf '%s: finished at %s', $action, scalar localtime;
 | |
| info sprintf '%s: status %s: %s', $action, $status, $log;
 | |
| 
 | |
| exit ($status eq 'done' ? 0 : 1);
 | |
| 
 | |
| =head1 NAME
 | |
| 
 | |
| netdisco-do - Run any Netdisco job from the command-line.
 | |
| 
 | |
| =head1 SYNOPSIS
 | |
| 
 | |
|  ~/bin/netdisco-do <action> [-D] [-d <device> [-p <port>] [-e <extra>]]
 | |
| 
 | |
| Regardless of Netdisco configuration, debug level logging is enabled if the
 | |
| C<-D> flag is given (up to seven times).
 | |
| 
 | |
| =head1 DESCRIPTION
 | |
| 
 | |
| This program allows you to run any Netdisco poller job from the command-line.
 | |
| 
 | |
| Note that some jobs (C<discoverall>, C<macwalk>, C<arpwalk>), C<nbtwalk>)
 | |
| simply add entries to the Netdisco job queue for other jobs, so won't seem
 | |
| to do much when you trigger them.
 | |
| 
 | |
| =head1 ACTIONS
 | |
| 
 | |
| =head2 discover
 | |
| 
 | |
| Run a discover on the device (specified with C<-d>).
 | |
| 
 | |
| =head2 macsuck
 | |
| 
 | |
| Run a macsuck on the device (specified with C<-d>).
 | |
| 
 | |
| =head2 arpnip
 | |
| 
 | |
| Run an arpnip on the device (specified with C<-d>).
 | |
| 
 | |
| =head2 nbtstat
 | |
| 
 | |
| Run an nbtstat on the node (specified with C<-d>).
 | |
| 
 | |
| =head2 graph
 | |
| 
 | |
| Generate GrapgViz graphs for the largest cluster of devices.
 | |
| 
 | |
| =head2 show
 | |
| 
 | |
| Dump the content of an SNMP MIB leaf, which is useful for diagnostics and
 | |
| troubleshooting. You should provide the "C<-e>" option which is the name of
 | |
| the leaf (such as C<interfaces> or C<uptime>).
 | |
| 
 | |
| If you wish to test with a device class other than that discovered, prefix the
 | |
| leaf with the class short name, for example "C<Layer3::C3550::interfaces>" or
 | |
| "C<Layer2::HP::uptime>".
 | |
| 
 | |
| =head2 set_location
 | |
| 
 | |
| Set the SNMP location field on the device (specified with C<-d>). Pass the
 | |
| location string in the C<-e> extra parameter.
 | |
| 
 | |
| =head2 set_contact
 | |
| 
 | |
| Set the SNMP contact field on the device (specified with C<-d>). Pass the
 | |
| contact name in the C<-e> extra parameter.
 | |
| 
 | |
| =head2 set_portname
 | |
| 
 | |
| Set the description on a device port. Requires the C<-d> parameter (device),
 | |
| C<-p> parameter (port), and C<-e> parameter (description).
 | |
| 
 | |
| =head2 set_portcontrol
 | |
| 
 | |
| Set the up/down status on a device port. Requires the C<-d> parameter
 | |
| (device), C<-p> parameter (port), and C<-e> parameter ("up" or "down").
 | |
| 
 | |
| =head2 set_vlan
 | |
| 
 | |
| Set the native VLAN on a device port. Requires the C<-d> parameter (device),
 | |
| C<-p> parameter (port), and C<-e> parameter (VLAN number).
 | |
| 
 | |
| =head2 set_power
 | |
| 
 | |
| Set the PoE on/off status on a device port. Requires the C<-d> parameter
 | |
| (device), C<-p> parameter (port), and C<-e> parameter ("true" or "false").
 | |
| 
 | |
| =head1 DEBUG LEVELS
 | |
| 
 | |
| The debug flag "C<-D>" can be specified up to seven times, and enables the
 | |
| following items in order:
 | |
| 
 | |
| =over 4
 | |
| 
 | |
| =item 1
 | |
| 
 | |
| Netdisco debug log level
 | |
| 
 | |
| =item 2
 | |
| 
 | |
| L<SNMP::Info> logging
 | |
| 
 | |
| =item 3
 | |
| 
 | |
| L<DBIx::Class> logging
 | |
| 
 | |
| =item 4
 | |
| 
 | |
| L<SNMP> (net-snmp) logging
 | |
| 
 | |
| =item 5
 | |
| 
 | |
| Even more L<SNMP::Info> logging
 | |
| 
 | |
| =item 6
 | |
| 
 | |
| Even more L<SNMP> (net-snmp) logging
 | |
| 
 | |
| =item 7
 | |
| 
 | |
| Even more than more L<SNMP> (net-snmp) logging
 | |
| 
 | |
| =back
 | |
| 
 | |
| =cut
 |