From 0d1b901f096eabb54f0cb89f67fead722ad77793 Mon Sep 17 00:00:00 2001 From: Quentin Garnier Date: Wed, 14 May 2014 14:35:50 +0200 Subject: [PATCH] Fix #5546 --- centreon/plugins/script.pm | 58 +++++++++++++++++++++++++++++++++++--- 1 file changed, 54 insertions(+), 4 deletions(-) diff --git a/centreon/plugins/script.pm b/centreon/plugins/script.pm index 2b435358d..6f527999e 100644 --- a/centreon/plugins/script.pm +++ b/centreon/plugins/script.pm @@ -94,16 +94,18 @@ sub get_plugin { $self->{options}->set_output(output => $self->{output}); $self->{options}->add_options(arguments => { - 'plugin:s' => { name => 'plugin' }, - 'help' => { name => 'help' }, - 'version' => { name => 'version' }, - 'runas:s' => { name => 'runas' }, + 'plugin:s' => { name => 'plugin' }, + 'list-plugin' => { name => 'list_plugin' }, + 'help' => { name => 'help' }, + 'version' => { name => 'version' }, + 'runas:s' => { name => 'runas' }, 'environment:s%' => { name => 'environment' }, } ); $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' ); @@ -129,6 +131,46 @@ sub display_local_help { $self->{output}->add_option_msg(long_msg => $stdout) if (defined($stdout)); } +sub check_directory { + my ($self, $directory) = @_; + + opendir(my $dh, $directory) || return ; + while (my $filename = readdir $dh) { + $self->check_directory($directory . "/" . $filename) if ($filename !~ /^\./ && -d $directory . "/" . $filename); + if ($filename eq 'plugin.pm') { + my $stdout = ''; + + { + local *STDOUT; + open STDOUT, '>', \$stdout; + pod2usage(-exitval => 'NOEXIT', -input => $directory . "/" . $filename, + -verbose => 99, + -sections => "PLUGIN DESCRIPTION"); + } + $self->{plugins_result}->{$directory . "/" . $filename} = $stdout; + } + } + closedir $dh; +} + +sub display_list_plugin { + my $self = shift; + $self->{plugins_result} = {}; + + # Search file 'plugin.pm' + $self->check_directory($FindBin::Bin); + foreach my $key (keys %{$self->{plugins_result}}) { + my $name = $key; + $name =~ s/^$FindBin::Bin\/(.*)\.pm/$1/; + $name =~ s/\//::/g; + $self->{plugins_result}->{$key} =~ s/^Plugin Description/DESCRIPTION:/i; + + $self->{output}->add_option_msg(long_msg => '-----------------'); + $self->{output}->add_option_msg(long_msg => 'PLUGIN: ' . $name); + $self->{output}->add_option_msg(long_msg => $self->{plugins_result}->{$key}); + } +} + sub check_relaunch { my $self = shift; my $need_restart = 0; @@ -200,6 +242,10 @@ sub run { $self->display_local_help(); $self->{output}->option_exit(); } + if (defined($self->{list_plugin})) { + $self->display_list_plugin(); + $self->{output}->option_exit(); + } if (!defined($self->{plugin}) || $self->{plugin} eq '') { $self->{output}->add_option_msg(short_msg => "Need to specify '--plugin' option."); $self->{output}->option_exit(); @@ -235,6 +281,10 @@ centreon_plugins.pl [options] Specify the path to the plugin. +=item B<--list-plugin> + +Print available plugins. + =item B<--version> Print plugin version.