This commit is contained in:
Colin Gagnaire 2019-07-11 13:20:55 +02:00
commit add1e8a8c2
1 changed files with 41 additions and 16 deletions

View File

@ -28,7 +28,7 @@ use FindBin;
use Pod::Usage; use Pod::Usage;
use Pod::Find qw(pod_where); use Pod::Find qw(pod_where);
my %handlers = (DIE => {}); my %handlers = (DIE => {}, ALRM => {});
my $global_version = '(dev)'; my $global_version = '(dev)';
my $alternative_fatpacker = 0; my $alternative_fatpacker = 0;
@ -45,18 +45,18 @@ sub new {
# Avoid to destroy because it keeps a ref on the object. # Avoid to destroy because it keeps a ref on the object.
# A problem if we execute it multiple times in the same perl execution # A problem if we execute it multiple times in the same perl execution
# Use prepare_destroy # Use prepare_destroy
$self->set_signal_handlers; $self->set_signal_handlers();
return $self; return $self;
} }
sub prepare_destroy { sub prepare_destroy {
my ($self) = @_; my ($self) = @_;
delete $handlers{DIE}->{$self}; %handlers = undef;
} }
sub set_signal_handlers { sub set_signal_handlers {
my $self = shift; my ($self) = @_;
$SIG{__DIE__} = \&class_handle_DIE; $SIG{__DIE__} = \&class_handle_DIE;
$handlers{DIE}->{$self} = sub { $self->handle_DIE($_[0]) }; $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 { sub handle_DIE {
my ($self, $msg) = @_; my ($self, $msg) = @_;
@ -78,6 +84,13 @@ sub handle_DIE {
$self->{output}->die_exit(); $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 { sub get_global_version {
return $global_version; return $global_version;
} }
@ -85,9 +98,7 @@ sub get_global_version {
sub get_plugin { sub get_plugin {
my ($self) = @_; my ($self) = @_;
######
# Need to load global 'Output' and 'Options' # Need to load global 'Output' and 'Options'
######
if ($alternative_fatpacker == 0) { if ($alternative_fatpacker == 0) {
require centreon::plugins::options; require centreon::plugins::options;
$self->{options} = centreon::plugins::options->new(); $self->{options} = centreon::plugins::options->new();
@ -105,20 +116,28 @@ sub get_plugin {
'ignore-warn-msg' => { name => 'ignore_warn_msg' }, 'ignore-warn-msg' => { name => 'ignore_warn_msg' },
'version' => { name => 'version' }, 'version' => { name => 'version' },
'runas:s' => { name => 'runas' }, 'runas:s' => { name => 'runas' },
'global-timeout:s' => { name => 'global_timeout' },
'environment:s%' => { name => 'environment' }, 'environment:s%' => { name => 'environment' },
'convert-args:s' => { name => 'convert_args' }, 'convert-args:s' => { name => 'convert_args' },
}); });
$self->{options}->parse_options(); $self->{options}->parse_options();
$self->{plugin} = $self->{options}->get_option(argument => 'plugin' ); $self->{plugin} = $self->{options}->get_option(argument => 'plugin');
$self->{list_plugin} = $self->{options}->get_option(argument => 'list_plugin' ); $self->{list_plugin} = $self->{options}->get_option(argument => 'list_plugin');
$self->{help} = $self->{options}->get_option(argument => 'help' ); $self->{help} = $self->{options}->get_option(argument => 'help');
$self->{version} = $self->{options}->get_option(argument => 'version' ); $self->{version} = $self->{options}->get_option(argument => 'version');
$self->{runas} = $self->{options}->get_option(argument => 'runas' ); $self->{runas} = $self->{options}->get_option(argument => 'runas');
$self->{environment} = $self->{options}->get_option(argument => 'environment' ); $self->{environment} = $self->{options}->get_option(argument => 'environment');
$self->{ignore_warn_msg} = $self->{options}->get_option(argument => 'ignore_warn_msg' ); $self->{ignore_warn_msg} = $self->{options}->get_option(argument => 'ignore_warn_msg');
$self->{convert_args} = $self->{options}->get_option(argument => 'convert_args' ); $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}->plugin(name => $self->{plugin});
$self->{output}->check_options(option_results => $self->{options}->get_options()); $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}, centreon::plugins::misc::mymodule_load(output => $self->{output}, module => $self->{plugin},
error_msg => 'Cannot load module --plugin.'); error_msg => 'Cannot load module --plugin.');
my $plugin = $self->{plugin}->new(options => $self->{options}, output => $self->{output}); my $plugin = $self->{plugin}->new(options => $self->{options}, output => $self->{output});
$plugin->init(help => $self->{help}, $plugin->init(
version => $self->{version}); help => $self->{help},
version => $self->{version}
);
$plugin->run(); $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). Run the script as a different user (prefer to use directly the good user).
=item B<--global-timeout>
Set script timeout.
=item B<--environment> =item B<--environment>
Set environment variables for the script (prefer to set it before running it for better performance). Set environment variables for the script (prefer to set it before running it for better performance).