Skip to content

Commit

Permalink
fix: Make network tasks expiration a function of backend-collect-timeout
Browse files Browse the repository at this point in the history
Closes #327
  • Loading branch information
g-bougard committed Nov 13, 2023
1 parent 609839c commit 64e405c
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 6 deletions.
2 changes: 2 additions & 0 deletions Changes
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ netdiscovery/netinventory:
* Use Parallel::ForkManager for netdiscovery & netinventory tasks
* Preload mibsupport as optimization
* Add ESX & RemoteInventory scanning support to netdiscovery
* fix #327: Make task expiration a function of backend-collect-timeout configuration
Add backend-collect-timeout support to glpi-netdiscovery & glpi-netinventory scripts

esx:
* Refactoring to share more code between task & glpi-esx script and to support netscan
Expand Down
10 changes: 9 additions & 1 deletion bin/glpi-netdiscovery
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ GetOptions(
'protocol=s@',
'threads=i',
'timeout=i',
'backend-collect-timeout=s',
'v1',
'v2c',
'control',
Expand Down Expand Up @@ -179,7 +180,9 @@ glpi-netdiscovery [options] --first <address> --last <address>
--v1 select SNMP version 1 (the default)
--v2c select SNMP version 2c (1 by default)
--credentials <STRING> SNMP credentials (version:1,community:public)
--timeout <TIME SNMP timeout, in seconds (1)
--timeout <TIME> SNMP timeout, in seconds (1)
--backend-collect-timeout <TIME>
base expiration timeout, in seconds (180)
--entity <ENTITY> GLPI entity
--threads <COUNT> number of discovery threads (1)
--control output control messages
Expand Down Expand Up @@ -293,6 +296,11 @@ In the case version is set to 3:
Set SNMP timeout, in seconds.
=item B<--backen-collect-timeout> I<TIME>
Set base expiration timeout, in seconds. Global task expiration will depend on
the number of ips.
=item B<--entity> I<ENTITY>
Set GLPI entity.
Expand Down
8 changes: 8 additions & 0 deletions bin/glpi-netinventory
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ GetOptions(
'protocol=s@',
'threads=i',
'timeout=i',
'backend-collect-timeout=s',
'v1',
'v2c',
'control',
Expand Down Expand Up @@ -181,6 +182,8 @@ glpi-netinventory [options] [--host <host>|--file <file>]
--community <STRING> community string (public)
--credentials <STRING> SNMP credentials (version:1,community:public)
--timeout <TIME> SNMP timeout, in seconds (15)
--backend-collect-timeout <TIME>
base expiration timeout, in seconds (180)
--type <TYPE> force device type
--threads <COUNT> number of inventory threads (1)
--control output control messages
Expand Down Expand Up @@ -282,6 +285,11 @@ In the case version is set to 3:
Set SNMP timeout, in seconds.
=item B<--backen-collect-timeout> I<TIME>
Set base expiration timeout, in seconds. It is used to set one device scan:
180 by default, means 900 (5x180) by device.
=item B<--type> I<TYPE>
Force device type, instead of relying on automatic identification. Currently
Expand Down
1 change: 0 additions & 1 deletion lib/GLPI/Agent/HTTP/Server/ToolBox/Inventory.pm
Original file line number Diff line number Diff line change
Expand Up @@ -907,7 +907,6 @@ sub netscan {
);

$logger->info("Running $task->{name} task...");
$netdisco->{target_expiration} = $ip ? 300 : 60;
$netdisco->run();
my $chrono = sprintf("%0.3f", gettimeofday() - $starttime);
$logger->info("$task->{name}: ".($name || $ip_ranges->[0])." $procname done");
Expand Down
11 changes: 9 additions & 2 deletions lib/GLPI/Agent/Task/NetDiscovery.pm
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,7 @@ sub run {
$manager->set_waitpid_blocking_sleep(0);

my %jobs = ();
my $netscan = 0;

# Callback to update %queues
$manager->run_on_finish(
Expand All @@ -189,6 +190,9 @@ sub run {
my $jobid = $job->pid;
$jobs{$jobid} = $job;

# We need to find if job is a netscan to compute the right expiration time
$netscan = 1 if $job->netscan();

$self->{logger}->debug("initializing job $jobid");

# process each iprange
Expand Down Expand Up @@ -239,8 +243,10 @@ sub run {

# Define a realistic block scan expiration : at least one minute by address

# Can be set from GLPI::Agent::HTTP::Server::ToolBox::Inventory
my $target_expiration = $self->{target_expiration} || 60;
# Define a job expiration based on backend-collect-timeout but not less than 1 minute by device
# Always make it larger if running a netscan
my $target_expiration = $self->{config}->{'backend-collect-timeout'} || 60;
$target_expiration *= 5 if $netscan;
$target_expiration = 60 if $target_expiration < 60;
setExpirationTime( timeout => $max_count * $target_expiration );
my $expiration = getExpirationTime();
Expand Down Expand Up @@ -305,6 +311,7 @@ sub run {

if ($expiration && time > $expiration) {
$self->{logger}->warning("Aborting netdiscovery task as it reached expiration time");
$self->{logger}->info("You can set backend-collect-timout higher than the default to use a longer expiration timeout");
$abort ++;
last;
}
Expand Down
6 changes: 4 additions & 2 deletions lib/GLPI/Agent/Task/NetInventory.pm
Original file line number Diff line number Diff line change
Expand Up @@ -130,8 +130,9 @@ sub run {
$skip_start_stop = $job->skip_start_stop || any { defined($_->{PID}) } $job->devices();
}

# Define a job expiration: 15 minutes by device to scan should be enough, but not less than an hour
my $target_expiration = 900;
# Define a job expiration based on backend-collect-timeout: by default 15 minutes
# by device to scan should be enough, keeping a large minimal global task expiration of one hour
my $target_expiration = 5*$self->{config}->{'backend-collect-timeout'};
my $global_timeout = $devices_count * $target_expiration;
$global_timeout = 3600 if $global_timeout < 3600;
setExpirationTime( timeout => $global_timeout );
Expand Down Expand Up @@ -215,6 +216,7 @@ sub run {

if ($expiration && time > $expiration) {
$self->{logger}->warning("Aborting netinventory job as it reached expiration time");
$self->{logger}->info("You can set backend-collect-timout higher than the default to use a longer expiration timeout");
$abort ++;
last;
}
Expand Down

0 comments on commit 64e405c

Please sign in to comment.