Merge the backend worker plugins branch og-coreplugins
Squashed commit of the following: commit86d0f61d0bAuthor: Oliver Gorwits <oliver@cpan.org> Date: Thu Nov 16 22:26:32 2017 +0000 fix typo commit5aff19621cAuthor: Oliver Gorwits <oliver@cpan.org> Date: Thu Nov 16 22:10:18 2017 +0000 fix use of snmp_connect_ip which does not work for SNMPv3 commit68a56d35bbAuthor: Oliver Gorwits <oliver@cpan.org> Date: Thu Nov 16 20:50:16 2017 +0000 no need for Array::Iterator even though it was cute commit71ee869c02Author: Oliver Gorwits <oliver@cpan.org> Date: Wed Nov 15 22:14:47 2017 +0000 additional doc examples commit620b3fe544Author: Oliver Gorwits <oliver@cpan.org> Date: Wed Nov 15 22:09:05 2017 +0000 stash workers within poller instance, and load plugins explicitly commit2431365583Author: Oliver Gorwits <oliver@cpan.org> Date: Mon Nov 13 22:17:11 2017 +0000 better fix for duplicate module entity index commita400b26704Author: Oliver Gorwits <oliver@cpan.org> Date: Mon Nov 13 22:14:42 2017 +0000 add ignore interfaces for HPE routers commit1502ec1966Author: Oliver Gorwits <oliver@cpan.org> Date: Mon Nov 13 22:08:02 2017 +0000 bug fixes after testing on a real network commit840b6b4069Author: Oliver Gorwits <oliver@cpan.org> Date: Sun Nov 12 20:38:35 2017 +0000 add tests commit2de36c69baAuthor: Oliver Gorwits <oliver@cpan.org> Date: Sun Nov 12 00:14:21 2017 +0000 some reengineering to support proper testing commitc5f138fe62Author: Oliver Gorwits <oliver@cpan.org> Date: Sat Nov 11 14:43:53 2017 +0000 correct algorithm on finalise status, correct logging commit98442a2308Author: Oliver Gorwits <oliver@cpan.org> Date: Thu Nov 9 21:49:45 2017 +0000 bug fixes commite0c6615c87Author: Oliver Gorwits <oliver@cpan.org> Date: Wed Nov 8 20:29:33 2017 +0000 fix bugs commit1eeaba441dAuthor: Oliver Gorwits <oliver@cpan.org> Date: Tue Nov 7 22:30:55 2017 +0000 finish refactor to new desired behaviour (buggy?) commit7edfe88f25Author: Oliver Gorwits <oliver@cpan.org> Date: Mon Nov 6 22:50:51 2017 +0000 fix to work, and correct namespace check commit25907d3544Author: Oliver Gorwits <oliver@cpan.org> Date: Mon Nov 6 21:26:01 2017 +0000 move status tracking and checking inside job instance commit4436150bf4Author: Oliver Gorwits <oliver@cpan.org> Date: Sun Nov 5 20:54:28 2017 +0000 remove global rubbish commit28b016e713Author: Oliver Gorwits <oliver@cpan.org> Date: Sat Nov 4 23:31:51 2017 +0000 fix docs commit650f6c719bAuthor: Oliver Gorwits <oliver@cpan.org> Date: Sat Nov 4 23:22:12 2017 +0000 tidy line commit10f78d5dbeAuthor: Oliver Gorwits <oliver@cpan.org> Date: Sat Nov 4 23:06:20 2017 +0000 add priority and namespace to support fancy worker overrides commitb9f9816d09Author: Oliver Gorwits <oliver@cpan.org> Date: Wed Oct 11 18:33:46 2017 +0100 release 2.036012_001 commitc33bf204a4Merge:5b7ce3f7d3d81eb6Author: Oliver Gorwits <oliver@cpan.org> Date: Wed Oct 11 18:30:23 2017 +0100 Merge branch 'master' into og-coreplugins commit5b7ce3f797Author: Oliver Gorwits <oliver@cpan.org> Date: Mon Oct 9 15:46:09 2017 +0100 cannot Sereal::Encode DBIC row commit0a575f02baAuthor: Oliver Gorwits <oliver@cpan.org> Date: Mon Oct 9 14:07:56 2017 +0100 fix bug in job->device init commit207476950dAuthor: Oliver Gorwits <oliver@cpan.org> Date: Mon Oct 9 14:03:37 2017 +0100 default causes no attr to be created?! commit912f2fa91fAuthor: Oliver Gorwits <oliver@cpan.org> Date: Sun Oct 8 18:43:51 2017 +0100 better debug logging commitdfeb9d9ddcAuthor: Oliver Gorwits <oliver@cpan.org> Date: Sun Oct 8 18:40:02 2017 +0100 make device_auth have driver setting for snmp entries commit460c0c0ee9Merge:3ccd107b98423445Author: Oliver Gorwits <oliver@cpan.org> Date: Sun Oct 8 18:08:58 2017 +0100 Merge branch 'master' into og-coreplugins commit3ccd107bd4Author: Oliver Gorwits <oliver@cpan.org> Date: Sat Oct 7 14:13:58 2017 +0100 fix bug in device->has_layer commita4b9bf2036Author: Oliver Gorwits <oliver@cpan.org> Date: Sat Oct 7 13:58:52 2017 +0100 netdisco-do show takes a param for method in -p commit4389cd0459Author: Oliver Gorwits <oliver@cpan.org> Date: Sat Oct 7 13:36:06 2017 +0100 fix to only check last poll on devices in storage commit58d0fbdddaAuthor: Oliver Gorwits <oliver@cpan.org> Date: Sat Oct 7 13:21:13 2017 +0100 do not run discover parts if properties failed to complete commitb52aaaf1a1Author: Oliver Gorwits <oliver@cpan.org> Date: Sat Oct 7 13:08:46 2017 +0100 fix typo commit41be926921Author: Oliver Gorwits <oliver@cpan.org> Date: Sat Oct 7 13:04:45 2017 +0100 run all check workers commita41d114965Author: Oliver Gorwits <oliver@cpan.org> Date: Sat Oct 7 13:02:46 2017 +0100 fix driver config commitb10908a138Author: Oliver Gorwits <oliver@cpan.org> Date: Sat Oct 7 12:43:50 2017 +0100 use vars() cache between phases commit08b34e083dAuthor: Oliver Gorwits <oliver@cpan.org> Date: Sat Oct 7 11:39:17 2017 +0100 remove die() calls commitb8108986fbAuthor: Oliver Gorwits <oliver@cpan.org> Date: Sat Oct 7 11:31:59 2017 +0100 phase fixups commit273cbbc11bAuthor: Oliver Gorwits <oliver@cpan.org> Date: Sat Oct 7 09:42:41 2017 +0100 change stage to phase commit256c10bae5Author: Oliver Gorwits <oliver@cpan.org> Date: Sat Oct 7 09:35:14 2017 +0100 multi worker actions need not return done from all workers commitee38bae48aAuthor: Oliver Gorwits <oliver@cpan.org> Date: Sat Oct 7 09:05:25 2017 +0100 store result of worker if best for this phase so far commit5bddfc73baAuthor: Oliver Gorwits <oliver@cpan.org> Date: Sat Oct 7 08:50:31 2017 +0100 auto debug-log worker return messages commit8b660a89c0Author: Oliver Gorwits <oliver@cpan.org> Date: Fri Oct 6 07:48:58 2017 +0100 bug fixes commitb58a5816a9Author: Oliver Gorwits <oliver@cpan.org> Date: Fri Oct 6 07:44:20 2017 +0100 remove unnecessary check phases commite44f06364aAuthor: Oliver Gorwits <oliver@cpan.org> Date: Fri Oct 6 07:18:03 2017 +0100 fix unknown command check in netdisco-do commit3af13f0dfeAuthor: Oliver Gorwits <oliver@cpan.org> Date: Fri Oct 6 07:15:59 2017 +0100 introduce noop and refactor checks in all workers commit98463c8cadAuthor: Oliver Gorwits <oliver@cpan.org> Date: Sun Oct 1 10:49:12 2017 +0100 no need to debug log if there are no hooks in phase commit3b32e84312Author: Oliver Gorwits <oliver@cpan.org> Date: Sun Oct 1 08:18:13 2017 +0100 fiddle about with runner logic to fix exit states commit8fdba38ee0Author: Oliver Gorwits <oliver@cpan.org> Date: Fri Sep 29 08:01:42 2017 +0100 cannot reuse a worker as the job will be already set and the wrong plugins loaded commita155d9cb77Author: Oliver Gorwits <oliver@cpan.org> Date: Fri Sep 29 08:01:06 2017 +0100 should defer when we cannot connect to device commit10b5f6cbc4Author: Oliver Gorwits <oliver@cpan.org> Date: Fri Sep 29 08:00:32 2017 +0100 fix bug in where workerconf acls are checked commit2a74e0befaAuthor: Oliver Gorwits <oliver@cpan.org> Date: Fri Sep 29 07:38:05 2017 +0100 can pass device instance to check_* commit4256b117dfAuthor: Oliver Gorwits <oliver@cpan.org> Date: Fri Sep 29 07:27:14 2017 +0100 move device_auth build to be with community defaults setting commita2de2c1616Merge:32be11c38dc4b9bcAuthor: Oliver Gorwits <oliver@cpan.org> Date: Fri Sep 29 07:21:03 2017 +0100 Merge branch 'master' into og-coreplugins commit32be11c3ffAuthor: Oliver Gorwits <oliver@cpan.org> Date: Thu Sep 21 00:09:29 2017 +0100 move remaining interactive actions to be plugins commit3e41c93f5aAuthor: Oliver Gorwits <oliver@cpan.org> Date: Wed Sep 20 21:47:50 2017 +0100 clean snmp handling commit30a2d5dd86Author: Oliver Gorwits <oliver@cpan.org> Date: Wed Sep 20 21:00:29 2017 +0100 make sure check plugins are loaded/run before phases commit3454d95a84Author: Oliver Gorwits <oliver@cpan.org> Date: Wed Sep 20 20:53:52 2017 +0100 capture result on main phase as well commit559fa4f93fAuthor: Oliver Gorwits <oliver@cpan.org> Date: Mon Sep 18 22:46:35 2017 +0100 build device_auth from communities commit1969291719Author: Oliver Gorwits <oliver@cpan.org> Date: Mon Sep 18 22:04:22 2017 +0100 simplify to remove phases and fewer hooks commit6f78032e28Author: Oliver Gorwits <oliver@cpan.org> Date: Thu Sep 14 21:30:03 2017 +0100 add phase to test worker commit6edd2dc879Author: Oliver Gorwits <oliver@cpan.org> Date: Wed Sep 13 21:51:40 2017 +0100 no need to list all plugins commitdfaeb34d8cAuthor: Oliver Gorwits <oliver@cpan.org> Date: Wed Sep 13 20:42:41 2017 +0100 add reset after messing with snmp context or community index commit09214dce92Author: Oliver Gorwits <oliver@cpan.org> Date: Wed Sep 13 20:29:21 2017 +0100 no need to pass $snmp around commit58cd488cccAuthor: Oliver Gorwits <oliver@cpan.org> Date: Wed Sep 13 19:22:40 2017 +0100 refactor layer and pseudo checks commit753acc607fAuthor: Oliver Gorwits <oliver@cpan.org> Date: Wed Sep 13 10:53:12 2017 +0100 use overloaded $device commitd5d39289d6Author: Oliver Gorwits <oliver@cpan.org> Date: Wed Sep 13 10:44:31 2017 +0100 rename init stage to check commit1fdb086183Author: Oliver Gorwits <oliver@cpan.org> Date: Tue Sep 12 08:12:12 2017 +0100 refactor to remove second loop commit64a9491115Author: Oliver Gorwits <oliver@cpan.org> Date: Sun Sep 10 16:09:45 2017 +0100 change to init, first, second stages commit5f2da69697Author: Oliver Gorwits <oliver@cpan.org> Date: Sat Sep 9 22:26:04 2017 +0100 move discover and discoverall to worker plugins commitc6ebb7cf07Author: Oliver Gorwits <oliver@cpan.org> Date: Sat Sep 9 16:44:32 2017 +0100 move arpnip and arpwalk to worker plugins commit16a79463cbAuthor: Oliver Gorwits <oliver@cpan.org> Date: Sat Sep 9 16:27:58 2017 +0100 set snmp driver on macsuck phase workers commit9167e02de5Author: Oliver Gorwits <oliver@cpan.org> Date: Sat Sep 9 15:55:53 2017 +0100 move macsuck and macwalk to worker plugins (macsuck needs snmp scope guard) commit68ca85643bAuthor: Oliver Gorwits <oliver@cpan.org> Date: Sat Sep 9 14:56:15 2017 +0100 move expire and expirenodes to worker plugins commit271ef1a25cAuthor: Oliver Gorwits <oliver@cpan.org> Date: Sat Sep 9 14:46:00 2017 +0100 move nbtstat and nbtwalk to worker plugins commite7508a9ecaAuthor: Oliver Gorwits <oliver@cpan.org> Date: Wed Sep 6 21:23:54 2017 +0100 move all netdisco-do action to worker plugins commit707fc82b99Author: Oliver Gorwits <oliver@cpan.org> Date: Wed Sep 6 21:01:37 2017 +0100 remove psql code from netdisco-do and fix detection of misspelled action commit411918e3f8Author: Oliver Gorwits <oliver@cpan.org> Date: Wed Sep 6 20:56:26 2017 +0100 only load worker plugins for the action commit1f9740c0e2Author: Oliver Gorwits <oliver@cpan.org> Date: Wed Sep 6 18:30:43 2017 +0100 shorten hook names commita59c23de79Author: Oliver Gorwits <oliver@cpan.org> Date: Wed Sep 6 18:27:34 2017 +0100 make psql worker primary, add hook debug log commit36c70220a2Author: Oliver Gorwits <oliver@cpan.org> Date: Tue Sep 5 22:39:22 2017 +0100 allow two forms of worker declaration, and update docs commita79cb9a9e4Author: Oliver Gorwits <oliver@cpan.org> Date: Tue Sep 5 22:10:53 2017 +0100 all the bug fixes and a working plugin!!!!!!!!! :-D commit04896202e0Author: Oliver Gorwits <oliver@cpan.org> Date: Tue Sep 5 21:39:41 2017 +0100 refine runner commit547fce2f3cAuthor: Oliver Gorwits <oliver@cpan.org> Date: Tue Sep 5 20:56:21 2017 +0100 hack the status class to regen if needed commitcd71a0b7a8Author: Oliver Gorwits <oliver@cpan.org> Date: Tue Sep 5 20:41:05 2017 +0100 move status update to job class commitc8e5cea4edAuthor: Oliver Gorwits <oliver@cpan.org> Date: Tue Sep 5 20:37:13 2017 +0100 objectify the running commitf48004fffaAuthor: Oliver Gorwits <oliver@cpan.org> Date: Tue Sep 5 19:58:28 2017 +0100 bug squish commit46ece568f6Author: Oliver Gorwits <oliver@cpan.org> Date: Tue Sep 5 19:54:57 2017 +0100 implement runner?! commitfc9c60f707Author: Oliver Gorwits <oliver@cpan.org> Date: Tue Sep 5 19:28:38 2017 +0100 rename ok to is_ok and change slot names to avoid conflict with creators commit3ee85383abAuthor: Oliver Gorwits <oliver@cpan.org> Date: Tue Sep 5 19:25:41 2017 +0100 skip worker when action is per-device but no creds commit75abdad812Author: Oliver Gorwits <oliver@cpan.org> Date: Mon Sep 4 21:54:37 2017 +0100 further work on retval handling from workers commit4c1fdf4f92Author: Oliver Gorwits <oliver@cpan.org> Date: Mon Sep 4 20:37:53 2017 +0100 move worker plugin loader to Worker.pm commitbe0c5181a3Author: Oliver Gorwits <oliver@cpan.org> Date: Mon Sep 4 20:35:42 2017 +0100 move Runner to Worker namespace commit1c2cf924bcAuthor: Oliver Gorwits <oliver@cpan.org> Date: Mon Sep 4 20:33:20 2017 +0100 worker roles in Role namespace commit3099eda393Author: Oliver Gorwits <oliver@cpan.org> Date: Mon Sep 4 20:30:58 2017 +0100 load workers when runner role is loaded commita8c58a7b05Author: Oliver Gorwits <oliver@cpan.org> Date: Sun Sep 3 22:30:28 2017 +0100 initial broken implementation of the runner commit49b5274c33Author: Oliver Gorwits <oliver@cpan.org> Date: Sun Sep 3 19:04:20 2017 +0100 use run() mixin to exec action commite0a666668aAuthor: Oliver Gorwits <oliver@cpan.org> Date: Sun Sep 3 18:54:44 2017 +0100 fix pod; set status defaults; stub runner mixin commit8eaa33770cAuthor: Oliver Gorwits <oliver@cpan.org> Date: Sun Sep 3 18:45:00 2017 +0100 rename Core to Worker and move other packages around commit4def0af0b0Author: Oliver Gorwits <oliver@cpan.org> Date: Sun Sep 3 17:58:03 2017 +0100 better use of new status class commit8675bf62c6Author: Oliver Gorwits <oliver@cpan.org> Date: Sun Sep 3 17:27:38 2017 +0100 fix hook naming and implement primary workers commitef1bb81f2bAuthor: Oliver Gorwits <oliver@cpan.org> Date: Sun Sep 3 17:26:27 2017 +0100 new backend status class commit5f50dfadf1Author: Oliver Gorwits <oliver@cpan.org> Date: Sun Sep 3 16:51:55 2017 +0100 new Backend package to load core plugins commit3baa7a818aAuthor: Oliver Gorwits <oliver@cpan.org> Date: Sun Sep 3 16:22:29 2017 +0100 remove unnecessary Worker::Common role commit36b4adcc06Author: Oliver Gorwits <oliver@cpan.org> Date: Sun Sep 3 16:17:29 2017 +0100 disambiguate util/backend package and remove backend prelaod commit98bff731bdAuthor: Oliver Gorwits <oliver@cpan.org> Date: Sat Sep 2 08:25:06 2017 +0100 settle on a design for hook override, I think commitfe5c16a16dAuthor: Oliver Gorwits <oliver@cpan.org> Date: Wed Aug 30 20:37:36 2017 +0100 rework docs to be more clear and reflect new operation commitb34ba1977cMerge:31d1977fc34ed61dAuthor: Oliver Gorwits <oliver@cpan.org> Date: Mon Aug 21 21:17:46 2017 +0100 Merge branch 'master' into og-coreplugins commit31d1977f1eAuthor: Oliver Gorwits <oliver@cpan.org> Date: Mon Aug 14 18:11:42 2017 +0100 Revert "move expire code to be initial plugin pilot (broken)" I think we'll only do the new backend code for jobs with a device. This reverts commit07998b72d9. commit61dc80aff8Merge:07998b72ade02db1Author: Oliver Gorwits <oliver@cpan.org> Date: Mon Aug 14 18:10:29 2017 +0100 Merge branch 'master' into og-coreplugins commit07998b72d9Author: Oliver Gorwits <oliver@cpan.org> Date: Sat Aug 5 22:15:00 2017 +0100 move expire code to be initial plugin pilot (broken) commit685ec02108Author: Oliver Gorwits <oliver@cpan.org> Date: Sat Aug 5 22:10:58 2017 +0100 pass $job to the core worker commitd6523fe543Author: Oliver Gorwits <oliver@cpan.org> Date: Sat Aug 5 22:01:49 2017 +0100 $job->device is always a DBIC row commitee6deea01bAuthor: Oliver Gorwits <oliver@cpan.org> Date: Sat Aug 5 18:12:34 2017 +0100 load plugins commitfd80096ca2Author: Oliver Gorwits <oliver@cpan.org> Date: Sat Aug 5 16:53:16 2017 +0100 rename all the things commit464c42d1f5Author: Oliver Gorwits <oliver@cpan.org> Date: Wed Aug 2 10:19:16 2017 +0100 use Scope::Guard to reduce device_auth commitec041dafd2Author: Oliver Gorwits <oliver@cpan.org> Date: Tue Aug 1 15:34:37 2017 +0100 the other way around commit33d2fe13bdAuthor: Oliver Gorwits <oliver@cpan.org> Date: Mon Jul 31 17:57:29 2017 +0100 fix pod commit3faee1cf16Author: Oliver Gorwits <oliver@cpan.org> Date: Mon Jul 31 17:55:10 2017 +0100 remove need for instance() call commitc6d0f1c035Author: Oliver Gorwits <oliver@cpan.org> Date: Wed Jul 26 13:51:23 2017 +0100 add doc note on accessing transports commitdca4b4fc03Author: Oliver Gorwits <oliver@cpan.org> Date: Wed Jul 26 11:50:10 2017 +0100 add backend driver documentation commit052a2acd79Author: Oliver Gorwits <oliver@cpan.org> Date: Wed Jul 26 10:16:58 2017 +0100 rename web plugins doc commit69c9a6393aAuthor: Oliver Gorwits <oliver@cpan.org> Date: Wed Jul 26 10:12:42 2017 +0100 rename args to driverconf commit2586a36f8cAuthor: Oliver Gorwits <oliver@cpan.org> Date: Tue Jul 25 22:41:10 2017 +0100 new version of core plugin manager with better config and filters commit4056831f99Author: Oliver Gorwits <oliver@cpan.org> Date: Tue Jul 25 20:53:56 2017 +0100 change SNMP to be a cached transport singleton commitc31030ef70Author: Oliver Gorwits <oliver@cpan.org> Date: Sun Jul 23 13:46:27 2017 +0100 fixes because Dancer docs are a mess! commitf65ef90b86Author: Oliver Gorwits <oliver@cpan.org> Date: Sat Jul 22 08:11:36 2017 +0100 rename snmp_auth to device_auth and include a little doc on transports commitd61556e1cfAuthor: Oliver Gorwits <oliver@cpan.org> Date: Sat Jul 22 07:54:26 2017 +0100 plugin config added commitde8de56308Author: Oliver Gorwits <oliver@cpan.org> Date: Wed Jul 12 21:38:31 2017 +0100 initial core plugin implementation
This commit is contained in:
@@ -1,18 +1,13 @@
|
||||
package App::Netdisco::Util::SNMP;
|
||||
|
||||
use Dancer qw/:syntax :script/;
|
||||
use App::Netdisco::Util::Device 'get_device';
|
||||
use App::Netdisco::Util::DNS 'hostname_from_ip';
|
||||
use App::Netdisco::Util::Permission ':all';
|
||||
|
||||
use SNMP::Info;
|
||||
use Try::Tiny;
|
||||
use Module::Load ();
|
||||
use Path::Class 'dir';
|
||||
|
||||
use base 'Exporter';
|
||||
our @EXPORT = ();
|
||||
our @EXPORT_OK = qw/
|
||||
snmp_connect snmp_connect_rw snmp_comm_reindex
|
||||
fixup_device_auth get_communities snmp_comm_reindex
|
||||
/;
|
||||
our %EXPORT_TAGS = (all => \@EXPORT_OK);
|
||||
|
||||
@@ -22,337 +17,139 @@ App::Netdisco::Util::SNMP
|
||||
|
||||
=head1 DESCRIPTION
|
||||
|
||||
A set of helper subroutines to support parts of the Netdisco application.
|
||||
Helper functions for L<SNMP::Info> instances.
|
||||
|
||||
There are no default exports, however the C<:all> tag will export all
|
||||
subroutines.
|
||||
|
||||
=head1 EXPORT_OK
|
||||
|
||||
=head2 snmp_connect( $ip )
|
||||
=head2 fixup_device_auth
|
||||
|
||||
Given an IP address, returns an L<SNMP::Info> instance configured for and
|
||||
connected to that device. The IP can be any on the device, and the management
|
||||
interface will be connected to.
|
||||
|
||||
If the device is known to Netdisco and there is a cached SNMP community
|
||||
string, this will be tried first, and then other community string(s) from the
|
||||
application configuration will be tried.
|
||||
|
||||
Returns C<undef> if the connection fails.
|
||||
Rebuilds the C<device_auth> config with missing defaults and other fixups for
|
||||
config changes over time. Returns a list which can replace C<device_auth>.
|
||||
|
||||
=cut
|
||||
|
||||
sub snmp_connect { _snmp_connect_generic('read', @_) }
|
||||
|
||||
=head2 snmp_connect_rw( $ip )
|
||||
|
||||
Same as C<snmp_connect> but uses the read-write community string(s) from the
|
||||
application configuration file.
|
||||
|
||||
Returns C<undef> if the connection fails.
|
||||
|
||||
=cut
|
||||
|
||||
sub snmp_connect_rw { _snmp_connect_generic('write', @_) }
|
||||
|
||||
sub _snmp_connect_generic {
|
||||
my ($mode, $ip, $useclass) = @_;
|
||||
$mode ||= 'read';
|
||||
|
||||
# get device details from db
|
||||
my $device = get_device($ip);
|
||||
|
||||
my %snmp_args = (
|
||||
AutoSpecify => 0,
|
||||
DestHost => $device->ip,
|
||||
# 0 is falsy. Using || with snmpretries equal to 0 will set retries to 2.
|
||||
# check if the setting is 0. If not, use the default value of 2.
|
||||
Retries => (setting('snmpretries') || setting('snmpretries') == 0 ? 0 : 2),
|
||||
Timeout => (setting('snmptimeout') || 1000000),
|
||||
NonIncreasing => (setting('nonincreasing') || 0),
|
||||
BulkWalk => ((defined setting('bulkwalk_off') && setting('bulkwalk_off'))
|
||||
? 0 : 1),
|
||||
BulkRepeaters => (setting('bulkwalk_repeaters') || 20),
|
||||
MibDirs => [ _build_mibdirs() ],
|
||||
IgnoreNetSNMPConf => 1,
|
||||
Debug => ($ENV{INFO_TRACE} || 0),
|
||||
DebugSNMP => ($ENV{SNMP_TRACE} || 0),
|
||||
);
|
||||
|
||||
# an override for bulkwalk
|
||||
$snmp_args{BulkWalk} = 0 if check_acl_no($device, 'bulkwalk_no');
|
||||
|
||||
# further protect against buggy Net-SNMP, and disable bulkwalk
|
||||
if ($snmp_args{BulkWalk}
|
||||
and ($SNMP::VERSION eq '5.0203' || $SNMP::VERSION eq '5.0301')) {
|
||||
|
||||
warning sprintf
|
||||
"[%s] turning off BulkWalk due to buggy Net-SNMP - please upgrade!",
|
||||
$device->ip;
|
||||
$snmp_args{BulkWalk} = 0;
|
||||
}
|
||||
|
||||
# get the community string(s)
|
||||
my @communities = _build_communities($device, $mode);
|
||||
|
||||
# which SNMP versions to try and in what order
|
||||
my @versions =
|
||||
( check_acl_no($device->ip, 'snmpforce_v3') ? (3)
|
||||
: check_acl_no($device->ip, 'snmpforce_v2') ? (2)
|
||||
: check_acl_no($device->ip, 'snmpforce_v1') ? (1)
|
||||
: (reverse (1 .. (setting('snmpver') || 3))) );
|
||||
|
||||
# use existing or new device class
|
||||
my @classes = ($useclass || 'SNMP::Info');
|
||||
if ($device->snmp_class and not $useclass) {
|
||||
unshift @classes, $device->snmp_class;
|
||||
}
|
||||
|
||||
my $info = undef;
|
||||
COMMUNITY: foreach my $comm (@communities) {
|
||||
next unless $comm;
|
||||
|
||||
VERSION: foreach my $ver (@versions) {
|
||||
next unless $ver;
|
||||
|
||||
next if $ver eq 3 and exists $comm->{community};
|
||||
next if $ver ne 3 and !exists $comm->{community};
|
||||
|
||||
CLASS: foreach my $class (@classes) {
|
||||
next unless $class;
|
||||
|
||||
my %local_args = (%snmp_args, Version => $ver);
|
||||
$info = _try_connect($device, $class, $comm, $mode, \%local_args,
|
||||
($useclass ? 0 : 1) );
|
||||
last COMMUNITY if $info;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $info;
|
||||
}
|
||||
|
||||
sub _try_connect {
|
||||
my ($device, $class, $comm, $mode, $snmp_args, $reclass) = @_;
|
||||
my %comm_args = _mk_info_commargs($comm);
|
||||
my $debug_comm = '<hidden>';
|
||||
if ($ENV{SHOW_COMMUNITY}) {
|
||||
$debug_comm = ($comm->{community} ||
|
||||
(sprintf 'v3:%s:%s/%s', ($comm->{user},
|
||||
($comm->{auth}->{proto} || 'noAuth'),
|
||||
($comm->{priv}->{proto} || 'noPriv'))) );
|
||||
}
|
||||
my $info = undef;
|
||||
|
||||
try {
|
||||
debug
|
||||
sprintf '[%s] try_connect with ver: %s, class: %s, comm: %s',
|
||||
$snmp_args->{DestHost}, $snmp_args->{Version}, $class, $debug_comm;
|
||||
Module::Load::load $class;
|
||||
|
||||
$info = $class->new(%$snmp_args, %comm_args) or return;
|
||||
$info = ($mode eq 'read' ? _try_read($info, $device, $comm)
|
||||
: _try_write($info, $device, $comm));
|
||||
|
||||
# first time a device is discovered, re-instantiate into specific class
|
||||
if ($reclass and $info and $info->device_type ne $class) {
|
||||
$class = $info->device_type;
|
||||
debug
|
||||
sprintf '[%s] try_connect with ver: %s, new class: %s, comm: %s',
|
||||
$snmp_args->{DestHost}, $snmp_args->{Version}, $class, $debug_comm;
|
||||
|
||||
Module::Load::load $class;
|
||||
$info = $class->new(%$snmp_args, %comm_args);
|
||||
}
|
||||
}
|
||||
catch {
|
||||
debug $_;
|
||||
};
|
||||
|
||||
return $info;
|
||||
}
|
||||
|
||||
sub _try_read {
|
||||
my ($info, $device, $comm) = @_;
|
||||
|
||||
return undef unless (
|
||||
(not defined $info->error)
|
||||
and defined $info->uptime
|
||||
and ($info->layers or $info->description)
|
||||
and $info->class
|
||||
);
|
||||
|
||||
$device->in_storage
|
||||
? $device->update({snmp_ver => $info->snmp_ver})
|
||||
: $device->set_column(snmp_ver => $info->snmp_ver);
|
||||
|
||||
if ($comm->{community}) {
|
||||
$device->in_storage
|
||||
? $device->update({snmp_comm => $comm->{community}})
|
||||
: $device->set_column(snmp_comm => $comm->{community});
|
||||
}
|
||||
|
||||
# regardless of device in storage, save the hint
|
||||
$device->update_or_create_related('community',
|
||||
{snmp_auth_tag_read => $comm->{tag}}) if $comm->{tag};
|
||||
|
||||
return $info;
|
||||
}
|
||||
|
||||
sub _try_write {
|
||||
my ($info, $device, $comm) = @_;
|
||||
|
||||
my $loc = $info->load_location;
|
||||
$info->set_location($loc) or return undef;
|
||||
return undef unless ($loc eq $info->load_location);
|
||||
|
||||
$device->in_storage
|
||||
? $device->update({snmp_ver => $info->snmp_ver})
|
||||
: $device->set_column(snmp_ver => $info->snmp_ver);
|
||||
|
||||
# one of these two cols must be set
|
||||
$device->update_or_create_related('community', {
|
||||
($comm->{tag} ? (snmp_auth_tag_write => $comm->{tag}) : ()),
|
||||
($comm->{community} ? (snmp_comm_rw => $comm->{community}) : ()),
|
||||
});
|
||||
|
||||
return $info;
|
||||
}
|
||||
|
||||
sub _mk_info_commargs {
|
||||
my $comm = shift;
|
||||
return () unless ref {} eq ref $comm and scalar keys %$comm;
|
||||
|
||||
return (Community => $comm->{community})
|
||||
if exists $comm->{community};
|
||||
|
||||
my $seclevel =
|
||||
(exists $comm->{auth} ?
|
||||
(exists $comm->{priv} ? 'authPriv' : 'authNoPriv' )
|
||||
: 'noAuthNoPriv');
|
||||
|
||||
return (
|
||||
SecName => $comm->{user},
|
||||
SecLevel => $seclevel,
|
||||
( exists $comm->{auth} ? (
|
||||
AuthProto => uc ($comm->{auth}->{proto} || 'MD5'),
|
||||
AuthPass => ($comm->{auth}->{pass} || ''),
|
||||
( exists $comm->{priv} ? (
|
||||
PrivProto => uc ($comm->{priv}->{proto} || 'DES'),
|
||||
PrivPass => ($comm->{priv}->{pass} || ''),
|
||||
) : ()),
|
||||
) : ()),
|
||||
);
|
||||
}
|
||||
|
||||
sub _build_mibdirs {
|
||||
my $home = (setting('mibhome') || dir(($ENV{NETDISCO_HOME} || $ENV{HOME}), 'netdisco-mibs'));
|
||||
return map { dir($home, $_)->stringify }
|
||||
@{ setting('mibdirs') || _get_mibdirs_content($home) };
|
||||
}
|
||||
|
||||
sub _get_mibdirs_content {
|
||||
my $home = shift;
|
||||
# warning 'Netdisco SNMP work will be slow - loading ALL MIBs. Consider setting mibdirs.';
|
||||
my @list = map {s|$home/||; $_} grep {-d} glob("$home/*");
|
||||
return \@list;
|
||||
}
|
||||
|
||||
sub _build_communities {
|
||||
my ($device, $mode) = @_;
|
||||
$mode ||= 'read';
|
||||
my $seen_tags = {}; # for cleaning community table
|
||||
|
||||
my $config = (setting('snmp_auth') || []);
|
||||
my $tag_name = 'snmp_auth_tag_'. $mode;
|
||||
my $stored_tag = eval { $device->community->$tag_name };
|
||||
my $snmp_comm_rw = eval { $device->community->snmp_comm_rw };
|
||||
my @communities = ();
|
||||
|
||||
# try last-known-good read
|
||||
push @communities, {read => 1, community => $device->snmp_comm}
|
||||
if defined $device->snmp_comm and $mode eq 'read';
|
||||
|
||||
# try last-known-good write
|
||||
push @communities, {write => 1, community => $snmp_comm_rw}
|
||||
if $snmp_comm_rw and $mode eq 'write';
|
||||
sub fixup_device_auth {
|
||||
my $config = (setting('device_auth') || setting('snmp_auth') || []);
|
||||
my @new_stanzas = ();
|
||||
|
||||
# new style snmp config
|
||||
foreach my $stanza (@$config) {
|
||||
# user tagged
|
||||
my $tag = '';
|
||||
if (1 == scalar keys %$stanza) {
|
||||
$tag = (keys %$stanza)[0];
|
||||
$stanza = $stanza->{$tag};
|
||||
# user tagged
|
||||
my $tag = '';
|
||||
if (1 == scalar keys %$stanza) {
|
||||
$tag = (keys %$stanza)[0];
|
||||
$stanza = $stanza->{$tag};
|
||||
|
||||
# corner case: untagged lone community
|
||||
if ($tag eq 'community') {
|
||||
$tag = $stanza;
|
||||
$stanza = {community => $tag};
|
||||
}
|
||||
# corner case: untagged lone community
|
||||
if ($tag eq 'community') {
|
||||
$tag = $stanza;
|
||||
$stanza = {community => $tag};
|
||||
}
|
||||
}
|
||||
|
||||
# defaults
|
||||
$stanza->{tag} ||= $tag;
|
||||
++$seen_tags->{ $stanza->{tag} };
|
||||
$stanza->{read} = 1 if !exists $stanza->{read};
|
||||
$stanza->{no} ||= [];
|
||||
$stanza->{only} ||= ['any'];
|
||||
$stanza->{no} = [$stanza->{no}] if ref '' eq ref $stanza->{no};
|
||||
$stanza->{only} = [$stanza->{only}] if ref '' eq ref $stanza->{only};
|
||||
# defaults
|
||||
$stanza->{tag} ||= $tag;
|
||||
$stanza->{read} = 1 if !exists $stanza->{read};
|
||||
$stanza->{no} ||= [];
|
||||
$stanza->{only} ||= ['any'];
|
||||
|
||||
die "error: config: snmpv2 community in snmp_auth must be single item, not list\n"
|
||||
if ref $stanza->{community};
|
||||
die "error: config: snmpv2 community in device_auth must be single item, not list\n"
|
||||
if ref $stanza->{community};
|
||||
|
||||
die "error: config: snmpv3 stanza in snmp_auth must have a tag\n"
|
||||
if not $stanza->{tag}
|
||||
and !exists $stanza->{community};
|
||||
die "error: config: stanza in device_auth must have a tag\n"
|
||||
if not $stanza->{tag} and exists $stanza->{user};
|
||||
|
||||
if ($stanza->{$mode} and check_acl_only($device, $stanza->{only})
|
||||
and not check_acl_no($device, $stanza->{no})) {
|
||||
if ($device->in_storage and
|
||||
$stored_tag and $stored_tag eq $stanza->{tag}) {
|
||||
# last known-good by tag
|
||||
unshift @communities, $stanza
|
||||
}
|
||||
else {
|
||||
push @communities, $stanza
|
||||
}
|
||||
}
|
||||
push @new_stanzas, $stanza
|
||||
}
|
||||
|
||||
# clean the community table of obsolete tags
|
||||
if ($stored_tag and !exists $seen_tags->{ $stored_tag }) {
|
||||
eval { $device->community->update({$tag_name => undef}) };
|
||||
# legacy config
|
||||
# note: read strings tried before write
|
||||
# note: read-write is no longer used for read operations
|
||||
|
||||
push @new_stanzas, map {{
|
||||
read => 1, write => 0,
|
||||
no => [], only => ['any'],
|
||||
community => $_,
|
||||
}} @{setting('community') || []};
|
||||
|
||||
push @new_stanzas, map {{
|
||||
write => 1, read => 0,
|
||||
no => [], only => ['any'],
|
||||
community => $_,
|
||||
}} @{setting('community_rw') || []};
|
||||
|
||||
foreach my $stanza (@new_stanzas) {
|
||||
$stanza->{driver} ||= 'snmp'
|
||||
if exists $stanza->{community}
|
||||
or exists $stanza->{user};
|
||||
}
|
||||
|
||||
# legacy config (note: read strings tried before write)
|
||||
if ($mode eq 'read') {
|
||||
push @communities, map {{
|
||||
read => 1,
|
||||
community => $_,
|
||||
}} @{setting('community') || []};
|
||||
}
|
||||
else {
|
||||
push @communities, map {{
|
||||
write => 1,
|
||||
community => $_,
|
||||
}} @{setting('community_rw') || []};
|
||||
}
|
||||
return @new_stanzas;
|
||||
}
|
||||
|
||||
# but first of all, use external command if configured
|
||||
unshift @communities, _get_external_community($device, $mode)
|
||||
=head2 get_communities( $device, $mode )
|
||||
|
||||
Takes the current C<device_auth> setting and pushes onto the front of the list
|
||||
the last known good SNMP settings used for this mode (C<read> or C<write>).
|
||||
|
||||
=cut
|
||||
|
||||
sub get_communities {
|
||||
my ($device, $mode) = @_;
|
||||
$mode ||= 'read';
|
||||
|
||||
my $seen_tags = {}; # for cleaning community table
|
||||
my $config = (setting('device_auth') || []);
|
||||
my @communities = ();
|
||||
|
||||
# first of all, use external command if configured
|
||||
push @communities, _get_external_community($device, $mode)
|
||||
if setting('get_community') and length setting('get_community');
|
||||
|
||||
return @communities;
|
||||
# last known-good by tag
|
||||
my $tag_name = 'snmp_auth_tag_'. $mode;
|
||||
my $stored_tag = eval { $device->community->$tag_name };
|
||||
|
||||
if ($device->in_storage and $stored_tag) {
|
||||
foreach my $stanza (@$config) {
|
||||
if ($stanza->{tag} and $stored_tag eq $stanza->{tag}) {
|
||||
push @communities, {%$stanza, only => [$device->ip]};
|
||||
last;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
# try last-known-good v2 read
|
||||
push @communities, {
|
||||
read => 1, write => 0, driver => 'snmp',
|
||||
only => [$device->ip],
|
||||
community => $device->snmp_comm,
|
||||
} if defined $device->snmp_comm and $mode eq 'read';
|
||||
|
||||
# try last-known-good v2 write
|
||||
my $snmp_comm_rw = eval { $device->community->snmp_comm_rw };
|
||||
push @communities, {
|
||||
write => 1, read => 0, driver => 'snmp',
|
||||
only => [$device->ip],
|
||||
community => $snmp_comm_rw,
|
||||
} if $snmp_comm_rw and $mode eq 'write';
|
||||
|
||||
# clean the community table of obsolete tags
|
||||
eval { $device->community->update({$tag_name => undef}) }
|
||||
if $device->in_storage
|
||||
and (not $stored_tag or !exists $seen_tags->{ $stored_tag });
|
||||
|
||||
return ( @communities, @$config );
|
||||
}
|
||||
|
||||
sub _get_external_community {
|
||||
my ($device, $mode) = @_;
|
||||
my $cmd = setting('get_community');
|
||||
my $ip = $device->ip;
|
||||
my $host = $device->dns || $ip;
|
||||
my $host = ($device->dns || hostname_from_ip($ip) || $ip);
|
||||
|
||||
if (defined $cmd and length $cmd) {
|
||||
# replace variables
|
||||
@@ -368,6 +165,7 @@ sub _get_external_community {
|
||||
if (length $1 and $mode eq 'read') {
|
||||
return map {{
|
||||
read => 1,
|
||||
only => [$device->ip],
|
||||
community => $_,
|
||||
}} split(m/\s*,\s*/,$1);
|
||||
}
|
||||
@@ -376,6 +174,7 @@ sub _get_external_community {
|
||||
if (length $1 and $mode eq 'write') {
|
||||
return map {{
|
||||
write => 1,
|
||||
only => [$device->ip],
|
||||
community => $_,
|
||||
}} split(m/\s*,\s*/,$1);
|
||||
}
|
||||
@@ -391,6 +190,9 @@ sub _get_external_community {
|
||||
Takes an established L<SNMP::Info> instance and makes a fresh connection using
|
||||
community indexing, with the given C<$vlan> ID. Works for all SNMP versions.
|
||||
|
||||
Passing VLAN "C<0>" (zero) will reset the indexing to the basic v2 community
|
||||
or v3 empty context.
|
||||
|
||||
=cut
|
||||
|
||||
sub snmp_comm_reindex {
|
||||
@@ -399,7 +201,8 @@ sub snmp_comm_reindex {
|
||||
|
||||
if ($ver == 3) {
|
||||
my $prefix = '';
|
||||
my @comms = _build_communities($device, 'read');
|
||||
my @comms = get_communities($device, 'read');
|
||||
# find a context prefix configured by the user
|
||||
foreach my $c (@comms) {
|
||||
next unless $c->{tag}
|
||||
and $c->{tag} eq (eval { $device->community->snmp_auth_tag_read } || '');
|
||||
@@ -410,15 +213,17 @@ sub snmp_comm_reindex {
|
||||
debug
|
||||
sprintf '[%s] reindexing to "%s%s" (ver: %s, class: %s)',
|
||||
$device->ip, $prefix, $vlan, $ver, $snmp->class;
|
||||
$snmp->update(Context => ($prefix . $vlan));
|
||||
$vlan ? $snmp->update(Context => ($prefix . $vlan))
|
||||
: $snmp->update(Context => '');
|
||||
}
|
||||
else {
|
||||
my $comm = $snmp->snmp_comm;
|
||||
|
||||
debug sprintf '[%s] reindexing to vlan %s (ver: %s, class: %s)',
|
||||
$device->ip, $vlan, $ver, $snmp->class;
|
||||
$snmp->update(Community => $comm . '@' . $vlan);
|
||||
$vlan ? $snmp->update(Community => $comm . '@' . $vlan)
|
||||
: $snmp->update(Community => $comm);
|
||||
}
|
||||
}
|
||||
|
||||
1;
|
||||
true;
|
||||
|
||||
Reference in New Issue
Block a user