| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | package SNMP::Insight; | 
| 2 |  |  |  |  |  |  |  | 
| 3 |  |  |  |  |  |  | # ABSTRACT: SNMP Moose interface | 
| 4 |  |  |  |  |  |  |  | 
| 5 | 2 |  |  | 2 |  | 29178 | use 5.010; | 
|  | 2 |  |  |  |  | 7 |  | 
|  | 2 |  |  |  |  | 67 |  | 
| 6 | 2 |  |  | 2 |  | 8 | use strict; | 
|  | 2 |  |  |  |  | 4 |  | 
|  | 2 |  |  |  |  | 60 |  | 
| 7 | 2 |  |  | 2 |  | 7 | use warnings FATAL => 'all'; | 
|  | 2 |  |  |  |  | 10 |  | 
|  | 2 |  |  |  |  | 130 |  | 
| 8 |  |  |  |  |  |  |  | 
| 9 |  |  |  |  |  |  | our $VERSION = '0.002';    # TRIAL VERSION: | 
| 10 |  |  |  |  |  |  |  | 
| 11 | 2 |  |  | 2 |  | 695 | use Module::Runtime 0.014 'use_package_optimistically'; | 
|  | 2 |  |  |  |  | 1384 |  | 
|  | 2 |  |  |  |  | 11 |  | 
| 12 | 2 |  |  | 2 |  | 614 | use Moose::Util 'is_role'; | 
|  | 2 |  |  |  |  | 123940 |  | 
|  | 2 |  |  |  |  | 17 |  | 
| 13 |  |  |  |  |  |  |  | 
| 14 | 2 |  |  | 2 |  | 550 | use Scalar::Util qw(blessed); | 
|  | 2 |  |  |  |  | 3 |  | 
|  | 2 |  |  |  |  | 117 |  | 
| 15 |  |  |  |  |  |  |  | 
| 16 | 2 |  |  | 2 |  | 861 | use SNMP::Insight::Device; | 
|  | 2 |  |  |  |  | 8 |  | 
|  | 2 |  |  |  |  | 105 |  | 
| 17 | 2 |  |  | 2 |  | 1117 | use SNMP::Insight::Utils qw(_debug); | 
|  | 2 |  |  |  |  | 6 |  | 
|  | 2 |  |  |  |  | 987 |  | 
| 18 |  |  |  |  |  |  |  | 
| 19 |  |  |  |  |  |  | sub open { | 
| 20 | 1 |  |  | 1 | 1 | 5 | my %args = @_; | 
| 21 |  |  |  |  |  |  |  | 
| 22 | 1 |  |  |  |  | 1 | my $session = $args{session}; | 
| 23 | 1 | 50 |  |  |  | 4 | if ( !defined($session) ) { | 
| 24 | 0 |  | 0 |  |  | 0 | my $session_class | 
| 25 |  |  |  |  |  |  | = $args{session_class} || 'SNMP::Insight::Session::NetSNMP'; | 
| 26 | 0 |  |  |  |  | 0 | $session = _load_class( | 
| 27 |  |  |  |  |  |  | $session_class, | 
| 28 | 0 |  |  |  |  | 0 | 'SNMP::Insight::Session', %{ $args{snmp_params} } | 
| 29 |  |  |  |  |  |  | ); | 
| 30 |  |  |  |  |  |  | } | 
| 31 |  |  |  |  |  |  |  | 
| 32 | 1 |  |  |  |  | 45 | my $device = SNMP::Insight::Device->new( session => $session ); | 
| 33 |  |  |  |  |  |  |  | 
| 34 | 1 |  |  |  |  | 2 | my $device_role; | 
| 35 |  |  |  |  |  |  |  | 
| 36 |  |  |  |  |  |  | # if classifier exists but is undef it means we should not use any | 
| 37 |  |  |  |  |  |  | # classifier | 
| 38 |  |  |  |  |  |  |  | 
| 39 | 1 | 50 | 33 |  |  | 12 | if ( $args{classifier} || !exists $args{classifier} ) { | 
| 40 |  |  |  |  |  |  |  | 
| 41 | 1 |  |  |  |  | 2 | my $classifier = $args{classifier}; | 
| 42 |  |  |  |  |  |  |  | 
| 43 | 1 | 50 | 33 |  |  | 10 | if ( !$classifier || !blessed($classifier) ) { | 
| 44 | 1 |  | 50 |  |  | 5 | my $classifier_class = $classifier || 'SNMP::Insight::Classifier'; | 
| 45 | 1 |  |  |  |  | 3 | $classifier = _load_class( | 
| 46 |  |  |  |  |  |  | $classifier_class, 'SNMP::Insight::Classifier', | 
| 47 |  |  |  |  |  |  | device => $device | 
| 48 |  |  |  |  |  |  | ); | 
| 49 |  |  |  |  |  |  | } | 
| 50 |  |  |  |  |  |  |  | 
| 51 | 1 |  |  |  |  | 451 | $device_role = $classifier->classify(); | 
| 52 |  |  |  |  |  |  | } | 
| 53 |  |  |  |  |  |  |  | 
| 54 | 1 | 50 |  |  |  | 98 | if ( !$device_role ) { | 
| 55 | 0 |  |  |  |  | 0 | _debug("debug: no info from classifier"); | 
| 56 | 0 |  |  |  |  | 0 | return $device; | 
| 57 |  |  |  |  |  |  | } | 
| 58 |  |  |  |  |  |  |  | 
| 59 | 1 |  |  |  |  | 5 | _debug("debug: classifier returned $device_role"); | 
| 60 | 1 |  |  |  |  | 3 | my $role_package | 
| 61 |  |  |  |  |  |  | = _load_device_role( $device_role, 'SNMP::Insight::Device' ); | 
| 62 | 1 | 50 |  |  |  | 37 | if ($role_package) { | 
| 63 | 1 |  |  |  |  | 9 | $role_package->meta->apply($device); | 
| 64 |  |  |  |  |  |  | } | 
| 65 |  |  |  |  |  |  | else { | 
| 66 | 0 |  |  |  |  | 0 | warn "$role_package not found, using bare device"; | 
| 67 |  |  |  |  |  |  | } | 
| 68 |  |  |  |  |  |  |  | 
| 69 | 1 |  |  |  |  | 337988 | return $device; | 
| 70 |  |  |  |  |  |  | } | 
| 71 |  |  |  |  |  |  |  | 
| 72 |  |  |  |  |  |  | sub _load_class { | 
| 73 | 1 |  |  | 1 |  | 3 | my ( $class_name, $search_base, %options ) = @_; | 
| 74 |  |  |  |  |  |  |  | 
| 75 | 1 |  |  |  |  | 9 | my $possible_full_name = $search_base . "::" . $class_name; | 
| 76 | 1 |  |  |  |  | 3 | my @possible = ( $possible_full_name, $class_name ); | 
| 77 | 1 |  |  |  |  | 3 | for my $name (@possible) { | 
| 78 | 2 |  |  |  |  | 7 | my $package = use_package_optimistically($name); | 
| 79 | 2 | 100 |  |  |  | 356 | $package->can('new') and return $package->new(%options); | 
| 80 |  |  |  |  |  |  | } | 
| 81 |  |  |  |  |  |  |  | 
| 82 | 0 |  |  |  |  | 0 | return; | 
| 83 |  |  |  |  |  |  | } | 
| 84 |  |  |  |  |  |  |  | 
| 85 |  |  |  |  |  |  | sub _load_device_role { | 
| 86 | 1 |  |  | 1 |  | 2 | my ( $role_name, $search_base ) = @_; | 
| 87 |  |  |  |  |  |  |  | 
| 88 | 1 |  |  |  |  | 3 | my $possible_full_name = $search_base . "::" . $role_name; | 
| 89 | 1 |  |  |  |  | 2 | my @possible = ( $possible_full_name, $role_name ); | 
| 90 | 1 |  |  |  |  | 2 | for my $name (@possible) { | 
| 91 | 1 |  |  |  |  | 3 | my $package = use_package_optimistically($name); | 
| 92 | 1 |  |  |  |  | 17 | use_package_optimistically($name); | 
| 93 | 1 | 50 |  |  |  | 64 | is_role($package) and return $package; | 
| 94 |  |  |  |  |  |  | } | 
| 95 |  |  |  |  |  |  |  | 
| 96 | 0 |  |  |  |  |  | return; | 
| 97 |  |  |  |  |  |  | } | 
| 98 |  |  |  |  |  |  |  | 
| 99 |  |  |  |  |  |  | 1;    # End of SNMP::Insight | 
| 100 |  |  |  |  |  |  |  | 
| 101 |  |  |  |  |  |  | # Local Variables: | 
| 102 |  |  |  |  |  |  | # mode: cperl | 
| 103 |  |  |  |  |  |  | # indent-tabs-mode: nil | 
| 104 |  |  |  |  |  |  | # cperl-indent-level: 4 | 
| 105 |  |  |  |  |  |  | # cperl-indent-parens-as-block: t | 
| 106 |  |  |  |  |  |  | # End: | 
| 107 |  |  |  |  |  |  |  | 
| 108 |  |  |  |  |  |  | __END__ | 
| 109 |  |  |  |  |  |  |  | 
| 110 |  |  |  |  |  |  | =pod | 
| 111 |  |  |  |  |  |  |  | 
| 112 |  |  |  |  |  |  | =head1 NAME | 
| 113 |  |  |  |  |  |  |  | 
| 114 |  |  |  |  |  |  | SNMP::Insight - SNMP Moose interface | 
| 115 |  |  |  |  |  |  |  | 
| 116 |  |  |  |  |  |  | =head1 VERSION | 
| 117 |  |  |  |  |  |  |  | 
| 118 |  |  |  |  |  |  | version 0.002 | 
| 119 |  |  |  |  |  |  |  | 
| 120 |  |  |  |  |  |  | =head1 SYNOPSIS | 
| 121 |  |  |  |  |  |  |  | 
| 122 |  |  |  |  |  |  | SNMP Moose interface: | 
| 123 |  |  |  |  |  |  |  | 
| 124 |  |  |  |  |  |  | use SNMP::Insight; | 
| 125 |  |  |  |  |  |  |  | 
| 126 |  |  |  |  |  |  | my $device = SNMP::Insight::open( | 
| 127 |  |  |  |  |  |  | snmp_params => { | 
| 128 |  |  |  |  |  |  | hostname  => 'localhost', | 
| 129 |  |  |  |  |  |  | community => 'public', | 
| 130 |  |  |  |  |  |  | version   => "2c", | 
| 131 |  |  |  |  |  |  | }); | 
| 132 |  |  |  |  |  |  |  | 
| 133 |  |  |  |  |  |  | ... | 
| 134 |  |  |  |  |  |  |  | 
| 135 |  |  |  |  |  |  | =head1 DESCRIPTION | 
| 136 |  |  |  |  |  |  |  | 
| 137 |  |  |  |  |  |  | SNMP::Insight is a Perl 5 module that provides a simple Object | 
| 138 |  |  |  |  |  |  | Oriented inteface to access SNMP enabled devices and to describe SNMP | 
| 139 |  |  |  |  |  |  | MIBs and devices. SNMP::Insight it's based on Moose and uses Net::SNMP | 
| 140 |  |  |  |  |  |  | for a pure Perl SNMP implementation. | 
| 141 |  |  |  |  |  |  |  | 
| 142 |  |  |  |  |  |  | Warning: this release is still alpha quality! | 
| 143 |  |  |  |  |  |  |  | 
| 144 |  |  |  |  |  |  | =head1 FUNCTIONS | 
| 145 |  |  |  |  |  |  |  | 
| 146 |  |  |  |  |  |  | =head2 open() | 
| 147 |  |  |  |  |  |  |  | 
| 148 |  |  |  |  |  |  | Create a SNMP::Insight::Device object, loading all the needed MIB roles. | 
| 149 |  |  |  |  |  |  |  | 
| 150 |  |  |  |  |  |  | =head1 SEE ALSO | 
| 151 |  |  |  |  |  |  |  | 
| 152 |  |  |  |  |  |  | =over 4 | 
| 153 |  |  |  |  |  |  |  | 
| 154 |  |  |  |  |  |  |  | 
| 155 |  |  |  |  |  |  |  | 
| 156 |  |  |  |  |  |  | =back | 
| 157 |  |  |  |  |  |  |  | 
| 158 |  |  |  |  |  |  | * In the SNMP::Insight distribution | 
| 159 |  |  |  |  |  |  | L<SNMP::Insight::Session> | 
| 160 |  |  |  |  |  |  | L<SNMP::Insight::Device> | 
| 161 |  |  |  |  |  |  | L<SNMP::Insight::Classifier> | 
| 162 |  |  |  |  |  |  | L<SNMP::Insight::MIB> | 
| 163 |  |  |  |  |  |  |  | 
| 164 |  |  |  |  |  |  | * Similar modules on CPAN L<SNMP::Info> | 
| 165 |  |  |  |  |  |  |  | 
| 166 |  |  |  |  |  |  | =head2 CONTRIBUTORS | 
| 167 |  |  |  |  |  |  |  | 
| 168 |  |  |  |  |  |  | =over 4 | 
| 169 |  |  |  |  |  |  |  | 
| 170 |  |  |  |  |  |  |  | 
| 171 |  |  |  |  |  |  |  | 
| 172 |  |  |  |  |  |  | =back | 
| 173 |  |  |  |  |  |  |  | 
| 174 |  |  |  |  |  |  | * Vittorio Nesta | 
| 175 |  |  |  |  |  |  |  | 
| 176 |  |  |  |  |  |  | * Enrico Liguori | 
| 177 |  |  |  |  |  |  |  | 
| 178 |  |  |  |  |  |  | =head1 AUTHOR | 
| 179 |  |  |  |  |  |  |  | 
| 180 |  |  |  |  |  |  | Gabriele Mambrini <g.mambrini@gmail.com> | 
| 181 |  |  |  |  |  |  |  | 
| 182 |  |  |  |  |  |  | =head1 COPYRIGHT AND LICENSE | 
| 183 |  |  |  |  |  |  |  | 
| 184 |  |  |  |  |  |  | This software is copyright (c) 2015 by Gabriele Mambrini. | 
| 185 |  |  |  |  |  |  |  | 
| 186 |  |  |  |  |  |  | This is free software; you can redistribute it and/or modify it under | 
| 187 |  |  |  |  |  |  | the same terms as the Perl 5 programming language system itself. | 
| 188 |  |  |  |  |  |  |  | 
| 189 |  |  |  |  |  |  | =cut |