diff --git a/centreon/plugins/script.pm b/centreon/plugins/script.pm index a38689806..86ea675fe 100644 --- a/centreon/plugins/script.pm +++ b/centreon/plugins/script.pm @@ -28,7 +28,7 @@ use FindBin; use Pod::Usage; use Pod::Find qw(pod_where); -my %handlers = (DIE => {}); +my %handlers = (DIE => {}, ALRM => {}); my $global_version = '(dev)'; my $alternative_fatpacker = 0; @@ -45,18 +45,18 @@ sub new { # Avoid to destroy because it keeps a ref on the object. # A problem if we execute it multiple times in the same perl execution # Use prepare_destroy - $self->set_signal_handlers; + $self->set_signal_handlers(); return $self; } sub prepare_destroy { my ($self) = @_; - delete $handlers{DIE}->{$self}; + %handlers = undef; } sub set_signal_handlers { - my $self = shift; + my ($self) = @_; $SIG{__DIE__} = \&class_handle_DIE; $handlers{DIE}->{$self} = sub { $self->handle_DIE($_[0]) }; @@ -70,6 +70,12 @@ sub class_handle_DIE { } } +sub class_handle_ALRM { + foreach (keys %{$handlers{ALRM}}) { + &{$handlers{ALRM}->{$_}}(); + } +} + sub handle_DIE { my ($self, $msg) = @_; @@ -78,6 +84,13 @@ sub handle_DIE { $self->{output}->die_exit(); } +sub handle_ALRM { + my ($self) = @_; + + $self->{output}->add_option_msg(short_msg => 'script global timeout'); + $self->{output}->option_exit(); +} + sub get_global_version { return $global_version; } @@ -85,9 +98,7 @@ sub get_global_version { sub get_plugin { my ($self) = @_; - ###### # Need to load global 'Output' and 'Options' - ###### if ($alternative_fatpacker == 0) { require centreon::plugins::options; $self->{options} = centreon::plugins::options->new(); @@ -105,20 +116,28 @@ sub get_plugin { 'ignore-warn-msg' => { name => 'ignore_warn_msg' }, 'version' => { name => 'version' }, 'runas:s' => { name => 'runas' }, + 'global-timeout:s' => { name => 'global_timeout' }, 'environment:s%' => { name => 'environment' }, 'convert-args:s' => { name => 'convert_args' }, }); $self->{options}->parse_options(); - $self->{plugin} = $self->{options}->get_option(argument => 'plugin' ); - $self->{list_plugin} = $self->{options}->get_option(argument => 'list_plugin' ); - $self->{help} = $self->{options}->get_option(argument => 'help' ); - $self->{version} = $self->{options}->get_option(argument => 'version' ); - $self->{runas} = $self->{options}->get_option(argument => 'runas' ); - $self->{environment} = $self->{options}->get_option(argument => 'environment' ); - $self->{ignore_warn_msg} = $self->{options}->get_option(argument => 'ignore_warn_msg' ); - $self->{convert_args} = $self->{options}->get_option(argument => 'convert_args' ); + $self->{plugin} = $self->{options}->get_option(argument => 'plugin'); + $self->{list_plugin} = $self->{options}->get_option(argument => 'list_plugin'); + $self->{help} = $self->{options}->get_option(argument => 'help'); + $self->{version} = $self->{options}->get_option(argument => 'version'); + $self->{runas} = $self->{options}->get_option(argument => 'runas'); + $self->{environment} = $self->{options}->get_option(argument => 'environment'); + $self->{ignore_warn_msg} = $self->{options}->get_option(argument => 'ignore_warn_msg'); + $self->{convert_args} = $self->{options}->get_option(argument => 'convert_args'); + + my $global_timeout = $self->{options}->get_option(argument => 'global_timeout'); + if (defined($global_timeout) && $global_timeout =~ /(\d+)/) { + $SIG{ALRM} = \&class_handle_ALRM; + $handlers{ALRM}->{$self} = sub { $self->handle_ALRM() }; + alarm($1); + } $self->{output}->plugin(name => $self->{plugin}); $self->{output}->check_options(option_results => $self->{options}->get_options()); @@ -354,8 +373,10 @@ sub run { centreon::plugins::misc::mymodule_load(output => $self->{output}, module => $self->{plugin}, error_msg => 'Cannot load module --plugin.'); my $plugin = $self->{plugin}->new(options => $self->{options}, output => $self->{output}); - $plugin->init(help => $self->{help}, - version => $self->{version}); + $plugin->init( + help => $self->{help}, + version => $self->{version} + ); $plugin->run(); } @@ -399,6 +420,10 @@ Perl warn messages are ignored (not displayed). Run the script as a different user (prefer to use directly the good user). +=item B<--global-timeout> + +Set script timeout. + =item B<--environment> Set environment variables for the script (prefer to set it before running it for better performance).