add database option

This commit is contained in:
garnier-quentin 2019-09-09 14:36:45 +02:00
parent bd38e474b7
commit 8b9d2decc1
2 changed files with 43 additions and 31 deletions

View File

@ -129,9 +129,13 @@ sub custom_metric_threshold {
my $label_warn = ($self->{result_values}->{type} eq 'unique') ? 'warning-metric' : 'warning-global-'.$self->{result_values}->{instance};
my $label_crit = ($self->{result_values}->{type} eq 'unique') ? 'critical-metric' : 'critical-global-'.$self->{result_values}->{instance};
my $exit = $self->{perfdata}->threshold_check(value => $self->{result_values}->{perfdata_value},
threshold => [ { label => $label_crit, exit_litteral => 'critical' },
{ label => $label_warn, exit_litteral => 'warning' } ]);
my $exit = $self->{perfdata}->threshold_check(
value => $self->{result_values}->{perfdata_value},
threshold => [
{ label => $label_crit, exit_litteral => 'critical' },
{ label => $label_warn, exit_litteral => 'warning' }
]
);
return $exit;
}
@ -169,11 +173,12 @@ sub new {
my $self = $class->SUPER::new(package => __PACKAGE__, %options);
bless $self, $class;
$options{options}->add_options(arguments =>
{
"config-file:s" => { name => 'config_file' },
"json-data:s" => { name => 'json_data' },
});
$options{options}->add_options(arguments => {
'config-file:s' => { name => 'config_file' },
'json-data:s' => { name => 'json_data' },
'database:s' => { name => 'database' },
});
return $self;
}
@ -205,6 +210,10 @@ sub check_options {
$config_data->{formatting}->{custom_message_metric} = "All metrics are OK" if (!exists($config_data->{formatting}->{custom_message_metric}));
$config_data->{formatting}->{cannonical_separator} = "#" if (!exists($config_data->{formatting}->{cannonical_separator}));
$config_data->{formatting}->{change_bytes} = 0 if (!exists($config_data->{formatting}->{change_bytes}));
$self->{option_results}->{database} =
(defined($self->{option_results}->{database}) && $self->{option_results}->{database} ne '') ?
$self->{option_results}->{database} . '.' : 'centreon_storage.';
}
sub parse_json_config {
@ -234,7 +243,6 @@ sub parse_json_config {
sub manage_selection {
my ($self, %options) = @_;
# $options{sql} = sqlmode object
$self->{sql} = $options{sql};
$self->{sql}->connect();
$self->{metrics} = {};
@ -253,7 +261,7 @@ sub manage_selection {
};
foreach my $id (keys %{$config_data->{selection}}) {
my $query = "SELECT index_data.host_name, index_data.service_description, metrics.metric_name, metrics.current_value, metrics.unit_name, metrics.min, metrics.max ";
$query .= "FROM centreon_storage.index_data, centreon_storage.metrics WHERE index_data.id = metrics.index_id ";
$query .= "FROM $self->{option_results}->{database}.index_data, $self->{option_results}->{database}.metrics WHERE index_data.id = metrics.index_id ";
$query .= "AND index_data.service_description = '" . $config_data->{selection}->{$id}->{service_name} . "'";
$query .= "AND index_data.host_name = '" . $config_data->{selection}->{$id}->{host_name} . "'" ;
$query .= "AND metrics.metric_name = '" . $config_data->{selection}->{$id}->{metric_name} . "'";
@ -274,7 +282,7 @@ sub manage_selection {
name => 'metric', type => 1, message_separator => $config_data->{formatting}->{message_separator}, message_multiple => $config_data->{formatting}->{custom_message_metric},
};
my $query = "SELECT index_data.host_name, index_data.service_description, metrics.metric_name, metrics.current_value, metrics.unit_name, metrics.min, metrics.max ";
$query .= "FROM centreon_storage.index_data, centreon_storage.metrics WHERE index_data.id = metrics.index_id ";
$query .= "FROM $self->{option_results}->{database}.index_data, $self->{option_results}->{database}.metrics WHERE index_data.id = metrics.index_id ";
$query .= "AND index_data.service_description LIKE '" . $config_data->{filters}->{service} . "'" if (defined($config_data->{filters}->{service}) && ($config_data->{filters}->{service} ne ''));
$query .= "AND index_data.host_name LIKE '" . $config_data->{filters}->{host} . "'" if (defined($config_data->{filters}->{host}) && ($config_data->{filters}->{host} ne ''));
$query .= "AND metrics.metric_name LIKE '" . $config_data->{filters}->{metric} . "'" if (defined($config_data->{filters}->{metric}) && ($config_data->{filters}->{metric} ne ''));
@ -325,24 +333,28 @@ sub manage_selection {
value => (defined($self->{option_results}->{'critical-global'})) ? $self->{option_results}->{'critical-global'} : $config_data->{virtualcurve}->{$vcurve}->{critical});
}
$self->{global}->{$vcurve} = {display => $vcurve,
type => 'global',
unit => $self->{vmetrics}->{$vcurve}->{unit},
value => $self->{vmetrics}->{$vcurve}->{aggregated_value},
min => $self->{vmetrics}->{$vcurve}->{min},
max => $self->{vmetrics}->{$vcurve}->{max} };
}
$self->{global}->{$vcurve} = {
display => $vcurve,
type => 'global',
unit => $self->{vmetrics}->{$vcurve}->{unit},
value => $self->{vmetrics}->{$vcurve}->{aggregated_value},
min => $self->{vmetrics}->{$vcurve}->{min},
max => $self->{vmetrics}->{$vcurve}->{max}
};
}
$self->{metric}->{$metric} = {display => $self->{metrics}->{$metric}->{display_name},
type => 'unique',
unit => $self->{metrics}->{$metric}->{unit},
value => $self->{metrics}->{$metric}->{current},
min => $self->{metrics}->{$metric}->{min},
max => $self->{metrics}->{$metric}->{max} } if ($self->{metrics}->{$metric}->{display} == 1);
$self->{metric}->{$metric} = {
display => $self->{metrics}->{$metric}->{display_name},
type => 'unique',
unit => $self->{metrics}->{$metric}->{unit},
value => $self->{metrics}->{$metric}->{current},
min => $self->{metrics}->{$metric}->{min},
max => $self->{metrics}->{$metric}->{max}
} if ($self->{metrics}->{$metric}->{display} == 1);
}
if (scalar(keys %{$self->{metric}}) <= 0 && scalar(keys %{$self->{vmetrics}}) <= 0) {
$self->{output}->add_option_msg(short_msg => "No metrics returned - are your selection/filters correct ?");
$self->{output}->add_option_msg(short_msg => 'No metrics returned - are your selection/filters correct ?');
$self->{output}->option_exit();
}
}

View File

@ -60,12 +60,12 @@ sub init {
my ($self, %options) = @_;
$self->{options}->add_options(
arguments => {
'host:s@' => { name => 'db_host' },
'port:s@' => { name => 'db_port' },
'socket:s@' => { name => 'db_socket' },
}
);
arguments => {
'host:s@' => { name => 'db_host' },
'port:s@' => { name => 'db_port' },
'socket:s@' => { name => 'db_socket' },
}
);
$self->{options}->parse_options();
my $options_result = $self->{options}->get_options();
$self->{options}->clean();