This commit is contained in:
garnier-quentin 2019-10-22 21:33:22 +02:00
parent 90e2318d17
commit d0db82a343
1 changed files with 86 additions and 63 deletions

View File

@ -36,11 +36,11 @@ sub new {
my $self = $class->SUPER::new(package => __PACKAGE__, %options); my $self = $class->SUPER::new(package => __PACKAGE__, %options);
bless $self, $class; bless $self, $class;
$options{options}->add_options(arguments => $options{options}->add_options(arguments => {
{ 'centreon-config:s' => { name => 'centreon_config', default => '/etc/centreon/centreon-config.pm' },
"centreon-config:s" => { name => 'centreon_config', default => '/etc/centreon/centreon-config.pm' }, 'meta-id:s' => { name => 'meta_id' },
"meta-id:s" => { name => 'meta_id', }, });
});
$self->{metric_selected} = {}; $self->{metric_selected} = {};
return $self; return $self;
} }
@ -61,8 +61,10 @@ sub execute_query {
my ($status, $stmt) = $db->query($query); my ($status, $stmt) = $db->query($query);
if ($status == -1) { if ($status == -1) {
$self->{output}->output_add(severity => 'UNKNOWN', $self->{output}->output_add(
short_msg => 'SQL Query error: ' . $query); severity => 'UNKNOWN',
short_msg => 'SQL Query error: ' . $query
);
$self->{output}->display(); $self->{output}->display();
$self->{output}->exit(); $self->{output}->exit();
} }
@ -73,8 +75,10 @@ sub select_by_regexp {
my ($self, %options) = @_; my ($self, %options) = @_;
my $count = 0; my $count = 0;
my $stmt = $self->execute_query($self->{centreon_db_centstorage}, my $stmt = $self->execute_query(
"SELECT metrics.metric_id, metrics.metric_name, metrics.current_value FROM index_data, metrics WHERE index_data.service_description LIKE " . $self->{centreon_db_centstorage}->quote($options{regexp_str}) . " AND index_data.id = metrics.index_id"); $self->{centreon_db_centstorage},
"SELECT metrics.metric_id, metrics.metric_name, metrics.current_value FROM index_data, metrics WHERE index_data.service_description LIKE " . $self->{centreon_db_centstorage}->quote($options{regexp_str}) . " AND index_data.id = metrics.index_id"
);
while ((my $row = $stmt->fetchrow_hashref())) { while ((my $row = $stmt->fetchrow_hashref())) {
if ($options{metric_select} eq $row->{metric_name}) { if ($options{metric_select} eq $row->{metric_name}) {
$self->{metric_selected}->{$row->{metric_id}} = $row->{current_value}; $self->{metric_selected}->{$row->{metric_id}} = $row->{current_value};
@ -82,8 +86,10 @@ sub select_by_regexp {
} }
} }
if ($count == 0) { if ($count == 0) {
$self->{output}->output_add(severity => 'UNKNOWN', $self->{output}->output_add(
short_msg => 'Cannot find a metric.'); severity => 'UNKNOWN',
short_msg => 'Cannot find a metric.'
);
$self->{output}->display(); $self->{output}->display();
$self->{output}->exit(); $self->{output}->exit();
} }
@ -100,22 +106,28 @@ sub select_by_list {
$count++; $count++;
} }
if ($count == 0) { if ($count == 0) {
$self->{output}->output_add(severity => 'UNKNOWN', $self->{output}->output_add(
short_msg => 'Cannot find a metric_id in table meta_service_relation.'); severity => 'UNKNOWN',
short_msg => 'Cannot find a metric_id in table meta_service_relation.'
);
$self->{output}->display(); $self->{output}->display();
$self->{output}->exit(); $self->{output}->exit();
} }
$count = 0; $count = 0;
$stmt = $self->execute_query($self->{centreon_db_centstorage}, $stmt = $self->execute_query(
"SELECT metric_id, current_value FROM metrics WHERE metric_id IN (" . join(',', keys %{$metric_ids}) . ")"); $self->{centreon_db_centstorage},
"SELECT metric_id, current_value FROM metrics WHERE metric_id IN (" . join(',', keys %{$metric_ids}) . ")"
);
while ((my $row = $stmt->fetchrow_hashref())) { while ((my $row = $stmt->fetchrow_hashref())) {
$self->{metric_selected}->{$row->{metric_id}} = $row->{current_value}; $self->{metric_selected}->{$row->{metric_id}} = $row->{current_value};
$count++; $count++;
} }
if ($count == 0) { if ($count == 0) {
$self->{output}->output_add(severity => 'UNKNOWN', $self->{output}->output_add(
short_msg => 'Cannot find a metric_id in metrics table.'); severity => 'UNKNOWN',
short_msg => 'Cannot find a metric_id in metrics table.'
);
$self->{output}->display(); $self->{output}->display();
$self->{output}->exit(); $self->{output}->exit();
} }
@ -125,21 +137,21 @@ sub calculate {
my ($self, %options) = @_; my ($self, %options) = @_;
my $result = 0; my $result = 0;
if ($options{calculation} eq "MIN") { if ($options{calculation} eq 'MIN') {
my @values = sort(values %{$self->{metric_selected}}); my @values = sort { $a <=> $b } values(%{$self->{metric_selected}});
if (defined($values[0])) { if (defined($values[0])) {
$result = $values[0]; $result = $values[0];
} }
} elsif ($options{calculation} eq "MAX") { } elsif ($options{calculation} eq 'MAX') {
my @values = sort(values %{$self->{metric_selected}}); my @values = sort { $a <=> $b } values(%{$self->{metric_selected}});
if (defined($values[0])) { if (defined($values[0])) {
$result = $values[scalar(@values) - 1]; $result = $values[scalar(@values) - 1];
} }
} elsif ($options{calculation} eq "SOM") { } elsif ($options{calculation} eq 'SOM') {
foreach my $value (values %{$self->{metric_selected}}) { foreach my $value (values %{$self->{metric_selected}}) {
$result += $value; $result += $value;
} }
} elsif ($options{calculation} eq "AVE") { } elsif ($options{calculation} eq 'AVE') {
my @values = values %{$self->{metric_selected}}; my @values = values %{$self->{metric_selected}};
foreach my $value (@values) { foreach my $value (@values) {
$result += $value; $result += $value;
@ -158,13 +170,15 @@ sub run {
$self->{logger} = centreon::common::logger->new(); $self->{logger} = centreon::common::logger->new();
$self->{logger}->severity('none'); $self->{logger}->severity('none');
$self->{centreon_db_centreon} = centreon::common::db->new(db => $centreon_config->{centreon_db}, $self->{centreon_db_centreon} = centreon::common::db->new(
host => $centreon_config->{db_host}, db => $centreon_config->{centreon_db},
port => $centreon_config->{db_port}, host => $centreon_config->{db_host},
user => $centreon_config->{db_user}, port => $centreon_config->{db_port},
password => $centreon_config->{db_passwd}, user => $centreon_config->{db_user},
force => 0, password => $centreon_config->{db_passwd},
logger => $self->{logger}); force => 0,
logger => $self->{logger}
);
my $status = $self->{centreon_db_centreon}->connect(); my $status = $self->{centreon_db_centreon}->connect();
if ($status == -1) { if ($status == -1) {
$self->{output}->output_add(severity => 'UNKNOWN', $self->{output}->output_add(severity => 'UNKNOWN',
@ -172,17 +186,21 @@ sub run {
$self->{output}->display(); $self->{output}->display();
$self->{output}->exit(); $self->{output}->exit();
} }
$self->{centreon_db_centstorage} = centreon::common::db->new(db => $centreon_config->{centstorage_db}, $self->{centreon_db_centstorage} = centreon::common::db->new(
host => $centreon_config->{db_host}, db => $centreon_config->{centstorage_db},
port => $centreon_config->{db_port}, host => $centreon_config->{db_host},
user => $centreon_config->{db_user}, port => $centreon_config->{db_port},
password => $centreon_config->{db_passwd}, user => $centreon_config->{db_user},
force => 0, password => $centreon_config->{db_passwd},
logger => $self->{logger}); force => 0,
logger => $self->{logger}
);
$status = $self->{centreon_db_centstorage}->connect(); $status = $self->{centreon_db_centstorage}->connect();
if ($status == -1) { if ($status == -1) {
$self->{output}->output_add(severity => 'UNKNOWN', $self->{output}->output_add(
short_msg => 'Cannot connect to Centstorage Database.'); severity => 'UNKNOWN',
short_msg => 'Cannot connect to Centstorage Database.'
);
$self->{output}->display(); $self->{output}->display();
$self->{output}->exit(); $self->{output}->exit();
} }
@ -190,20 +208,22 @@ sub run {
my $stmt = $self->execute_query($self->{centreon_db_centreon}, "SELECT meta_display, calcul_type, regexp_str, warning, critical, metric, meta_select_mode, data_source_type FROM `meta_service` WHERE meta_id = '". $self->{option_results}->{meta_id} . "' LIMIT 1"); my $stmt = $self->execute_query($self->{centreon_db_centreon}, "SELECT meta_display, calcul_type, regexp_str, warning, critical, metric, meta_select_mode, data_source_type FROM `meta_service` WHERE meta_id = '". $self->{option_results}->{meta_id} . "' LIMIT 1");
my $row = $stmt->fetchrow_hashref(); my $row = $stmt->fetchrow_hashref();
if (!defined($row)) { if (!defined($row)) {
$self->{output}->output_add(severity => 'UNKNOWN', $self->{output}->output_add(
short_msg => 'Cannot get meta service informations.'); severity => 'UNKNOWN',
short_msg => 'Cannot get meta service informations.'
);
$self->{output}->display(); $self->{output}->display();
$self->{output}->exit(); $self->{output}->exit();
} }
# Set threshold # Set threshold
if (($self->{perfdata}->threshold_validate(label => 'warning', value => $row->{warning})) == 0) { if (($self->{perfdata}->threshold_validate(label => 'warning', value => $row->{warning})) == 0) {
$self->{output}->add_option_msg(short_msg => "Wrong warning threshold '" . $row->{warning} . "'."); $self->{output}->add_option_msg(short_msg => "Wrong warning threshold '" . $row->{warning} . "'.");
$self->{output}->option_exit(); $self->{output}->option_exit();
} }
if (($self->{perfdata}->threshold_validate(label => 'critical', value => $row->{critical})) == 0) { if (($self->{perfdata}->threshold_validate(label => 'critical', value => $row->{critical})) == 0) {
$self->{output}->add_option_msg(short_msg => "Wrong critical threshold '" . $row->{critical} . "'."); $self->{output}->add_option_msg(short_msg => "Wrong critical threshold '" . $row->{critical} . "'.");
$self->{output}->option_exit(); $self->{output}->option_exit();
} }
if ($row->{meta_select_mode} == 2) { if ($row->{meta_select_mode} == 2) {
@ -216,13 +236,16 @@ sub run {
my $exit = $self->{perfdata}->threshold_check(value => $result, threshold => [ { label => 'critical', 'exit_litteral' => 'critical' }, { label => 'warning', exit_litteral => 'warning' } ]); my $exit = $self->{perfdata}->threshold_check(value => $result, threshold => [ { label => 'critical', 'exit_litteral' => 'critical' }, { label => 'warning', exit_litteral => 'warning' } ]);
my $display = defined($row->{meta_display}) ? $row->{meta_display} : $row->{calcul_type} . ' - value : %f'; my $display = defined($row->{meta_display}) ? $row->{meta_display} : $row->{calcul_type} . ' - value : %f';
$self->{output}->output_add(severity => $exit, $self->{output}->output_add(
short_msg => sprintf($display, $result)); severity => $exit,
$self->{output}->perfdata_add(label => (defined($DSTYPE{$row->{data_source_type}}) ? $DSTYPE{$row->{data_source_type}} : 'g') . '[' . $row->{metric} . ']', short_msg => sprintf($display, $result)
value => sprintf("%02.2f", $result), );
warning => $self->{perfdata}->get_perfdata_for_output(label => 'warning'), $self->{output}->perfdata_add(
critical => $self->{perfdata}->get_perfdata_for_output(label => 'critical') label => (defined($DSTYPE{$row->{data_source_type}}) ? $DSTYPE{$row->{data_source_type}} : 'g') . '[' . $row->{metric} . ']',
); value => sprintf("%02.2f", $result),
warning => $self->{perfdata}->get_perfdata_for_output(label => 'warning'),
critical => $self->{perfdata}->get_perfdata_for_output(label => 'critical')
);
$self->{output}->display(); $self->{output}->display();
$self->{output}->exit(); $self->{output}->exit();