Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add --sql-file option to modes sql and sql-string #5404

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 18 additions & 7 deletions src/centreon/common/protocols/sql/mode/sql.pm
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ use base qw(centreon::plugins::templates::counter);

use strict;
use warnings;
use centreon::plugins::misc;
use Time::HiRes qw(gettimeofday tv_interval);

sub custom_value_output {
Expand Down Expand Up @@ -76,8 +77,9 @@ sub new {
my $self = $class->SUPER::new(package => __PACKAGE__, %options, force_new_perfdata => 1);
bless $self, $class;

$options{options}->add_options(arguments => {
$options{options}->add_options(arguments => {
'sql-statement:s' => { name => 'sql_statement' },
'sql-file:s' => { name => 'sql_file' },
'format:s' => { name => 'format', default => 'SQL statement result : %i.' },
'perfdata-unit:s' => { name => 'perfdata_unit', default => '' },
'perfdata-name:s' => { name => 'perfdata_name', default => 'value' },
Expand All @@ -94,10 +96,15 @@ sub check_options {
my ($self, %options) = @_;
$self->SUPER::check_options(%options);

if (!defined($self->{option_results}->{sql_statement}) || $self->{option_results}->{sql_statement} eq '') {
$self->{output}->add_option_msg(short_msg => "Need to specify '--sql-statement' option.");
$self->{output}->option_exit();
$self->{query} = $self->{option_results}->{sql_statement};
if (!defined($self->{query}) || $self->{query} eq '') {
if (!defined($self->{option_results}->{sql_file}) || $self->{option_results}->{sql_file} eq '') {
$self->{output}->add_option_msg(short_msg => "Need to specify --sql-statement or --sql-file option.");
$self->{output}->option_exit();
}
$self->{query} = centreon::plugins::misc::slurp_file(output => $self->{output}, file => $self->{option_results}->{sql_file});
}

if (!defined($self->{option_results}->{format}) || $self->{option_results}->{format} eq '') {
$self->{output}->add_option_msg(short_msg => "Need to specify '--format' option.");
$self->{output}->option_exit();
Expand All @@ -108,9 +115,9 @@ sub manage_selection {
my ($self, %options) = @_;

$options{sql}->connect();

my $timing0 = [gettimeofday];
$options{sql}->query(query => $self->{option_results}->{sql_statement});
$options{sql}->query(query => $self->{query});
my $value = $options{sql}->fetchrow_array();
$self->{global} = {
value => $value,
Expand All @@ -134,6 +141,10 @@ Check SQL statement.

SQL statement that returns a number.

=item B<--sql-file>

Define the file with the SQL request.

=item B<--format>

Output format (default: 'SQL statement result : %i.').
Expand Down Expand Up @@ -161,4 +172,4 @@ Can be: 'value', 'execution-time'.

=back

=cut
=cut
18 changes: 14 additions & 4 deletions src/centreon/common/protocols/sql/mode/sqlstring.pm
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ package centreon::common::protocols::sql::mode::sqlstring;
use base qw(centreon::plugins::templates::counter);
use strict;
use warnings;
use centreon::plugins::misc;
use centreon::plugins::templates::catalog_functions qw(catalog_status_threshold_ng);

sub set_counters {
Expand Down Expand Up @@ -73,6 +74,7 @@ sub new {

$options{options}->add_options(arguments => {
'sql-statement:s' => { name => 'sql_statement' },
'sql-file:s' => { name => 'sql_file' },
'key-column:s' => { name => 'key_column' },
'value-column:s' => { name => 'value_column' },
'printf-format:s' => { name => 'printf_format' },
Expand All @@ -88,9 +90,13 @@ sub check_options {
my ($self, %options) = @_;
$self->SUPER::check_options(%options);

if (!defined($self->{option_results}->{sql_statement}) || $self->{option_results}->{sql_statement} eq '') {
$self->{output}->add_option_msg(short_msg => "Need to specify '--sql-statement' option.");
$self->{output}->option_exit();
$self->{query} = $self->{option_results}->{sql_statement};
if (!defined($self->{query}) || $self->{query} eq '') {
if (!defined($self->{option_results}->{sql_file}) || $self->{option_results}->{sql_file} eq '') {
$self->{output}->add_option_msg(short_msg => "Need to specify --sql-statement or --sql-file option.");
$self->{output}->option_exit();
}
$self->{query} = centreon::plugins::misc::slurp_file(output => $self->{output}, file => $self->{option_results}->{sql_file});
}

$self->{printf_value} = 'value_field';
Expand All @@ -108,7 +114,7 @@ sub manage_selection {
my ($self, %options) = @_;

$options{sql}->connect();
$options{sql}->query(query => $self->{option_results}->{sql_statement});
$options{sql}->query(query => $self->{query});
$self->{rows} = {};
my $row_count = 0;

Expand Down Expand Up @@ -156,6 +162,10 @@ Check SQL statement to query string pattern (You cannot have more than to fiels

SQL statement that returns a string.

=item B<--sql-file>

Define the file with the SQL request.

=item B<--key-column>

Key column (must be one of the selected field). NOT mandatory if you select only one field
Expand Down
2 changes: 2 additions & 0 deletions src/database/db2/plugin.pm
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ sub new {
'database-usage' => 'database::db2::mode::databaseusage',
'hadr' => 'database::db2::mode::hadr',
'list-tablespaces' => 'database::db2::mode::listtablespaces',
'sql' => 'centreon::common::protocols::sql::mode::sql',
'sql-string' => 'centreon::common::protocols::sql::mode::sqlstring',
'tablespaces' => 'database::db2::mode::tablespaces'
};

Expand Down