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

@ -35,12 +35,12 @@ sub new {
my ($class, %options) = @_;
my $self = $class->SUPER::new(package => __PACKAGE__, %options);
bless $self, $class;
$options{options}->add_options(arguments =>
{
"centreon-config:s" => { name => 'centreon_config', default => '/etc/centreon/centreon-config.pm' },
"meta-id:s" => { name => 'meta_id', },
});
$options{options}->add_options(arguments => {
'centreon-config:s' => { name => 'centreon_config', default => '/etc/centreon/centreon-config.pm' },
'meta-id:s' => { name => 'meta_id' },
});
$self->{metric_selected} = {};
return $self;
}
@ -58,11 +58,13 @@ sub check_options {
sub execute_query {
my ($self, $db, $query) = @_;
my ($status, $stmt) = $db->query($query);
if ($status == -1) {
$self->{output}->output_add(severity => 'UNKNOWN',
short_msg => 'SQL Query error: ' . $query);
$self->{output}->output_add(
severity => 'UNKNOWN',
short_msg => 'SQL Query error: ' . $query
);
$self->{output}->display();
$self->{output}->exit();
}
@ -71,10 +73,12 @@ sub execute_query {
sub select_by_regexp {
my ($self, %options) = @_;
my $count = 0;
my $stmt = $self->execute_query($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");
my $stmt = $self->execute_query(
$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())) {
if ($options{metric_select} eq $row->{metric_name}) {
$self->{metric_selected}->{$row->{metric_id}} = $row->{current_value};
@ -82,8 +86,10 @@ sub select_by_regexp {
}
}
if ($count == 0) {
$self->{output}->output_add(severity => 'UNKNOWN',
short_msg => 'Cannot find a metric.');
$self->{output}->output_add(
severity => 'UNKNOWN',
short_msg => 'Cannot find a metric.'
);
$self->{output}->display();
$self->{output}->exit();
}
@ -91,7 +97,7 @@ sub select_by_regexp {
sub select_by_list {
my ($self, %options) = @_;
my $count = 0;
my $metric_ids = {};
my $stmt = $self->execute_query($self->{centreon_db_centreon}, "SELECT metric_id FROM `meta_service_relation` WHERE meta_id = '". $self->{option_results}->{meta_id} . "' AND activate = '1'");
@ -100,22 +106,28 @@ sub select_by_list {
$count++;
}
if ($count == 0) {
$self->{output}->output_add(severity => 'UNKNOWN',
short_msg => 'Cannot find a metric_id in table meta_service_relation.');
$self->{output}->output_add(
severity => 'UNKNOWN',
short_msg => 'Cannot find a metric_id in table meta_service_relation.'
);
$self->{output}->display();
$self->{output}->exit();
}
$count = 0;
$stmt = $self->execute_query($self->{centreon_db_centstorage},
"SELECT metric_id, current_value FROM metrics WHERE metric_id IN (" . join(',', keys %{$metric_ids}) . ")");
$stmt = $self->execute_query(
$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())) {
$self->{metric_selected}->{$row->{metric_id}} = $row->{current_value};
$count++;
}
if ($count == 0) {
$self->{output}->output_add(severity => 'UNKNOWN',
short_msg => 'Cannot find a metric_id in metrics table.');
$self->{output}->output_add(
severity => 'UNKNOWN',
short_msg => 'Cannot find a metric_id in metrics table.'
);
$self->{output}->display();
$self->{output}->exit();
}
@ -124,22 +136,22 @@ sub select_by_list {
sub calculate {
my ($self, %options) = @_;
my $result = 0;
if ($options{calculation} eq "MIN") {
my @values = sort(values %{$self->{metric_selected}});
if ($options{calculation} eq 'MIN') {
my @values = sort { $a <=> $b } values(%{$self->{metric_selected}});
if (defined($values[0])) {
$result = $values[0];
}
} elsif ($options{calculation} eq "MAX") {
my @values = sort(values %{$self->{metric_selected}});
} elsif ($options{calculation} eq 'MAX') {
my @values = sort { $a <=> $b } values(%{$self->{metric_selected}});
if (defined($values[0])) {
$result = $values[scalar(@values) - 1];
}
} elsif ($options{calculation} eq "SOM") {
} elsif ($options{calculation} eq 'SOM') {
foreach my $value (values %{$self->{metric_selected}}) {
$result += $value;
}
} elsif ($options{calculation} eq "AVE") {
} elsif ($options{calculation} eq 'AVE') {
my @values = values %{$self->{metric_selected}};
foreach my $value (@values) {
$result += $value;
@ -158,13 +170,15 @@ sub run {
$self->{logger} = centreon::common::logger->new();
$self->{logger}->severity('none');
$self->{centreon_db_centreon} = centreon::common::db->new(db => $centreon_config->{centreon_db},
host => $centreon_config->{db_host},
port => $centreon_config->{db_port},
user => $centreon_config->{db_user},
password => $centreon_config->{db_passwd},
force => 0,
logger => $self->{logger});
$self->{centreon_db_centreon} = centreon::common::db->new(
db => $centreon_config->{centreon_db},
host => $centreon_config->{db_host},
port => $centreon_config->{db_port},
user => $centreon_config->{db_user},
password => $centreon_config->{db_passwd},
force => 0,
logger => $self->{logger}
);
my $status = $self->{centreon_db_centreon}->connect();
if ($status == -1) {
$self->{output}->output_add(severity => 'UNKNOWN',
@ -172,40 +186,46 @@ sub run {
$self->{output}->display();
$self->{output}->exit();
}
$self->{centreon_db_centstorage} = centreon::common::db->new(db => $centreon_config->{centstorage_db},
host => $centreon_config->{db_host},
port => $centreon_config->{db_port},
user => $centreon_config->{db_user},
password => $centreon_config->{db_passwd},
force => 0,
logger => $self->{logger});
$self->{centreon_db_centstorage} = centreon::common::db->new(
db => $centreon_config->{centstorage_db},
host => $centreon_config->{db_host},
port => $centreon_config->{db_port},
user => $centreon_config->{db_user},
password => $centreon_config->{db_passwd},
force => 0,
logger => $self->{logger}
);
$status = $self->{centreon_db_centstorage}->connect();
if ($status == -1) {
$self->{output}->output_add(severity => 'UNKNOWN',
short_msg => 'Cannot connect to Centstorage Database.');
$self->{output}->output_add(
severity => 'UNKNOWN',
short_msg => 'Cannot connect to Centstorage Database.'
);
$self->{output}->display();
$self->{output}->exit();
}
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();
if (!defined($row)) {
$self->{output}->output_add(severity => 'UNKNOWN',
short_msg => 'Cannot get meta service informations.');
$self->{output}->output_add(
severity => 'UNKNOWN',
short_msg => 'Cannot get meta service informations.'
);
$self->{output}->display();
$self->{output}->exit();
}
# Set threshold
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}->option_exit();
$self->{output}->add_option_msg(short_msg => "Wrong warning threshold '" . $row->{warning} . "'.");
$self->{output}->option_exit();
}
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}->option_exit();
$self->{output}->add_option_msg(short_msg => "Wrong critical threshold '" . $row->{critical} . "'.");
$self->{output}->option_exit();
}
if ($row->{meta_select_mode} == 2) {
$self->select_by_regexp(regexp_str => $row->{regexp_str}, metric_select => $row->{metric});
} else {
@ -213,17 +233,20 @@ sub run {
}
my $result = $self->calculate(calculation => $row->{calcul_type});
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';
$self->{output}->output_add(severity => $exit,
short_msg => sprintf($display, $result));
$self->{output}->perfdata_add(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}->output_add(
severity => $exit,
short_msg => sprintf($display, $result)
);
$self->{output}->perfdata_add(
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}->exit();
}