File Coverage

blib/lib/App/Manoc/Netwalker/Discover/Workers.pm
Criterion Covered Total %
statement 7 9 77.7
branch n/a
condition n/a
subroutine 3 3 100.0
pod n/a
total 10 12 83.3


line stmt bran cond sub pod time code
1             package App::Manoc::Netwalker::Discover::Workers;
2 1     1   1574 use Moose;
  1         2  
  1         6  
3             our $VERSION = '2.99.2'; ##TRIAL VERSION
4              
5 1     1   6101 use namespace::autoclean;
  1         39  
  1         10  
6              
7             with 'App::Manoc::Netwalker::WorkersRole';
8             with 'App::Manoc::Logger::Role';
9              
10 1     1   476 use App::Manoc::Netwalker::Discover::Task;
  0            
  0            
11              
12             use Try::Tiny;
13             use POE qw(Filter::Reference Filter::Line);
14              
15             use aliased 'App::Manoc::DB::Result::DiscoverSession';
16              
17             has config => (
18             is => 'ro',
19             isa => 'App::Manoc::Netwalker::Config',
20             required => 1
21             );
22              
23             has schema => (
24             is => 'ro',
25             required => 1
26             );
27              
28             has scoreboard => (
29             is => 'ro',
30             isa => 'HashRef',
31             default => sub { {} },
32             );
33              
34             has current_session => (
35             is => 'rw',
36             isa => 'Maybe[Object]'
37             );
38              
39             sub BUILD {
40              
41             my $self = shift;
42              
43             $self->schema->resultset('DiscoverSession')
44             ->search( { status => DiscoverSession->STATUS_RUNNING } )
45             ->update( { status => DiscoverSession->STATUS_WAITING } );
46             }
47              
48              
49             sub on_tick {
50             my $self = shift;
51              
52             $self->schedule_hosts;
53             }
54              
55              
56             sub schedule_session {
57             my $self = shift;
58              
59             if ( $self->current_session && !$self->current_session->is_done ) {
60             return;
61             }
62              
63             my $rs = $self->schema->resultset('DiscoverSession');
64             my $session = $rs->search(
65             {
66             status =>
67             { -in => [ DiscoverSession->STATUS_WAITING, DiscoverSession->STATUS_NEW, ] }
68             }
69             )->first();
70              
71             $self->current_session($session);
72             return unless $session;
73              
74             $self->log->debug( "found waiting discover session " . $session->id );
75             $session->status( DiscoverSession->STATUS_RUNNING );
76             $session->update;
77             }
78              
79              
80             sub schedule_hosts {
81             my $self = shift;
82              
83             $self->current_session or $self->schedule_session;
84             $self->current_session or return;
85              
86             my $session = $self->current_session;
87              
88             my $curr_addr = $session->next_addr;
89             my $to_addr = $session->to_addr;
90              
91             $curr_addr ||= $session->from_addr;
92              
93             while ( $curr_addr <= $to_addr ) {
94              
95             if ( $self->check_worker_threshold ) {
96             $self->log->debug("queue is full, stop scheduling addresses");
97             last;
98             }
99              
100             $self->scoreboard->{$curr_addr} = 'QUEUED';
101             $self->enqueue( sub { $self->discover_address( $session->id, $curr_addr ) } );
102             $self->log->debug("enqueued address $curr_addr");
103              
104             $curr_addr = App::Manoc::IPAddress::IPv4->new( { numeric => $curr_addr->numeric + 1 } );
105             }
106              
107             $session->next_addr($curr_addr);
108             if ( $curr_addr > $to_addr ) {
109             $session->status( $session->STATUS_DONE );
110             $self->current_session(undef);
111             }
112              
113             $session->update();
114             }
115              
116              
117             sub worker_done {
118             my $self = shift;
119              
120             my $session = $self->current_session;
121             return unless $session;
122             $session->discard_changes;
123              
124             if ( $session->is_running ) {
125             $self->schedule_hosts;
126             }
127             else {
128             $self->log->debug("session has been stopped");
129             $self->current_session(undef);
130             }
131             }
132              
133              
134             sub discover_address {
135             my ( $self, $session_id, $address ) = @_;
136              
137             my $updater = App::Manoc::Netwalker::DiscoverTask->new(
138             {
139             schema => $self->schema,
140             config => $self->config,
141             session_id => $session_id,
142             address => $address,
143             }
144             );
145             try {
146             $self->log->debug("running scanner for $address");
147             $updater->scan();
148             }
149             catch {
150             $self->log->debug("Got error $_ while scanning $address");
151             };
152              
153             }
154              
155             no Moose;
156             __PACKAGE__->meta->make_immutable;
157              
158             # Local Variables:
159             # mode: cperl
160             # indent-tabs-mode: nil
161             # cperl-indent-level: 4
162             # cperl-indent-parens-as-block: t
163             # End:
164              
165             __END__
166              
167             =pod
168              
169             =head1 NAME
170              
171             App::Manoc::Netwalker::Discover::Workers
172              
173             =head1 VERSION
174              
175             version 2.99.2
176              
177             =head1 METHODS
178              
179             =head2 on_tick
180              
181             Called by the scheduler, calls C<schedule_hosts>.
182              
183             =head2 schedule_session
184              
185             Select a new session to be processed
186              
187             =head2 schedule_hosts
188              
189             If there is a discovery session in progress generates new address to be scanned and enqueues them.
190             Number of generated addresses is limited by the worker queue max lenght.
191              
192             When a session is over schedule the next one if exists.
193              
194             =head2 worker_done
195              
196             Called at the end of session. Schedule the next hosts unless the session has been stopped.
197              
198             =head2 discover_address( $session_id, $address )
199              
200             Worker entry point, starting a new L<App::Manoc::Netwalker::DiscoverTask>.
201              
202             =head1 AUTHORS
203              
204             =over 4
205              
206             =item *
207              
208             Gabriele Mambrini <gmambro@cpan.org>
209              
210             =item *
211              
212             Enrico Liguori
213              
214             =back
215              
216             =head1 COPYRIGHT AND LICENSE
217              
218             This software is copyright (c) 2017 by Gabriele Mambrini.
219              
220             This is free software; you can redistribute it and/or modify it under
221             the same terms as the Perl 5 programming language system itself.
222              
223             =cut