@@ -203,24 +203,17 @@ sub terminate {
203
203
if ($self -> {current_task });
204
204
}
205
205
206
- sub runTarget {
207
- my ($self , $target , $responses_only ) = @_ ;
206
+ sub getContact {
207
+ my ($self , $target , $plannedTasks ) = @_ ;
208
208
209
- if ($target -> isType(' local' ) || $target -> isType(' server' )) {
210
- $self -> {logger }-> info(" target $target ->{id}: " . $target -> getType() . " " . $target -> getName());
211
- }
209
+ my $response ;
212
210
213
- # the prolog/contact dialog must be done once for all tasks,
214
- # but only for server targets
215
- my ($response , $contact_response );
216
- my $client ;
217
- my @plannedTasks = $target -> plannedTasks();
218
211
if ($target -> isGlpiServer()) {
219
212
GLPI::Agent::HTTP::Client::GLPI-> require();
220
213
return $self -> {logger }-> error(" GLPI Protocol library can't be loaded" )
221
214
if $EVAL_ERROR ;
222
215
223
- $client = GLPI::Agent::HTTP::Client::GLPI-> new(
216
+ my $client = GLPI::Agent::HTTP::Client::GLPI-> new(
224
217
logger => $self -> {logger },
225
218
config => $self -> {config },
226
219
agentid => uuid_to_string($self -> {agentid }),
@@ -236,7 +229,7 @@ sub runTarget {
236
229
$httpd_conf {" httpd-port" } = $self -> {server }-> {port };
237
230
}
238
231
239
- my %enabled = map { lc ($_ ) => 1 } @plannedTasks ;
232
+ my %enabled = map { lc ($_ ) => 1 } @{ $ plannedTasks} ;
240
233
my $contact = GLPI::Agent::Protocol::Contact-> new(
241
234
logger => $self -> {logger },
242
235
deviceid => $self -> {deviceid },
@@ -306,14 +299,14 @@ sub runTarget {
306
299
$self -> {_disabled_remoteinventory } = delete $disabled {remoteinventory };
307
300
# Never disable inventory if force option is used
308
301
delete $disabled {inventory } if $self -> {config }-> {force };
309
- @ plannedTasks = grep { ! exists ($disabled {lc ($_ )}) } @plannedTasks ;
302
+ $response -> { plannedTasks } = [ grep { ! exists ($disabled {lc ($_ )}) } @{ $ plannedTasks} ] ;
310
303
} elsif (!ref ($disabled )) {
311
304
$disabled = lc ($disabled );
312
305
# Only disable inventory if force option is not set
313
306
if ($disabled ne " inventory" || !$self -> {config }-> {force }) {
314
- @ plannedTasks = grep {
315
- lc ($_ ) ne $disabled
316
- } @plannedTasks ;
307
+ $response -> { plannedTasks } = [
308
+ grep { lc ($_ ) ne $disabled } @{ $plannedTasks }
309
+ ] ;
317
310
}
318
311
}
319
312
}
@@ -322,7 +315,7 @@ sub runTarget {
322
315
# Handle tasks informations returned by server in CONTACT answer
323
316
if (ref ($tasks ) eq " HASH" ) {
324
317
# Only keep task server support for planned tasks
325
- foreach my $task (map { lc ($_ ) } @plannedTasks ) {
318
+ foreach my $task (map { lc ($_ ) } @{ $ plannedTasks} ) {
326
319
next unless ref ($tasks -> {$task }) eq ' HASH' ;
327
320
328
321
# Keep task supporting announced by server
@@ -354,15 +347,17 @@ sub runTarget {
354
347
}
355
348
}
356
349
}
357
-
358
- # Keep contact response
359
- $contact_response = $response ;
360
350
}
361
351
362
- # By default, PROLOG request could be avoided when communicating with a GLPI server
363
- # But it still may be required if we detect server supports any task due to glpiinventory plugin
364
- if ($target -> isType(' server' ) && $target -> doProlog()) {
352
+ return $response ;
353
+ }
354
+
355
+ sub getProlog {
356
+ my ($self , $target ) = @_ ;
357
+
358
+ my $response ;
365
359
360
+ if ($target -> isType(' server' )) {
366
361
return unless GLPI::Agent::HTTP::Client::OCS-> require();
367
362
368
363
my $agentid ;
@@ -372,7 +367,7 @@ sub runTarget {
372
367
$agentid = uuid_to_string($self -> {agentid })
373
368
unless $target -> isGlpiServer();
374
369
375
- $client = GLPI::Agent::HTTP::Client::OCS-> new(
370
+ my $client = GLPI::Agent::HTTP::Client::OCS-> new(
376
371
logger => $self -> {logger },
377
372
config => $self -> {config },
378
373
agentid => $agentid ,
@@ -401,7 +396,6 @@ sub runTarget {
401
396
unless ($target -> isGlpiServer()) {
402
397
$self -> {logger }-> info(" $target ->{id} answer shows it supports GLPI Agent protocol" );
403
398
$target -> isGlpiServer(' true' );
404
- return $self -> runTarget($target ) unless $response -> expiration;
405
399
}
406
400
} else {
407
401
# update target
@@ -413,20 +407,72 @@ sub runTarget {
413
407
}
414
408
}
415
409
416
- # Used when running tasks after a taskrun event
417
- if ($responses_only ) {
418
- return {
419
- contact => $contact_response ,
420
- response => $response
421
- };
410
+ return $response ;
411
+ }
412
+
413
+ sub runTarget {
414
+ my ($self , $target , $responses_only ) = @_ ;
415
+
416
+ if ($target -> isType(' local' ) || $target -> isType(' server' )) {
417
+ $self -> {logger }-> info(" target $target ->{id}: " . $target -> getType() . " " . $target -> getName());
418
+ }
419
+
420
+ # the prolog/contact dialog must be done once for all tasks,
421
+ # but only for server targets
422
+ my @plannedTasks = $target -> plannedTasks();
423
+ my @requests = ();
424
+ my $responses = {};
425
+ push @requests , ' CONTACT' if $target -> isGlpiServer();
426
+ push @requests , ' PROLOG' if !@requests && $target -> isType(' server' );
427
+ my %requested = qw( CONTACT 0 PROLOG 0) ;
428
+
429
+ while (@requests ) {
430
+ my $request = shift @requests ;
431
+ next if $responses -> {$request };
432
+
433
+ my $response ;
434
+ $requested {$request } = 1;
435
+
436
+ if ($request eq ' CONTACT' ) {
437
+
438
+ $response = $self -> getContact($target , \@plannedTasks );
439
+ # Still return on error
440
+ return $response if $response && !ref ($response );
441
+
442
+ # Update plannedTasks
443
+ if (ref ($response ) && $response -> {plannedTasks }) {
444
+ my $plannedTasks = delete $response -> {plannedTasks };
445
+ @plannedTasks = @{$plannedTasks };
446
+ }
447
+
448
+ # Check condition where we also need to request PROLOG after a CONTACT
449
+ push @requests , ' PROLOG'
450
+ if ref ($response ) && $target -> doProlog() && !$requested {PROLOG };
451
+
452
+ # By default, PROLOG request could be avoided when communicating with a GLPI server
453
+ # But it still may be required if we detect server supports any task due to glpiinventory plugin
454
+ } elsif ($request eq ' PROLOG' ) {
455
+
456
+ $response = $self -> getProlog($target , \@plannedTasks );
457
+ # Still return on error
458
+ return $response if $response && !ref ($response );
459
+
460
+ push @requests , ' CONTACT'
461
+ if ref ($response ) && $target -> isGlpiServer() && !$requested {CONTACT };
462
+ }
463
+
464
+ $responses -> {$request } = $response if ref ($response );
422
465
}
423
466
467
+ # Used when running tasks after a taskrun event
468
+ return $responses if $responses_only ;
469
+
424
470
foreach my $name (@plannedTasks ) {
425
- my $server_response = $response ;
426
- if ($contact_response ) {
471
+ my $server_response = $responses -> { PROLOG } // $responses -> { CONTACT } ;
472
+ if ($responses -> { CONTACT } ) {
427
473
# Be sure to use expected response for task
428
474
my $task_server = $target -> getTaskServer($name ) // ' glpi' ;
429
- $server_response = $contact_response
475
+ $server_response = $responses -> { CONTACT }
430
476
if $task_server eq ' glpi' ;
431
477
}
432
478
eval {
0 commit comments