OCSInventory-NG / UnixAgent

This is the OCS unified agent for Unix operating systems
http://www.ocsinventory-ng.org/en/
GNU General Public License v2.0
90 stars 85 forks source link

`ocsinventory-agent` stuck in a loop with 100% CPU usage when using `--delaytime` option on command-line #426

Closed eguaj closed 1 year ago

eguaj commented 1 year ago

General informations

Operating system : Linux Ubuntu 20.04, Linux Ubuntu 22.04 Perl version : v5.30.0, v5.34.0

OCS Inventory informations

Unix agent version : v2.10.0 (installed from http://deb.ocsinventory-ng.org/ubuntu/)

Problem's description

I would like to use the --delaytime option when running the ocsinventory-agent as a daemon, but the --delaytime option does not seems to support specifying the delay in seconds and if I try to specify it with --delaytime 600 (and the server is not available on the first run), then the process goes into an infinite loop with 100% CPU usage and the message "Going to sleep for 0 second(s)" printed in the log.

The problem comes from the fact that the getopt definition of the --delaytime option does not specify that it takes an argument (number of seconds for the delay).

So, the option is considered a boolean option and affected a value of 1 when present.

Later, when the value is used to select a random delay (with rand($delaytime = 1)) it always returns 0 causing the process to retry immediately without delay.

https://github.com/OCSInventory-NG/UnixAgent/blob/9eedb6be4572036617ba1ffb35d16d5b2c1f4db9/lib/Ocsinventory/Agent/Config.pm#L138

https://github.com/OCSInventory-NG/UnixAgent/blob/9eedb6be4572036617ba1ffb35d16d5b2c1f4db9/lib/Ocsinventory/Agent.pm#L269

The getop definition should be :

        "delaytime=s"      =>   \$self->{config}{delaytime},

The problem only occurs when trying to specify the delaytime from the command-line. Specifying the delaytime in the configuration file does not shows the problem.

Inventory log file ( optional )

Try to specify a --delaytime 600 when running the agent:

# /usr/bin/ocsinventory-agent -d --delaytime 600

The agent goes into an infinite loop with 0 sec. delay:

# tail -F /var/log/ocsinventory-agent.log
[…]
[Thu Jan 19 13:40:53 2023][error] Cannot establish communication : 500 Can't connect to xxx:443 (Nom ou service inconnu)
[Thu Jan 19 13:40:53 2023][info] Don't send the inventory
[Thu Jan 19 13:40:53 2023][info] Going to sleep for 0 second(s)
[Thu Jan 19 13:40:53 2023][error] Cannot establish communication : 500 Can't connect to xxx:443 (Nom ou service inconnu)
[Thu Jan 19 13:40:53 2023][info] Don't send the inventory
[Thu Jan 19 13:40:53 2023][info] Going to sleep for 0 second(s)
[Thu Jan 19 13:40:53 2023][error] Cannot establish communication : 500 Can't connect to xxx:443 (Nom ou service inconnu)
[Thu Jan 19 13:40:53 2023][info] Don't send the inventory
[Thu Jan 19 13:40:53 2023][info] Going to sleep for 0 second(s)
[Thu Jan 19 13:40:53 2023][error] Cannot establish communication : 500 Can't connect to xxx:443 (Nom ou service inconnu)
[Thu Jan 19 13:40:53 2023][info] Don't send the inventory
[Thu Jan 19 13:40:53 2023][info] Going to sleep for 0 second(s)
[…]