File Coverage

blib/lib/App/Manoc/Netwalker/Discover/Task.pm
Criterion Covered Total %
statement 10 12 83.3
branch n/a
condition n/a
subroutine 4 4 100.0
pod n/a
total 14 16 87.5


line stmt bran cond sub pod time code
1             package App::Manoc::Netwalker::Discover::Task;
2             #ABSTRACT: Netwalker discover task
3              
4              
5 1     1   6 use Moose;
  1         2  
  1         5  
6              
7             our $VERSION = '2.99.2'; ##TRIAL VERSION
8              
9             with 'App::Manoc::Logger::Role';
10              
11 1     1   5742 use Try::Tiny;
  1         3  
  1         59  
12 1     1   6 use Class::Load qw(load_class);
  1         1  
  1         36  
13              
14 1     1   422 use App::Manoc::Manifold::SNMP::Simple;
  0            
  0            
15             use App::Manoc::IPAddress::IPv4;
16              
17             use Net::Ping;
18             use Socket;
19              
20              
21             has 'schema' => (
22             is => 'ro',
23             isa => 'Object',
24             required => 1
25             );
26              
27              
28             has 'config' => (
29             is => 'ro',
30             isa => 'App::Manoc::Netwalker::Config',
31             required => 1
32             );
33              
34              
35             has 'session_id' => (
36             is => 'ro',
37             isa => 'Int',
38             required => 1
39             );
40              
41              
42             has 'session' => (
43             is => 'ro',
44             isa => 'Maybe[Object]',
45             lazy => 1,
46             builder => '_build_session',
47             );
48              
49              
50             has 'address' => (
51             is => 'ro',
52             isa => 'App::Manoc::IPAddress::IPv4',
53             required => 1
54             );
55              
56              
57             has 'credentials' => (
58             is => 'ro',
59             isa => 'HashRef',
60             lazy => 1,
61             builder => '_build_credentials',
62             );
63              
64             has '_ping_handler' => (
65             is => 'ro',
66             isa => 'Maybe[Object]',
67             lazy => 1,
68             builder => '_build_ping_handler'
69             );
70              
71             sub _build_credentials {
72             my $self = shift;
73              
74             my $credentials = { snmp_community => $self->session->snmp_community };
75             $credentials->{snmp_community} ||= $self->config->snmp_community;
76             $credentials->{snmp_version} = $self->config->snmp_version;
77              
78             return $credentials;
79             }
80              
81             sub _build_session {
82             my $self = shift;
83              
84             return $self->schema->resultset('DiscoverSession')->find( $self->session_id );
85             }
86              
87             sub _build_ping_handler {
88             my $self = shift;
89             return Net::Ping->new();
90             }
91              
92             sub _create_manifold {
93             my $self = shift;
94             my $manifold_name = shift;
95             my %params = @_;
96              
97             my $manifold;
98             try {
99             $manifold = App::Manoc::Manifold->new_manifold( $manifold_name, %params );
100             }
101             catch {
102             my $error = "Internal error while creating manifold $manifold_name: $_";
103             $self->log->debug($error);
104             return;
105             };
106              
107             $manifold or $self->log->debug("Manifold constructor returned undef");
108             return $manifold;
109             }
110              
111              
112             sub scan {
113             my ($self) = @_;
114              
115             my $address = $self->address->unpadded;
116             $self->log->debug("ping $address");
117              
118             if ( !$self->_ping_handler->ping($address) ) {
119             $self->log->debug("$address is not alive, skipping");
120             return;
121             }
122              
123             my $discovered_host =
124             $self->session->find_or_create_related( 'discovered_hosts', { address => $address } );
125              
126             try {
127             my $name = gethostbyaddr( $address, AF_INET );
128             $self->log->debug("querying dns for $address");
129             $discovered_host->hostname($name);
130             };
131              
132             if ( $self->session->use_snmp ) {
133             $self->log->debug("snmp scan $address");
134             try {
135             my $m = $self->_create_manifold(
136             'SNMP::Simple',
137             credentials => $self->credentials,
138             host => $address
139             );
140             $m->connect;
141             $discovered_host->vendor( $m->vendor );
142             $discovered_host->os( $m->os );
143             $discovered_host->os( $m->os_ver );
144              
145             $discovered_host->hostname( $m->name );
146             }
147             catch {
148             $self->log->debug("got error $_ while scanning snmp");
149             };
150             }
151              
152             $discovered_host->update();
153             }
154              
155             1;
156              
157             # Local Variables:
158             # mode: cperl
159             # indent-tabs-mode: nil
160             # cperl-indent-level: 4
161             # cperl-indent-parens-as-block: t
162             # End:
163              
164             __END__
165              
166             =pod
167              
168             =head1 NAME
169              
170             App::Manoc::Netwalker::Discover::Task - Netwalker discover task
171              
172             =head1 VERSION
173              
174             version 2.99.2
175              
176             =head1 DESCRIPTION
177              
178             A class which implements a scan on a single IP address and eventuallu
179             store its findings in Manoc DB.
180              
181             =head1 ATTRIBUTES
182              
183             =head2 schema
184              
185             =head2 config
186              
187             =head2 session_id
188              
189             Identifier for the current discovery session. Required.
190              
191             =head2 session
192              
193             Current discovery session, identified by session id.
194              
195             =head2 address
196              
197             L<App::Manoc::IPAddress::IPv4> object pointing the target of the scan. Required.
198              
199             =head2 credentials
200              
201             Netwalker credentials hash. Defaults to use Netwalker configuration.
202              
203             =head1 METHODS
204              
205             =head2 scan
206              
207             Ping C<$self->address> by using ping and if it's alive try to get information using DNS and SNMP::Simple.
208              
209             =head1 AUTHORS
210              
211             =over 4
212              
213             =item *
214              
215             Gabriele Mambrini <gmambro@cpan.org>
216              
217             =item *
218              
219             Enrico Liguori
220              
221             =back
222              
223             =head1 COPYRIGHT AND LICENSE
224              
225             This software is copyright (c) 2017 by Gabriele Mambrini.
226              
227             This is free software; you can redistribute it and/or modify it under
228             the same terms as the Perl 5 programming language system itself.
229              
230             =cut